Skip to content

HttpClientMixin

HttpClientMixin is a mixin function for building light-weight HTTP clients.

Classes

HttpError

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:86

Represents an error that occurred during an HTTP request triggered by the client.

Extends

  • Error

Constructors

new HttpError()

new HttpError(init): HttpError

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:96

Parameters
init

HttpError

Returns

HttpError

Overrides

Error.constructor

Properties

body?

optional body: string | object

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:94

The response body that came with the error.

cause?

optional cause: unknown

Defined in: node_modules/.pnpm/typescript@5.8.2/node_modules/typescript/lib/lib.es2022.error.d.ts:26

Inherited from

Error.cause

message

message: string

Defined in: node_modules/.pnpm/typescript@5.8.2/node_modules/typescript/lib/lib.es5.d.ts:1077

Inherited from

Error.message

name

name: string

Defined in: node_modules/.pnpm/typescript@5.8.2/node_modules/typescript/lib/lib.es5.d.ts:1076

Inherited from

Error.name

stack?

optional stack: string

Defined in: node_modules/.pnpm/typescript@5.8.2/node_modules/typescript/lib/lib.es5.d.ts:1078

Inherited from

Error.stack

status?

optional status: number

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:90

The status code of the response that caused the error.

prepareStackTrace()?

static optional prepareStackTrace: (err, stackTraces) => any

Defined in: node_modules/.pnpm/@types+node@22.13.8/node_modules/@types/node/globals.d.ts:143

Optional override for formatting stack traces

Parameters
err

Error

stackTraces

CallSite[]

Returns

any

See

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from

Error.prepareStackTrace

stackTraceLimit

static stackTraceLimit: number

Defined in: node_modules/.pnpm/@types+node@22.13.8/node_modules/@types/node/globals.d.ts:145

Inherited from

Error.stackTraceLimit

Methods

captureStackTrace()

static captureStackTrace(targetObject, constructorOpt?): void

Defined in: node_modules/.pnpm/@types+node@22.13.8/node_modules/@types/node/globals.d.ts:136

Create .stack property on a target object

Parameters
targetObject

object

constructorOpt?

Function

Returns

void

Inherited from

Error.captureStackTrace

Interfaces

EndpointDefinition<TFunction, TTransformedResult>

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:39

The definition of an endpoint to be used by the client.

Type Parameters

TFunction extends EndpointFunction = EndpointFunction

The type of the endpoint function.

TTransformedResult = any

The type of the transformed result.

Properties

errorHandlers?

optional errorHandlers: ErrorHandler[]

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:59

The set of error handlers for the endpoint. These local error handlers will be called before the global error handlers.

function

function: TFunction

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:46

The function that will be called when the endpoint is invoked.

transformer()?

optional transformer: (response) => TTransformedResult

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:54

A function that will be called to transform the response from the server. The transformer will be called implicitly after the original promise has resolved.

Parameters
response

Awaited<ReturnType<TFunction>>

The responsese from the server.

Returns

TTransformedResult

The transformed result.


HttpClientOptions<TEndpoints>

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:70

The options to hand over to the client.

Type Parameters

TEndpoints extends Endpoints

The set of registered Endpoints.

Properties

endpoints

endpoints: TEndpoints

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:74

The set of Endpoints to be used by the client.

globalErrorHandlers?

optional globalErrorHandlers: ErrorHandler[]

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:80

A set of global ErrorHandlers. These will be called for every error that occurs, regardless of the endpoint. These global error handlers will be called after any local error handlers.

Type Aliases

EndpointFunction()

EndpointFunction: (…args) => Promise<any>

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:32

An endpoint function that will be called when the endpoint is invoked.

Parameters

args

any[]

The arguments to be passed to the endpoint function.

Returns

Promise<any>

A promise of the response from the server.


Endpoints

Endpoints: Record<string, EndpointDefinition>

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:64

The set of endpoints to be used by the client.


ErrorHandler

ErrorHandler: object

Defined in: packages/js-utils/src/services/http-client-mixin/types.ts:6

Defines how the client should handle errors.

Type declaration

continue?

optional continue: boolean

Whether the client should continue to call subsequent error handlers after this one.

Default
false;
function()

function: (error) => void | Promise<void>

The function to be called when an error occurs.

Parameters
error

The error.

HttpError | unknown

Returns

void | Promise<void>

A promise that resolves when the error has been handled.

statusFilter()?

optional statusFilter: (status) => boolean

The status code of the response that will trigger this error handler. If left empty, the error handler will be called for all errors.

Parameters
status

number

The status code.

Returns

boolean

Whether this status code should

Functions

defineEndpoint()

defineEndpoint<TFunction, TTransformedResult>(definition): EndpointDefinition<TFunction, TTransformedResult>

Defined in: packages/js-utils/src/services/http-client-mixin/http-client.utils.ts:25

A helper function to create an endpoint definition in a type-safe way.

Type Parameters

TFunction extends EndpointFunction

TTransformedResult = Awaited<ReturnType<TFunction>>

Parameters

definition

EndpointDefinition<TFunction, TTransformedResult>

The endpoint definition.

Returns

EndpointDefinition<TFunction, TTransformedResult>

The endpoint definition.


HttpClientMixin()

HttpClientMixin<TEndpoints>(options): HttpClient<TEndpoints>

Defined in: packages/js-utils/src/services/http-client-mixin/http-client.service-mixin.ts:49

A mixin that adds HTTP client functionality to a class. Can be used to create a strongly typed HTTP client.

Type Parameters

TEndpoints extends Endpoints

The set of Endpoints to register with the HTTP client.

Parameters

options

HttpClientOptions<TEndpoints>

The options for the HTTP client.

Returns

HttpClient<TEndpoints>

Example

import { HttpClientMixin, defineEndpoint } from '@spuxx/js-utils';
// Define your endpoints
const endpoints = {
getRandomJoke: defineEndpoint({
function: async () => {
const response = await fetch('https://api.chucknorris.io/jokes/random');
},
transformer: async (response: Response}): string => {
const json = await response.json();
return json.value;
},
}),
}
// Create your client
class HttpClient extends HttpClientMixin({ endpoints }) {}
// Use your client
const joke = await HttpClient.getRandomJoke();
console.log(joke); // Chuck Norris can divide by zero.

transformFetchJson()

transformFetchJson<TResult>(response): Promise<TResult>

Defined in: packages/js-utils/src/services/http-client-mixin/http-client.utils.ts:15

A very simple transformer that will transform the response from a fetch call into a JSON object and cast it to the specified type. Intended for use with defineEndpoint.

Type Parameters

TResult

Parameters

response

Response

The response from a fetch call.

Returns

Promise<TResult>

The transformed JSON object.

Example

const endpoint = defineEndpoint({
function: async (): Response => {
return fetch('https://example.com/api/users');
},
transform: transformFetchJson<User[]>,