Skip to main content

@rtorcato/api-http v1.0.0

Interfaces

HttpClientOptions

Defined in: index.ts:7

Properties

baseURL?

optional baseURL?: string

Defined in: index.ts:9

Prepended to every request path.

headers?

optional headers?: Record<string, string>

Defined in: index.ts:11

Sent on every request (per-request headers merge on top).

timeoutMs?

optional timeoutMs?: number

Defined in: index.ts:13

Per-request timeout in ms. Default: 30_000.

retries?

optional retries?: number

Defined in: index.ts:15

Retry attempts on network error or 5xx. Default: 0 (no retry).

retryDelayMs?

optional retryDelayMs?: number

Defined in: index.ts:17

Fixed delay between retries in ms. Default: 200.


RequestOptions

Defined in: index.ts:20

Properties

headers?

optional headers?: Record<string, string>

Defined in: index.ts:22

Merged on top of the client's default headers.

query?

optional query?: Record<string, string | number | boolean | undefined>

Defined in: index.ts:24

Appended as a query string; undefined values are skipped.

timeoutMs?

optional timeoutMs?: number

Defined in: index.ts:26

Overrides the client timeout for this request.

signal?

optional signal?: AbortSignal

Defined in: index.ts:28

Caller abort signal — combined with the timeout signal.


HttpClient

Defined in: index.ts:31

Methods

get()

get<T>(path, options?): Promise<T>

Defined in: index.ts:32

Type Parameters
T

T = unknown

Parameters
path

string

options?

RequestOptions

Returns

Promise<T>

delete()

delete<T>(path, options?): Promise<T>

Defined in: index.ts:33

Type Parameters
T

T = unknown

Parameters
path

string

options?

RequestOptions

Returns

Promise<T>

post()

post<T>(path, body?, options?): Promise<T>

Defined in: index.ts:34

Type Parameters
T

T = unknown

Parameters
path

string

body?

unknown

options?

RequestOptions

Returns

Promise<T>

put()

put<T>(path, body?, options?): Promise<T>

Defined in: index.ts:35

Type Parameters
T

T = unknown

Parameters
path

string

body?

unknown

options?

RequestOptions

Returns

Promise<T>

patch()

patch<T>(path, body?, options?): Promise<T>

Defined in: index.ts:36

Type Parameters
T

T = unknown

Parameters
path

string

body?

unknown

options?

RequestOptions

Returns

Promise<T>

Functions

createHttpClient()

createHttpClient(clientOptions?): HttpClient

Defined in: index.ts:147

Create a typed HTTP client bound to a base URL and default headers.

Parameters

clientOptions?

HttpClientOptions = {}

Returns

HttpClient

Example

const api = createHttpClient({ baseURL: 'https://api.example.com', headers: { authorization: `Bearer ${token}` } })
const user = await api.get<{ id: string }>('/users/me')
await api.post('/users', { name: 'Ada' })

Non-2xx responses throw an HttpError (from @rtorcato/api-errors) carrying the status and a message pulled from the response body, so it slots straight into the error-handler middleware. Network failures throw HttpError with status 0 and code network_error.