Skip to content

ApiClient

The client returned by createClient: a callable object that carries the default fetch options and the extensions added to it.

Type Definition

ts
type Fn<T = any> = (...args: any[]) => T

type ClientOptions<BaseURL extends string = string> = Omit<FetchOptions, 'baseURL'> & { baseURL?: BaseURL }

interface ApiClient<BaseURL extends string = string> extends Function {
  _handler?: Fn
  _extensions: Record<PropertyKey, unknown>
  defaultOptions: ClientOptions<BaseURL>
  fetch: $Fetch
  with: <Extension extends ApiExtension>(
    createExtension: (client: this) => Extension,
  ) => this & Extension
}

createExtension receives the client as it stands, carrying every extension added before it – see reaching the client.

ApiExtension is either a HandlerExtension, which becomes the client's call signature, or a MethodsExtension, whose entries become methods on the client. FetchOptions and $Fetch come from ofetch.

fetch is what every extension makes its requests through. It defaults to an ofetch instance built from defaultOptions, and createClient takes one of your own.

Calling a client that has no handler extension throws a TypeError – add one with with, such as ofetchBuilder().

_handler and _extensions are the bookkeeping behind with: the current call signature and the properties resolved through the proxy. They are visible on the type, but reading or assigning them is not part of the API.