Skip to main content
Version: 5.x.x

Client


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

Description

Client is a class that allows you to configure the connection with the server and then use it to create requests which, when called using the appropriate method, will cause the server to be queried for the endpoint and method specified in the request.

Defined in client/client.ts:43

Parameters

NameDetails

options

Required

ClientOptionsType<Client<GlobalErrorType, Adapter, EndpointMapper>>

Properties

adapter

Description

Defined in client/client.ts:64

Type

Adapter

appManager

Description

Defined in client/client.ts:60

Type

AppManager

cache

Description

Defined in client/client.ts:65

Type

Cache<Client<GlobalErrorType, Adapter, EndpointMapper>>

debug

Description

Defined in client/client.ts:49

Type

boolean

defaultExtra

Description

Defined in client/client.ts:70

Type

ExtractAdapterExtraType<Adapter>

defaultMethod

Description

Defined in client/client.ts:69

Type

ExtractAdapterMethodType<Adapter>

effects

Description

Defined in client/client.ts:75

Type

RequestEffectInstance[]

endpointMapper

Description

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

Defined in client/client.ts:107

Type

EndpointMapper

fetchDispatcher

Description

Defined in client/client.ts:66

Type

Dispatcher

isMockEnabled

Description

Defined in client/client.ts:72

Type

boolean

logger

Description

Defined in client/client.ts:110

Type

LoggerType

loggerManager

Description

Defined in client/client.ts:61

Type

LoggerManager

options

Description

Defined in client/client.ts:112

Type

ClientOptionsType<Client<GlobalErrorType, Adapter, EndpointMapper>>

queryParamsConfig

Description

Defined in client/client.ts:78

Type

QueryStringifyOptionsType

requestManager

Description

Defined in client/client.ts:59

Type

RequestManager

submitDispatcher

Description

Defined in client/client.ts:67

Type

Dispatcher

url

Description

Defined in client/client.ts:48

Type

string

__onAuthCallbacks

Description

Defined in client/client.ts:55

Type

RequestInterceptorType[]

__onErrorCallbacks

Description

Defined in client/client.ts:52

Type

ResponseInterceptorType<any, any, AdapterType<Partial<XMLHttpRequest>, HttpMethodsType, number, AdapterExtraType, string | QueryParamsType, string>>[]

__onRequestCallbacks

Description

Defined in client/client.ts:56

Type

RequestInterceptorType[]

__onResponseCallbacks

Description

Defined in client/client.ts:54

Type

ResponseInterceptorType<any, any, AdapterType<Partial<XMLHttpRequest>, HttpMethodsType, number, AdapterExtraType, string | QueryParamsType, string>>[]

__onSuccessCallbacks

Description

Defined in client/client.ts:53

Type

ResponseInterceptorType<any, any, AdapterType<Partial<XMLHttpRequest>, HttpMethodsType, number, AdapterExtraType, string | QueryParamsType, string>>[]

Methods

abortKeyMapper()

Description

Defined in client/client.ts:83

Return

(request: RequestInstance) => string

adapterDefaultOptions()

Description

Defined in client/client.ts:79

Return

(request: RequestInstance) => ExtractAdapterOptionsType<Adapter>

cacheKeyMapper()

Description

Defined in client/client.ts:84

Return

(request: RequestInstance) => string

effectKeyMapper()

Description

Defined in client/client.ts:86

Return

(request: RequestInstance) => string

headerMapper()

Description

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

Defined in client/client.ts:98

Return

(request: T) => HeadersInit

payloadMapper()

Description

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

Defined in client/client.ts:102

Return

(data: unknown) => string | FormData

queueKeyMapper()

Description

Defined in client/client.ts:85

Return

(request: RequestInstance) => string

requestDefaultOptions()

Description

Defined in client/client.ts:80

Return

(options: RequestOptionsType<string, ExtractAdapterOptionsType<Adapter>, ExtractAdapterMethodType<Adapter>>) => Partial<RequestOptionsType<string, ExtractAdapterOptionsType<Adapter>, ExtractAdapterMethodType<Adapter>>>

stringifyQueryParams()

Description

Method to stringify query params from objects.

Defined in client/client.ts:93

Return

(queryParams: QueryParamsType | string | NegativeTypes) => string

addEffect()

Preview

addEffect(effect)

Description

Add persistent effects which trigger on the request lifecycle

Defined in client/client.ts:358

Parameters

NameDetails

effect

Required

RequestEffectInstance | RequestEffectInstance[]

Return

this

clear()

Preview

clear()

Description

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

Defined in client/client.ts:426

Return

void

createRequest()

Preview

createRequest<Response, Payload, LocalError, QueryParams>()

Description

Create requests based on the Client setup

Defined in client/client.ts:448

Return

(params: RequestOptionsType<EndpointType, AdapterOptions, MethodType>) => Request<Response, Payload, QueryParams, GlobalErrorType, LocalError, EndpointType extends string ? EndpointType : string, ExtractedAdapterType, false, false, false>

onAuth()

Preview

onAuth(callback)

Description

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.

Defined in client/client.ts:262

Parameters

NameDetails

callback

Required

RequestInterceptorType

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

onError()

Preview

onError<ErrorType>(callback)

Description

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

Defined in client/client.ts:280

Parameters

NameDetails

callback

Required

ResponseInterceptorType<any, GlobalErrorType | ErrorType, Adapter>

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

onRequest()

Preview

onRequest(callback)

Description

Method of manipulating requests before sending the request.

Defined in client/client.ts:320

Parameters

NameDetails

callback

Required

RequestInterceptorType

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

onResponse()

Preview

onResponse<ErrorType>(callback)

Description

Method for intercepting any responses.

Defined in client/client.ts:338

Parameters

NameDetails

callback

Required

ResponseInterceptorType<any, GlobalErrorType | ErrorType, Adapter>

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

onSuccess()

Preview

onSuccess<ErrorType>(callback)

Description

Method for intercepting success responses.

Defined in client/client.ts:300

Parameters

NameDetails

callback

Required

ResponseInterceptorType<any, GlobalErrorType | ErrorType, Adapter>

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

removeEffect()

Preview

removeEffect(effect)

Description

Remove effects from Client

Defined in client/client.ts:367

Parameters

NameDetails

effect

Required

string | RequestEffectInstance

Return

this

removeOnAuthInterceptors()

Preview

removeOnAuthInterceptors(callbacks)

Description

Method for removing listeners on auth.

Defined in client/client.ts:270

Parameters

NameDetails

callbacks

Required

RequestInterceptorType[]

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

removeOnErrorInterceptors()

Preview

removeOnErrorInterceptors(callbacks)

Description

Method for removing listeners on error.

Defined in client/client.ts:290

Parameters

NameDetails

callbacks

Required

ResponseInterceptorType<any, GlobalErrorType, Adapter>[]

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

removeOnRequestInterceptors()

Preview

removeOnRequestInterceptors(callbacks)

Description

Method for removing listeners on request.

Defined in client/client.ts:328

Parameters

NameDetails

callbacks

Required

RequestInterceptorType[]

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

removeOnResponseInterceptors()

Preview

removeOnResponseInterceptors(callbacks)

Description

Method for removing listeners on request.

Defined in client/client.ts:348

Parameters

NameDetails

callbacks

Required

ResponseInterceptorType<any, GlobalErrorType, Adapter>[]

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

removeOnSuccessInterceptors()

Preview

removeOnSuccessInterceptors(callbacks)

Description

Method for removing listeners on success.

Defined in client/client.ts:310

Parameters

NameDetails

callbacks

Required

ResponseInterceptorType<any, GlobalErrorType, Adapter>[]

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

setAbortKeyMapper()

Preview

setAbortKeyMapper(callback)

Description

Key setters

Defined in client/client.ts:378

Parameters

NameDetails

callback

Required

(request: RequestInstance) => string

Return

void

setAdapter()

Preview

setAdapter<NewAdapter, Returns>(callback)

Description

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

Defined in client/client.ts:224

Parameters

NameDetails

callback

Required

(client: this) => Returns extends AdapterInstance ? NewAdapter : Client<GlobalErrorType, NewAdapter, EndpointMapper>

Return

Client<GlobalErrorType, Returns extends AdapterInstance ? NewAdapter : ExtractAdapterType<NewAdapter>, EndpointMapper>

setAdapterDefaultOptions()

Preview

setAdapterDefaultOptions(callback)

Description

Defined in client/client.ts:136

Parameters

NameDetails

callback

Required

(request: RequestInstance) => ExtractAdapterOptionsType<Adapter>

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

setCacheKeyMapper()

Preview

setCacheKeyMapper(callback)

Description

Defined in client/client.ts:381

Parameters

NameDetails

callback

Required

(request: RequestInstance) => string

Return

void

setDebug()

Preview

setDebug(debug)

Description

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

Defined in client/client.ts:146

Parameters

NameDetails

debug

Required

boolean

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

setDefaultExtra()

Preview

setDefaultExtra(defaultExtra)

Description

Set default additional data for initial state.

Defined in client/client.ts:254

Parameters

NameDetails

defaultExtra

Required

ExtractAdapterExtraType<Adapter>

Return

Client<any, AdapterType<any, any, any, any>>

setDefaultMethod()

Preview

setDefaultMethod(defaultMethod)

Description

Set default method for requests.

Defined in client/client.ts:246

Parameters

NameDetails

defaultMethod

Required

ExtractAdapterMethodType<Adapter>

Return

Client<any, AdapterType<any, any, any, any>>

setEffectKeyMapper()

Preview

setEffectKeyMapper(callback)

Description

Defined in client/client.ts:387

Parameters

NameDetails

callback

Required

(request: RequestInstance) => string

Return

void

setEnableGlobalMocking()

Preview

setEnableGlobalMocking(isMockEnabled)

Description

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

Defined in client/client.ts:208

Parameters

NameDetails

isMockEnabled

Required

boolean

Return

this

setEndpointMapper()

Preview

setEndpointMapper<NewEndpointMapper>(endpointMapper)

Description

Set the request payload mapping function which get triggered before request get send

Defined in client/client.ts:216

Parameters

NameDetails

endpointMapper

Required

NewEndpointMapper

Return

Client<GlobalErrorType, Adapter, NewEndpointMapper>

setHeaderMapper()

Preview

setHeaderMapper(headerMapper)

Description

Set the custom header mapping function

Defined in client/client.ts:191

Parameters

NameDetails

headerMapper

Required

HeaderMappingType

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

setLogger()

Preview

setLogger(callback)

Description

Set the new logger instance to the Client

Defined in client/client.ts:162

Parameters

NameDetails

callback

Required

(Client: ClientInstance) => LoggerManager

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

setLoggerSeverity()

Preview

setLoggerSeverity(severity)

Description

Set the logger severity of the messages displayed to the console

Defined in client/client.ts:154

Parameters

NameDetails

severity

Required

SeverityType

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

setPayloadMapper()

Preview

setPayloadMapper(payloadMapper)

Description

Set the request payload mapping function which get triggered before request get send

Defined in client/client.ts:199

Parameters

NameDetails

payloadMapper

Required

AdapterPayloadMappingType

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

setQueryParamsConfig()

Preview

setQueryParamsConfig(queryParamsConfig)

Description

Set config for the query params stringify method, we can set here, among others, arrayFormat, skipNull, encode, skipEmptyString and more

Defined in client/client.ts:172

Parameters

NameDetails

queryParamsConfig

Required

QueryStringifyOptionsType

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

setQueueKeyMapper()

Preview

setQueueKeyMapper(callback)

Description

Defined in client/client.ts:384

Parameters

NameDetails

callback

Required

(request: RequestInstance) => string

Return

void

setRequestDefaultOptions()

Preview

setRequestDefaultOptions(callback)

Description

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

Defined in client/client.ts:127

Parameters

NameDetails

callback

Required

(request: RequestInstance) => Partial<RequestOptionsType<string, ExtractAdapterOptionsType<Adapter>, ExtractAdapterMethodType<Adapter>>>

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

setStringifyQueryParams()

Preview

setStringifyQueryParams(stringifyFn)

Description

Set the custom query params stringify method to the Client

Defined in client/client.ts:183

Parameters

NameDetails

stringifyFn

Required

Custom callback handling query params stringify

StringifyCallbackType

Return

Client<GlobalErrorType, Adapter, EndpointMapper>

__modifyAuth()

Preview

__modifyAuth(request)

Description

Helper used by http adapter to apply the modifications on response error

Defined in client/client.ts:394

Parameters

NameDetails

request

Required

RequestInstance

Return

Promise<RequestInstance>

__modifyErrorResponse()

Preview

__modifyErrorResponse(response, request)

Description

Private helper to run async on-error response processing

Defined in client/client.ts:404

Parameters

NameDetails

response

Required

ResponseReturnType<any, GlobalErrorType, Adapter>

request

Required

RequestInstance

Return

Promise<ResponseReturnType<any, GlobalErrorType, Adapter>>

__modifyRequest()

Preview

__modifyRequest(request)

Description

Private helper to run async pre-request processing

Defined in client/client.ts:399

Parameters

NameDetails

request

Required

RequestInstance

Return

Promise<RequestInstance>

__modifyResponse()

Preview

__modifyResponse(response, request)

Description

Private helper to run async response processing

Defined in client/client.ts:420

Parameters

NameDetails

response

Required

ResponseReturnType<any, GlobalErrorType, Adapter>

request

Required

RequestInstance

Return

Promise<ResponseReturnType<any, GlobalErrorType, Adapter>>

__modifySuccessResponse()

Preview

__modifySuccessResponse(response, request)

Description

Private helper to run async on-success response processing

Defined in client/client.ts:412

Parameters

NameDetails

response

Required

ResponseReturnType<any, GlobalErrorType, Adapter>

request

Required

RequestInstance

Return

Promise<ResponseReturnType<any, GlobalErrorType, Adapter>>