Skip to content

Commit 332f30e

Browse files
committed
Move fetch to webapi top level, and better integrate it as an internal component
1 parent 70669ac commit 332f30e

16 files changed

+136
-237
lines changed

examples/reason_examples.res renamed to examples/fetch_examples.res

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
open Webapi;
12
let _ = {
23
open Js.Promise
34
Fetch.fetch("/api/hellos/1")
@@ -41,8 +42,8 @@ let _ = {
4142
}
4243

4344
let _ = {
44-
let formData = Fetch.FormData.make()
45-
Fetch.FormData.appendObject(
45+
let formData = Webapi.FormData.make()
46+
Webapi.FormData.appendObject(
4647
formData,
4748
"image0",
4849
{"type": "image/jpg", "uri": "path/to/it", "name": "image0.jpg"},

examples/ocaml_examples.res

Lines changed: 0 additions & 43 deletions
This file was deleted.

lib/js/examples/reason_examples.js renamed to lib/js/examples/fetch_examples.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
'use strict';
22

3-
var Fetch = require("../src/Webapi/Fetch/Fetch.js");
43
var Js_json = require("rescript/lib/js/js_json.js");
54
var Belt_Option = require("rescript/lib/js/belt_Option.js");
65
var Caml_option = require("rescript/lib/js/caml_option.js");
6+
var Webapi__Fetch = require("../src/Webapi/Webapi__Fetch.js");
77

88
fetch("/api/hellos/1").then(function (prim) {
99
return prim.text();
1010
}).then(function (text) {
1111
return Promise.resolve((console.log(text), undefined));
1212
});
1313

14-
fetch("/api/hello", Fetch.RequestInit.make(/* Post */2, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then(function (prim) {
14+
fetch("/api/hello", Webapi__Fetch.RequestInit.make(/* Post */2, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then(function (prim) {
1515
return prim.text();
1616
}).then(function (text) {
1717
return Promise.resolve((console.log(text), undefined));
@@ -33,7 +33,7 @@ var payload = {};
3333

3434
payload["hello"] = "world";
3535

36-
fetch("/api/hello", Fetch.RequestInit.make(/* Post */2, {
36+
fetch("/api/hello", Webapi__Fetch.RequestInit.make(/* Post */2, {
3737
"Content-Type": "application/json"
3838
}, Caml_option.some(JSON.stringify(payload)), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then(function (prim) {
3939
return prim.json();
@@ -47,7 +47,7 @@ formData.append("image0", {
4747
name: "image0.jpg"
4848
}, undefined);
4949

50-
fetch("/api/upload", Fetch.RequestInit.make(/* Post */2, {
50+
fetch("/api/upload", Webapi__Fetch.RequestInit.make(/* Post */2, {
5151
Accept: "*"
5252
}, Caml_option.some(formData), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined)(undefined)).then(function (prim) {
5353
return prim.json();

lib/js/examples/ocaml_examples.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Webapi.res

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
module Base64 = Webapi__Base64
2-
3-
/** @since 0.18.0 */
42
module Blob = Webapi__Blob
53
module Canvas = Webapi__Canvas
64
module Dom = Webapi__Dom
75
module File = Webapi__File
6+
module Fetch = Webapi__Fetch
7+
module FormData = Webapi__FormData
88

9-
/** Re-export from [bs-fetch] for convenience. To use, you will also
10-
need to add the [bs-fetch] package as a dependency.
11-
12-
To get the [FormData] of an HTML form, use
13-
[Webapi.Dom.HtmlFormElement.data].
14-
15-
@since 0.18.0 */
16-
module FormData = Fetch.FormData
17-
18-
/** Re-export from [bs-fetch] for convenience. See also
19-
{!module:FormData}.
20-
21-
@since 0.18.0 */
22-
module Iterator = FormData.Iterator
9+
module Iterator = Webapi__Iterator
2310

2411
module Performance = Webapi__Performance
2512

src/Webapi/Dom/Webapi__Dom__HtmlFormElement.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module Impl = (
3636
@send external checkValidity: t_htmlFormElement => bool = ""
3737
@send external reportValidity: t_htmlFormElement => bool = ""
3838

39-
/** @since 0.18.0 */ @new external data: T.t => Fetch.FormData.t = "FormData"
39+
/** @since 0.18.0 */ @new external data: T.t => Webapi__FormData.t = "FormData"
4040
}
4141

4242
type t = Dom.htmlFormElement

src/Webapi/Fetch/Fetch__Iterator.res

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/Webapi/Fetch/Fetch__Iterator.resi

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Webapi/Fetch/Fetch.res renamed to src/Webapi/Webapi__Fetch.res

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type signal
1111
/* external */
1212
type arrayBuffer /* TypedArray */
1313
type bufferSource /* Web IDL, either an arrayBuffer or arrayBufferView */
14-
type formData /* XMLHttpRequest */
14+
type formData = Webapi__FormData.t/* XMLHttpRequest */
1515
type readableStream = Webapi__ReadableStream.t /* Streams */
1616
type urlSearchParams /* URL */
1717

@@ -437,45 +437,8 @@ module Response = {
437437
@send external clone: t => t = "clone"
438438
}
439439

440-
module FormData = {
441-
module EntryValue = {
442-
type t
443-
444-
let classify: t => [> #String(string) | #File(file)] = t =>
445-
if Js.typeof(t) == "string" {
446-
#String(Obj.magic(t))
447-
} else {
448-
#File(Obj.magic(t))
449-
}
450-
}
451-
452-
module Iterator = Fetch__Iterator
453-
type t = formData
454-
455-
@new external make: unit => t = "FormData"
456-
@send external append: (t, string, string) => unit = "append"
457-
@send external delete: (t, string) => unit = "delete"
458-
@send external get: (t, string) => option<EntryValue.t> = "get"
459-
@send external getAll: (t, string) => array<EntryValue.t> = "getAll"
460-
@send external set: (t, string, string) => unit = "set"
461-
@send external has: (t, string) => bool = "has"
462-
@send external keys: t => Iterator.t<string> = "keys"
463-
@send external values: t => Iterator.t<EntryValue.t> = "values"
464-
465-
@send external appendObject: (t, string, {..}, ~filename: string=?, unit) => unit = "append"
466-
467-
@send external appendBlob: (t, string, blob, ~filename: string=?, unit) => unit = "append"
468-
469-
@send external appendFile: (t, string, file, ~filename: string=?, unit) => unit = "append"
470-
471-
@send external setObject: (t, string, {..}, ~filename: string=?, unit) => unit = "set"
472-
473-
@send external setBlob: (t, string, blob, ~filename: string=?, unit) => unit = "set"
474-
475-
@send external setFile: (t, string, file, ~filename: string=?, unit) => unit = "set"
476-
477-
@send external entries: t => Iterator.t<(string, EntryValue.t)> = "entries"
478-
}
440+
/** Alias for anyone upgrading */
441+
module FormData = Webapi__FormData
479442

480443
@val external fetch: string => Js.Promise.t<response> = "fetch"
481444
@val external fetchWithInit: (string, requestInit) => Js.Promise.t<response> = "fetch"

src/Webapi/Fetch/Fetch.resi renamed to src/Webapi/Webapi__Fetch.resi

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type requestInit
99
/* external */
1010
type arrayBuffer /* TypedArray */
1111
type bufferSource /* Web IDL, either an arrayBuffer or arrayBufferView */
12-
type formData /* XMLHttpRequest */
12+
type formData = Webapi__FormData.t /* XMLHttpRequest */
1313
type readableStream /* Streams */
1414
type urlSearchParams /* URL */
1515
type abortController
@@ -236,46 +236,8 @@ module Response: {
236236
@send external text: t => Js.Promise.t<string> = "text"
237237
}
238238

239-
module FormData: {
240-
module EntryValue: {
241-
@ocaml.doc(" This represents a
242-
{{: https://developer.mozilla.org/en-US/docs/Web/API/FormDataEntryValue} FormDataEntryValue}. ")
243-
type t
244-
245-
@ocaml.doc(" [classify entryValue] safely casts the [entryValue] to its
246-
correct runtime type. ")
247-
let classify: t => [> #String(string) | #File(file)]
248-
}
249-
250-
module Iterator = Fetch__Iterator
251-
type t = formData
252-
253-
@new external make: unit => t = "FormData"
254-
@send external append: t => (string, string) => unit = "append"
255-
@send external delete: t => string => unit = "delete"
256-
@send external get: t => string => option<EntryValue.t> = "get"
257-
@send external getAll: t => string => array<EntryValue.t> = "getAll"
258-
@send external set: t => (string, string) => unit = "set"
259-
@send external has: t => string => bool = "has"
260-
@send external keys: t => Iterator.t<string> = "keys"
261-
@send external values: t => Iterator.t<EntryValue.t> = "values"
262-
263-
@send @ocaml.doc(" This is for React Native compatibility purposes ")
264-
external appendObject: t => (string, {..}, ~filename: string=?, unit) => unit = "append"
265-
266-
@send external appendBlob: t => (string, blob, ~filename: string=?, unit) => unit = "append"
267-
268-
@send external appendFile: t => (string, file, ~filename: string=?, unit) => unit = "append"
269-
270-
@send @ocaml.doc(" This is for React Native compatibility purposes ")
271-
external setObject: t => (string, {..}, ~filename: string=?, unit) => unit = "set"
272-
273-
@send external setBlob: t => (string, blob, ~filename: string=?, unit) => unit = "set"
274-
275-
@send external setFile: t => (string, file, ~filename: string=?, unit) => unit = "set"
276-
277-
@send external entries: t => Iterator.t<(string, EntryValue.t)> = "entries"
278-
}
239+
/** Alias for anyone upgrading */
240+
module FormData = Webapi__FormData
279241

280242
@val external fetch: string => Js.Promise.t<response> = "fetch"
281243
@val external fetchWithInit: (string, requestInit) => Js.Promise.t<response> = "fetch"

0 commit comments

Comments
 (0)