CodeVetter · Repo Unpack · Data flow
unjs/ofetch
Inputs enter as FetchRequest and FetchOptions, then become a mutable FetchContext. Resolution merges shared defaults with call-specific headers and query values before hooks and normalization. Fetch produces a Response whose body becomes _data, which hooks can inspect before return or rejection. Retries carry transformed context back through the same pipeline.
- Each raw call allocates a context containing the original request, resolved options, and initially undefined response and error fields.
- Header resolution chooses explicit input headers over Request headers, then overlays them on defaults using Headers.set; it does not merge Request headers when explicit headers are supplied.
- Query precedence is default params, default query, input params, then input query; other options use shallow defaults/input spread.
- Method capitalization precedes onRequest; that hook can change the request or options before string URL transformation and body serialization.
- withQuery omits undefined values in new queries; in existing queries it deletes undefined keys, appends array elements, and replaces scalar values.
- Query normalization turns null into an empty string, numbers and booleans into strings, and objects into JSON; serialization uses URLSearchParams.
- Truthy timeout adds a generated AbortSignal, optionally combined with the caller signal, before resolved options cross the fetch boundary as RequestInit.
- Decoded data belongs to response._data; the normal client returns that value, while FetchError getters retain access to context and parsed error data.