Repository navigation
feat(fetch): add result-based SafeFetch wrapper [Codex] - #367
Conversation
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Also maybe add some examples that use |
tsnobip
left a comment
There was a problem hiding this comment.
there are a few open questions we should likely settle here before we generalize this kind of helper modules. Please chime in @fhammerschmidt @jderochervlk and anybody who'd like to! :)
|
The requested |
Closes #363
Summary
Add an opt-in
WebAPI.SafeFetchmodule that converts expected Fetch, HTTP-status, and response-body failures into composableresultvalues. RawWebAPI.FetchandWebAPI.Responsebindings remain unchanged.WebAPI.SafeFetchfeature; it enablesWebAPI.Fetchand the shared raw types transitively.#FetchRejected(cause),#ResponseNotOk({response}), and#BodyReadRejected({response, cause}).let?can compose fetch, body reading, and application decoding withoutmapErroradapters.{response, body}; the body has been consumed.jsonreturnsJSON.t; application-specific validation stays with the caller.This intentionally changes the experimental API: consumers that previously selected only
WebAPI.Fetchmust explicitly selectWebAPI.SafeFetchto use the wrapper.Usage
The locked ReScript 13 alpha compiler requires
LetUnwrapforlet?; ordinaryswitchusage does not require that setting.{ "experimental-features": {"LetUnwrap": true}, "dependencies": [ {"name": "@rescript/webapi", "features": ["WebAPI.SafeFetch"]} ] }The caller handles four distinct error tags in one switch: request rejection, non-OK HTTP status, body-read rejection, and application-owned
#InvalidUser. Malformed JSON is a body-read error; valid JSON with the wrong shape is an application-decoding error. A non-OK response remains available for reading error details.Review follow-up
checkOkand body readers.checkOkresponse annotation required for record-field inference.src/safe-fetchwith its own feature and added isolated public-consumer checks.let?examples plus runtime propagation/short-circuit tests.Both fetch entry points still construct their final result directly and call native
fetch. The late public alias prevents native-fetch shadowing, optional initializers are forwarded or omitted, and the only generated runtime import is exception normalization.exnpreserves arbitrary JavaScript abort reasons viaJsExn.fromException.Validation
Passed locally with Node 26.3.0 and locked ReScript 13.0.0-alpha.5:
npm cinpm run format:checknpm run buildnpm run check:features— four checker unit tests, all 28 clean public feature builds, a SafeFetch-only consumer with composed application errors, a raw Fetch-only consumer, and a negative check proving SafeFetch is unavailable with only Fetch enablednpm test— all seven runtime suites, including native default/custom abort reasons, both fetch entry points, optional initialization, synchronous/asynchronous rejection, body readers, and composed stage-specific failures/short-circuitingnpm run build:docs— 305 pages, including the generated SafeFetch API page