@plowtech/rescript-fetch 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -13
- package/rescript.json +1 -20
- package/src/Fetch.res +28 -28
- package/src/Fetch.res.js +162 -168
- package/src/Fetch.resi +21 -21
- package/tests/FetchTest.res +0 -155
- package/tests/FetchTest.res.js +0 -162
- package/tests/utils/Assert.res +0 -20
- package/tests/utils/Assert.res.js +0 -25
- package/tests/utils/ReactTest.res +0 -47
- package/tests/utils/ReactTest.res.js +0 -41
package/package.json
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plowtech/rescript-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "ReScript Bindings for the Fetch API",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "retest --with-dom tests/*.res.js",
|
|
8
7
|
"res:build": "rescript",
|
|
9
8
|
"res:dev": "rescript -w",
|
|
10
9
|
"res:clean": "rescript clean"
|
|
@@ -23,16 +22,6 @@
|
|
|
23
22
|
},
|
|
24
23
|
"homepage": "https://github.com/plow-technologies/rescript-fetch#readme",
|
|
25
24
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
-
"rescript": "^11.1.3"
|
|
28
|
-
},
|
|
29
|
-
"devDependencies": {
|
|
30
|
-
"@rescript/react": "^0.13.0",
|
|
31
|
-
"@testing-library/dom": "^10.4.0",
|
|
32
|
-
"@testing-library/react": "^16.0.0",
|
|
33
|
-
"react": "^18.3.1",
|
|
34
|
-
"react-dom": "^18.3.1",
|
|
35
|
-
"rescript-test": "^7.0.1",
|
|
36
|
-
"rescript-webapi": "^0.9.1"
|
|
25
|
+
"rescript": "^12.1.0"
|
|
37
26
|
}
|
|
38
27
|
}
|
package/rescript.json
CHANGED
|
@@ -4,11 +4,6 @@
|
|
|
4
4
|
{
|
|
5
5
|
"dir": "src",
|
|
6
6
|
"subdirs": true
|
|
7
|
-
},
|
|
8
|
-
{
|
|
9
|
-
"dir": "tests",
|
|
10
|
-
"subdirs": true,
|
|
11
|
-
"type": "dev"
|
|
12
7
|
}
|
|
13
8
|
],
|
|
14
9
|
"package-specs": [
|
|
@@ -17,19 +12,5 @@
|
|
|
17
12
|
"in-source": true
|
|
18
13
|
}
|
|
19
14
|
],
|
|
20
|
-
"suffix": ".res.js"
|
|
21
|
-
"bs-dependencies": [
|
|
22
|
-
"@rescript/core",
|
|
23
|
-
"@rescript/react"
|
|
24
|
-
],
|
|
25
|
-
"bs-dev-dependencies": [
|
|
26
|
-
"rescript-test",
|
|
27
|
-
"rescript-webapi"
|
|
28
|
-
],
|
|
29
|
-
"bsc-flags": [
|
|
30
|
-
"-open RescriptCore"
|
|
31
|
-
],
|
|
32
|
-
"jsx": {
|
|
33
|
-
"version": 4
|
|
34
|
-
}
|
|
15
|
+
"suffix": ".res.js"
|
|
35
16
|
}
|
package/src/Fetch.res
CHANGED
|
@@ -114,7 +114,7 @@ let decodeReferrerPolicy = (referrerPolicy: string) =>
|
|
|
114
114
|
| "origin-when-cross-origin" => OriginWhenCrossOrigin
|
|
115
115
|
| "strict-origin-when-cross-origin" => StrictOriginWhenCrossOrigin
|
|
116
116
|
| "unsafe-url" => UnsafeUrl
|
|
117
|
-
| e =>
|
|
117
|
+
| e => throw(UnknownReferrerPolicy(e))
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
type requestType =
|
|
@@ -139,7 +139,7 @@ let decodeRequestType = (requestType: string) =>
|
|
|
139
139
|
| "style" => Style
|
|
140
140
|
| "track" => Track
|
|
141
141
|
| "video" => Video
|
|
142
|
-
| e =>
|
|
142
|
+
| e => throw(UnknownRequestType(e))
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
type requestDestination =
|
|
@@ -178,7 +178,7 @@ let decodeRequestDestination = (requestDestination: string) =>
|
|
|
178
178
|
| "style" => Style
|
|
179
179
|
| "worker" => Worker
|
|
180
180
|
| "xslt" => Xslt
|
|
181
|
-
| e =>
|
|
181
|
+
| e => throw(UnknownRequestDestination(e))
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
type requestMode =
|
|
@@ -203,7 +203,7 @@ let decodeRequestMode = (requestMode: string) =>
|
|
|
203
203
|
| "same-origin" => SameOrigin
|
|
204
204
|
| "no-cors" => NoCORS
|
|
205
205
|
| "cors" => CORS
|
|
206
|
-
| e =>
|
|
206
|
+
| e => throw(UnknownRequestMode(e))
|
|
207
207
|
}
|
|
208
208
|
|
|
209
209
|
type requestCredentials =
|
|
@@ -225,7 +225,7 @@ let decodeRequestCredentials = (requestCredentials: string) =>
|
|
|
225
225
|
| "omit" => Omit
|
|
226
226
|
| "same-origin" => SameOrigin
|
|
227
227
|
| "include" => Include
|
|
228
|
-
| e =>
|
|
228
|
+
| e => throw(UnknownRequestCredentials(e))
|
|
229
229
|
}
|
|
230
230
|
|
|
231
231
|
type requestCache =
|
|
@@ -256,7 +256,7 @@ let decodeRequestCache = (requestCache: string) =>
|
|
|
256
256
|
| "no-cache" => NoCache
|
|
257
257
|
| "force-cache" => ForceCache
|
|
258
258
|
| "only-if-cached" => OnlyIfCached
|
|
259
|
-
| e =>
|
|
259
|
+
| e => throw(UnknownRequestCache(e))
|
|
260
260
|
}
|
|
261
261
|
|
|
262
262
|
type requestRedirect =
|
|
@@ -278,14 +278,14 @@ let decodeRequestRedirect = (requestRedirect: string) =>
|
|
|
278
278
|
| "follow" => Follow
|
|
279
279
|
| "error" => Error
|
|
280
280
|
| "manual" => Manual
|
|
281
|
-
| e =>
|
|
281
|
+
| e => throw(UnknownRequestRedirect(e))
|
|
282
282
|
}
|
|
283
283
|
|
|
284
284
|
module HeadersInit = {
|
|
285
285
|
type t = headersInit
|
|
286
286
|
|
|
287
|
-
external make:
|
|
288
|
-
external makeWithDict:
|
|
287
|
+
external make: {..} => t = "%identity"
|
|
288
|
+
external makeWithDict: dict<string> => t = "%identity"
|
|
289
289
|
external makeWithArray: array<(string, string)> => t = "%identity"
|
|
290
290
|
}
|
|
291
291
|
|
|
@@ -334,19 +334,19 @@ module Body = {
|
|
|
334
334
|
external bodyUsed: t => bool = "bodyUsed"
|
|
335
335
|
|
|
336
336
|
@send
|
|
337
|
-
external arrayBuffer: t =>
|
|
337
|
+
external arrayBuffer: t => promise<arrayBuffer> = "arrayBuffer"
|
|
338
338
|
|
|
339
339
|
@send
|
|
340
|
-
external blob: t =>
|
|
340
|
+
external blob: t => promise<blob> = "blob"
|
|
341
341
|
|
|
342
342
|
@send
|
|
343
|
-
external formData: t =>
|
|
343
|
+
external formData: t => promise<formData> = "formData"
|
|
344
344
|
|
|
345
345
|
@send
|
|
346
|
-
external json: t =>
|
|
346
|
+
external json: t => promise<JSON.t> = "json"
|
|
347
347
|
|
|
348
348
|
@send
|
|
349
|
-
external text: t =>
|
|
349
|
+
external text: t => promise<string> = "text"
|
|
350
350
|
}
|
|
351
351
|
|
|
352
352
|
module RequestInit = {
|
|
@@ -473,19 +473,19 @@ module Request = {
|
|
|
473
473
|
external bodyUsed: t => bool = "bodyUsed"
|
|
474
474
|
|
|
475
475
|
@send
|
|
476
|
-
external arrayBuffer: t =>
|
|
476
|
+
external arrayBuffer: t => promise<arrayBuffer> = "arrayBuffer"
|
|
477
477
|
|
|
478
478
|
@send
|
|
479
|
-
external blob: t =>
|
|
479
|
+
external blob: t => promise<blob> = "blob"
|
|
480
480
|
|
|
481
481
|
@send
|
|
482
|
-
external formData: t =>
|
|
482
|
+
external formData: t => promise<formData> = "formData"
|
|
483
483
|
|
|
484
484
|
@send
|
|
485
|
-
external json: t =>
|
|
485
|
+
external json: t => promise<JSON.t> = "json"
|
|
486
486
|
|
|
487
487
|
@send
|
|
488
|
-
external text: t =>
|
|
488
|
+
external text: t => promise<string> = "text"
|
|
489
489
|
}
|
|
490
490
|
|
|
491
491
|
module Response = {
|
|
@@ -533,29 +533,29 @@ module Response = {
|
|
|
533
533
|
external bodyUsed: t => bool = "bodyUsed"
|
|
534
534
|
|
|
535
535
|
@send
|
|
536
|
-
external arrayBuffer: t =>
|
|
536
|
+
external arrayBuffer: t => promise<arrayBuffer> = "arrayBuffer"
|
|
537
537
|
|
|
538
538
|
@send
|
|
539
|
-
external blob: t =>
|
|
539
|
+
external blob: t => promise<blob> = "blob"
|
|
540
540
|
|
|
541
541
|
@send
|
|
542
|
-
external formData: t =>
|
|
542
|
+
external formData: t => promise<formData> = "formData"
|
|
543
543
|
|
|
544
544
|
@send
|
|
545
|
-
external json: t =>
|
|
545
|
+
external json: t => promise<JSON.t> = "json"
|
|
546
546
|
|
|
547
547
|
@send
|
|
548
|
-
external text: t =>
|
|
548
|
+
external text: t => promise<string> = "text"
|
|
549
549
|
}
|
|
550
550
|
|
|
551
551
|
@val
|
|
552
|
-
external fetch: string =>
|
|
552
|
+
external fetch: string => promise<response> = "fetch"
|
|
553
553
|
|
|
554
554
|
@val
|
|
555
|
-
external fetchWithInit: (string, requestInit) =>
|
|
555
|
+
external fetchWithInit: (string, requestInit) => promise<response> = "fetch"
|
|
556
556
|
|
|
557
557
|
@val
|
|
558
|
-
external fetchWithRequest: request =>
|
|
558
|
+
external fetchWithRequest: request => promise<response> = "fetch"
|
|
559
559
|
|
|
560
560
|
@val
|
|
561
|
-
external fetchWithRequestInit: (request, requestInit) =>
|
|
561
|
+
external fetchWithRequestInit: (request, requestInit) => promise<response> = "fetch"
|
package/src/Fetch.res.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
let Stdlib_Option = require("@rescript/runtime/lib/js/Stdlib_Option.js");
|
|
5
|
+
let Primitive_option = require("@rescript/runtime/lib/js/Primitive_option.js");
|
|
6
|
+
let Primitive_exceptions = require("@rescript/runtime/lib/js/Primitive_exceptions.js");
|
|
7
7
|
|
|
8
8
|
function encodeRequestMethod(requestMethod) {
|
|
9
9
|
if (typeof requestMethod === "object") {
|
|
@@ -11,163 +11,157 @@ function encodeRequestMethod(requestMethod) {
|
|
|
11
11
|
}
|
|
12
12
|
switch (requestMethod) {
|
|
13
13
|
case "Get" :
|
|
14
|
-
|
|
14
|
+
return "GET";
|
|
15
15
|
case "Head" :
|
|
16
|
-
|
|
16
|
+
return "HEAD";
|
|
17
17
|
case "Post" :
|
|
18
|
-
|
|
18
|
+
return "POST";
|
|
19
19
|
case "Put" :
|
|
20
|
-
|
|
20
|
+
return "PUT";
|
|
21
21
|
case "Delete" :
|
|
22
|
-
|
|
22
|
+
return "DELETE";
|
|
23
23
|
case "Connect" :
|
|
24
|
-
|
|
24
|
+
return "CONNECT";
|
|
25
25
|
case "Options" :
|
|
26
|
-
|
|
26
|
+
return "OPTIONS";
|
|
27
27
|
case "Trace" :
|
|
28
|
-
|
|
28
|
+
return "TRACE";
|
|
29
29
|
case "Patch" :
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
return "PATCH";
|
|
32
31
|
}
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
|
|
34
|
+
let AbortController = {};
|
|
36
35
|
|
|
37
36
|
function encodeReferrerPolicy(referrerPolicy) {
|
|
38
37
|
switch (referrerPolicy) {
|
|
39
38
|
case "None" :
|
|
40
|
-
|
|
39
|
+
return "";
|
|
41
40
|
case "NoReferrer" :
|
|
42
|
-
|
|
41
|
+
return "no-referrer";
|
|
43
42
|
case "NoReferrerWhenDowngrade" :
|
|
44
|
-
|
|
43
|
+
return "no-referrer-when-downgrade";
|
|
45
44
|
case "SameOrigin" :
|
|
46
|
-
|
|
45
|
+
return "same-origin";
|
|
47
46
|
case "Origin" :
|
|
48
|
-
|
|
47
|
+
return "origin";
|
|
49
48
|
case "StrictOrigin" :
|
|
50
|
-
|
|
49
|
+
return "strict-origin";
|
|
51
50
|
case "OriginWhenCrossOrigin" :
|
|
52
|
-
|
|
51
|
+
return "origin-when-cross-origin";
|
|
53
52
|
case "StrictOriginWhenCrossOrigin" :
|
|
54
|
-
|
|
53
|
+
return "strict-origin-when-cross-origin";
|
|
55
54
|
case "UnsafeUrl" :
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
return "unsafe-url";
|
|
58
56
|
}
|
|
59
57
|
}
|
|
60
58
|
|
|
61
|
-
|
|
59
|
+
let UnknownReferrerPolicy = /* @__PURE__ */Primitive_exceptions.create("Fetch.UnknownReferrerPolicy");
|
|
62
60
|
|
|
63
|
-
|
|
61
|
+
let UnknownRequestType = /* @__PURE__ */Primitive_exceptions.create("Fetch.UnknownRequestType");
|
|
64
62
|
|
|
65
|
-
|
|
63
|
+
let UnknownRequestDestination = /* @__PURE__ */Primitive_exceptions.create("Fetch.UnknownRequestDestination");
|
|
66
64
|
|
|
67
65
|
function encodeRequestMode(requestMode) {
|
|
68
66
|
switch (requestMode) {
|
|
69
67
|
case "Navigate" :
|
|
70
|
-
|
|
68
|
+
return "navigate";
|
|
71
69
|
case "SameOrigin" :
|
|
72
|
-
|
|
70
|
+
return "same-origin";
|
|
73
71
|
case "NoCORS" :
|
|
74
|
-
|
|
72
|
+
return "no-cors";
|
|
75
73
|
case "CORS" :
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
return "cors";
|
|
78
75
|
}
|
|
79
76
|
}
|
|
80
77
|
|
|
81
|
-
|
|
78
|
+
let UnknownRequestMode = /* @__PURE__ */Primitive_exceptions.create("Fetch.UnknownRequestMode");
|
|
82
79
|
|
|
83
80
|
function encodeRequestCredentials(requestCredentials) {
|
|
84
81
|
switch (requestCredentials) {
|
|
85
82
|
case "Omit" :
|
|
86
|
-
|
|
83
|
+
return "omit";
|
|
87
84
|
case "SameOrigin" :
|
|
88
|
-
|
|
85
|
+
return "same-origin";
|
|
89
86
|
case "Include" :
|
|
90
|
-
|
|
91
|
-
|
|
87
|
+
return "include";
|
|
92
88
|
}
|
|
93
89
|
}
|
|
94
90
|
|
|
95
|
-
|
|
91
|
+
let UnknownRequestCredentials = /* @__PURE__ */Primitive_exceptions.create("Fetch.UnknownRequestCredentials");
|
|
96
92
|
|
|
97
93
|
function encodeRequestCache(requestCache) {
|
|
98
94
|
switch (requestCache) {
|
|
99
95
|
case "Default" :
|
|
100
|
-
|
|
96
|
+
return "default";
|
|
101
97
|
case "NoStore" :
|
|
102
|
-
|
|
98
|
+
return "no-store";
|
|
103
99
|
case "Reload" :
|
|
104
|
-
|
|
100
|
+
return "reload";
|
|
105
101
|
case "NoCache" :
|
|
106
|
-
|
|
102
|
+
return "no-cache";
|
|
107
103
|
case "ForceCache" :
|
|
108
|
-
|
|
104
|
+
return "force-cache";
|
|
109
105
|
case "OnlyIfCached" :
|
|
110
|
-
|
|
111
|
-
|
|
106
|
+
return "only-if-cached";
|
|
112
107
|
}
|
|
113
108
|
}
|
|
114
109
|
|
|
115
|
-
|
|
110
|
+
let UnknownRequestCache = /* @__PURE__ */Primitive_exceptions.create("Fetch.UnknownRequestCache");
|
|
116
111
|
|
|
117
112
|
function encodeRequestRedirect(requestRedirect) {
|
|
118
113
|
switch (requestRedirect) {
|
|
119
114
|
case "Follow" :
|
|
120
|
-
|
|
115
|
+
return "follow";
|
|
121
116
|
case "Error" :
|
|
122
|
-
|
|
117
|
+
return "error";
|
|
123
118
|
case "Manual" :
|
|
124
|
-
|
|
125
|
-
|
|
119
|
+
return "manual";
|
|
126
120
|
}
|
|
127
121
|
}
|
|
128
122
|
|
|
129
|
-
|
|
123
|
+
let UnknownRequestRedirect = /* @__PURE__ */Primitive_exceptions.create("Fetch.UnknownRequestRedirect");
|
|
130
124
|
|
|
131
|
-
|
|
125
|
+
let HeadersInit = {};
|
|
132
126
|
|
|
133
|
-
|
|
127
|
+
let Headers = {};
|
|
134
128
|
|
|
135
|
-
|
|
129
|
+
let BodyInit = {};
|
|
136
130
|
|
|
137
|
-
|
|
131
|
+
let Body = {};
|
|
138
132
|
|
|
139
133
|
function make(method_, headers, body, referrer, referrerPolicy, mode, credentials, cache, redirect, integrity, keepalive, signal, param) {
|
|
140
|
-
|
|
141
|
-
|
|
134
|
+
let tmp = {};
|
|
135
|
+
let tmp$1 = Stdlib_Option.map(method_, encodeRequestMethod);
|
|
142
136
|
if (tmp$1 !== undefined) {
|
|
143
137
|
tmp.method = tmp$1;
|
|
144
138
|
}
|
|
145
139
|
if (headers !== undefined) {
|
|
146
|
-
tmp.headers =
|
|
140
|
+
tmp.headers = Primitive_option.valFromOption(headers);
|
|
147
141
|
}
|
|
148
142
|
if (body !== undefined) {
|
|
149
|
-
tmp.body =
|
|
143
|
+
tmp.body = Primitive_option.valFromOption(body);
|
|
150
144
|
}
|
|
151
145
|
if (referrer !== undefined) {
|
|
152
146
|
tmp.referrer = referrer;
|
|
153
147
|
}
|
|
154
|
-
|
|
148
|
+
let tmp$2 = Stdlib_Option.map(referrerPolicy, encodeReferrerPolicy);
|
|
155
149
|
if (tmp$2 !== undefined) {
|
|
156
150
|
tmp.referrerPolicy = tmp$2;
|
|
157
151
|
}
|
|
158
|
-
|
|
152
|
+
let tmp$3 = Stdlib_Option.map(mode, encodeRequestMode);
|
|
159
153
|
if (tmp$3 !== undefined) {
|
|
160
154
|
tmp.mode = tmp$3;
|
|
161
155
|
}
|
|
162
|
-
|
|
156
|
+
let tmp$4 = Stdlib_Option.map(credentials, encodeRequestCredentials);
|
|
163
157
|
if (tmp$4 !== undefined) {
|
|
164
158
|
tmp.credentials = tmp$4;
|
|
165
159
|
}
|
|
166
|
-
|
|
160
|
+
let tmp$5 = Stdlib_Option.map(cache, encodeRequestCache);
|
|
167
161
|
if (tmp$5 !== undefined) {
|
|
168
162
|
tmp.cache = tmp$5;
|
|
169
163
|
}
|
|
170
|
-
|
|
164
|
+
let tmp$6 = Stdlib_Option.map(redirect, encodeRequestRedirect);
|
|
171
165
|
if (tmp$6 !== undefined) {
|
|
172
166
|
tmp.redirect = tmp$6;
|
|
173
167
|
}
|
|
@@ -178,225 +172,225 @@ function make(method_, headers, body, referrer, referrerPolicy, mode, credential
|
|
|
178
172
|
tmp.keepalive = keepalive;
|
|
179
173
|
}
|
|
180
174
|
if (signal !== undefined) {
|
|
181
|
-
tmp.signal =
|
|
175
|
+
tmp.signal = Primitive_option.valFromOption(signal);
|
|
182
176
|
}
|
|
183
177
|
return tmp;
|
|
184
178
|
}
|
|
185
179
|
|
|
186
|
-
|
|
180
|
+
let RequestInit = {
|
|
187
181
|
make: make
|
|
188
182
|
};
|
|
189
183
|
|
|
190
184
|
function method_(self) {
|
|
191
|
-
|
|
185
|
+
let requestMethod = self.method;
|
|
192
186
|
switch (requestMethod) {
|
|
193
187
|
case "CONNECT" :
|
|
194
|
-
|
|
188
|
+
return "Connect";
|
|
195
189
|
case "DELETE" :
|
|
196
|
-
|
|
190
|
+
return "Delete";
|
|
197
191
|
case "GET" :
|
|
198
|
-
|
|
192
|
+
return "Get";
|
|
199
193
|
case "HEAD" :
|
|
200
|
-
|
|
194
|
+
return "Head";
|
|
201
195
|
case "OPTIONS" :
|
|
202
|
-
|
|
196
|
+
return "Options";
|
|
203
197
|
case "PATCH" :
|
|
204
|
-
|
|
198
|
+
return "Patch";
|
|
205
199
|
case "POST" :
|
|
206
|
-
|
|
200
|
+
return "Post";
|
|
207
201
|
case "PUT" :
|
|
208
|
-
|
|
202
|
+
return "Put";
|
|
209
203
|
case "TRACE" :
|
|
210
|
-
|
|
204
|
+
return "Trace";
|
|
211
205
|
default:
|
|
212
206
|
return {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
207
|
+
TAG: "Other",
|
|
208
|
+
_0: requestMethod
|
|
209
|
+
};
|
|
216
210
|
}
|
|
217
211
|
}
|
|
218
212
|
|
|
219
213
|
function type_(self) {
|
|
220
|
-
|
|
214
|
+
let requestType = self.type;
|
|
221
215
|
switch (requestType) {
|
|
222
216
|
case "" :
|
|
223
|
-
|
|
217
|
+
return "None";
|
|
224
218
|
case "audio" :
|
|
225
|
-
|
|
219
|
+
return "Audio";
|
|
226
220
|
case "font" :
|
|
227
|
-
|
|
221
|
+
return "Font";
|
|
228
222
|
case "image" :
|
|
229
|
-
|
|
223
|
+
return "Image";
|
|
230
224
|
case "script" :
|
|
231
|
-
|
|
225
|
+
return "Script";
|
|
232
226
|
case "style" :
|
|
233
|
-
|
|
227
|
+
return "Style";
|
|
234
228
|
case "track" :
|
|
235
|
-
|
|
229
|
+
return "Track";
|
|
236
230
|
case "video" :
|
|
237
|
-
|
|
231
|
+
return "Video";
|
|
238
232
|
default:
|
|
239
233
|
throw {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
234
|
+
RE_EXN_ID: UnknownRequestType,
|
|
235
|
+
_1: requestType,
|
|
236
|
+
Error: new Error()
|
|
237
|
+
};
|
|
244
238
|
}
|
|
245
239
|
}
|
|
246
240
|
|
|
247
241
|
function destination(self) {
|
|
248
|
-
|
|
242
|
+
let requestDestination = self.destination;
|
|
249
243
|
switch (requestDestination) {
|
|
250
244
|
case "" :
|
|
251
|
-
|
|
245
|
+
return "None";
|
|
252
246
|
case "document" :
|
|
253
|
-
|
|
247
|
+
return "Document";
|
|
254
248
|
case "embed" :
|
|
255
|
-
|
|
249
|
+
return "Embed";
|
|
256
250
|
case "font" :
|
|
257
|
-
|
|
251
|
+
return "Font";
|
|
258
252
|
case "image" :
|
|
259
|
-
|
|
253
|
+
return "Image";
|
|
260
254
|
case "manifest" :
|
|
261
|
-
|
|
255
|
+
return "Manifest";
|
|
262
256
|
case "media" :
|
|
263
|
-
|
|
257
|
+
return "Media";
|
|
264
258
|
case "object" :
|
|
265
|
-
|
|
259
|
+
return "Object";
|
|
266
260
|
case "report" :
|
|
267
|
-
|
|
261
|
+
return "Report";
|
|
268
262
|
case "script" :
|
|
269
|
-
|
|
263
|
+
return "Script";
|
|
270
264
|
case "serviceworker" :
|
|
271
|
-
|
|
265
|
+
return "ServiceWorker";
|
|
272
266
|
case "sharedworker" :
|
|
273
|
-
|
|
267
|
+
return "SharedWorker";
|
|
274
268
|
case "style" :
|
|
275
|
-
|
|
269
|
+
return "Style";
|
|
276
270
|
case "worker" :
|
|
277
|
-
|
|
271
|
+
return "Worker";
|
|
278
272
|
case "xslt" :
|
|
279
|
-
|
|
273
|
+
return "Xslt";
|
|
280
274
|
default:
|
|
281
275
|
throw {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
276
|
+
RE_EXN_ID: UnknownRequestDestination,
|
|
277
|
+
_1: requestDestination,
|
|
278
|
+
Error: new Error()
|
|
279
|
+
};
|
|
286
280
|
}
|
|
287
281
|
}
|
|
288
282
|
|
|
289
283
|
function referrerPolicy(self) {
|
|
290
|
-
|
|
284
|
+
let referrerPolicy$1 = self.referrerPolicy;
|
|
291
285
|
switch (referrerPolicy$1) {
|
|
292
286
|
case "" :
|
|
293
|
-
|
|
287
|
+
return "None";
|
|
294
288
|
case "no-referrer" :
|
|
295
|
-
|
|
289
|
+
return "NoReferrer";
|
|
296
290
|
case "no-referrer-when-downgrade" :
|
|
297
|
-
|
|
291
|
+
return "NoReferrerWhenDowngrade";
|
|
298
292
|
case "origin" :
|
|
299
|
-
|
|
293
|
+
return "Origin";
|
|
300
294
|
case "origin-when-cross-origin" :
|
|
301
|
-
|
|
295
|
+
return "OriginWhenCrossOrigin";
|
|
302
296
|
case "same-origin" :
|
|
303
|
-
|
|
297
|
+
return "SameOrigin";
|
|
304
298
|
case "strict-origin" :
|
|
305
|
-
|
|
299
|
+
return "StrictOrigin";
|
|
306
300
|
case "strict-origin-when-cross-origin" :
|
|
307
|
-
|
|
301
|
+
return "StrictOriginWhenCrossOrigin";
|
|
308
302
|
case "unsafe-url" :
|
|
309
|
-
|
|
303
|
+
return "UnsafeUrl";
|
|
310
304
|
default:
|
|
311
305
|
throw {
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
306
|
+
RE_EXN_ID: UnknownReferrerPolicy,
|
|
307
|
+
_1: referrerPolicy$1,
|
|
308
|
+
Error: new Error()
|
|
309
|
+
};
|
|
316
310
|
}
|
|
317
311
|
}
|
|
318
312
|
|
|
319
313
|
function mode(self) {
|
|
320
|
-
|
|
314
|
+
let requestMode = self.mode;
|
|
321
315
|
switch (requestMode) {
|
|
322
316
|
case "cors" :
|
|
323
|
-
|
|
317
|
+
return "CORS";
|
|
324
318
|
case "navigate" :
|
|
325
|
-
|
|
319
|
+
return "Navigate";
|
|
326
320
|
case "no-cors" :
|
|
327
|
-
|
|
321
|
+
return "NoCORS";
|
|
328
322
|
case "same-origin" :
|
|
329
|
-
|
|
323
|
+
return "SameOrigin";
|
|
330
324
|
default:
|
|
331
325
|
throw {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
326
|
+
RE_EXN_ID: UnknownRequestMode,
|
|
327
|
+
_1: requestMode,
|
|
328
|
+
Error: new Error()
|
|
329
|
+
};
|
|
336
330
|
}
|
|
337
331
|
}
|
|
338
332
|
|
|
339
333
|
function credentials(self) {
|
|
340
|
-
|
|
334
|
+
let requestCredentials = self.credentials;
|
|
341
335
|
switch (requestCredentials) {
|
|
342
336
|
case "include" :
|
|
343
|
-
|
|
337
|
+
return "Include";
|
|
344
338
|
case "omit" :
|
|
345
|
-
|
|
339
|
+
return "Omit";
|
|
346
340
|
case "same-origin" :
|
|
347
|
-
|
|
341
|
+
return "SameOrigin";
|
|
348
342
|
default:
|
|
349
343
|
throw {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
344
|
+
RE_EXN_ID: UnknownRequestCredentials,
|
|
345
|
+
_1: requestCredentials,
|
|
346
|
+
Error: new Error()
|
|
347
|
+
};
|
|
354
348
|
}
|
|
355
349
|
}
|
|
356
350
|
|
|
357
351
|
function cache(self) {
|
|
358
|
-
|
|
352
|
+
let requestCache = self.cache;
|
|
359
353
|
switch (requestCache) {
|
|
360
354
|
case "default" :
|
|
361
|
-
|
|
355
|
+
return "Default";
|
|
362
356
|
case "force-cache" :
|
|
363
|
-
|
|
357
|
+
return "ForceCache";
|
|
364
358
|
case "no-cache" :
|
|
365
|
-
|
|
359
|
+
return "NoCache";
|
|
366
360
|
case "no-store" :
|
|
367
|
-
|
|
361
|
+
return "NoStore";
|
|
368
362
|
case "only-if-cached" :
|
|
369
|
-
|
|
363
|
+
return "OnlyIfCached";
|
|
370
364
|
case "reload" :
|
|
371
|
-
|
|
365
|
+
return "Reload";
|
|
372
366
|
default:
|
|
373
367
|
throw {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
368
|
+
RE_EXN_ID: UnknownRequestCache,
|
|
369
|
+
_1: requestCache,
|
|
370
|
+
Error: new Error()
|
|
371
|
+
};
|
|
378
372
|
}
|
|
379
373
|
}
|
|
380
374
|
|
|
381
375
|
function redirect(self) {
|
|
382
|
-
|
|
376
|
+
let requestRedirect = self.redirect;
|
|
383
377
|
switch (requestRedirect) {
|
|
384
378
|
case "error" :
|
|
385
|
-
|
|
379
|
+
return "Error";
|
|
386
380
|
case "follow" :
|
|
387
|
-
|
|
381
|
+
return "Follow";
|
|
388
382
|
case "manual" :
|
|
389
|
-
|
|
383
|
+
return "Manual";
|
|
390
384
|
default:
|
|
391
385
|
throw {
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
386
|
+
RE_EXN_ID: UnknownRequestRedirect,
|
|
387
|
+
_1: requestRedirect,
|
|
388
|
+
Error: new Error()
|
|
389
|
+
};
|
|
396
390
|
}
|
|
397
391
|
}
|
|
398
392
|
|
|
399
|
-
|
|
393
|
+
let Request = {
|
|
400
394
|
method_: method_,
|
|
401
395
|
type_: type_,
|
|
402
396
|
destination: destination,
|
|
@@ -407,14 +401,14 @@ var $$Request = {
|
|
|
407
401
|
redirect: redirect
|
|
408
402
|
};
|
|
409
403
|
|
|
410
|
-
|
|
404
|
+
let Response = {};
|
|
411
405
|
|
|
412
|
-
exports
|
|
406
|
+
exports.AbortController = AbortController;
|
|
413
407
|
exports.HeadersInit = HeadersInit;
|
|
414
|
-
exports
|
|
408
|
+
exports.Headers = Headers;
|
|
415
409
|
exports.BodyInit = BodyInit;
|
|
416
410
|
exports.Body = Body;
|
|
417
411
|
exports.RequestInit = RequestInit;
|
|
418
|
-
exports
|
|
419
|
-
exports
|
|
412
|
+
exports.Request = Request;
|
|
413
|
+
exports.Response = Response;
|
|
420
414
|
/* No side effect */
|
package/src/Fetch.resi
CHANGED
|
@@ -110,8 +110,8 @@ type requestRedirect =
|
|
|
110
110
|
module HeadersInit: {
|
|
111
111
|
type t = headersInit
|
|
112
112
|
|
|
113
|
-
external make:
|
|
114
|
-
external makeWithDict:
|
|
113
|
+
external make: {..} => t = "%identity"
|
|
114
|
+
external makeWithDict: dict<string> => t = "%identity"
|
|
115
115
|
external makeWithArray: array<(string, string)> => t = "%identity"
|
|
116
116
|
}
|
|
117
117
|
|
|
@@ -160,19 +160,19 @@ module Body: {
|
|
|
160
160
|
external bodyUsed: t => bool = "bodyUsed"
|
|
161
161
|
|
|
162
162
|
@send
|
|
163
|
-
external arrayBuffer: t =>
|
|
163
|
+
external arrayBuffer: t => promise<arrayBuffer> = "arrayBuffer"
|
|
164
164
|
|
|
165
165
|
@send
|
|
166
|
-
external blob: t =>
|
|
166
|
+
external blob: t => promise<blob> = "blob"
|
|
167
167
|
|
|
168
168
|
@send
|
|
169
|
-
external formData: t =>
|
|
169
|
+
external formData: t => promise<formData> = "formData"
|
|
170
170
|
|
|
171
171
|
@send
|
|
172
|
-
external json: t =>
|
|
172
|
+
external json: t => promise<JSON.t> = "json"
|
|
173
173
|
|
|
174
174
|
@send
|
|
175
|
-
external text: t =>
|
|
175
|
+
external text: t => promise<string> = "text"
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
module RequestInit: {
|
|
@@ -249,19 +249,19 @@ module Request: {
|
|
|
249
249
|
external bodyUsed: t => bool = "bodyUsed"
|
|
250
250
|
|
|
251
251
|
@send
|
|
252
|
-
external arrayBuffer: t =>
|
|
252
|
+
external arrayBuffer: t => promise<arrayBuffer> = "arrayBuffer"
|
|
253
253
|
|
|
254
254
|
@send
|
|
255
|
-
external blob: t =>
|
|
255
|
+
external blob: t => promise<blob> = "blob"
|
|
256
256
|
|
|
257
257
|
@send
|
|
258
|
-
external formData: t =>
|
|
258
|
+
external formData: t => promise<formData> = "formData"
|
|
259
259
|
|
|
260
260
|
@send
|
|
261
|
-
external json: t =>
|
|
261
|
+
external json: t => promise<JSON.t> = "json"
|
|
262
262
|
|
|
263
263
|
@send
|
|
264
|
-
external text: t =>
|
|
264
|
+
external text: t => promise<string> = "text"
|
|
265
265
|
}
|
|
266
266
|
|
|
267
267
|
module Response: {
|
|
@@ -309,29 +309,29 @@ module Response: {
|
|
|
309
309
|
external bodyUsed: t => bool = "bodyUsed"
|
|
310
310
|
|
|
311
311
|
@send
|
|
312
|
-
external arrayBuffer: t =>
|
|
312
|
+
external arrayBuffer: t => promise<arrayBuffer> = "arrayBuffer"
|
|
313
313
|
|
|
314
314
|
@send
|
|
315
|
-
external blob: t =>
|
|
315
|
+
external blob: t => promise<blob> = "blob"
|
|
316
316
|
|
|
317
317
|
@send
|
|
318
|
-
external formData: t =>
|
|
318
|
+
external formData: t => promise<formData> = "formData"
|
|
319
319
|
|
|
320
320
|
@send
|
|
321
|
-
external json: t =>
|
|
321
|
+
external json: t => promise<JSON.t> = "json"
|
|
322
322
|
|
|
323
323
|
@send
|
|
324
|
-
external text: t =>
|
|
324
|
+
external text: t => promise<string> = "text"
|
|
325
325
|
}
|
|
326
326
|
|
|
327
327
|
@val
|
|
328
|
-
external fetch: string =>
|
|
328
|
+
external fetch: string => promise<response> = "fetch"
|
|
329
329
|
|
|
330
330
|
@val
|
|
331
|
-
external fetchWithInit: (string, requestInit) =>
|
|
331
|
+
external fetchWithInit: (string, requestInit) => promise<response> = "fetch"
|
|
332
332
|
|
|
333
333
|
@val
|
|
334
|
-
external fetchWithRequest: request =>
|
|
334
|
+
external fetchWithRequest: request => promise<response> = "fetch"
|
|
335
335
|
|
|
336
336
|
@val
|
|
337
|
-
external fetchWithRequestInit: (request, requestInit) =>
|
|
337
|
+
external fetchWithRequestInit: (request, requestInit) => promise<response> = "fetch"
|
package/tests/FetchTest.res
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
open ReactTest
|
|
2
|
-
|
|
3
|
-
let placeholderText = `{"userId":1,"id":1,"title":"delectus aut autem","completed":false}`
|
|
4
|
-
let postPlaceholderText = `{"title":"foo","body":"bar","userId":1,"id":101}`
|
|
5
|
-
|
|
6
|
-
module Hooks = {
|
|
7
|
-
let useFetch = () => {
|
|
8
|
-
let (state, setState) = React.useState(() => None)
|
|
9
|
-
|
|
10
|
-
React.useEffect0(() => {
|
|
11
|
-
let promise = async () => {
|
|
12
|
-
let response = await Fetch.fetch("https://jsonplaceholder.typicode.com/todos/1")
|
|
13
|
-
let body = await response->Fetch.Response.json
|
|
14
|
-
|
|
15
|
-
setState(_ => Some(body))
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
promise()->Promise.thenResolve(_ => ())->ignore
|
|
19
|
-
|
|
20
|
-
None
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
state
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
let useFetchWithInit = () => {
|
|
27
|
-
let (state, setState) = React.useState(() => None)
|
|
28
|
-
|
|
29
|
-
React.useEffect0(() => {
|
|
30
|
-
let promise = async () => {
|
|
31
|
-
let response = await Fetch.fetchWithInit(
|
|
32
|
-
"https://jsonplaceholder.typicode.com/todos/1",
|
|
33
|
-
Fetch.RequestInit.make(~method_=Get, ()),
|
|
34
|
-
)
|
|
35
|
-
let body = await response->Fetch.Response.json
|
|
36
|
-
|
|
37
|
-
setState(_ => Some(body))
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
promise()->Promise.thenResolve(_ => ())->ignore
|
|
41
|
-
|
|
42
|
-
None
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
state
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
let usePostFetchWithInit = () => {
|
|
49
|
-
let postBody = {
|
|
50
|
-
"title": "foo",
|
|
51
|
-
"body": "bar",
|
|
52
|
-
"userId": 1,
|
|
53
|
-
}
|
|
54
|
-
let (state, setState) = React.useState(() => None)
|
|
55
|
-
|
|
56
|
-
React.useEffect0(() => {
|
|
57
|
-
let promise = async () => {
|
|
58
|
-
let body = Fetch.BodyInit.make(JSON.stringifyAny(postBody)->Option.getOr(""))
|
|
59
|
-
let headers = Fetch.HeadersInit.makeWithArray([
|
|
60
|
-
("Accept", "application/json"),
|
|
61
|
-
("Content-Type", "application/json"),
|
|
62
|
-
])
|
|
63
|
-
let response = await Fetch.fetchWithInit(
|
|
64
|
-
"https://jsonplaceholder.typicode.com/posts",
|
|
65
|
-
Fetch.RequestInit.make(~method_=Post, ~headers, ~body, ()),
|
|
66
|
-
)
|
|
67
|
-
let body = await response->Fetch.Response.json
|
|
68
|
-
|
|
69
|
-
setState(_ => Some(body))
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
promise()->Promise.thenResolve(_ => ())->ignore
|
|
73
|
-
|
|
74
|
-
None
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
state
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
module Home = {
|
|
82
|
-
@react.component
|
|
83
|
-
let make = (~fetchHok: unit => option<JSON.t>) => {
|
|
84
|
-
let jsonPlaceholder = fetchHok()
|
|
85
|
-
|
|
86
|
-
<div id="placeholder">
|
|
87
|
-
{React.string(jsonPlaceholder->Option.mapOr("Loading...", JSON.stringify(_)))}
|
|
88
|
-
</div>
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
@get external textContent: Dom.element => string = "textContent"
|
|
93
|
-
|
|
94
|
-
testAsyncWithReact("fetch", (root, done) => {
|
|
95
|
-
let promise = async () => {
|
|
96
|
-
let _ = await act(async () => {
|
|
97
|
-
root->ReactDOM.Client.Root.render(<Home fetchHok=Hooks.useFetch />)
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
let _ = await screen->findByText(placeholderText)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
promise()
|
|
104
|
-
->Promise.thenResolve(_ => {
|
|
105
|
-
Assert.elementContains(
|
|
106
|
-
~message="has result",
|
|
107
|
-
ReactDOM.querySelector("#placeholder"),
|
|
108
|
-
placeholderText,
|
|
109
|
-
)
|
|
110
|
-
done()
|
|
111
|
-
})
|
|
112
|
-
->ignore
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
testAsyncWithReact("fetchWithInit", (root, done) => {
|
|
116
|
-
let promise = async () => {
|
|
117
|
-
let _ = await act(async () => {
|
|
118
|
-
root->ReactDOM.Client.Root.render(<Home fetchHok=Hooks.useFetchWithInit />)
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
let _ = await screen->findByText(placeholderText)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
promise()
|
|
125
|
-
->Promise.thenResolve(_ => {
|
|
126
|
-
Assert.elementContains(
|
|
127
|
-
~message="has result",
|
|
128
|
-
ReactDOM.querySelector("#placeholder"),
|
|
129
|
-
placeholderText,
|
|
130
|
-
)
|
|
131
|
-
done()
|
|
132
|
-
})
|
|
133
|
-
->ignore
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
testAsyncWithReact("postFetchWithInit", (root, done) => {
|
|
137
|
-
let promise = async () => {
|
|
138
|
-
let _ = await act(async () => {
|
|
139
|
-
root->ReactDOM.Client.Root.render(<Home fetchHok=Hooks.usePostFetchWithInit />)
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
let _ = await screen->findByText(postPlaceholderText)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
promise()
|
|
146
|
-
->Promise.thenResolve(_ => {
|
|
147
|
-
Assert.elementContains(
|
|
148
|
-
~message="has result",
|
|
149
|
-
ReactDOM.querySelector("#placeholder"),
|
|
150
|
-
postPlaceholderText,
|
|
151
|
-
)
|
|
152
|
-
done()
|
|
153
|
-
})
|
|
154
|
-
->ignore
|
|
155
|
-
})
|
package/tests/FetchTest.res.js
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
var Fetch = require("../src/Fetch.res.js");
|
|
5
|
-
var React = require("react");
|
|
6
|
-
var Assert = require("./utils/Assert.res.js");
|
|
7
|
-
var ReactTest = require("./utils/ReactTest.res.js");
|
|
8
|
-
var Caml_option = require("rescript/lib/js/caml_option.js");
|
|
9
|
-
var Core__Option = require("@rescript/core/src/Core__Option.res.js");
|
|
10
|
-
var JsxRuntime = require("react/jsx-runtime");
|
|
11
|
-
var React$1 = require("@testing-library/react");
|
|
12
|
-
|
|
13
|
-
var placeholderText = "{\"userId\":1,\"id\":1,\"title\":\"delectus aut autem\",\"completed\":false}";
|
|
14
|
-
|
|
15
|
-
var postPlaceholderText = "{\"title\":\"foo\",\"body\":\"bar\",\"userId\":1,\"id\":101}";
|
|
16
|
-
|
|
17
|
-
function useFetch() {
|
|
18
|
-
var match = React.useState(function () {
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
var setState = match[1];
|
|
22
|
-
React.useEffect((function () {
|
|
23
|
-
var promise = async function () {
|
|
24
|
-
var response = await fetch("https://jsonplaceholder.typicode.com/todos/1");
|
|
25
|
-
var body = await response.json();
|
|
26
|
-
return setState(function (param) {
|
|
27
|
-
return body;
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
promise().then(function () {
|
|
31
|
-
|
|
32
|
-
});
|
|
33
|
-
}), []);
|
|
34
|
-
return match[0];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function useFetchWithInit() {
|
|
38
|
-
var match = React.useState(function () {
|
|
39
|
-
|
|
40
|
-
});
|
|
41
|
-
var setState = match[1];
|
|
42
|
-
React.useEffect((function () {
|
|
43
|
-
var promise = async function () {
|
|
44
|
-
var response = await fetch("https://jsonplaceholder.typicode.com/todos/1", Fetch.RequestInit.make("Get", undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined));
|
|
45
|
-
var body = await response.json();
|
|
46
|
-
return setState(function (param) {
|
|
47
|
-
return body;
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
promise().then(function () {
|
|
51
|
-
|
|
52
|
-
});
|
|
53
|
-
}), []);
|
|
54
|
-
return match[0];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function usePostFetchWithInit() {
|
|
58
|
-
var postBody = {
|
|
59
|
-
title: "foo",
|
|
60
|
-
body: "bar",
|
|
61
|
-
userId: 1
|
|
62
|
-
};
|
|
63
|
-
var match = React.useState(function () {
|
|
64
|
-
|
|
65
|
-
});
|
|
66
|
-
var setState = match[1];
|
|
67
|
-
React.useEffect((function () {
|
|
68
|
-
var promise = async function () {
|
|
69
|
-
var body = Core__Option.getOr(JSON.stringify(postBody), "");
|
|
70
|
-
var headers = [
|
|
71
|
-
[
|
|
72
|
-
"Accept",
|
|
73
|
-
"application/json"
|
|
74
|
-
],
|
|
75
|
-
[
|
|
76
|
-
"Content-Type",
|
|
77
|
-
"application/json"
|
|
78
|
-
]
|
|
79
|
-
];
|
|
80
|
-
var response = await fetch("https://jsonplaceholder.typicode.com/posts", Fetch.RequestInit.make("Post", Caml_option.some(headers), Caml_option.some(body), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined));
|
|
81
|
-
var body$1 = await response.json();
|
|
82
|
-
return setState(function (param) {
|
|
83
|
-
return body$1;
|
|
84
|
-
});
|
|
85
|
-
};
|
|
86
|
-
promise().then(function () {
|
|
87
|
-
|
|
88
|
-
});
|
|
89
|
-
}), []);
|
|
90
|
-
return match[0];
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
var Hooks = {
|
|
94
|
-
useFetch: useFetch,
|
|
95
|
-
useFetchWithInit: useFetchWithInit,
|
|
96
|
-
usePostFetchWithInit: usePostFetchWithInit
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
function FetchTest$Home(props) {
|
|
100
|
-
var jsonPlaceholder = props.fetchHok();
|
|
101
|
-
return JsxRuntime.jsx("div", {
|
|
102
|
-
children: Core__Option.mapOr(jsonPlaceholder, "Loading...", (function (__x) {
|
|
103
|
-
return JSON.stringify(__x);
|
|
104
|
-
})),
|
|
105
|
-
id: "placeholder"
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
var Home = {
|
|
110
|
-
make: FetchTest$Home
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
ReactTest.testAsyncWithReact("fetch", undefined, (function (root, done) {
|
|
114
|
-
var promise = async function () {
|
|
115
|
-
await React$1.act(async function () {
|
|
116
|
-
root.render(JsxRuntime.jsx(FetchTest$Home, {
|
|
117
|
-
fetchHok: useFetch
|
|
118
|
-
}));
|
|
119
|
-
});
|
|
120
|
-
await React$1.screen.findByText(placeholderText);
|
|
121
|
-
};
|
|
122
|
-
promise().then(function () {
|
|
123
|
-
Assert.elementContains("has result", Caml_option.nullable_to_opt(document.querySelector("#placeholder")), placeholderText);
|
|
124
|
-
done(undefined, undefined);
|
|
125
|
-
});
|
|
126
|
-
}));
|
|
127
|
-
|
|
128
|
-
ReactTest.testAsyncWithReact("fetchWithInit", undefined, (function (root, done) {
|
|
129
|
-
var promise = async function () {
|
|
130
|
-
await React$1.act(async function () {
|
|
131
|
-
root.render(JsxRuntime.jsx(FetchTest$Home, {
|
|
132
|
-
fetchHok: useFetchWithInit
|
|
133
|
-
}));
|
|
134
|
-
});
|
|
135
|
-
await React$1.screen.findByText(placeholderText);
|
|
136
|
-
};
|
|
137
|
-
promise().then(function () {
|
|
138
|
-
Assert.elementContains("has result", Caml_option.nullable_to_opt(document.querySelector("#placeholder")), placeholderText);
|
|
139
|
-
done(undefined, undefined);
|
|
140
|
-
});
|
|
141
|
-
}));
|
|
142
|
-
|
|
143
|
-
ReactTest.testAsyncWithReact("postFetchWithInit", undefined, (function (root, done) {
|
|
144
|
-
var promise = async function () {
|
|
145
|
-
await React$1.act(async function () {
|
|
146
|
-
root.render(JsxRuntime.jsx(FetchTest$Home, {
|
|
147
|
-
fetchHok: usePostFetchWithInit
|
|
148
|
-
}));
|
|
149
|
-
});
|
|
150
|
-
await React$1.screen.findByText(postPlaceholderText);
|
|
151
|
-
};
|
|
152
|
-
promise().then(function () {
|
|
153
|
-
Assert.elementContains("has result", Caml_option.nullable_to_opt(document.querySelector("#placeholder")), postPlaceholderText);
|
|
154
|
-
done(undefined, undefined);
|
|
155
|
-
});
|
|
156
|
-
}));
|
|
157
|
-
|
|
158
|
-
exports.placeholderText = placeholderText;
|
|
159
|
-
exports.postPlaceholderText = postPlaceholderText;
|
|
160
|
-
exports.Hooks = Hooks;
|
|
161
|
-
exports.Home = Home;
|
|
162
|
-
/* Not a pure module */
|
package/tests/utils/Assert.res
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
open Test
|
|
2
|
-
|
|
3
|
-
@get external textContent: Dom.element => string = "textContent"
|
|
4
|
-
|
|
5
|
-
let isSome = (~message: string, value: option<'a>) =>
|
|
6
|
-
switch value {
|
|
7
|
-
| Some(_) => pass(~message, ())
|
|
8
|
-
| None => fail(~message="Expected Some, got None", ())
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
let elementContains = (~message=?, element: option<Dom.element>, substring: string) =>
|
|
12
|
-
assertion(
|
|
13
|
-
~message?,
|
|
14
|
-
~operator="elementContains",
|
|
15
|
-
(textContent, substring) => {
|
|
16
|
-
textContent->String.includes(substring)
|
|
17
|
-
},
|
|
18
|
-
element->Option.mapOr("Not Found", element => element->textContent),
|
|
19
|
-
substring,
|
|
20
|
-
)
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
var Test = require("rescript-test/src/Test.res.js");
|
|
5
|
-
var Core__Option = require("@rescript/core/src/Core__Option.res.js");
|
|
6
|
-
|
|
7
|
-
function isSome(message, value) {
|
|
8
|
-
if (value !== undefined) {
|
|
9
|
-
return Test.pass(message, undefined);
|
|
10
|
-
} else {
|
|
11
|
-
return Test.fail("Expected Some, got None", undefined);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function elementContains(message, element, substring) {
|
|
16
|
-
Test.assertion(message, "elementContains", (function (textContent, substring) {
|
|
17
|
-
return textContent.includes(substring);
|
|
18
|
-
}), Core__Option.mapOr(element, "Not Found", (function (element) {
|
|
19
|
-
return element.textContent;
|
|
20
|
-
})), substring);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
exports.isSome = isSome;
|
|
24
|
-
exports.elementContains = elementContains;
|
|
25
|
-
/* Test Not a pure module */
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
open Test
|
|
2
|
-
open Webapi.Dom
|
|
3
|
-
|
|
4
|
-
@send external remove: Dom.element => unit = "remove"
|
|
5
|
-
|
|
6
|
-
let createContainer = () => {
|
|
7
|
-
let containerElement = Webapi.Dom.document->Document.createElement("div")
|
|
8
|
-
let _ = containerElement->Element.setAttribute("id", "test")
|
|
9
|
-
|
|
10
|
-
let _ =
|
|
11
|
-
Webapi.Dom.document
|
|
12
|
-
->Document.asHtmlDocument
|
|
13
|
-
->Option.map(htmlDocument =>
|
|
14
|
-
htmlDocument
|
|
15
|
-
->HtmlDocument.body
|
|
16
|
-
->Option.forEach(body => body->Element.appendChild(~child=containerElement))
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
ReactDOM.Client.createRoot(containerElement)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let cleanupContainer = container => {
|
|
23
|
-
let containerElement = ReactDOM.querySelector("#test")
|
|
24
|
-
switch containerElement {
|
|
25
|
-
| Some(element) => element->remove
|
|
26
|
-
| None => ()
|
|
27
|
-
}
|
|
28
|
-
container->ReactDOM.Client.Root.unmount()
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
let testWithReact = testWith(~setup=createContainer, ~teardown=cleanupContainer, ...)
|
|
32
|
-
|
|
33
|
-
let testAsyncWithReact = testAsyncWith(~setup=createContainer, ~teardown=cleanupContainer, ...)
|
|
34
|
-
|
|
35
|
-
// @testing-library/react
|
|
36
|
-
@module("@testing-library/react") external act: (unit => Promise.t<unit>) => Promise.t<unit> = "act"
|
|
37
|
-
|
|
38
|
-
@module("@@testing-library/react")
|
|
39
|
-
external waitFor: (unit => Promise.t<'a>) => Promise.t<'a> = "waitFor"
|
|
40
|
-
|
|
41
|
-
type screen
|
|
42
|
-
|
|
43
|
-
@module("@testing-library/react") @val
|
|
44
|
-
external screen: screen = "screen"
|
|
45
|
-
|
|
46
|
-
@send
|
|
47
|
-
external findByText: (screen, string) => Promise.t<Dom.element> = "findByText"
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
var Test = require("rescript-test/src/Test.res.js");
|
|
5
|
-
var Caml_option = require("rescript/lib/js/caml_option.js");
|
|
6
|
-
var Core__Option = require("@rescript/core/src/Core__Option.res.js");
|
|
7
|
-
var Client = require("react-dom/client");
|
|
8
|
-
var Webapi__Dom__Document = require("rescript-webapi/src/Webapi/Dom/Webapi__Dom__Document.res.js");
|
|
9
|
-
|
|
10
|
-
function createContainer() {
|
|
11
|
-
var containerElement = document.createElement("div");
|
|
12
|
-
containerElement.setAttribute("id", "test");
|
|
13
|
-
Core__Option.map(Webapi__Dom__Document.asHtmlDocument(document), (function (htmlDocument) {
|
|
14
|
-
Core__Option.forEach(Caml_option.nullable_to_opt(htmlDocument.body), (function (body) {
|
|
15
|
-
body.appendChild(containerElement);
|
|
16
|
-
}));
|
|
17
|
-
}));
|
|
18
|
-
return Client.createRoot(containerElement);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function cleanupContainer(container) {
|
|
22
|
-
var containerElement = document.querySelector("#test");
|
|
23
|
-
if (!(containerElement == null)) {
|
|
24
|
-
containerElement.remove();
|
|
25
|
-
}
|
|
26
|
-
container.unmount();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function testWithReact(extra, extra$1) {
|
|
30
|
-
return Test.testWith(createContainer, cleanupContainer, extra, extra$1);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function testAsyncWithReact(extra, extra$1, extra$2) {
|
|
34
|
-
return Test.testAsyncWith(createContainer, cleanupContainer, extra, extra$1, extra$2);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
exports.createContainer = createContainer;
|
|
38
|
-
exports.cleanupContainer = cleanupContainer;
|
|
39
|
-
exports.testWithReact = testWithReact;
|
|
40
|
-
exports.testAsyncWithReact = testAsyncWithReact;
|
|
41
|
-
/* Test Not a pure module */
|