Skip to content

Commit 1106232

Browse files
bloodyowlcknitt
authored andcommitted
proposal: make FormData more usable to get values
this **kinda** goes against the zero-cost philosophy, but I don't see a world where users would not have to reimplement those.
1 parent 1694f22 commit 1106232

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

src/ReactDOM.bs.js

+38-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ReactDOM.res

+32-3
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,43 @@ module Client = {
2828
// Very rudimentary form data bindings
2929
module FormData = {
3030
type t
31-
type value
31+
type file
32+
33+
type formValue =
34+
| String(string)
35+
| File(file)
3236

3337
@new external make: unit => t = "FormData"
3438

3539
@send external append: (t, string, ~filename: string=?) => unit = "append"
3640
@send external delete: (t, string) => unit = "delete"
37-
@send external get: (t, string) => option<value> = "get"
38-
@send external getAll: (t, string) => array<value> = "getAll"
41+
@return(nullable) @send external getUnsafe: (t, string) => option<'a> = "get"
42+
@send external getAllUnsafe: (t, string) => array<'a> = "getAll"
43+
44+
let getString = (formData, name) => {
45+
switch formData->getUnsafe(name) {
46+
| Some(value) => Js.typeof(value) === "string" ? Some(value) : None
47+
| _ => None
48+
}
49+
}
50+
51+
external _asFile: 'a => file = "%identity"
52+
53+
let getFile = (formData, name) => {
54+
switch formData->getUnsafe(name) {
55+
| Some(value) => Js.typeof(value) === "string" ? None : Some(value->_asFile)
56+
| _ => None
57+
}
58+
}
59+
60+
let getAll = (t, string) => {
61+
t
62+
->getAllUnsafe(string)
63+
->Js.Array2.map(value => {
64+
Js.typeof(value) === "string" ? String(value) : File(value->_asFile)
65+
})
66+
}
67+
3968
@send external set: (string, string) => unit = "set"
4069
@send external has: string => bool = "has"
4170
// @send external keys: t => Iterator.t<string> = "keys";

0 commit comments

Comments
 (0)