Skip to main content
Version: v8.0.0

Adapter

Base adapter class responsible for executing HTTP requests. Adapters handle endpoint resolution, header/payload/query-params mapping, and the actual fetch call. Extend or configure this class to integrate custom transport layers (e.g., axios, graphql, firebase).


Import
import { Adapter } from "@hyper-fetch/core"

Parameters​

class Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType> {
constructor(options: { defaultExtra: Extra; defaultMethod: MethodType; defaultRequestOptions?: RequestOptionsType<EndpointType, AdapterOptions, MethodType>; name: string; systemErrorExtra: Extra; systemErrorStatus: StatusType }) {};
unstable_queryParamsMapperConfig: Parameters<QueryParamsMapperType>[1];
unstable_queryParamsMapper: QueryParamsMapperType;
unstable_payloadMapperConfig: Parameters<PayloadMapperType>[1];
unstable_payloadMapper: PayloadMapperType;
unstable_headerMapperConfig: Parameters<HeaderMapperType>[1];
unstable_headerMapper: HeaderMapperType;
unstable_endpointMapperConfig: Parameters<EndpointMapperType>[1];
unstable_endpointMapper: EndpointMapperType;
systemErrorStatus: StatusType;
systemErrorExtra: Extra;
options: { defaultExtra: Extra; defaultMethod: MethodType; defaultRequestOptions?: RequestOptionsType<EndpointType, AdapterOptions, MethodType>; name: string; systemErrorExtra: Extra; systemErrorStatus: StatusType };
name: string;
logger: LoggerMethods;
initialized: boolean;
defaultRequestOptions: RequestOptionsType<EndpointType, AdapterOptions, MethodType>;
defaultMethod: MethodType;
defaultExtra: Extra;
client: ClientInstance;
unstable_devtoolsEndpointGetter() => (endpoint: string) => string;
unstable_fetcher() => AdapterFetcherType<Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>>;
unstable_getAdapterDefaults() => (request: ExtendRequest<RequestInstance, { client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>> }>) => AdapterOptions;
unstable_getRequestDefaults() => (options: RequestOptionsType<EndpointType, AdapterOptions, MethodType>) => Partial<RequestOptionsType<EndpointType, AdapterOptions, MethodType>>;
unstable_internalErrorMapping() => (error: TimeoutError | AbortError | DeletedError | RequestProcessingError | UnexpectedError) => any;
unstable_onInitializeCallback() => (options: { client: ClientInstance }) => void;
fetch(request: ExtendRequest<RequestInstance, { client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>> }>, requestId: string) => Promise<RequestResponseType<RequestInstance>>;
initialize(client: ClientInstance) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
onInitialize(callback: (options: { client: ClientInstance }) => void) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setAdapterDefaults(callback: (request: ExtendRequest<RequestInstance, { client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>> }>) => AdapterOptions) => this;
setDefaultExtra(extra: Extra) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setDefaultMethod(method: MethodType) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setDevtoolsEndpointGetter(callback: (endpoint: string) => string) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setEndpointMapper<NewEndpointMapper>(endpointMapper: NewEndpointMapper) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, NewEndpointMapper, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setEndpointMapperConfig<NewEndpointMapperConfig>(config: NewEndpointMapperConfig) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setFetcher(fetcher: AdapterFetcherType<Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>>) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setHeaderMapper<NewMapper>(headerMapper: NewMapper) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, NewMapper, PayloadMapperType>;
setHeaderMapperConfig<NewHeaderMapperConfig>(config: NewHeaderMapperConfig) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setInternalErrorMapping(callback: (error: TimeoutError | AbortError | DeletedError | RequestProcessingError | UnexpectedError) => any) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setPayloadMapper<NewMapper>(payloadMapper: NewMapper) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, NewMapper>;
setPayloadMapperConfig<NewPayloadMapperConfig>(config: NewPayloadMapperConfig) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setQueryParamsMapper<NewQueryParamsMapper>(queryParamsMapper: NewQueryParamsMapper) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, NewQueryParamsMapper, HeaderMapperType, PayloadMapperType>;
setQueryParamsMapperConfig<NewQueryParamsMapperConfig>(config: NewQueryParamsMapperConfig) => Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>;
setRequestDefaults(callback: undefined | (options: RequestOptionsType<EndpointType, AdapterOptions, MethodType>) => Partial<RequestOptionsType<EndpointType, AdapterOptions, MethodType>>) => this;
}
Parameters
NameTypeDescription
options
{ defaultExtra: Extra; defaultMethod: MethodType; defaultRequestOptions?: RequestOptionsType<EndpointType, AdapterOptions, MethodType>; name: string; systemErrorExtra: Extra; systemErrorStatus: StatusType }

Properties​

client​

Type
ClientInstance

defaultExtra​

Type
Extra

defaultMethod​

Type
MethodType

defaultRequestOptions​

Type
RequestOptionsType<EndpointType, AdapterOptions, MethodType>

initialized​

Type
boolean

logger​

Type
LoggerMethods

name​


Defaults


Type
string

options​

Type
{ defaultExtra: Extra; defaultMethod: MethodType; defaultRequestOptions?: RequestOptionsType<EndpointType, AdapterOptions, MethodType>; name: string; systemErrorExtra: Extra; systemErrorStatus: StatusType }

systemErrorExtra​

Type
Extra

systemErrorStatus​

Type
StatusType

unstable_endpointMapper​

Method to get the endpoint for the adapter request.

Type
EndpointMapperType

unstable_endpointMapperConfig​

Type
Parameters<EndpointMapperType>[1]

unstable_headerMapper​

Method to get default headers and to map them based on the data format exchange, by default it handles FormData / JSON formats.

Type
HeaderMapperType

unstable_headerMapperConfig​

Type
Parameters<HeaderMapperType>[1]

unstable_payloadMapper​

Method to get request data and transform them to the required format. It handles FormData and JSON by default.

Type
PayloadMapperType

unstable_payloadMapperConfig​

Type
Parameters<PayloadMapperType>[1]

unstable_queryParamsMapper​

Method to get request data and transform them to the required format.

Type
QueryParamsMapperType

unstable_queryParamsMapperConfig​

Type
Parameters<QueryParamsMapperType>[1]

Methods​

fetch()​

Execute a request through the adapter pipeline: maps endpoint/headers/payload, runs plugins, then calls the fetcher.

Preview
fetch(request: ExtendRequest<RequestInstance, { client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>> }>, requestId: string)
Parameters
Parameters
NameTypeDescription
request
ExtendRequest<RequestInstance, { client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>> }>
requestId
string
Returns
Promise<RequestResponseType<RequestInstance>>

initialize()​

Initialize the adapter with a client reference and set up logging. Called automatically when the client is created.

Preview
initialize(client: ClientInstance)
Parameters
Parameters
NameTypeDescription
client
ClientInstance
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

onInitialize()​

Register a callback invoked once the adapter is initialized with a client.

Preview
onInitialize(callback: (options: { client: ClientInstance }) => void)
Parameters
Parameters
NameTypeDescription
callback
(options: { client: ClientInstance }) => void
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setAdapterDefaults()​

Set the adapter default options added to every sent request

Preview
setAdapterDefaults(callback: (request: ExtendRequest<RequestInstance, { client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>> }>) => AdapterOptions)
Parameters
Parameters
NameTypeDescription
callback
(request: ExtendRequest<RequestInstance, { client: Client<any, Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>> }>) => AdapterOptions
Returns
this

setDefaultExtra()​

Set the default extra metadata attached to every request response (e.g., headers, status).

Preview
setDefaultExtra(extra: Extra)
Parameters
Parameters
NameTypeDescription
extra
Extra
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setDefaultMethod()​

Set the default HTTP method used when a request does not specify one.

Preview
setDefaultMethod(method: MethodType)
Parameters
Parameters
NameTypeDescription
method
MethodType
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setDevtoolsEndpointGetter()​

Set a function that formats the endpoint string for display in devtools.

Preview
setDevtoolsEndpointGetter(callback: (endpoint: string) => string)
Parameters
Parameters
NameTypeDescription
callback
(endpoint: string) => string
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setEndpointMapper()​

Set the endpoint mapping function which transforms the endpoint string before the request is sent

Preview
setEndpointMapper<NewEndpointMapper>(endpointMapper: NewEndpointMapper)
Parameters
Parameters
NameTypeDescription
endpointMapper
NewEndpointMapper
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, NewEndpointMapper, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setEndpointMapperConfig()​

Set the configuration object passed as the second argument to the endpoint mapper.

Preview
setEndpointMapperConfig<NewEndpointMapperConfig>(config: NewEndpointMapperConfig)
Parameters
Parameters
NameTypeDescription
config
NewEndpointMapperConfig
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setFetcher()​

Set the fetcher function that performs the actual network request. Must be called before any request can be executed.

Preview
setFetcher(fetcher: AdapterFetcherType<Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>>)
Parameters
Parameters
NameTypeDescription
fetcher
AdapterFetcherType<Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>>
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setHeaderMapper()​

Set the custom header mapping function

Preview
setHeaderMapper<NewMapper>(headerMapper: NewMapper)
Parameters
Parameters
NameTypeDescription
headerMapper
NewMapper
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, NewMapper, PayloadMapperType>

setHeaderMapperConfig()​

Set the configuration object passed as the second argument to the header mapper.

Preview
setHeaderMapperConfig<NewHeaderMapperConfig>(config: NewHeaderMapperConfig)
Parameters
Parameters
NameTypeDescription
config
NewHeaderMapperConfig
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setInternalErrorMapping()​

Set a custom mapping for internal adapter errors before they are returned in the response.

Preview
setInternalErrorMapping(callback: (error: TimeoutError | AbortError | DeletedError | RequestProcessingError | UnexpectedError) => any)
Parameters
Parameters
NameTypeDescription
callback
(error: TimeoutError | AbortError | DeletedError | RequestProcessingError | UnexpectedError) => any
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setPayloadMapper()​

Set the request payload mapping function which gets triggered before request is send

Preview
setPayloadMapper<NewMapper>(payloadMapper: NewMapper)
Parameters
Parameters
NameTypeDescription
payloadMapper
NewMapper
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, NewMapper>

setPayloadMapperConfig()​

Set the configuration object passed as the second argument to the payload mapper.

Preview
setPayloadMapperConfig<NewPayloadMapperConfig>(config: NewPayloadMapperConfig)
Parameters
Parameters
NameTypeDescription
config
NewPayloadMapperConfig
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setQueryParamsMapper()​

Set the query params mapping function which get triggered before request get sent

Preview
setQueryParamsMapper<NewQueryParamsMapper>(queryParamsMapper: NewQueryParamsMapper)
Parameters
Parameters
NameTypeDescription
queryParamsMapper
NewQueryParamsMapper
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, NewQueryParamsMapper, HeaderMapperType, PayloadMapperType>

setQueryParamsMapperConfig()​

Set the configuration object passed as the second argument to the query params mapper.

Preview
setQueryParamsMapperConfig<NewQueryParamsMapperConfig>(config: NewQueryParamsMapperConfig)
Parameters
Parameters
NameTypeDescription
config
NewQueryParamsMapperConfig
Returns
Adapter<AdapterOptions, MethodType, StatusType, Extra, QueryParams, DefaultQueryParams, EndpointType, EndpointMapperType, QueryParamsMapperType, HeaderMapperType, PayloadMapperType>

setRequestDefaults()​

This method allows to configure global defaults for the request configuration like method, auth, deduplication etc.

Preview
setRequestDefaults(callback: undefined | (options: RequestOptionsType<EndpointType, AdapterOptions, MethodType>) => Partial<RequestOptionsType<EndpointType, AdapterOptions, MethodType>>)
Parameters
Parameters
NameTypeDescription
callback
undefined | (options: RequestOptionsType<EndpointType, AdapterOptions, MethodType>) => Partial<RequestOptionsType<EndpointType, AdapterOptions, MethodType>>
Returns
this