Skip to content

Commit

Permalink
Merge pull request #15 from rescript-lang/refactor-fetch
Browse files Browse the repository at this point in the history
Improve fetch binding
  • Loading branch information
nojaf authored Nov 20, 2024
2 parents 47c6b2c + fb6427a commit 20e9e63
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/DOMAPI/Window.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions src/DOMAPI/Window.res
Original file line number Diff line number Diff line change
Expand Up @@ -294,16 +294,33 @@ external structuredClone: (window, 't, ~options: structuredSerializeOptions=?) =
"structuredClone"

/**
`fetch(window, string, init)`
Starts the process of fetching a resource from the network,
returning a promise that is fulfilled once the response is available.
```res
let response = await window->Window.fetch("https://rescript-lang.org")
```
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Window/fetch)
*/
@send
external fetch: (window, ~input: request, ~init: requestInit=?) => Promise.t<response> = "fetch"
external fetch: (window, string, ~init: requestInit=?) => Promise.t<response> = "fetch"

/**
`fetch_withRequest(window, request, init)`
Starts the process of fetching a resource from the network,
returning a promise that is fulfilled once the response is available.
```res
let response = await window->Window.fetch(myRequest)
```
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Window/fetch)
*/
@send
external fetch2: (window, ~input: string, ~init: requestInit=?) => Promise.t<response> = "fetch"
external fetch_withRequest: (window, request, ~init: requestInit=?) => Promise.t<response> = "fetch"

/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/requestAnimationFrame)
Expand Down
23 changes: 21 additions & 2 deletions src/Global.res
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,33 @@ external createImageBitmap18: (
external structuredClone: ('t, ~options: structuredSerializeOptions=?) => 't = "structuredClone"

/**
`fetch(string, init)`
Starts the process of fetching a resource from the network,
returning a promise that is fulfilled once the response is available.
```res
let response = await fetch("https://rescript-lang.org")
```
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Window/fetch)
*/
external fetch: (~input: request, ~init: requestInit=?) => Promise.t<response> = "fetch"
external fetch: (string, ~init: requestInit=?) => Promise.t<response> = "fetch"

/**
`fetch_withRequest(request, init)`
Starts the process of fetching a resource from the network,
returning a promise that is fulfilled once the response is available.
```res
let response = await fetch(myRequest)
```
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/Window/fetch)
*/
external fetch2: (~input: string, ~init: requestInit=?) => Promise.t<response> = "fetch"
@send
external fetch_withRequest: (request, ~init: requestInit=?) => Promise.t<response> = "fetch"

/**
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/DedicatedWorkerGlobalScope/requestAnimationFrame)
Expand Down
9 changes: 9 additions & 0 deletions tests/Global__test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/Global__test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
open WebAPI.Global

let response = await fetch("https://rescript-lang.org/")

0 comments on commit 20e9e63

Please sign in to comment.