Skip to main content
Version: v7.0.0

Client

Client is a class that allows you to configure the connection with the server and then use it to create requests. It allows you to set global defaults for the requests configuration, query params configuration. It is also orchestrator for all of the HyperFetch modules like Cache, Dispatcher, AppManager, LoggerManager, RequestManager and more.


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

Parameters

class Client<GlobalErrorType, Adapter> {
constructor(options: ClientOptionsType<Client<GlobalErrorType, Adapter>>) {};
url: string;
unstable_onSuccessCallbacks: ResponseInterceptorType<ClientInstance>[];
unstable_onResponseCallbacks: ResponseInterceptorType<ClientInstance>[];
unstable_onRequestCallbacks: RequestInterceptorType[];
unstable_onErrorCallbacks: ResponseInterceptorType<ClientInstance>[];
unstable_onAuthCallbacks: RequestInterceptorType[];
submitDispatcher: Dispatcher<Adapter>;
requestManager: RequestManager;
plugins: PluginInstance[];
options: ClientOptionsType<Client<GlobalErrorType, Adapter>>;
loggerManager: LoggerManager;
logger: LoggerMethods;
isMockerEnabled: boolean;
fetchDispatcher: Dispatcher<Adapter>;
debug: boolean;
cache: Cache<Adapter>;
appManager: AppManager;
adapter: Adapter;
addPlugin(plugin: PluginInstance) => Client<GlobalErrorType, Adapter>;
clear() => void;
createRequest<RequestProperties>(_USE_DOUBLE_INITIALIZATION?: undefined) => <EndpointType, AdapterOptions, MethodType>(params: RequestOptionsType<EndpointType, AdapterOptions, MethodType>) => Request<TypeWithDefaults<RequestProperties, response, undefined, void>, TypeWithDefaults<RequestProperties, payload, undefined, void>, TypeWithDefaults<RequestProperties, queryParams, ExtractAdapterDefaultQueryParamsType<Adapter>, never>, TypeWithDefaults<RequestProperties, error, GlobalErrorType, void>, TypeWithDefaults<RequestProperties, endpoint, EndpointType, void> extends string ? string & TypeWithDefaults<RequestProperties, endpoint, EndpointType, void> : any, Client<GlobalErrorType, ExtractedAdapterType>, false, false, false>;
hydrate(hydrationData: (EmptyTypes | HydrateDataType)[], options?: Partial<HydrationOptions> | (item: HydrateDataType) => Partial<HydrationOptions>) => void;
onAuth(callback: RequestInterceptorType) => Client<GlobalErrorType, Adapter>;
onError<ErrorType>(callback: ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>) => Client<GlobalErrorType, Adapter>;
onRequest(callback: RequestInterceptorType) => Client<GlobalErrorType, Adapter>;
onResponse<ErrorType>(callback: ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>) => Client<GlobalErrorType, Adapter>;
onSuccess<ErrorType>(callback: ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>) => Client<GlobalErrorType, Adapter>;
removeOnAuthInterceptors(callbacks: RequestInterceptorType[]) => Client<GlobalErrorType, Adapter>;
removeOnErrorInterceptors(callbacks: ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[]) => Client<GlobalErrorType, Adapter>;
removeOnRequestInterceptors(callbacks: RequestInterceptorType[]) => Client<GlobalErrorType, Adapter>;
removeOnResponseInterceptors(callbacks: ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[]) => Client<GlobalErrorType, Adapter>;
removeOnSuccessInterceptors(callbacks: ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[]) => Client<GlobalErrorType, Adapter>;
removePlugin(plugin: PluginInstance) => Client<GlobalErrorType, Adapter>;
setAbortKeyMapper(callback: (request: RequestInstance) => string) => Client<GlobalErrorType, Adapter>;
setAdapter<NewAdapter>(adapter: NewAdapter) => Client<GlobalErrorType, NewAdapter>;
setCacheKeyMapper(callback: (request: RequestInstance) => string) => Client<GlobalErrorType, Adapter>;
setDebug(enabled: boolean) => Client<GlobalErrorType, Adapter>;
setEnableGlobalMocking(isMockerEnabled: boolean) => Client<GlobalErrorType, Adapter>;
setLogLevel(severity: LogLevel) => Client<GlobalErrorType, Adapter>;
setLogger(callback: (Client: ClientInstance) => LoggerManager) => Client<GlobalErrorType, Adapter>;
setQueryKeyMapper(callback: (request: RequestInstance) => string) => Client<GlobalErrorType, Adapter>;
setRequestIdMapper(callback: (request: RequestInstance) => string) => Client<GlobalErrorType, Adapter>;
triggerPlugins<Key>(key: Key, data: PluginMethodParameters<Key, Client<Error, HttpAdapterType>>) => Client<GlobalErrorType, Adapter>;
}
Parameters
NameTypeDescription
options
ClientOptionsType<Client<GlobalErrorType, Adapter>>

Properties

adapter

Type
Adapter

appManager

Type
AppManager

cache

Type
Cache<Adapter>

debug

Type
boolean

fetchDispatcher

Type
Dispatcher<Adapter>

isMockerEnabled

Type
boolean

logger

Type
LoggerMethods

loggerManager

Type
LoggerManager

options

Type
ClientOptionsType<Client<GlobalErrorType, Adapter>>

plugins

Type
PluginInstance[]

requestManager

Type
RequestManager

submitDispatcher

Type
Dispatcher<Adapter>

unstable_onAuthCallbacks

Type
RequestInterceptorType[]

unstable_onErrorCallbacks

Type
ResponseInterceptorType<ClientInstance>[]

unstable_onRequestCallbacks

Type
RequestInterceptorType[]

unstable_onResponseCallbacks

Type
ResponseInterceptorType<ClientInstance>[]

unstable_onSuccessCallbacks

Type
ResponseInterceptorType<ClientInstance>[]

url

Type
string

Methods

addPlugin()

Add persistent plugins which trigger on the request lifecycle

Preview
addPlugin(plugin: PluginInstance)
Parameters
Parameters
NameTypeDescription
plugin
PluginInstance
Returns
Client<GlobalErrorType, Adapter>

clear()

Clears the Client instance and remove all listeners on it's dependencies

Preview
clear()
Returns
void

createRequest()

Create requests based on the Client setup

Preview
createRequest<RequestProperties>(_USE_DOUBLE_INITIALIZATION: undefined)
Parameters
Parameters
NameTypeDescription
_USE_DOUBLE_INITIALIZATION
undefined

createRequest must be initialized twice(currying).

✅ Good:

const request = createRequest&lt;RequestProperties&gt;()(params)

⛔ Bad:

const request = createRequest&lt;RequestProperties&gt;(params)

We are using currying to achieve auto generated types for the endpoint string.

This solution will be removed once https://github.com/microsoft/TypeScript/issues/10571 get resolved.

Returns
<EndpointType, AdapterOptions, MethodType>(params: RequestOptionsType<EndpointType, AdapterOptions, MethodType>) => Request<TypeWithDefaults<RequestProperties, response, undefined, void>, TypeWithDefaults<RequestProperties, payload, undefined, void>, TypeWithDefaults<RequestProperties, queryParams, ExtractAdapterDefaultQueryParamsType<Adapter>, never>, TypeWithDefaults<RequestProperties, error, GlobalErrorType, void>, TypeWithDefaults<RequestProperties, endpoint, EndpointType, void> extends string ? string & TypeWithDefaults<RequestProperties, endpoint, EndpointType, void> : any, Client<GlobalErrorType, ExtractedAdapterType>, false, false, false>

hydrate()

Hydrate your SSR cache data

Preview
hydrate(hydrationData: (EmptyTypes | HydrateDataType)[], options: Partial<HydrationOptions> | (item: HydrateDataType) => Partial<HydrationOptions>)
Parameters
Parameters
NameTypeDescription
hydrationData
(EmptyTypes | HydrateDataType)[]
options
Partial<HydrationOptions> | (item: HydrateDataType) => Partial<HydrationOptions>
Returns
void

onAuth()

Method of manipulating requests before sending the request. We can for example add custom header with token to the request which request had the auth set to true.

Preview
onAuth(callback: RequestInterceptorType)
Parameters
Parameters
NameTypeDescription
callback
RequestInterceptorType
Returns
Client<GlobalErrorType, Adapter>

onError()

Method for intercepting error responses. It can be used for example to refresh tokens.

Preview
onError<ErrorType>(callback: ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>)
Parameters
Parameters
NameTypeDescription
callback
ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>
Returns
Client<GlobalErrorType, Adapter>

onRequest()

Method of manipulating requests before sending the request.

Preview
onRequest(callback: RequestInterceptorType)
Parameters
Parameters
NameTypeDescription
callback
RequestInterceptorType
Returns
Client<GlobalErrorType, Adapter>

onResponse()

Method for intercepting any responses.

Preview
onResponse<ErrorType>(callback: ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>)
Parameters
Parameters
NameTypeDescription
callback
ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>
Returns
Client<GlobalErrorType, Adapter>

onSuccess()

Method for intercepting success responses.

Preview
onSuccess<ErrorType>(callback: ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>)
Parameters
Parameters
NameTypeDescription
callback
ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>
Returns
Client<GlobalErrorType, Adapter>

removeOnAuthInterceptors()

Method for removing listeners on auth.

Preview
removeOnAuthInterceptors(callbacks: RequestInterceptorType[])
Parameters
Parameters
NameTypeDescription
callbacks
RequestInterceptorType[]
Returns
Client<GlobalErrorType, Adapter>

removeOnErrorInterceptors()

Method for removing listeners on error.

Preview
removeOnErrorInterceptors(callbacks: ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[])
Parameters
Parameters
NameTypeDescription
callbacks
ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[]
Returns
Client<GlobalErrorType, Adapter>

removeOnRequestInterceptors()

Method for removing listeners on request.

Preview
removeOnRequestInterceptors(callbacks: RequestInterceptorType[])
Parameters
Parameters
NameTypeDescription
callbacks
RequestInterceptorType[]
Returns
Client<GlobalErrorType, Adapter>

removeOnResponseInterceptors()

Method for removing listeners on request.

Preview
removeOnResponseInterceptors(callbacks: ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[])
Parameters
Parameters
NameTypeDescription
callbacks
ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[]
Returns
Client<GlobalErrorType, Adapter>

removeOnSuccessInterceptors()

Method for removing listeners on success.

Preview
removeOnSuccessInterceptors(callbacks: ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[])
Parameters
Parameters
NameTypeDescription
callbacks
ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[]
Returns
Client<GlobalErrorType, Adapter>

removePlugin()

Remove plugins from Client

Preview
removePlugin(plugin: PluginInstance)
Parameters
Parameters
NameTypeDescription
plugin
PluginInstance
Returns
Client<GlobalErrorType, Adapter>

setAbortKeyMapper()

Key setters

Preview
setAbortKeyMapper(callback: (request: RequestInstance) => string)
Parameters
Parameters
NameTypeDescription
callback
(request: RequestInstance) => string
Returns
Client<GlobalErrorType, Adapter>

setAdapter()

Set custom http adapter to handle graphql, rest, firebase or others

Preview
setAdapter<NewAdapter>(adapter: NewAdapter)
Parameters
Parameters
NameTypeDescription
adapter
NewAdapter
Returns
Client<GlobalErrorType, NewAdapter>

setCacheKeyMapper()

Preview
setCacheKeyMapper(callback: (request: RequestInstance) => string)
Parameters
Parameters
NameTypeDescription
callback
(request: RequestInstance) => string
Returns
Client<GlobalErrorType, Adapter>

setDebug()

This method enables the logger usage and display the logs in console

Preview
setDebug(enabled: boolean)
Parameters
Parameters
NameTypeDescription
enabled
boolean
Returns
Client<GlobalErrorType, Adapter>

setEnableGlobalMocking()

Set globally if mocking should be enabled or disabled for all client requests.

Preview
setEnableGlobalMocking(isMockerEnabled: boolean)
Parameters
Parameters
NameTypeDescription
isMockerEnabled
boolean
Returns
Client<GlobalErrorType, Adapter>

setLogLevel()

Set the logger severity of the messages displayed to the console

Preview
setLogLevel(severity: LogLevel)
Parameters
Parameters
NameTypeDescription
severity
LogLevel
Returns
Client<GlobalErrorType, Adapter>

setLogger()

Set the new logger instance to the Client

Preview
setLogger(callback: (Client: ClientInstance) => LoggerManager)
Parameters
Parameters
NameTypeDescription
callback
(Client: ClientInstance) => LoggerManager
Returns
Client<GlobalErrorType, Adapter>

setQueryKeyMapper()

Preview
setQueryKeyMapper(callback: (request: RequestInstance) => string)
Parameters
Parameters
NameTypeDescription
callback
(request: RequestInstance) => string
Returns
Client<GlobalErrorType, Adapter>

setRequestIdMapper()

Preview
setRequestIdMapper(callback: (request: RequestInstance) => string)
Parameters
Parameters
NameTypeDescription
callback
(request: RequestInstance) => string
Returns
Client<GlobalErrorType, Adapter>

triggerPlugins()

Preview
triggerPlugins<Key>(key: Key, data: PluginMethodParameters<Key, Client<Error, HttpAdapterType>>)
Parameters
Parameters
NameTypeDescription
key
Key
data
PluginMethodParameters<Key, Client<Error, HttpAdapterType>>
Returns
Client<GlobalErrorType, Adapter>