@plowtech/rescript-fetch 0.5.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Plow Technologies
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # rescript-fetch
2
+
3
+ ReScript bindings for the Fetch API.
4
+
5
+ These bindings are a direct translation of [bs-fetch](https://www.npmjs.com/package/bs-fetch) version 0.5.2 to ReScript syntax. Time permitting this package will be updated to match the latest version of bs-fetch after the initial release.
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@plowtech/rescript-fetch",
3
+ "version": "0.5.2",
4
+ "description": "ReScript Bindings for the Fetch API",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "retest --with-dom tests/*.res.js",
8
+ "res:build": "rescript",
9
+ "res:dev": "rescript -w",
10
+ "res:clean": "rescript clean"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/plow-technologies/rescript-fetch.git"
15
+ },
16
+ "keywords": [
17
+ "rescript"
18
+ ],
19
+ "author": "Logan Tibbetts",
20
+ "license": "MIT",
21
+ "bugs": {
22
+ "url": "https://github.com/plow-technologies/rescript-fetch/issues"
23
+ },
24
+ "homepage": "https://github.com/plow-technologies/rescript-fetch#readme",
25
+ "dependencies": {
26
+ "@rescript/core": "^1.5.2",
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"
37
+ }
38
+ }
package/rescript.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@plowtech/rescript-fetch",
3
+ "sources": [
4
+ {
5
+ "dir": "src",
6
+ "subdirs": true
7
+ },
8
+ {
9
+ "dir": "tests",
10
+ "subdirs": true,
11
+ "type": "dev"
12
+ }
13
+ ],
14
+ "package-specs": [
15
+ {
16
+ "module": "commonjs",
17
+ "in-source": true
18
+ }
19
+ ],
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
+ }
35
+ }
package/src/Fetch.res ADDED
@@ -0,0 +1,561 @@
1
+ type body
2
+ type bodyInit
3
+ type headers
4
+ type headersInit
5
+ type response
6
+ type request
7
+ type requestInit
8
+
9
+ // external
10
+ type arrayBuffer /* TypedArray */
11
+ type blob /* FileAPI */
12
+ type bufferSource /* Web IDL, either an arrayBuffer or arrayBufferView */
13
+ type formData /* XMLHttpRequest */
14
+ type readableStream /* Streams */
15
+ type urlSearchParams /* URL */
16
+ type abortController
17
+ type signal
18
+
19
+ type requestMethod =
20
+ | Get
21
+ | Head
22
+ | Post
23
+ | Put
24
+ | Delete
25
+ | Connect
26
+ | Options
27
+ | Trace
28
+ | Patch
29
+ | Other(string)
30
+
31
+ /* internal */
32
+ let encodeRequestMethod = (requestMethod: requestMethod): string =>
33
+ switch requestMethod {
34
+ | Get => "GET"
35
+ | Head => "HEAD"
36
+ | Post => "POST"
37
+ | Put => "PUT"
38
+ | Delete => "DELETE"
39
+ | Connect => "CONNECT"
40
+ | Options => "OPTIONS"
41
+ | Trace => "TRACE"
42
+ | Patch => "PATCH"
43
+ | Other(method_) => method_
44
+ }
45
+
46
+ /* internal */
47
+ let decodeRequestMethod = (requestMethod: string): requestMethod =>
48
+ switch requestMethod {
49
+ | "GET" => Get
50
+ | "HEAD" => Head
51
+ | "POST" => Post
52
+ | "PUT" => Put
53
+ | "DELETE" => Delete
54
+ | "CONNECT" => Connect
55
+ | "OPTIONS" => Options
56
+ | "TRACE" => Trace
57
+ | "PATCH" => Patch
58
+ | method_ => Other(method_)
59
+ }
60
+
61
+ module AbortController = {
62
+ /* Experimental API */
63
+ type t = abortController
64
+
65
+ /* Experimental API */
66
+ @get
67
+ external signal: t => signal = "signal"
68
+
69
+ /* Experimental API */
70
+ @send
71
+ external abort: t => unit = "abort"
72
+
73
+ /* Experimental API */
74
+ @new
75
+ external make: unit => t = "AbortController"
76
+ }
77
+
78
+ type referrerPolicy =
79
+ | None
80
+ | NoReferrer
81
+ | NoReferrerWhenDowngrade
82
+ | SameOrigin
83
+ | Origin
84
+ | StrictOrigin
85
+ | OriginWhenCrossOrigin
86
+ | StrictOriginWhenCrossOrigin
87
+ | UnsafeUrl
88
+
89
+ /* internal */
90
+ let encodeReferrerPolicy = (referrerPolicy: referrerPolicy) =>
91
+ switch referrerPolicy {
92
+ | None => ""
93
+ | NoReferrer => "no-referrer"
94
+ | NoReferrerWhenDowngrade => "no-referrer-when-downgrade"
95
+ | SameOrigin => "same-origin"
96
+ | Origin => "origin"
97
+ | StrictOrigin => "strict-origin"
98
+ | OriginWhenCrossOrigin => "origin-when-cross-origin"
99
+ | StrictOriginWhenCrossOrigin => "strict-origin-when-cross-origin"
100
+ | UnsafeUrl => "unsafe-url"
101
+ }
102
+
103
+ exception UnknownReferrerPolicy(string)
104
+
105
+ /* internal */
106
+ let decodeReferrerPolicy = (referrerPolicy: string) =>
107
+ switch referrerPolicy {
108
+ | "" => None
109
+ | "no-referrer" => NoReferrer
110
+ | "no-referrer-when-downgrade" => NoReferrerWhenDowngrade
111
+ | "same-origin" => SameOrigin
112
+ | "origin" => Origin
113
+ | "strict-origin" => StrictOrigin
114
+ | "origin-when-cross-origin" => OriginWhenCrossOrigin
115
+ | "strict-origin-when-cross-origin" => StrictOriginWhenCrossOrigin
116
+ | "unsafe-url" => UnsafeUrl
117
+ | e => raise(UnknownReferrerPolicy(e))
118
+ }
119
+
120
+ type requestType =
121
+ | None /* default? unknown? just empty string in spec */
122
+ | Audio
123
+ | Font
124
+ | Image
125
+ | Script
126
+ | Style
127
+ | Track
128
+ | Video
129
+
130
+ exception UnknownRequestType(string)
131
+
132
+ let decodeRequestType = (requestType: string) =>
133
+ switch requestType {
134
+ | "" => None
135
+ | "audio" => Audio
136
+ | "font" => Font
137
+ | "image" => Image
138
+ | "script" => Script
139
+ | "style" => Style
140
+ | "track" => Track
141
+ | "video" => Video
142
+ | e => raise(UnknownRequestType(e))
143
+ }
144
+
145
+ type requestDestination =
146
+ | None /* default? unknown? just empty string in spec */
147
+ | Document
148
+ | Embed
149
+ | Font
150
+ | Image
151
+ | Manifest
152
+ | Media
153
+ | Object
154
+ | Report
155
+ | Script
156
+ | ServiceWorker
157
+ | SharedWorker
158
+ | Style
159
+ | Worker
160
+ | Xslt
161
+
162
+ exception UnknownRequestDestination(string)
163
+
164
+ let decodeRequestDestination = (requestDestination: string) =>
165
+ switch requestDestination {
166
+ | "" => None
167
+ | "document" => Document
168
+ | "embed" => Embed
169
+ | "font" => Font
170
+ | "image" => Image
171
+ | "manifest" => Manifest
172
+ | "media" => Media
173
+ | "object" => Object
174
+ | "report" => Report
175
+ | "script" => Script
176
+ | "serviceworker" => ServiceWorker
177
+ | "sharedworker" => SharedWorker
178
+ | "style" => Style
179
+ | "worker" => Worker
180
+ | "xslt" => Xslt
181
+ | e => raise(UnknownRequestDestination(e))
182
+ }
183
+
184
+ type requestMode =
185
+ | Navigate
186
+ | SameOrigin
187
+ | NoCORS
188
+ | CORS
189
+
190
+ let encodeRequestMode = (requestMode: requestMode) =>
191
+ switch requestMode {
192
+ | Navigate => "navigate"
193
+ | SameOrigin => "same-origin"
194
+ | NoCORS => "no-cors"
195
+ | CORS => "cors"
196
+ }
197
+
198
+ exception UnknownRequestMode(string)
199
+
200
+ let decodeRequestMode = (requestMode: string) =>
201
+ switch requestMode {
202
+ | "navigate" => Navigate
203
+ | "same-origin" => SameOrigin
204
+ | "no-cors" => NoCORS
205
+ | "cors" => CORS
206
+ | e => raise(UnknownRequestMode(e))
207
+ }
208
+
209
+ type requestCredentials =
210
+ | Omit
211
+ | SameOrigin
212
+ | Include
213
+
214
+ let encodeRequestCredentials = (requestCredentials: requestCredentials) =>
215
+ switch requestCredentials {
216
+ | Omit => "omit"
217
+ | SameOrigin => "same-origin"
218
+ | Include => "include"
219
+ }
220
+
221
+ exception UnknownRequestCredentials(string)
222
+
223
+ let decodeRequestCredentials = (requestCredentials: string) =>
224
+ switch requestCredentials {
225
+ | "omit" => Omit
226
+ | "same-origin" => SameOrigin
227
+ | "include" => Include
228
+ | e => raise(UnknownRequestCredentials(e))
229
+ }
230
+
231
+ type requestCache =
232
+ | Default
233
+ | NoStore
234
+ | Reload
235
+ | NoCache
236
+ | ForceCache
237
+ | OnlyIfCached
238
+
239
+ let encodeRequestCache = (requestCache: requestCache) =>
240
+ switch requestCache {
241
+ | Default => "default"
242
+ | NoStore => "no-store"
243
+ | Reload => "reload"
244
+ | NoCache => "no-cache"
245
+ | ForceCache => "force-cache"
246
+ | OnlyIfCached => "only-if-cached"
247
+ }
248
+
249
+ exception UnknownRequestCache(string)
250
+
251
+ let decodeRequestCache = (requestCache: string) =>
252
+ switch requestCache {
253
+ | "default" => Default
254
+ | "no-store" => NoStore
255
+ | "reload" => Reload
256
+ | "no-cache" => NoCache
257
+ | "force-cache" => ForceCache
258
+ | "only-if-cached" => OnlyIfCached
259
+ | e => raise(UnknownRequestCache(e))
260
+ }
261
+
262
+ type requestRedirect =
263
+ | Follow
264
+ | Error
265
+ | Manual
266
+
267
+ let encodeRequestRedirect = (requestRedirect: requestRedirect) =>
268
+ switch requestRedirect {
269
+ | Follow => "follow"
270
+ | Error => "error"
271
+ | Manual => "manual"
272
+ }
273
+
274
+ exception UnknownRequestRedirect(string)
275
+
276
+ let decodeRequestRedirect = (requestRedirect: string) =>
277
+ switch requestRedirect {
278
+ | "follow" => Follow
279
+ | "error" => Error
280
+ | "manual" => Manual
281
+ | e => raise(UnknownRequestRedirect(e))
282
+ }
283
+
284
+ module HeadersInit = {
285
+ type t = headersInit
286
+
287
+ external make: Js.t<{..}> => t = "%identity"
288
+ external makeWithDict: Js.Dict.t<string> => t = "%identity"
289
+ external makeWithArray: array<(string, string)> => t = "%identity"
290
+ }
291
+
292
+ module Headers = {
293
+ type t = headers
294
+
295
+ @new
296
+ external make: t = "Headers"
297
+
298
+ @new
299
+ external makeWithInit: headersInit => t = "Headers"
300
+
301
+ @send
302
+ external append: (t, string, string) => unit = "append"
303
+
304
+ @send
305
+ external delete: (t, string) => unit = "delete"
306
+
307
+ @send @return(nullable)
308
+ external get: (t, string) => option<string> = "get"
309
+
310
+ @send
311
+ external has: (t, string) => bool = "has"
312
+
313
+ @send
314
+ external set: (t, string, string) => unit = "set"
315
+ }
316
+
317
+ module BodyInit = {
318
+ type t = bodyInit
319
+
320
+ external make: string => t = "%identity"
321
+ external makeWithBlob: blob => t = "%identity"
322
+ external makeWithBufferSource: bufferSource => t = "%identity"
323
+ external makeWithFormData: formData => t = "%identity"
324
+ external makeWithUrlSearchParams: urlSearchParams => t = "%identity"
325
+ }
326
+
327
+ module Body = {
328
+ type t = body
329
+
330
+ @get
331
+ external body: t => readableStream = "body"
332
+
333
+ @get
334
+ external bodyUsed: t => bool = "bodyUsed"
335
+
336
+ @send
337
+ external arrayBuffer: t => Js.Promise.t<arrayBuffer> = "arrayBuffer"
338
+
339
+ @send
340
+ external blob: t => Js.Promise.t<blob> = "blob"
341
+
342
+ @send
343
+ external formData: t => Js.Promise.t<formData> = "formData"
344
+
345
+ @send
346
+ external json: t => Js.Promise.t<Js.Json.t> = "json"
347
+
348
+ @send
349
+ external text: t => Js.Promise.t<string> = "text"
350
+ }
351
+
352
+ module RequestInit = {
353
+ type t = requestInit
354
+
355
+ @obj
356
+ external make: (
357
+ @as("method") ~method_: string=?,
358
+ ~headers: headersInit=?,
359
+ ~body: bodyInit=?,
360
+ ~referrer: string=?,
361
+ ~referrerPolicy: string=?,
362
+ ~mode: string=?,
363
+ ~credentials: string=?,
364
+ ~cache: string=?,
365
+ ~redirect: string=?,
366
+ ~integrity: string=?,
367
+ ~keepalive: bool=?,
368
+ ~signal: signal=?,
369
+ unit,
370
+ ) => t = ""
371
+
372
+ let make = (
373
+ ~method_: option<requestMethod>=?,
374
+ ~headers: option<headersInit>=?,
375
+ ~body: option<bodyInit>=?,
376
+ ~referrer: option<string>=?,
377
+ ~referrerPolicy: option<referrerPolicy>=?,
378
+ ~mode: option<requestMode>=?,
379
+ ~credentials: option<requestCredentials>=?,
380
+ ~cache: option<requestCache>=?,
381
+ ~redirect: option<requestRedirect>=?,
382
+ ~integrity: option<string>=?,
383
+ ~keepalive: option<bool>=?,
384
+ ~signal: option<signal>=?,
385
+ (),
386
+ ) => {
387
+ // ReScript syntax
388
+ make(
389
+ ~method_=?Option.map(method_, encodeRequestMethod),
390
+ ~headers?,
391
+ ~body?,
392
+ ~referrer?,
393
+ ~referrerPolicy=?Option.map(referrerPolicy, encodeReferrerPolicy),
394
+ ~mode=?Option.map(mode, encodeRequestMode),
395
+ ~credentials=?Option.map(credentials, encodeRequestCredentials),
396
+ ~cache=?Option.map(cache, encodeRequestCache),
397
+ ~redirect=?Option.map(redirect, encodeRequestRedirect),
398
+ ~integrity?,
399
+ ~keepalive?,
400
+ ~signal?,
401
+ (),
402
+ )
403
+ }
404
+ }
405
+
406
+ module Request = {
407
+ type t = request
408
+
409
+ @new
410
+ external make: string => t = "Request"
411
+ @new
412
+ external makeWithInit: (string, requestInit) => t = "Request"
413
+ @new
414
+ external makeWithRequest: t => t = "Request"
415
+ @new
416
+ external makeWithRequestInit: (t, requestInit) => t = "Request"
417
+
418
+ @get
419
+ external method_: t => string = "method"
420
+ let method_ = (self: t) => decodeRequestMethod(method_(self))
421
+
422
+ @get
423
+ external url: t => string = "url"
424
+
425
+ @get
426
+ external headers: t => headers = "headers"
427
+
428
+ @get
429
+ external type_: t => string = "type"
430
+ let type_ = (self: t) => decodeRequestType(type_(self))
431
+
432
+ @get
433
+ external destination: t => string = "destination"
434
+ let destination = (self: t) => decodeRequestDestination(destination(self))
435
+
436
+ @get
437
+ external referrer: t => string = "referrer"
438
+
439
+ @get
440
+ external referrerPolicy: t => string = "referrerPolicy"
441
+ let referrerPolicy = (self: t) => decodeReferrerPolicy(referrerPolicy(self))
442
+
443
+ @get
444
+ external mode: t => string = "mode"
445
+ let mode = (self: t) => decodeRequestMode(mode(self))
446
+
447
+ @get
448
+ external credentials: t => string = "credentials"
449
+ let credentials = (self: t) => decodeRequestCredentials(credentials(self))
450
+
451
+ @get
452
+ external cache: t => string = "cache"
453
+ let cache = (self: t) => decodeRequestCache(cache(self))
454
+
455
+ @get
456
+ external redirect: t => string = "redirect"
457
+ let redirect = (self: t) => decodeRequestRedirect(redirect(self))
458
+
459
+ @get
460
+ external integrity: t => string = "integrity"
461
+
462
+ @get
463
+ external keepalive: t => bool = "keepalive"
464
+
465
+ @get
466
+ external signal: t => signal = "signal"
467
+
468
+ /* Body Impl */
469
+ @get
470
+ external body: t => readableStream = "body"
471
+
472
+ @get
473
+ external bodyUsed: t => bool = "bodyUsed"
474
+
475
+ @send
476
+ external arrayBuffer: t => Js.Promise.t<arrayBuffer> = "arrayBuffer"
477
+
478
+ @send
479
+ external blob: t => Js.Promise.t<blob> = "blob"
480
+
481
+ @send
482
+ external formData: t => Js.Promise.t<formData> = "formData"
483
+
484
+ @send
485
+ external json: t => Js.Promise.t<Js.Json.t> = "json"
486
+
487
+ @send
488
+ external text: t => Js.Promise.t<string> = "text"
489
+ }
490
+
491
+ module Response = {
492
+ type t = response
493
+
494
+ @val
495
+ external error: unit => t = "error"
496
+
497
+ @val
498
+ external redirect: string => t = "redirect"
499
+
500
+ @val
501
+ external redirectWithStatus: (string, int) => t = "redirect"
502
+
503
+ @get
504
+ external headers: t => headers = "headers"
505
+
506
+ @get
507
+ external ok: t => bool = "ok"
508
+
509
+ @get
510
+ external redirected: t => bool = "redirected"
511
+
512
+ @get
513
+ external status: t => int = "status"
514
+
515
+ @get
516
+ external statusText: t => string = "statusText"
517
+
518
+ @get
519
+ external _type: t => string = "_type"
520
+
521
+ @get
522
+ external url: t => string = "url"
523
+
524
+ @send
525
+ external clone: t => t = "clone"
526
+
527
+ /* Body.Impl */
528
+
529
+ @get
530
+ external body: t => readableStream = "body"
531
+
532
+ @get
533
+ external bodyUsed: t => bool = "bodyUsed"
534
+
535
+ @send
536
+ external arrayBuffer: t => Js.Promise.t<arrayBuffer> = "arrayBuffer"
537
+
538
+ @send
539
+ external blob: t => Js.Promise.t<blob> = "blob"
540
+
541
+ @send
542
+ external formData: t => Js.Promise.t<formData> = "formData"
543
+
544
+ @send
545
+ external json: t => Js.Promise.t<Js.Json.t> = "json"
546
+
547
+ @send
548
+ external text: t => Js.Promise.t<string> = "text"
549
+ }
550
+
551
+ @val
552
+ external fetch: string => Js.Promise.t<response> = "fetch"
553
+
554
+ @val
555
+ external fetchWithInit: (string, requestInit) => Js.Promise.t<response> = "fetch"
556
+
557
+ @val
558
+ external fetchWithRequest: request => Js.Promise.t<response> = "fetch"
559
+
560
+ @val
561
+ external fetchWithRequestInit: (request, requestInit) => Js.Promise.t<response> = "fetch"