Skip to content

Commit f6a77d1

Browse files
committed
Remodel formDataEntryValue
1 parent 469231f commit f6a77d1

File tree

8 files changed

+115
-14
lines changed

8 files changed

+115
-14
lines changed

src/FetchAPI.res

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ type formData = {}
218218

219219
type requestInfo = any
220220

221-
type formDataEntryValue = any
221+
@editor.completeFrom(FormDataEntryValue)
222+
type formDataEntryValue
222223

223224
type requestInit = {
224225
/**

src/FetchAPI/FormData.res

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ external delete: (formData, string) => unit = "delete"
3131
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/FormData/get)
3232
*/
3333
@send
34-
external get: (formData, string) => null<string> = "get"
35-
36-
/**
37-
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/FormData/get)
38-
*/
39-
@send
40-
external getFile: (formData, string) => null<file> = "get"
34+
external get: (formData, string) => null<formDataEntryValue> = "get"
4135

4236
/**
4337
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/FormData/getAll)

src/FetchAPI/FormDataEntryValue.js

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
open Prelude
2+
open FetchAPI
3+
open FileAPI
4+
5+
external fromString: string => formDataEntryValue = "%identity"
6+
external fromFile: file => formDataEntryValue = "%identity"
7+
8+
/**
9+
Represents a decoded version of the abstract `formDataEntryValue` type.
10+
A FormData entry value is either a string or a File.
11+
*/
12+
type decoded =
13+
| String(string)
14+
| File(file)
15+
16+
let decode = (t: formDataEntryValue): decoded => {
17+
if File.isInstanceOf(t) {
18+
File(unsafeConversation(t))
19+
} else {
20+
String(unsafeConversation(t))
21+
}
22+
}

src/FileAPI/File.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FileAPI/File.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ external make: (
1111
~fileName: string,
1212
~options: filePropertyBag=?,
1313
) => file = "File"
14+
15+
let isInstanceOf = (_: 't): bool => %raw(`param instanceof File`)

tests/FetchAPI/FormData__test.js

Lines changed: 31 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/FetchAPI/FormData__test.res

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,31 @@
33
external myForm: DOMAPI.htmlFormElement = "myForm"
44

55
let formData = FormData.make(~form=myForm)
6-
let phone: null<string> = formData->FormData.get("phone")
7-
let image: null<FileAPI.file> = formData->FormData.getFile("image")
6+
7+
// Get a form field - returns formDataEntryValue which could be string or File
8+
let phoneEntry: null<FetchAPI.formDataEntryValue> = formData->FormData.get("phone")
9+
10+
// Decode the entry to handle both string and File cases
11+
let _ = switch phoneEntry->Null.toOption {
12+
| None => Console.log("No phone field")
13+
| Some(entry) =>
14+
switch entry->FormDataEntryValue.decode {
15+
| FormDataEntryValue.String(value) => Console.log(`Phone: ${value}`)
16+
| FormDataEntryValue.File(file) => Console.log(`Unexpected file: ${file.name}`)
17+
}
18+
}
19+
20+
// Get all values for a field (useful for multi-select or multiple file inputs)
21+
let allImages: array<FetchAPI.formDataEntryValue> = formData->FormData.getAll("images")
22+
23+
// Process all entries
24+
let _ = allImages->Array.forEach(entry => {
25+
switch entry->FormDataEntryValue.decode {
26+
| FormDataEntryValue.String(value) => Console.log(`String value: ${value}`)
27+
| FormDataEntryValue.File(file) => Console.log(`File: ${file.name}`)
28+
}
29+
})
30+
31+
// Create formDataEntryValue from string or file
32+
let stringEntry = FormDataEntryValue.fromString("test value")
33+
let fileEntry = FormDataEntryValue.fromFile(File.make(~fileBits=[], ~fileName="test.txt"))

0 commit comments

Comments
 (0)