@rtorcato/api-http v1.0.0
Interfaces
HttpClientOptions
Defined in: index.ts:7
Properties
baseURL?
optionalbaseURL?:string
Defined in: index.ts:9
Prepended to every request path.
headers?
optionalheaders?:Record<string,string>
Defined in: index.ts:11
Sent on every request (per-request headers merge on top).
timeoutMs?
optionaltimeoutMs?:number
Defined in: index.ts:13
Per-request timeout in ms. Default: 30_000.
retries?
optionalretries?:number
Defined in: index.ts:15
Retry attempts on network error or 5xx. Default: 0 (no retry).
retryDelayMs?
optionalretryDelayMs?:number
Defined in: index.ts:17
Fixed delay between retries in ms. Default: 200.
RequestOptions
Defined in: index.ts:20
Properties
headers?
optionalheaders?:Record<string,string>
Defined in: index.ts:22
Merged on top of the client's default headers.
query?
optionalquery?:Record<string,string|number|boolean|undefined>
Defined in: index.ts:24
Appended as a query string; undefined values are skipped.
timeoutMs?
optionaltimeoutMs?:number
Defined in: index.ts:26
Overrides the client timeout for this request.
signal?
optionalsignal?: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?
Returns
Promise<T>
delete()
delete<
T>(path,options?):Promise<T>
Defined in: index.ts:33
Type Parameters
T
T = unknown
Parameters
path
string
options?
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?
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?
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?
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
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.