hapic
v3.4.0
Published
A fetch-based HTTP client for Node.js, browsers and workers.
Maintainers
Readme
hapic ("HTTP API Client") wraps fetch in one Client class. It resolves a base URL and query parameters, encodes JSON bodies, decodes responses by type, rejects on a 4xx/5xx status, and runs request, response and error hooks around every call. The same class is the base of the typed service clients in this repository (@hapic/harbor, @hapic/loki, @hapic/oauth2, @hapic/vault, @hapic/victorialogs), so everything below applies to them too.
import { createClient, isClientErrorWithStatusCode } from 'hapic';
const client = createClient({
baseURL: 'https://api.example.com/',
timeout: 10_000,
});
client.setAuthorizationHeader({ type: 'Bearer', token: 'my-token' });
try {
const { data } = await client.post('users', { name: 'Peter' });
console.log(data.id);
} catch (error) {
if (isClientErrorWithStatusCode(error, 409)) {
// the server answered 409; error.response.data holds its body
}
}Table of Contents
- Installation
- How a request runs
- Creating a client
- Options
- Request methods
- Headers and authorization
- Hooks
- Transformers
- Responses
- Timeouts and abort signals
- Errors
- Instance registry
- Copying a configuration
- Transports
- Proxy
- Building a service client
- Notes
- Contributing
- License
Installation
npm install hapic --save- ESM only. The package ships
dist/index.mjsanddist/index.d.mts. Useimport, or a dynamicimport()from CommonJS. Type declarations are included. - Node.js
>=22.0.0. Browsers, workers, Deno and Bun use their platformfetch. Wherefetch,Headers,Blob,FormDataorAbortControllerare missing from the global object, hapic falls back tonode-fetch-native. - One entry point for every environment.
How a request runs
Every method (get, post, ...) calls request(config), which runs the same pipeline:
- Resolve options. Per-request options replace the client defaults; headers are layered (request headers win, client headers fill the gaps). The URL is resolved against
baseURL, thenparamsandqueryare appended. - Request hooks run in registration order. Each may return new
RequestOptions. - Transformers (
transform) turn the body into what is sent. The default one encodes plain objects as JSON. - Dispatch. The body is dropped for methods other than
POST,PUTandPATCH, and the transport (FetchTransportby default) sends the request. Atimeoutand asignalare combined into one abort signal. - Decode. The body is read by
responseType, or by theContent-Typeheader, then passed throughresponseTransform. - Status check. A status from 400 to 599 becomes an
HttpResponseErrorand runs theresponseErrorhooks. A failed, aborted or timed-out dispatch becomes aNetworkErrorand runs therequestErrorhooks. - Response hooks run for every other status, then the call resolves with the
Response.
An error hook can recover: return a Response to resolve with it, or RequestOptions to run the whole pipeline again. See Hooks.
Creating a client
import hapic, { Client, createClient } from 'hapic';
// the default export is a client created without options
await hapic.get('https://api.example.com/users');
// both lines create a new, independent client
const a = new Client({ baseURL: 'https://api.example.com/' });
const b = createClient({ baseURL: 'https://api.example.com/' });The constructor options become client.defaults, the client's default headers and its transport. To share one configured client across modules, register it in the instance registry.
Options
The options extend the platform RequestInit, so credentials, mode, cache, redirect, signal and the other fetch options are passed through. Every option can be set on the client and per request, except transport.
import { createClient } from 'hapic';
const client = createClient({
baseURL: 'https://api.example.com/',
headers: { 'x-tenant': 'acme' },
credentials: 'include',
query: { locale: 'en' },
timeout: 10_000,
proxy: { url: 'http://localhost:9080', noProxy: '.internal.example' },
});| Option | Type | Default | Description |
|---|---|---|---|
| baseURL | string | none | Resolved in front of each request URL. A request URL that is already absolute is kept as it is. |
| headers | HeadersInit | none | Default headers. Per-request headers are layered over them. |
| params / query | Record<string, any> | none | Appended to the URL as a query string. Both are merged (query wins on a shared key); bigint values become strings. |
| body | BodyInit \| Record<string, any> | none | The request body. post, put and patch take it as an argument; it is dropped for other methods. |
| transform | RequestTransformer \| RequestTransformer[] | the JSON transformer | (data, headers) => body, run after the request hooks. Setting it replaces the default. See Transformers. |
| responseType | 'json' \| 'text' \| 'blob' \| 'arrayBuffer' \| 'stream' | from Content-Type | How the body is read. See Responses. |
| responseTransform | ResponseTransformer \| ResponseTransformer[] | none | (data, headers, status) => data, run on the decoded body, also for 4xx/5xx. Not run for streams. |
| timeout | number | none | Abort after this many milliseconds, reading the body included. See Timeouts and abort signals. |
| signal | AbortSignal | none | Abort the request yourself. Combined with timeout. |
| bearerToken | string \| (() => string \| Promise<string>) | none | Per request only (ignored as a client default): the bearer token a bearer token hook prefers over its own; empty sends none. The base client never sends it. |
| proxy | ProxyOptions \| boolean | true | true reads the proxy environment variables, false turns proxying off, an object sets url and noProxy. Node.js only. See Proxy. |
| transport | ITransport | new FetchTransport() | Constructor only. What sends the request. See Transports. |
How client defaults and per-request options combine:
- A per-request option replaces the client default as a whole,
transform,responseTransform,paramsandqueryincluded. headersare layered: per-request headers win, client headers fill in the names the request does not set.- A per-request
timeoutorsignalofundefinedkeeps the client default.
Request methods
import { createClient } from 'hapic';
type User = { id: number, name: string };
const client = createClient({ baseURL: 'https://api.example.com/' });
const { data: users } = await client.get<User[]>('users', { query: { page: 2 } });
const { data: created } = await client.post<User>('users', { name: 'Hans' });
await client.put('users/3', { id: 3, name: 'Hans' });
await client.patch('users/3', { name: 'Hans P.' });
await client.delete('users/3');
const head = await client.head('users/3');
console.log(head.status, head.headers.get('etag'));| Method | Signature | Body |
|---|---|---|
| get | get(url, config?) | dropped |
| delete | delete(url, config?) | dropped |
| head | head(url, config?) | dropped |
| post | post(url, body?, config?) | sent |
| put | put(url, body?, config?) | sent |
| patch | patch(url, body?, config?) | sent |
| request | request({ url, method, ...config }) | sent for POST, PUT and PATCH |
- Each resolves with a
Responsewhosedataholds the decoded body, and rejects for a status from 400 to 599. - The first type argument types
datafor JSON. WithresponseTypeset to'text','blob','arrayBuffer'or'stream',datais typedstring,Blob,ArrayBufferorReadableStream. configtakes the same options as the constructor, for this call only.
Call request for full control, with a method from the MethodName enum or a plain string:
import { MethodName, createClient } from 'hapic';
const client = createClient({ baseURL: 'https://api.example.com/' });
const { data } = await client.request({
method: MethodName.POST,
url: 'users',
body: { name: 'Peter' },
query: { dryRun: true },
});Headers and authorization
import { createClient } from 'hapic';
const client = createClient({
baseURL: 'https://api.example.com/',
headers: { accept: 'application/json' },
});
client
.setHeader('x-tenant', 'acme')
.setAuthorizationHeader({ type: 'Bearer', token: 'my-token' });
client.getHeader('x-tenant'); // 'acme'
client.getAuthorizationHeader(); // 'Bearer my-token'
client.unsetHeader('x-tenant');
client.unsetAuthorizationHeader();
client.unsetHeaders(); // remove every default header
// a per-request header overrides the default for one call
await client.get('report.csv', { headers: { accept: 'text/csv' } });- Header names are case-insensitive; the client keeps its default headers in a
Headersobject. The setters return the client, so they chain. setAuthorizationHeadertakes a typed descriptor (AuthorizationHeader) and sets theauthorizationheader for every upcoming request:
| type | Fields | Header value |
|---|---|---|
| 'Bearer' | token | Bearer <token> |
| 'Basic' | username, password | Basic <base64 of username:password> |
| 'API-Key' / 'X-API-Key' | key | API-Key <key> / X-API-Key <key> |
stringifyAuthorizationHeader(descriptor)andparseAuthorizationHeader(value)convert between the descriptor and the header string. Parsing throws anAuthorizationHeaderErrorfor an unknown scheme or a malformed value.HeaderNameholds the names hapic itself uses:HeaderName.ACCEPT,HeaderName.AUTHORIZATIONandHeaderName.CONTENT_TYPE.- To compute a header per request (a token that changes, a request id), set it in a request hook.
createBearerTokenHookbuilds such a hook that sends a bearer token only to the origins you list, never to another host:
import { createBearerTokenHook, createClient } from 'hapic';
const options = { token: () => store.accessToken() }; // string or (async) function
const client = createClient({ baseURL: 'https://api.example.com/v1/', redirect: 'manual' });
client.on('request', createBearerTokenHook({
options, // read on every request
origins: () => [client.getBaseURL()],
headers: () => ({ 'x-api-key': 'my-key' }), // optional, API origins only
}));
await client.get('videos'); // Bearer <options.token>
await client.get('videos', { bearerToken: 'user-b' }); // the per-request token wins
await client.get('https://cdn.example.com/a.mp4'); // no token, no x-api-keyA request with its own authorization header keeps it (the extra headers are still added); an empty token, also an empty per-request bearerToken, sends nothing; options is never changed by a per-request token. A retry from an error hook resolves the token again (the header the hook set is replaced). Register the hook after any request hook that rewrites the URL.
Hooks
import { Headers, createClient } from 'hapic';
const client = createClient({ baseURL: 'https://api.example.com/' });
const id = client.on('request', (options) => {
const headers = new Headers(options.headers);
headers.set('x-request-id', crypto.randomUUID());
return { ...options, headers };
});
client.on('response', (response) => {
console.debug(response.status, response.url);
return response;
});
client.off('request', id); // remove this hook
client.off('response'); // remove every response hook| Hook | HookName | Runs when | Receives | Return value |
|---|---|---|---|---|
| request | REQUEST | before the transformers and the dispatch | RequestOptions | RequestOptions replace the options; anything else keeps them |
| response | RESPONSE | the status is below 400 | Response | a Response replaces it; anything else keeps it |
| requestError | REQUEST_ERROR | the dispatch failed, was aborted or timed out, so there is no response | NetworkError | a Response resolves the call; RequestOptions retry it |
| responseError | RESPONSE_ERROR | the status is from 400 to 599 | HttpResponseError | a Response resolves the call; RequestOptions retry it |
- Hooks of one name run in registration order and may be
async. Each request and response hook receives what the previous one returned. on(name, fn)returns an id.off(name, id)oroff(name, fn)removes one hook;off(name)removes every hook of that name.- In a request hook,
options.headersis aHeadersobject. Copy it withnew Headers(options.headers)as above: spreading it ({ ...options.headers }) gives an empty object, loses every header and makes the default transformer throw.
Error hooks recover or retry
Error hooks run in registration order, each receiving the ClientError. The first hook that returns a Response or RequestOptions decides the outcome, and the remaining hooks are skipped. A hook that throws hands its error to the next hook. When no hook recovers, the call rejects with the last error.
| The error hook | Result |
|---|---|
| returns a Response | the call resolves with that response |
| returns RequestOptions | the request runs again through the whole pipeline, hooks included |
| returns nothing, or throws | the next hook runs; after the last one the call rejects |
Refresh an expired token and retry. A 401 is a response, so the hook is responseError, not requestError:
import { Headers, createClient, isClientErrorWithStatusCode } from 'hapic';
declare function refreshAccessToken(): Promise<string>;
const client = createClient({ baseURL: 'https://api.example.com/' });
client.on('responseError', async (error) => {
const headers = new Headers(error.request.headers);
if (isClientErrorWithStatusCode(error, 401) && !headers.has('x-token-refreshed')) {
const token = await refreshAccessToken();
client.setAuthorizationHeader({ type: 'Bearer', token });
headers.set('authorization', `Bearer ${token}`);
headers.set('x-token-refreshed', '1');
return { ...error.request, headers };
}
throw error;
});error.requestholds the options as they were dispatched: the URL with the base URL and query applied, the merged headers and the transformed body. Returning it, changed or not, runs the request again.- A retried request passes through this hook again. hapic sets no retry limit, so guard against loops: here the marker header stops a second refresh.
Answer a network failure with a fallback response instead of rejecting:
import { createClient, isClientErrorDueNetworkIssue } from 'hapic';
const client = createClient({ baseURL: 'https://api.example.com/' });
client.on('requestError', (error) => {
if (isClientErrorDueNetworkIssue(error)) {
return new Response(JSON.stringify({ offline: true }), {
headers: { 'content-type': 'application/json' },
});
}
throw error;
});A Response returned by an error hook is used as it is: it is not decoded, so its data is not set. Read it with response.json() or set data in the hook.
Transformers
A request transformer receives the body and the request Headers and returns the body to send. Set it on the client or per request:
import { createClient, createDefaultRequestTransformer } from 'hapic';
import type { RequestTransformer } from 'hapic';
const toForm: RequestTransformer = (data, headers) => {
headers.set('content-type', 'application/x-www-form-urlencoded');
return new URLSearchParams(data).toString();
};
const client = createClient({ baseURL: 'https://api.example.com/' });
await client.post('login', { username: 'admin', password: 'secret' }, { transform: toForm });
// body: username=admin&password=secret
// an array runs in order; add the default one to keep JSON encoding
const stamped = createClient({
transform: [
(data) => ({ ...data, sentAt: Date.now() }),
createDefaultRequestTransformer(),
],
});The default transformer (createDefaultRequestTransformer()) does this:
| Body | Sent as | Headers |
|---|---|---|
| FormData | as it is | removes content-type, so fetch sets the multipart boundary |
| Blob, File, ArrayBuffer, a Node.js stream (has pipe()) | as it is | none |
| URLSearchParams | as it is | content-type: application/x-www-form-urlencoded;charset=utf-8 |
| a string | as it is | content-type and accept set to application/json when absent |
| any other object, array, number, boolean or bigint | JSON.stringify, bigint as a string | content-type and accept set to application/json when absent |
Setting transform replaces this default, on the client or for one request. Typed arrays, Node.js Buffers and web ReadableStreams count as "any other object" and are JSON-encoded: send binary data as a Blob or ArrayBuffer.
A response transformer receives the decoded data, the response headers and the status, and returns the new data:
import { createClient } from 'hapic';
const client = createClient({
responseTransform: (data, headers, status) => {
if (status >= 400) {
return data?.error ?? data;
}
// unwrap the { result } envelope of JSON responses
if (headers.get('content-type')?.includes('json')) {
return data?.result ?? data;
}
return data;
},
});- It runs once per response on the decoded body, for
4xx/5xxbodies too, soerror.response.datais transformed as well. - It is skipped when the body is a stream (
responseType: 'stream', a detectedtext/event-stream, or a response withoutContent-Type). - An array runs in order, each function receiving the previous output. A per-request
responseTransformreplaces the client one. - If it throws on a
4xx/5xxbody, the body is left untransformed and the usualHttpResponseErroris thrown, so error hooks keep working. If it throws on a successful response, the call rejects with that error as it is (not aClientError).
Responses
A call resolves with the platform Response, plus a data property holding the decoded body:
import { ResponseType, createClient } from 'hapic';
const client = createClient({ baseURL: 'https://api.example.com/' });
const response = await client.get('users/1');
response.status; // 200
response.headers.get('etag'); // a Headers object
response.data; // the decoded body
const { data: archive } = await client.get('archive.zip', {
responseType: ResponseType.BLOB,
});| responseType | data | Chosen when responseType is not set and Content-Type is |
|---|---|---|
| json | the parsed value, or the raw text if JSON.parse fails | application/json, application/*+json, and every type not listed in the other rows |
| text | string | text/*, application/xml, application/xhtml, application/html |
| stream | ReadableStream (the unread response.body, null without a body) | text/event-stream, or no Content-Type at all |
| blob | Blob | never detected; set it yourself |
| arrayBuffer | ArrayBuffer | never detected; set it yourself |
- Binary types such as
application/octet-streamorimage/pngfall into thejsonrow and are read as text. SetresponseType: 'blob'or'arrayBuffer'for downloads. datais a getter; assigningresponse.datareplaces the value.- For
stream, hapic does not read the body. Read it yourself, for example withresponse.data.getReader().
Server-sent events
A text/event-stream response is detected as a stream, because it stays open. Pass the response (or its body) to parseEventStream():
import { createClient, parseEventStream } from 'hapic';
const client = createClient({ baseURL: 'https://api.example.com/' });
const response = await client.get('events');
for await (const message of parseEventStream(response)) {
console.log(message.type, message.data, message.lastEventId);
}| Property | Description |
|---|---|
| type | the event field, 'message' when absent |
| data | the data fields, joined with a line feed |
| lastEventId | the last id seen on the stream, '' when none |
| retry | the last valid retry field seen on the stream, in milliseconds; absent until one arrives |
- Comments (
: ping) are skipped. An event without its closing blank line when the stream ends is discarded. parseEventStream()reads one response and does not reconnect.- For a stream that is already decoded to text, pipe it through
new EventStreamParserStream().
Timeouts and abort signals
import { createClient, isClientErrorDueTimeout } from 'hapic';
const client = createClient({ baseURL: 'https://api.example.com/', timeout: 10_000 });
const controller = new AbortController();
try {
await client.get('slow', { timeout: 2_000, signal: controller.signal });
} catch (error) {
if (isClientErrorDueTimeout(error)) {
// no complete response within 2 seconds
}
}timeoutis in milliseconds. Only a finite number above0arms it;0, a negative number orNaNmean no timeout. A fraction is rounded up, a value above2147483647(about 24.8 days) is capped.- Each dispatch arms a fresh timer, a retry from an error hook included.
- The timeout covers reading the body. For
responseType: 'stream'the timer keeps running while you consume the stream. timeoutandsignalare combined; whichever fires first aborts the request. Asignalthat is already aborted rejects before anything is sent.- The rejection is a
NetworkErrorthat runs therequestErrorhooks:
| Cause | error.code | isClientErrorDueTimeout | isClientErrorDueNetworkIssue |
|---|---|---|---|
| timeout elapsed | 'ETIMEDOUT' | true | true |
| signal aborted with a TimeoutError (AbortSignal.timeout()) | 'ETIMEDOUT' | true | true |
| signal aborted for any other reason | 'ECONNABORTED' | false | false |
Errors
Everything hapic throws descends from HapicError:
BaseError (@ebec/core)
└─ HapicError isHapicError()
├─ ClientError isClientError()
│ ├─ NetworkError isNetworkError() no response
│ └─ HttpResponseError isHttpResponseError() status 400 to 599
├─ AuthorizationHeaderError isAuthorizationHeaderError()
└─ ConnectionStringParseError isConnectionStringParseError()import {
createClient,
isClientErrorDueNetworkIssue,
isClientErrorWithStatusCode,
isHttpResponseError,
} from 'hapic';
const client = createClient({ baseURL: 'https://api.example.com/' });
try {
await client.get('users/1');
} catch (error) {
if (isClientErrorWithStatusCode(error, [401, 403])) {
// not allowed
} else if (isHttpResponseError(error)) {
console.error(error.status, error.response?.data);
} else if (isClientErrorDueNetworkIssue(error)) {
// never reached the server
} else {
throw error;
}
}A ClientError carries:
| Property | Description |
|---|---|
| request | the RequestOptions that were dispatched |
| response | the decoded Response; undefined for a NetworkError |
| status / statusCode | response.status |
| statusText / statusMessage | response.statusText |
| code | 'HTTP_RESPONSE_ERROR' for an HttpResponseError; for a NetworkError an ErrorCode: 'ECONNRESET' (dispatch failed), 'ECONNABORTED' (aborted) or 'ETIMEDOUT' (timed out) |
| cause | the underlying error of a NetworkError |
| message | for example 404 Not Found (GET https://api.example.com/users/1) |
| Guard | True for |
|---|---|
| isHapicError(e) | anything thrown by hapic or a @hapic/* package |
| isClientError(e) | a NetworkError or an HttpResponseError |
| isNetworkError(e) / isHttpResponseError(e) | that subclass |
| isClientErrorWithStatusCode(e, code \| code[]) | a ClientError whose response has one of the statuses |
| isClientErrorDueNetworkIssue(e) | no response and a code other than 'ECONNABORTED' |
| isClientErrorDueTimeout(e) | no response and the code 'ETIMEDOUT' |
| isAuthorizationHeaderError(e) / isConnectionStringParseError(e) | that class |
- Prefer the guards over
instanceof: they check a marker chain that each class adds in its constructor, so they also match errors from a second copy of hapic in the bundle. - The guards also match the plain object
error.toJSON()produces, after it was sent as JSON and parsed again. ErrorCodealso names the codes ofAuthorizationHeaderError('auth_header_invalid','auth_header_parse','auth_header_type_parse') andConnectionStringParseError('connection_string_invalid').
Instance registry
import { createClient, setClient, useClient } from 'hapic';
// at startup
setClient(createClient({ baseURL: 'https://api.example.com/' }));
setClient(createClient({ baseURL: 'https://auth.example.com/' }), 'auth');
// anywhere else
const { data } = await useClient().get('users');
await useClient('auth').post('login', { username: 'admin', password: 'secret' });| Function | Description |
|---|---|
| createClient(options?) | creates a new client; does not register it |
| setClient(client, key?) | registers client under key (default 'default') and returns it |
| useClient(key?) | returns the client under key; creates and registers a client without options on first use |
| hasClient(key?) | whether a client is registered under key |
| unsetClient(key?) | removes the client under key |
| isClient(input) | whether input is a hapic client, including a service client or one from a second copy of hapic |
- The default export is not in the registry:
useClient()returns a different client unless yousetClient(hapic). - Each
@hapic/*package exports the same six functions for its own client type.createClientRegistry({ create, id })builds them for your own client.
Copying a configuration
toOptions() returns the client's configuration as constructor options: the defaults, the current headers and the transport.
import { Client, createClient } from 'hapic';
const client = createClient({ baseURL: 'https://api.example.com/' });
client.setHeader('x-tenant', 'acme');
const staging = new Client(client.toOptions());
staging.setBaseURL('https://staging.example.com/');- Headers set or removed after construction are included.
- Arrays and plain objects among the options (
transform,responseTransform,params,query, aproxyobject) are copied one level deep, so changing one client does not change the other. Functions, asignaland the transport are the same instances. - Hooks are not included; register them on the new client.
Transports
The transport is the one place a request leaves the client. It receives the finished request (URL, method, merged headers, transformed body, signal, proxy) and returns the raw Response with its body unread. It must reject on a network failure and must not reject for a 4xx/5xx status: the pipeline does that.
import { Client, FetchTransport } from 'hapic';
const client = new Client({
baseURL: 'https://api.example.com/',
transport: new FetchTransport({
fetch: (url, init) => {
console.debug(init?.method, url);
return fetch(url, init);
},
}),
});FetchTransportis the default. Itsfetchoption replaces thefetchit calls, for example one that adds an undicidispatcherfor mutual TLS, a CA bundle or connection pooling.- Any object with a
dispatch(request)method that returns aPromise<Response>is a transport (ITransport). The constructor throws aTypeErrorfor anything else. - Retries, logging and caching belong in hooks, which run for every transport.
Testing with MemoryTransport
MemoryTransport answers requests in memory and records them, while the real pipeline (header merge, URL building, transformers, hooks, decoding, errors) runs unchanged:
import { MemoryTransport, createClient, isHttpResponseError } from 'hapic';
const transport = new MemoryTransport({
fetch: (request) => {
if (request.url.endsWith('/users/1')) {
return { status: 200, body: { id: 1, name: 'Ada' } };
}
return { status: 404, body: { message: 'not found' } };
},
});
const client = createClient({ baseURL: 'https://api.test/', transport });
const { data } = await client.get('users/1');
expect(data).toEqual({ id: 1, name: 'Ada' });
expect(transport.requests.at(-1)?.method).toBe('GET');
await expect(client.get('users/2')).rejects.toSatisfy(isHttpResponseError);
transport.reset();| Member | Description |
|---|---|
| new MemoryTransport({ fetch? }) | fetch(request) answers each dispatch with { status?, statusText?, headers?, body? } or a Response. A plain object or array body is JSON-encoded, with a JSON content-type unless one is set. Throw or reject to take the requestError path. Without fetch, every request gets an empty 200. |
| requests | every dispatched request, in order |
| reset() | clears requests; keeps the fetch handler |
The same works for service clients and single domain APIs: pass transport in the client's request options, or a client built with it to the API.
Proxy
import { createClient } from 'hapic';
// the default: reads HTTPS_PROXY, HTTP_PROXY and NO_PROXY (or their lowercase forms)
const fromEnv = createClient({ proxy: true });
const custom = createClient({
proxy: {
url: 'http://localhost:9080', // replaces the environment variables
noProxy: '.internal.example', // hosts that skip the proxy
},
});
const direct = createClient({ proxy: false });
await custom.get('https://status.internal.example/health', { proxy: false });- Proxy support comes from
node-fetch-native/proxy;ProxyOptionsis its option type. It is meant for Node.js; in browsers and workers the proxy step does nothing. - While
proxyis notfalse, theagentanddispatcheroptions of a request are replaced by the proxy's (which areundefinedwhen no proxy URL is configured). To pass your own, setproxy: falseor add them in a customFetchTransportfetch.
Building a service client
The @hapic/* packages extend Client with domain APIs. The pieces are exported, so you can build your own:
import { BaseAPI, Client } from 'hapic';
import type { ClientOptionsInput } from 'hapic';
type Project = { id: number, name: string };
class ProjectAPI extends BaseAPI {
async getOne(id: number): Promise<Project> {
const { data } = await this.client.get(`projects/${id}`);
return data;
}
}
class RegistryClient extends Client {
public readonly project: ProjectAPI;
constructor(input: ClientOptionsInput = {}) {
super(input);
this.project = new ProjectAPI({ client: this });
}
}
const registry = new RegistryClient({ baseURL: 'https://registry.example.com/api/' });
const project = await registry.project.getOne(1);BaseAPItakes{ client }: a client to share, or request options to create its own. Domain APIs issue every request throughthis.client.this.call(options)(protected) requests through the client and rejects any status outside200–299, a3xxincluded, with anHttpResponseErrornaming the absolute URL, passed through theerrorTransform(elsestripClientError).this.createError(options, response)builds the same error for a response you refuse yourself.createClientRegistry,markInstanceof,splitConnectionStringandresolveConnectionOptionscover the instance registry, the cross-realmisClientcheck andcredentials@hostconnection strings.- The Building a Service Client guide walks through all of them.
Notes
fetchresolves on404; hapic rejects. Every status from 400 to 599 rejects with anHttpResponseError, anderror.response.dataholds the decoded body. Redirects are followed byfetch(redirect: 'follow'), so a3xxonly reaches you withredirect: 'manual', and then it resolves (BaseAPI.callrejects it, without running theresponseErrorhooks).- A
401runsresponseError, notrequestError.requestErroris only for requests that got no response. - Setting
transformdrops the JSON encoding. Your transformer replaces the default one. AddcreateDefaultRequestTransformer()to your array to keep it. - Per-request options replace, they do not merge. A per-request
query,transformorresponseTransformreplaces the client's. Only headers are layered. - Binary bodies need a
BloborArrayBuffer. The default transformer JSON-encodes typed arrays,Buffers and webReadableStreams. - Binary downloads need a
responseType. Without it, an unknownContent-Typeis read as text and parsed as JSON where possible. - No
Content-Typemeans a stream.datais then the unread body (nullfor an empty body), andresponseTransformdoes not run. - Retries are unbounded. An error hook that returns
RequestOptionsruns the request again, through the same hooks. Stop the loop yourself. - A
Responsereturned by an error hook is not decoded. Itsdatais whatever you set. - Basic authorization needs
Buffer.setAuthorizationHeader({ type: 'Basic', ... }),stringifyAuthorizationHeaderandparseAuthorizationHeaderuse the Node.jsBufferglobal. Without it (a browser without a polyfill) they throw aReferenceError; setauthorizationto`Basic ${btoa(`${username}:${password}`)}`yourself. - The default export is not the registry's default client.
import hapic from 'hapic'anduseClient()are two different clients. toOptions()does not copy hooks.- The proxy replaces a request's
agentanddispatcher. Setproxy: falseto pass your own.
Contributing
hapic is developed in the tada5hi/hapic monorepo. Its README lists the development commands; commits follow Conventional Commits.
License
Made with 💚
Published under MIT License.
