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 { 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>;
}
| Name | Type | Description |
|---|---|---|
| options | |
Methods
addPlugin()
Add persistent plugins which trigger on the request lifecycle
Preview
addPlugin(plugin: PluginInstance)
Parameters
| Name | Type | Description |
|---|---|---|
| plugin | |
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
| Name | Type | Description |
|---|---|---|
| _USE_DOUBLE_INITIALIZATION | |
✅ Good: ⛔ Bad: 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
| Name | Type | Description |
|---|---|---|
| hydrationData | | |
| options | |
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
| Name | Type | Description |
|---|---|---|
| callback | |
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
| Name | Type | Description |
|---|---|---|
| callback | |
Returns
Client<GlobalErrorType, Adapter>
onRequest()
Method of manipulating requests before sending the request.
Preview
onRequest(callback: RequestInterceptorType)
Parameters
| Name | Type | Description |
|---|---|---|
| callback | |
Returns
Client<GlobalErrorType, Adapter>
onResponse()
Method for intercepting any responses.
Preview
onResponse<ErrorType>(callback: ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>)
Parameters
| Name | Type | Description |
|---|---|---|
| callback | |
Returns
Client<GlobalErrorType, Adapter>
onSuccess()
Method for intercepting success responses.
Preview
onSuccess<ErrorType>(callback: ResponseInterceptorType<ClientInstance, any, GlobalErrorType | ErrorType>)
Parameters
| Name | Type | Description |
|---|---|---|
| callback | |
Returns
Client<GlobalErrorType, Adapter>
removeOnAuthInterceptors()
Method for removing listeners on auth.
Preview
removeOnAuthInterceptors(callbacks: RequestInterceptorType[])
Parameters
| Name | Type | Description |
|---|---|---|
| callbacks | |
Returns
Client<GlobalErrorType, Adapter>
removeOnErrorInterceptors()
Method for removing listeners on error.
Preview
removeOnErrorInterceptors(callbacks: ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[])
Parameters
| Name | Type | Description |
|---|---|---|
| callbacks | |
Returns
Client<GlobalErrorType, Adapter>
removeOnRequestInterceptors()
Method for removing listeners on request.
Preview
removeOnRequestInterceptors(callbacks: RequestInterceptorType[])
Parameters
| Name | Type | Description |
|---|---|---|
| callbacks | |
Returns
Client<GlobalErrorType, Adapter>
removeOnResponseInterceptors()
Method for removing listeners on request.
Preview
removeOnResponseInterceptors(callbacks: ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[])
Parameters
| Name | Type | Description |
|---|---|---|
| callbacks | |
Returns
Client<GlobalErrorType, Adapter>
removeOnSuccessInterceptors()
Method for removing listeners on success.
Preview
removeOnSuccessInterceptors(callbacks: ResponseInterceptorType<ClientInstance, any, (null | GlobalErrorType)>[])
Parameters
| Name | Type | Description |
|---|---|---|
| callbacks | |
Returns
Client<GlobalErrorType, Adapter>
removePlugin()
Remove plugins from Client
Preview
removePlugin(plugin: PluginInstance)
Parameters
| Name | Type | Description |
|---|---|---|
| plugin | |
Returns
Client<GlobalErrorType, Adapter>
setAbortKeyMapper()
Key setters
Preview
setAbortKeyMapper(callback: (request: RequestInstance) => string)
Parameters
| Name | Type | Description |
|---|---|---|
| callback | |
Returns
Client<GlobalErrorType, Adapter>
setAdapter()
Set custom http adapter to handle graphql, rest, firebase or others
Preview
setAdapter<NewAdapter>(adapter: NewAdapter)
Parameters
| Name | Type | Description |
|---|---|---|
| adapter | |
Returns
Client<GlobalErrorType, NewAdapter>
setCacheKeyMapper()
Preview
setCacheKeyMapper(callback: (request: RequestInstance) => string)
Parameters
| Name | Type | Description |
|---|---|---|
| callback | |
Returns
Client<GlobalErrorType, Adapter>
setDebug()
This method enables the logger usage and display the logs in console
Preview
setDebug(enabled: boolean)
Parameters
| Name | Type | Description |
|---|---|---|
| enabled | |
Returns
Client<GlobalErrorType, Adapter>
setEnableGlobalMocking()
Set globally if mocking should be enabled or disabled for all client requests.
Preview
setEnableGlobalMocking(isMockerEnabled: boolean)
Parameters
| Name | Type | Description |
|---|---|---|
| isMockerEnabled | |
Returns
Client<GlobalErrorType, Adapter>
setLogLevel()
Set the logger severity of the messages displayed to the console
Preview
setLogLevel(severity: LogLevel)
Parameters
| Name | Type | Description |
|---|---|---|
| severity | |
Returns
Client<GlobalErrorType, Adapter>
setLogger()
Set the new logger instance to the Client
Preview
setLogger(callback: (Client: ClientInstance) => LoggerManager)
Parameters
| Name | Type | Description |
|---|---|---|
| callback | |
Returns
Client<GlobalErrorType, Adapter>
setQueryKeyMapper()
Preview
setQueryKeyMapper(callback: (request: RequestInstance) => string)
Parameters
| Name | Type | Description |
|---|---|---|
| callback | |
Returns
Client<GlobalErrorType, Adapter>
setRequestIdMapper()
Preview
setRequestIdMapper(callback: (request: RequestInstance) => string)
Parameters
| Name | Type | Description |
|---|---|---|
| callback | |
Returns
Client<GlobalErrorType, Adapter>
triggerPlugins()
Preview
triggerPlugins<Key>(key: Key, data: PluginMethodParameters<Key, Client<Error, HttpAdapterType>>)
Parameters
| Name | Type | Description |
|---|---|---|
| key | | |
| data | |
Returns
Client<GlobalErrorType, Adapter>