Repository navigation
feat(fetch): add result-based SafeFetch wrapper [Codex] #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jderochervlk
wants to merge
3
commits into
main
Choose a base branch
from
feat/safe-fetch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat(fetch): add result-based SafeFetch wrapper
- Loading branch information
commit 34977ccebdc49662d94c4b94fab07fd3eb7273dc
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -330,7 +330,8 @@ | |
| "Headers", | ||
| "HeadersInit", | ||
| "Request", | ||
| "Response" | ||
| "Response", | ||
| "SafeFetch" | ||
| ] | ||
| }, | ||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| type httpError = { | ||
| response: Response.t, | ||
| } | ||
|
|
||
| type fetchError = | ||
| | FetchRejected(exn) | ||
| | ResponseNotOk(httpError) | ||
|
|
||
| type readError = { | ||
| response: Response.t, | ||
| cause: exn, | ||
| } | ||
|
|
||
| type response<'body> = { | ||
| response: Response.t, | ||
| body: 'body, | ||
| } | ||
|
|
||
| let checkOk = (response: Response.t): result<Response.t, httpError> => | ||
| if response.ok { | ||
| Ok(response) | ||
| } else { | ||
| Error({response: response}) | ||
| } | ||
|
|
||
| let fetch = async (url: string, ~init: option<Request.requestInit>=?) => { | ||
| try { | ||
| let response = await Fetch.fetch(url, ~init?) | ||
| response->checkOk->Result.mapError(error => ResponseNotOk(error)) | ||
| } catch { | ||
| | cause => Error(FetchRejected(cause)) | ||
| } | ||
| } | ||
|
|
||
| let fetchWithRequest = async (request: Request.t, ~init: option<Request.requestInit>=?) => { | ||
| try { | ||
| let response = await Fetch.fetchWithRequest(request, ~init?) | ||
| response->checkOk->Result.mapError(error => ResponseNotOk(error)) | ||
| } catch { | ||
| | cause => Error(FetchRejected(cause)) | ||
| } | ||
| } | ||
|
|
||
| let read = async (response: Response.t, readBody: Response.t => promise<'body>) => { | ||
| try { | ||
| Ok({response, body: await readBody(response)}) | ||
| } catch { | ||
| | cause => Error({response, cause}) | ||
| } | ||
| } | ||
|
|
||
| let arrayBuffer = response => read(response, Response.arrayBuffer) | ||
| let blob = response => read(response, Response.blob) | ||
| let bytes = response => read(response, Response.bytes) | ||
| let formData = response => read(response, Response.formData) | ||
| let json = response => read(response, Response.json) | ||
| let text = response => read(response, Response.text) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| type httpError = { | ||
| response: Response.t, | ||
| } | ||
|
|
||
| type fetchError = | ||
| | FetchRejected(exn) | ||
| | ResponseNotOk(httpError) | ||
|
|
||
| type readError = { | ||
| response: Response.t, | ||
| cause: exn, | ||
| } | ||
|
|
||
| type response<'body> = { | ||
| response: Response.t, | ||
| body: 'body, | ||
| } | ||
|
|
||
| let checkOk: Response.t => result<Response.t, httpError> | ||
|
|
||
| let fetch: (string, ~init: Request.requestInit=?) => promise<result<Response.t, fetchError>> | ||
|
|
||
| let fetchWithRequest: ( | ||
| Request.t, | ||
| ~init: Request.requestInit=?, | ||
| ) => promise<result<Response.t, fetchError>> | ||
|
|
||
| let arrayBuffer: Response.t => promise<result<response<ArrayBuffer.t>, readError>> | ||
| let blob: Response.t => promise<result<response<Blob.t>, readError>> | ||
| let bytes: Response.t => promise<result<response<array<int>>, readError>> | ||
| let formData: Response.t => promise<result<response<FormData.t>, readError>> | ||
| let json: Response.t => promise<result<response<JSON.t>, readError>> | ||
| let text: Response.t => promise<result<response<string>, readError>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| @scope("Object") | ||
| external isSame: ('value, 'value) => bool = "is" | ||
|
|
||
| let okResponse = Response.fromString("ok", ~init={status: 200}) | ||
| switch okResponse->SafeFetch.checkOk { | ||
| | Ok(response) => assert(isSame(response, okResponse)) | ||
| | Error(_) => assert(false) | ||
| } | ||
|
|
||
| let notOkResponse = Response.fromString("missing", ~init={status: 404}) | ||
| switch notOkResponse->SafeFetch.checkOk { | ||
| | Error({response}) => assert(isSame(response, notOkResponse)) | ||
| | Ok(_) => assert(false) | ||
| } | ||
|
|
||
| %%raw(` | ||
| globalThis.__safeFetchResponse = new Response("ok", {status: 200}) | ||
| globalThis.fetch = (_input, init) => { | ||
| globalThis.__safeFetchInit = init | ||
| return Promise.resolve(globalThis.__safeFetchResponse) | ||
| } | ||
| `) | ||
|
|
||
| switch await SafeFetch.fetch("https://example.com/ok", ~init={method: "POST"}) { | ||
| | Ok(response) => { | ||
| let expected: Response.t = %raw(`globalThis.__safeFetchResponse`) | ||
| let method: string = %raw(`globalThis.__safeFetchInit.method`) | ||
| assert(isSame(response, expected)) | ||
| assert(method == "POST") | ||
| } | ||
| | Error(_) => assert(false) | ||
| } | ||
|
|
||
| %%raw(` | ||
| globalThis.__safeFetchResponse = new Response("missing", {status: 404}) | ||
| globalThis.fetch = () => Promise.resolve(globalThis.__safeFetchResponse) | ||
| `) | ||
|
|
||
| switch await SafeFetch.fetch("https://example.com/missing") { | ||
| | Error(ResponseNotOk({response})) => { | ||
| let expected: Response.t = %raw(`globalThis.__safeFetchResponse`) | ||
| assert(isSame(response, expected)) | ||
| } | ||
| | Error(FetchRejected(_)) | Ok(_) => assert(false) | ||
| } | ||
|
|
||
| %%raw(` | ||
| globalThis.__safeFetchCause = new TypeError("request rejected") | ||
| globalThis.fetch = () => Promise.reject(globalThis.__safeFetchCause) | ||
| `) | ||
|
|
||
| switch await SafeFetch.fetch("https://example.com/rejected") { | ||
| | Error(FetchRejected(cause)) => | ||
| switch cause->JsExn.fromException { | ||
| | Some(rawCause) => assert(isSame(rawCause, %raw(`globalThis.__safeFetchCause`))) | ||
| | None => assert(false) | ||
| } | ||
| | Error(ResponseNotOk(_)) | Ok(_) => assert(false) | ||
| } | ||
|
|
||
| let request = Request.fromURL("https://example.com/synchronous") | ||
|
|
||
| %%raw(` | ||
| globalThis.__safeFetchCause = new TypeError("request threw") | ||
| globalThis.fetch = () => { throw globalThis.__safeFetchCause } | ||
| `) | ||
|
|
||
| switch await SafeFetch.fetchWithRequest(request) { | ||
| | Error(FetchRejected(cause)) => | ||
| switch cause->JsExn.fromException { | ||
| | Some(rawCause) => assert(isSame(rawCause, %raw(`globalThis.__safeFetchCause`))) | ||
| | None => assert(false) | ||
| } | ||
| | Error(ResponseNotOk(_)) | Ok(_) => assert(false) | ||
| } | ||
|
|
||
| let textResponse = Response.fromString("hello") | ||
| switch await textResponse->SafeFetch.text { | ||
| | Ok({response, body}) => { | ||
| assert(isSame(response, textResponse)) | ||
| assert(body == "hello") | ||
| } | ||
| | Error(_) => assert(false) | ||
| } | ||
|
|
||
| let jsonResponse = Response.fromString(`{"name":"Ada"}`) | ||
| switch await jsonResponse->SafeFetch.json { | ||
| | Ok({response, body}) => { | ||
| let _json: JSON.t = body | ||
| assert(isSame(response, jsonResponse)) | ||
| } | ||
| | Error(_) => assert(false) | ||
| } | ||
|
|
||
| let malformedResponse = Response.fromString("not json") | ||
| switch await malformedResponse->SafeFetch.json { | ||
| | Error({response, cause: _}) => assert(isSame(response, malformedResponse)) | ||
| | Ok(_) => assert(false) | ||
| } | ||
|
|
||
| let consumedResponse = Response.fromString("once") | ||
| let _ = await consumedResponse->Response.text | ||
| switch await consumedResponse->SafeFetch.text { | ||
| | Error({response, cause: _}) => assert(isSame(response, consumedResponse)) | ||
| | Ok(_) => assert(false) | ||
| } | ||
|
|
||
| let errorBodyResponse = Response.fromString("details", ~init={status: 500}) | ||
| switch await errorBodyResponse->SafeFetch.text { | ||
| | Ok({response, body}) => { | ||
| assert(isSame(response, errorBodyResponse)) | ||
| assert(body == "details") | ||
| } | ||
| | Error(_) => assert(false) | ||
| } | ||
|
|
||
| let arrayBufferResult: result< | ||
| SafeFetch.response<ArrayBuffer.t>, | ||
| SafeFetch.readError, | ||
| > = await Response.fromString("buffer")->SafeFetch.arrayBuffer | ||
| assert(arrayBufferResult->Result.isOk) | ||
|
|
||
| let blobResult: result<SafeFetch.response<Blob.t>, SafeFetch.readError> = await Response.fromString( | ||
| "blob", | ||
| )->SafeFetch.blob | ||
| assert(blobResult->Result.isOk) | ||
|
|
||
| let bytesResult: result< | ||
| SafeFetch.response<array<int>>, | ||
| SafeFetch.readError, | ||
| > = await Response.fromString("bytes")->SafeFetch.bytes | ||
| assert(bytesResult->Result.isOk) | ||
|
|
||
| let formDataResponse = Response.fromString( | ||
| "fruit=peach", | ||
| ~init={ | ||
| headers: HeadersInit.fromDict(dict{"Content-Type": "application/x-www-form-urlencoded"}), | ||
| }, | ||
| ) | ||
| let formDataResult: result< | ||
| SafeFetch.response<FormData.t>, | ||
| SafeFetch.readError, | ||
| > = await formDataResponse->SafeFetch.formData | ||
| assert(formDataResult->Result.isOk) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.