Skip to content

Commit

Permalink
Merge pull request #17 from rescript-lang/fetch-with-options
Browse files Browse the repository at this point in the history
Add fetch with headers and body sample
  • Loading branch information
nojaf authored Nov 20, 2024
2 parents 20e9e63 + 9d44769 commit 9b9723d
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"scripts": {
"test": "node tests/index.js",
"build": "rewatch",
"format": "rescript format -all && prettier --write ./docs ./tests/index.js ./package.json",
"format": "rescript format -all && prettier --write ./tests/index.js ./package.json",
"docs": "astro dev",
"build:docs": "astro build"
},
Expand Down
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.

6 changes: 4 additions & 2 deletions src/FetchAPI.res
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ type urlSearchParams = {
size: int,
}

type headersInit = any
type headersInit

type bodyInit

type requestInfo = any

Expand All @@ -236,7 +238,7 @@ type requestInit = {
/**
A BodyInit object or null to set request's body.
*/
mutable body?: Null.t<bodyInit>,
mutable body?: bodyInit,
/**
A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/FetchAPI/BodyInit.js

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

47 changes: 47 additions & 0 deletions src/FetchAPI/BodyInit.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
open FileAPI
open FetchAPI

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)
*/
external fromString: string => bodyInit = "%identity"

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)
*/
external fromArrayBuffer: ArrayBuffer.t => bodyInit = "%identity"

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)
*/
external fromTypedArray: TypedArray.t<'t> => bodyInit = "%identity"

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)
*/
external fromDataView: DataView.t => bodyInit = "%identity"

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)
*/
external fromBlob: blob => bodyInit = "%identity"

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)
*/
external fromFile: file => bodyInit = "%identity"

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)
*/
external fromURLSearchParams: urlSearchParams => bodyInit = "%identity"

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)
*/
external fromFormData: formData => bodyInit = "%identity"

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body)
*/
external fromReadableStream: readableStream<'t> => bodyInit = "%identity"
2 changes: 2 additions & 0 deletions src/FetchAPI/HeadersInit.js

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

11 changes: 11 additions & 0 deletions src/FetchAPI/HeadersInit.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
open FetchAPI

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_headers)
*/
external fromDict: dict<string> => headersInit = "%identity"

/**
[Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_headers)
*/
external fromHeaders: headers => headersInit = "%identity"
2 changes: 0 additions & 2 deletions src/Prelude.res
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ external unsafeConversation: 'tinput => 'toutput = "%identity"

type bufferSource = any

type bodyInit = any

type sharedArrayBuffer = any

type float64Array = any
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.

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

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

let response2 = await fetch(
"https://rescript-lang.org/",
~init={
headers: HeadersInit.fromDict(
dict{
"Content-Type": "application/json",
"Authorization": "Bearer token",
},
),
body: BodyInit.fromString(`secret=foo&response=bar`),
},
)

0 comments on commit 9b9723d

Please sign in to comment.