API Reference
Fetch Core
Section titled “Fetch Core”Result: the base network result type,Either<FetchError, Response>.defaultContext: reusableContextbacked byglobalThis.fetch.defaultFetch: default fetch handler created fromdefaultContext.createFetchHandler(context): creates a fetch handler from a supplied context.Handler<TResult>: a normalized request handler that is also executable with fetch-style(input, init?)arguments.normalizeInput(input, init?): converts normalized input or fetch-style arguments into the request shape used by interceptors.
Composition
Section titled “Composition”compose(...interceptors): creates a composed handler around a terminal fetch handler.defineInterceptor(interceptor): defines a request interceptor.Interceptor<AddedResult, DownstreamResult>: an async pipeline step that can preserve downstream results or add its own typedEitherresult.ComposeInterceptorsResult<TInterceptors, TResult>: computes the finalEitherresult type for a composed pipeline.
const handler = compose(withTimeout(250))(defaultFetch);
type HandlerResult = Awaited<ReturnType<typeof handler>>;// Either<FetchError | TimeoutError, Response>Interceptors
Section titled “Interceptors”withBaseUrl(baseUrl): resolves relative request paths against a base URL.withHeaders(headers): adds headers to outgoing requests.withAuth(tokenOrCallback): adds authentication data to outgoing requests.withCache(options): caches request results with a configured store.withRetry(options): retries eligible failed requests.withTimeout(ms): aborts requests that exceed the timeout and returns aTimeoutError.
Decoders And Validation
Section titled “Decoders And Validation”text(responseOrResult): reads a response body as text.json(responseOrResult): reads a response body as JSON.decodeJson(responseOrResult, decoder): decodes a JSON response with a supplied decoder.validate(result, schema): validates a result with a Standard Schema compatible validator.expectStatus(responseOrResult, status): requires a matching response status.
Errors
Section titled “Errors”FetchError: normalized network/request failure returned bycreateFetchHandler(...).TimeoutError: returned whenwithTimeoutaborts the underlying request.ParseError: returned by body helpers such asjson(...),text(...), andempty(...).DecodeError: the base shape for decoder failures returned bydecodeJson(...).SchemaError: returned byvalidate(...)when a Standard Schema compatible validator rejects the value.StatusError: returned byexpectStatus(...)and status-range helpers when the response status does not match.