Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FormData under melange.dom #1153

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jscomp/others/dom.ml
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,4 @@ type svgPoint
type eventPointerId

module Storage = Dom_storage
module FormData = Dom_formData
44 changes: 44 additions & 0 deletions jscomp/others/dom_formData.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(* Provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data". *)

type file
type blob
type entryValue

let classify : entryValue -> [> `String of string | `File of file ] =
fun t ->
if Js.typeof t = "string" then `String (Obj.magic t)
else `File (Obj.magic t)
Comment on lines +7 to +10
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really happy with the classify, but I haven't seen any other way to achieve support for File

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you show me examples of why classify is needed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm reading it in reasonml/reason-react#846, but I wonder whether this is needed at all or if folks can classify it downstream, or even use Js.Types.classify.

Besides, if we were to keep this, I'd rather call it Blob than File, since File is a subclass of Blob.


type t

external make : unit -> t = "FormData" [@@mel.new]
external append : string -> string -> unit = "append" [@@mel.send.pipe: t]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

append also takes a ?filename

external delete : string -> unit = "delete" [@@mel.send.pipe: t]
external get : string -> entryValue option = "get" [@@mel.send.pipe: t]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function doesn't return entryValue option, but rather null when not found. we should use [@@mel.return null_to_opt]

external getAll : string -> entryValue array = "getAll" [@@mel.send.pipe: t]
external set : string -> string -> unit = "set" [@@mel.send.pipe: t]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set also takes ?filename

external has : string -> bool = "has" [@@mel.send.pipe: t]
external keys : t -> string Js.Iterator.t = "keys" [@@mel.send]
external values : t -> entryValue Js.Iterator.t = "values" [@@mel.send]

external appendObject : string -> < .. > Js.t -> ?filename:string -> unit
= "append"
[@@mel.send.pipe: t]

external appendBlob : string -> blob -> ?filename:string -> unit = "append"
[@@mel.send.pipe: t]

external appendFile : string -> file -> ?filename:string -> unit = "append"
[@@mel.send.pipe: t]

external setObject : string -> < .. > Js.t -> ?filename:string -> unit = "set"
[@@mel.send.pipe: t]

external setBlob : string -> blob -> ?filename:string -> unit = "set"
[@@mel.send.pipe: t]

external setFile : string -> file -> ?filename:string -> unit = "set"
[@@mel.send.pipe: t]

external entries : t -> (string * entryValue) Js.Iterator.t = "entries"
[@@mel.send]
7 changes: 3 additions & 4 deletions jscomp/runtime/js.pre.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@
It contains all bindings into [Js] namespace.

{[
[| 1;2;3;4|]
|. Js.Array2.map (fun x -> x + 1 )
|. Js.Array2.reduce (+) 0
|. Js.log
[| 1; 2; 3; 4 |]
|> Js.Array.map ~f:(fun x -> x + 1)
|> Js.Array.reduce ~f:( + ) ~init:0
]}
*)

Expand Down