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

Add FormData under melange.dom #1153

wants to merge 3 commits into from

Conversation

davesnx
Copy link
Member

@davesnx davesnx commented Jul 16, 2024

Moved and adapted FormData from https://github.com/melange-community/melange-fetch/blob/master/src/Fetch.ml

Since reasonml/reason-react#846, React would benefit to have FormData as part of melange.dom rather than their own bindings

PS. Fixes a documentation typo in the first commit.

Comment on lines +7 to +10
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)
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.

@anmonteiro
Copy link
Member

anmonteiro commented Aug 10, 2024

How is this working in DOM? my understanding is that this code is being added to melange.belt:

(modules
:standard
\
node
node_buffer
node_child_process
node_fs
node_module
node_path
node_process
node_process
dom
dom_storage)

this code doesn't compile locally:

        melc jscomp/others/.dom.objs/melange/dom.{cmi,cmj,cmt} (exit 2)
File "jscomp/others/dom.ml", line 296, characters 18-30:
296 | module FormData = Dom_formData
                        ^^^^^^^^^^^^
Error (warning 49 [no-cmi-file]): no cmi file was found in path for module Dom_formData

Copy link
Member

@anmonteiro anmonteiro left a comment

Choose a reason for hiding this comment

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

One issue with form data here is that it sort of breaks the assumption that melange.dom only has types, and no functions.

What do you think about adding the necessary types in this PR and then making a small library for form data using those types?

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 make : unit -> t = "FormData" [@@mel.new]
external append : string -> string -> unit = "append" [@@mel.send.pipe: t]
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 delete : string -> unit = "delete" [@@mel.send.pipe: t]
external get : string -> entryValue option = "get" [@@mel.send.pipe: t]
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants