Skip to content

Client and registration

Create a Fetcher for shared URL, headers, timeout, and interceptor policy. Requests still use the runtime's global fetch; a client does not own a connection pool or require destroy().

Construction and defaults

new Fetcher(options?: FetcherOptions) accepts the following options. Supplying an options object requires baseURL; omitting the object uses DEFAULT_OPTIONS.

OptionDefaultContract
baseURL: string''Used by the mutable urlBuilder; relative URLs still need a runtime that accepts them.
headers?: RequestHeaders{'Content-Type': 'application/json'}A supplied object replaces these constructor defaults. Each request gets a defensive, case-insensitive merge.
timeout?: numberundefinedMilliseconds; no timer until configured. Request 0 disables the inherited timeout.
urlTemplateStyle?UrlTemplateStyle.UriTemplate{id} syntax; Express selects :id.
interceptors?New InterceptorManagerSupplied manager is used as-is; it can be shared across clients.
validateStatus?200 <= status < 300Used only when constructing the default manager.

urlBuilder, headers, and timeout can be changed for subsequent requests. interceptors is readonly as a property, while its registries remain mutable. Default header objects are shared references: replace client.headers or use request headers rather than mutating exported DEFAULT_OPTIONS.headers globally.

Choosing an entry point

MethodInputResolved value without an explicit extractor
resolveExchange(request, options?)FetchRequest, RequestOptionsA synchronous FetchExchange; no HTTP I/O.
exchange(request, options?)SamePromise<FetchExchange> after the interceptor pipeline; does not extract a body.
request<R = FetchExchange>(request, options?)SamePromise<R> through the selected extractor; default exchange.
fetch<R = Response>(url, request = {}, options?)FetchRequestInitPromise<R>; default native Response.
get, head, options, traceURL, request without method/body, optionsPromise<R = Response>; method fixed by the helper.
post, put, patch, deleteURL, request without method, optionsSame, with optional body.

DEFAULT_REQUEST_OPTIONS selects ResultExtractors.Exchange; DEFAULT_FETCH_OPTIONS selects ResultExtractors.Response. A generic such as get<User>() alone does not parse JSON: select an extractor or call response.json<User>(). Native Fetch may reject unsupported methods such as TRACE. See results and errors.

Named clients

new NamedFetcher(name, options?) extends Fetcher and immediately registers itself in the singleton fetcherRegistrar. fetcher is the exported default named instance, with DEFAULT_FETCHER_NAME = 'default'. Duplicate registration replaces the prior client.

FetcherRegistrar.register(name, client): void, unregister(name): boolean, and get(name): Fetcher | undefined manage entries. requiredGet(name) and the default getter throw Error when absent. Assigning default registers under 'default'; fetchers returns a new Map whose values are the same clients. Unregistering does not abort in-flight requests.

getFetcher(fetcher?, defaultFetcher?) accepts a direct instance, a registry name, or no value. An instance wins; a name must exist; a falsy argument chooses the supplied fallback and then the global default. FetcherCapable exposes that optional field. NamedCapable supplies name, and FetcherConfigurer.applyTo(fetcher): void is the structural contract used by configurable extensions.

Complete example

ts
import {
  Fetcher,
  NamedFetcher,
  fetcherRegistrar,
  getFetcher,
} from '@ahoo-wang/fetcher';

const local = new Fetcher({
  baseURL: 'https://api.example.com',
  timeout: 3000,
});
const named = new NamedFetcher('reports', {
  baseURL: 'https://reports.example.com',
});
console.assert(getFetcher('reports') === named);
const exchange = local.resolveExchange({ url: '/users/1' });
console.assert(exchange.request.timeout === 3000);
fetcherRegistrar.unregister('reports');

Public symbols and source

SymbolImplementation
FetcherOptionsfetcher.ts:52
DEFAULT_OPTIONSfetcher.ts:87
RequestOptionsfetcher.ts:95
DEFAULT_REQUEST_OPTIONSfetcher.ts:98
DEFAULT_FETCH_OPTIONSfetcher.ts:101
Fetcherfetcher.ts:124
FetcherCapablefetcherCapable.ts:22
getFetcherfetcherCapable.ts:37
DEFAULT_FETCHER_NAMEfetcherRegistrar.ts:19
FetcherRegistrarfetcherRegistrar.ts:41
fetcherRegistrarfetcherRegistrar.ts:166
NamedFetchernamedFetcher.ts:38
fetchernamedFetcher.ts:89
NamedCapabletypes.ts:141
FetcherConfigurertypes.ts:248

Package index

Released under the Apache License 2.0.