Import
ts
import type { CallOptions, WithOptions } from "@noverax/courier/client"Signature
A caller's .with(options) returns a caller whose every call carries them. It takes WithOptions, which is CallOptions without rawData: the status and headers of an answer come from the group's .raw accessor instead.
ts
type CallOptions = Omit<RequestInit, "method" | "body" | "signal"> & {
signal?: AbortSignal | null
timeout?: number
stream?: boolean
forwardVariables?: boolean
method?: "GET" | "POST"
responseType?: "json" | "text" | "blob" | "arrayBuffer" | "formData"
onUploadProgress?: (progress: UploadProgress) => void
onDownloadProgress?: (progress: DownloadProgress) => void
coalesce?: boolean
cancelPrevious?: boolean
rawData?: boolean
}
type WithOptions = Omit<CallOptions, "rawData">Properties
signal— aborts the call when it firestimeout— milliseconds each request the call sends may take, from the moment it is sent, so each attempt of a retried call has its own; a call's own wins over the source'sheaders— added to the call's headers, name by name, whatever their case;.with()at the client, a group and an endpoint merges them allstream— answers with the response's body as a stream, unread, instead of the parsed data, where thefetchhands one over as it comes: React Native's own does not, and a call that asks for one there is refused with aConfigurationErrorRESPONSE_STREAM_UNAVAILABLEforwardVariables— whether the HTTP methods an endpoint calls send the call's variables by themselves, as the body of a write or the query of a read;falseleaves them to the endpoint's own.via()code. Defaults totruemethod— the verb the call goes to the proxy with: GET for a read that may be cached — one that declares.cache()and whose variables are not sensitive — and POST for every other call by default;"POST"keeps a read's variables out of the addressresponseType— how a successful answer is read, when not as the source reads every answeronUploadProgress— called as the body is sent, withpercentfrom 0 to 1,transferredBytesandtotalBytes; a callback that throws fails the call with what it threw, which no retry sends again, and so does one given toonDownloadProgressonDownloadProgress— called as the answer comes in, in the same terms: as it is read, or asXMLHttpRequesttakes it in for a call sent through it — one that does not read its answer as a stream: an upload that reports its progress, and in React Native a download that reports its own, unless the API sends its calls with afetchof its owncoalesce—falsesends the call alone, never joining an identical read already in flight; see Deduplication & AbortcancelPrevious— whether this call cancels the older call of its read still in flight, and may be cancelled by a newer one, over what the endpoint, its groups, the client and the API say..with()given to a whole client still decides call by call. A write is never cancelled, and a value other thantrueorfalsemakes the call reject withConfigurationError("CANCEL_PREVIOUS_INVALID"); see Deduplication & AbortRequestInit— the rest offetch's own options,cache,credentials,keepalive,mode,priority,redirect,referrer,referrerPolicyandintegrity, passed to it as they are
Basic Usage
app/ reports.ts
import { caller } from "@/lib/caller"
const controller = new AbortController()
// A slow endpoint gets more time, and the user can cancel it
const report = await caller.reports
.with({ timeout: 30_000, signal: controller.signal })
.generate({ type: "annual" })
// An upload reports how far it got, from 0 to 1
await caller.files
.with({ onUploadProgress: ({ percent }) => console.log(`${Math.round(percent * 100)}%`) })
.upload({ body: blob })Related
- Request options — every option a source, a group and an endpoint take, and which wins
- Types —
TypedClient,GroupClientand the other types around a caller - Raw data — the
.rawaccessor for the status and headers