Skip to main content

HttpClient

HttpClient is an abstract base class that provides handler lifecycle management, request/response callbacks, and parallel request utilities. Platform SDKs extend it to add token attachment, retry logic, or custom transport implementations.

Usage

Extend HttpClient and implement the abstract transport() method:

import { HttpClient } from '@thunderid/javascript'
import type { HttpRequestConfig, HttpResponse } from '@thunderid/javascript'

class FetchHttpClient extends HttpClient {
async transport<T>(config: HttpRequestConfig): Promise<HttpResponse<T>> {
const response = await fetch(config.url, {
method: config.method,
headers: config.headers as HeadersInit,
body: config.data ? JSON.stringify(config.data) : undefined,
})

return {
data: await response.json(),
status: response.status,
statusText: response.statusText,
headers: Object.fromEntries(response.headers.entries()),
}
}
}

Abstract Methods

transport<T>(config)

Implement the actual HTTP transport. Called by request() after handler and callback checks.

ParameterTypeDescription
configHttpRequestConfigRequest configuration

Returns: Promise<HttpResponse<T>>

Methods

Request Execution

request<T>(config)

Execute a request through the handler pipeline. If the handler is disabled, the transport is still invoked but callbacks are suppressed.

const response = await client.request<{ id: string }>({
url: 'https://api.example.com/users/me',
method: 'GET',
})

Returns: Promise<HttpResponse<T>>

all<T>(values)

Execute multiple requests in parallel (analogous to Promise.all).

const [users, roles] = await client.all([
client.request({ url: '/users', method: 'GET' }),
client.request({ url: '/roles', method: 'GET' }),
])

Returns: Promise<T[]>

spread<T, R>(callback)

Spread an array result as individual arguments to a callback. Use with all() for typed destructuring.

const handler = client.spread((users, roles) => ({ users, roles }))
const result = await client.all([userReq, roleReq]).then(handler)

Returns: (array: T[]) => R

Handler Control

enableHandler()

Enable the request handler. Callbacks fire and the handler-enabled path executes.

client.enableHandler()

disableHandler()

Disable the request handler immediately.

client.disableHandler()

disableHandlerWithTimeout(timeout?)

Disable the handler for a fixed duration, then automatically re-enable it.

client.disableHandlerWithTimeout(5000) // disabled for 5 seconds
ParameterTypeDefaultDescription
timeoutnumber100Duration in milliseconds

Callbacks

Register callbacks to observe the request lifecycle. All callbacks are optional.

setHttpRequestStartCallback(callback)

Called before the request is dispatched.

client.setHttpRequestStartCallback((req) => {
console.log('Starting request to', req.url)
})

setHttpRequestSuccessCallback(callback)

Called when a request completes successfully.

client.setHttpRequestSuccessCallback((res) => {
console.log('Request succeeded with status', res.status)
})

setHttpRequestErrorCallback(callback)

Called when a request fails.

client.setHttpRequestErrorCallback((err) => {
console.error('Request failed:', err.message)
})

setHttpRequestFinishCallback(callback)

Called after every request, whether it succeeded or failed.

client.setHttpRequestFinishCallback(() => {
console.log('Request finished')
})

Types

HttpRequestConfig

PropertyTypeDescription
urlstringRequest URL
methodstringHTTP method (GET, POST, PUT, PATCH, DELETE)
headersRecord<string, string>Request headers
dataunknownRequest body
paramsRecord<string, string>URL query parameters
withCredentialsbooleanInclude credentials in cross-origin requests

HttpResponse<T>

PropertyTypeDescription
dataTParsed response body
statusnumberHTTP status code
statusTextstringHTTP status text
headersRecord<string, string>Response headers

HttpError

PropertyTypeDescription
messagestringError message
codestringError code
responseHttpResponseThe response that triggered the error
ThunderID LogoThunderID Logo

Product

DocsAPIsSDKs
© WSO2 LLC. All rights reserved.Privacy PolicyCookie Policy