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

Merged
merged 16 commits into from
Dec 8, 2024
Merged

Add FormData under melange.dom #1153

merged 16 commits into from
Dec 8, 2024

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Totally, the discussion for 0 runtime goes away if we remove this. If it's needed can be added in webapi

Copy link
Member Author

Choose a reason for hiding this comment

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

it's a bit weird that entryValue is provided by this module but unnaccessible from this module, but it's simpler to keep it like this

@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

Copy link
Member Author

Choose a reason for hiding this comment

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

append can't have a filename when it's a string. appendBlob or appendFile is used and have the ?filename there

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

Copy link
Member Author

Choose a reason for hiding this comment

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

same as append. Can't have a filename when it's a string. setObject or setFile or setBlob can be used and have ?filename

@davesnx
Copy link
Member Author

davesnx commented Nov 15, 2024

Do you prefer to keep bindings outside of melange.dom?

There's currently dom_storage (and this PR adds FormData and future prs add Blob and File). Moving those into another lib sounds good melange.web, maybe? Better inside this repo or outside?

@davesnx
Copy link
Member Author

davesnx commented Nov 15, 2024

As far as I know dom is just a dependency of melange.belt, and there's a library (name melange.dom).

@davesnx davesnx requested a review from anmonteiro November 18, 2024 22:16
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.

I think that FormData isn't really specific to JS environments that have a DOM, so this could rather be added to Js.FormData. what do you think?

@davesnx
Copy link
Member Author

davesnx commented Nov 25, 2024

That make sense, moved also https://github.com/melange-re/melange/pull/1218/files into js

@davesnx
Copy link
Member Author

davesnx commented Nov 26, 2024

Don't understand why opam macos fails while ubuntu is ok

@anmonteiro
Copy link
Member

@davesnx what do you think of the new API for set and append? I think we can get away with a single function for each and use @mel.unwrap for polymorphism.

@anmonteiro
Copy link
Member

@davesnx what do you think of the new API for set and append? I think we can get away with a single function for each and use @mel.unwrap for polymorphism.

in 32cf627 I changed it again to take the distinction between blobs / primitives into account (the functions only accept filename as a third argument if the 2nd arg is a Blob).

jscomp/runtime/js_formData.ml Outdated Show resolved Hide resolved
@anmonteiro anmonteiro merged commit 053bcff into main Dec 8, 2024
5 checks passed
@anmonteiro anmonteiro deleted the Add-dom-formdata branch December 8, 2024 06:21
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