Skip to main content

Builder

Class


Preview

new Builder<GlobalErrorType, RequestConfigType>(options)

Description

Builder is a class that allows you to configure the connection with the server and then use it to create

commands which, when called using the appropriate method, will cause the server to be queried for the endpoint and

method specified in the command.

Import

import { Builder } from "@better-typed/hyper-fetch"

Parameters

Name Type Default Value Description
options BuilderConfig - -

Methods

setStringifyQueryParams()

Set the custom query params stringify method to the builder

setStringifyQueryParams(stringifyFn)

Parameters

Name Type Default Value Description
stringifyFn StringifyCallbackType - Custom callback handling query params stringify

setQueryParamsConfig()

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

setQueryParamsConfig(queryParamsConfig)

Parameters

Name Type Default Value Description
queryParamsConfig QueryStringifyOptions - -

setPayloadMapper()

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

setPayloadMapper(payloadMapper)

Parameters

Name Type Default Value Description
payloadMapper ClientPayloadMappingCallback - -

setLoggerSeverity()

Set the logger severity of the messages displayed to the console

setLoggerSeverity(severity)

Parameters

Name Type Default Value Description
severity SeverityType - -

setLogger()

Set the new logger instance to the builder

setLogger(callback)

Parameters

Name Type Default Value Description
callback (builder: BuilderInstance) => LoggerManager - -

setHeaderMapper()

Set the custom header mapping function

setHeaderMapper(headerMapper)

Parameters

Name Type Default Value Description
headerMapper ClientHeaderMappingCallback - -

setDebug()

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

setDebug(debug)

Parameters

Name Type Default Value Description
debug boolean - -

setCommandConfig()

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

setCommandConfig(callback)

Parameters

Name Type Default Value Description
callback (command: CommandInstance) => Partial<CommandConfig<string, RequestConfigType>> - -

setClient()

Set custom http client to handle graphql, rest, firebase or other

setClient(callback)

Parameters

Name Type Default Value Description
callback (builder: BuilderInstance) => ClientType - -

removeEffect()

Remove effects from builder

removeEffect(effect)
Returns a this

Parameters

Name Type Default Value Description
effect string | FetchEffectInstance - -

onSuccess()

Method for intercepting success responses.

onSuccess<ErrorType>(callback)

Parameters

Name Type Default Value Description
callback ResponseInterceptorCallback<any, GlobalErrorType | ErrorType> - -

onResponse()

Method for intercepting any responses.

onResponse<ErrorType>(callback)

Parameters

Name Type Default Value Description
callback ResponseInterceptorCallback<any, GlobalErrorType | ErrorType> - -

onRequest()

Method of manipulating commands before sending the request.

onRequest(callback)

Parameters

Name Type Default Value Description
callback RequestInterceptorCallback - -

onError()

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

onError<ErrorType>(callback)

Parameters

Name Type Default Value Description
callback ResponseInterceptorCallback<any, GlobalErrorType | ErrorType> - -

onAuth()

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

onAuth(callback)

Parameters

Name Type Default Value Description
callback RequestInterceptorCallback - -

createCommand()

Create commands based on the builder setup

createCommand<ResponseType, RequestDataType, LocalErrorType, QueryParamsType>()

clear()

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

clear()
Returns a void

addEffect()

Add persistent effects which trigger on the request lifecycle

addEffect(effect)
Returns a this

Parameters

Name Type Default Value Description
effect FetchEffectInstance | FetchEffectInstance[] - -

__modifyAuth()

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

__modifyAuth(command)
Returns a Promise<CommandInstance>

Parameters

Name Type Default Value Description
command CommandInstance - -

__modifyErrorResponse()

Private helper to run async on-error response processing

__modifyErrorResponse(response, command)

Parameters

Name Type Default Value Description
response ClientResponseType<any, GlobalErrorType> - -
command CommandInstance - -

__modifyRequest()

Private helper to run async pre-request processing

__modifyRequest(command)
Returns a Promise<CommandInstance>

Parameters

Name Type Default Value Description
command CommandInstance - -

__modifyResponse()

Private helper to run async response processing

__modifyResponse(response, command)

Parameters

Name Type Default Value Description
response ClientResponseType<any, GlobalErrorType> - -
command CommandInstance - -

__modifySuccessResponse()

Private helper to run async on-success response processing

__modifySuccessResponse(response, command)

Parameters

Name Type Default Value Description
response ClientResponseType<any, GlobalErrorType> - -
command CommandInstance - -

References