@molecule/app-http-axios
Provider bond · http · App (browser) · v1.4.0 · Apache-2.0
Axios HTTP client provider for @molecule/app-http
npm install @molecule/app-http-axiosnpm · Source on GitHub · Implements @molecule/app-http
How it works
@molecule/app-http-axios is a provider bond on the app (browser) side: it implements the http core interface (@molecule/app-http) with a concrete vendor or library behind it.
Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.
import { createAxiosClient } from '@molecule/app-http-axios'
import { setClient } from '@molecule/app-http'
const client = createAxiosClient({
baseURL: 'https://api.example.com',
timeout: 10000,
})
setClient(client)
// Now use via `@molecule/app-http`
import { get, post } from '@molecule/app-http'
const users = await get('/users')
const newUser = await post('/users', { name: 'John' })Works with: @molecule/app-http
Reference
Axios HTTP client provider for @molecule/app-http.
This package provides an Axios-based implementation of the molecule HttpClient interface, allowing you to use molecule's HTTP abstractions with Axios.
The body is the SECOND argument: post(url, data, config?) — likewise put and patch.
data IS the request body, so post(url, { data: body }) sends the JSON body
{"data":body}, not body, and the API finds none of its fields at the top level (it
answers 422 {"errors":{"username":["can't be blank"],…}}). Write post('/users', { user }),
never post('/users', { data: { user } }); a config (headers, params) is the THIRD argument.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { createAxiosClient } from '@molecule/app-http-axios'
import { setClient } from '@molecule/app-http'
const client = createAxiosClient({
baseURL: 'https://api.example.com',
timeout: 10000,
})
setClient(client)
// Now use via `@molecule/app-http`
import { get, post } from '@molecule/app-http'
const users = await get('/users')
const newUser = await post('/users', { name: 'John' })
Type
provider
Installation
npm install @molecule/app-http-axios @molecule/app-http axios
API
Interfaces
AxiosHttpClientConfig
Axios-specific configuration.
interface AxiosHttpClientConfig {
/**
* Base URL for all requests — also when `instance` is given: a path (`/api`) or an absolute
* URL (`https://api.example.com/api`). A request path that already starts with the base's
* path (`/api/users`) is requested as written, never prefixed again.
*/
baseURL?: string
/**
* Default headers for all requests.
*/
defaultHeaders?: Record<string, string>
/**
* Default timeout in milliseconds.
*/
timeout?: number
/**
* Whether to include credentials by default.
*/
withCredentials?: boolean
/**
* The scheme of the `Authorization` header the auth token is sent with:
* `Bearer` (default), or what your API expects, e.g. `Token` for an API that
* reads `Authorization: Token <jwt>`. Pass it to this config, at construction —
* `setClient(createAxiosClient({ baseURL, authScheme: 'Token' }))` — never as a
* property of the client it returns. Same option as `@molecule/app-http`'s
* `HttpClientConfig.authScheme`, so the config reads the same whichever
* transport is bonded.
*/
authScheme?: string
/**
* Existing Axios instance to wrap (optional).
*/
instance?: AxiosInstance
/**
* Additional Axios-specific config.
*/
axiosConfig?: AxiosRequestConfig
/**
* Default retry policy for every request this client makes, or `false` to
* disable retries. A request's own `retry` overrides it. The policy is the
* same one `@molecule/app-http`'s fetch client uses: idempotent requests
* only, on 502/503/504 or a reset/refused connection, with backoff and
* jitter, honoring `Retry-After`.
*/
retry?: RetryOptions | false
}
HttpClient
HTTP client interface.
All HTTP providers must implement this interface. An explicit text, blob or arraybuffer parser determines the data type even when a JSON generic is supplied. Keep JSON as the default for mutations returning a record. A successful DELETE with no body already resolves with null data.
Every verb also takes a URL alone, through a signature with the same three parameters on every
verb, so a verb chosen at runtime (client[verb](url)) type-checks and a wrapper can implement
a verb with an arrow (get: (url, config) => inner.get(url, config)). With a body or a config,
choose the verb through request({ method, url, data }) instead: the second argument is the
body for post/put/patch but the config for get/delete, so one call cannot serve both.
interface HttpClient {
/**
* Base URL for all requests.
*/
baseURL: string
/**
* Default headers for all requests.
*/
defaultHeaders: Record<string, string>
/**
* Makes a generic HTTP request. `data` is a field of THIS config object — the same body
* `post(url, data, config)` takes as its second argument.
*/
request<T = unknown>(
config: FullRequestConfig & { responseType: 'text' },
): Promise<HttpResponse<string>>
request<T = unknown>(
config: FullRequestConfig & { responseType: 'blob' },
): Promise<HttpResponse<Blob>>
request<T = unknown>(
config: FullRequestConfig & { responseType: 'arraybuffer' },
): Promise<HttpResponse<ArrayBuffer>>
request<T = unknown>(config: FullRequestConfig): Promise<HttpResponse<T>>
/**
* Makes a GET request.
*/
get<T = unknown>(
url: string,
config: RequestConfig & { responseType: 'text' },
): Promise<HttpResponse<string>>
get<T = unknown>(
url: string,
config: RequestConfig & { responseType: 'blob' },
): Promise<HttpResponse<Blob>>
get<T = unknown>(
url: string,
config: RequestConfig & { responseType: 'arraybuffer' },
): Promise<HttpResponse<ArrayBuffer>>
get<T = unknown>(url: string, config?: undefined, unused?: undefined): Promise<HttpResponse<T>>
get<T = unknown>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>
/**
* Makes a POST request. `data` is the request BODY, not a config object: the wire body is
* `data` itself, so `post(url, { data: body })` sends `{"data":body}` — pass `body` directly.
* A config (headers, params) is the THIRD argument.
*/
post<T = unknown>(
url: string,
data: unknown,
config: RequestConfig & { responseType: 'text' },
): Promise<HttpResponse<string>>
post<T = unknown>(
url: string,
data: unknown,
config: RequestConfig & { responseType: 'blob' },
): Promise<HttpResponse<Blob>>
post<T = unknown>(
url: string,
data: unknown,
config: RequestConfig & { responseType: 'arraybuffer' },
): Promise<HttpResponse<ArrayBuffer>>
post<T = unknown>(url: string, data?: undefined, config?: undefined): Promise<HttpResponse<T>>
post<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>
/**
* Makes a PUT request. `data` is the request BODY (second argument) — never `{ data: body }`,
* which sends `{"data":body}`; a config is the THIRD argument.
*/
put<T = unknown>(
url: string,
data: unknown,
config: RequestConfig & { responseType: 'text' },
): Promise<HttpResponse<string>>
put<T = unknown>(
url: string,
data: unknown,
config: RequestConfig & { responseType: 'blob' },
): Promise<HttpResponse<Blob>>
put<T = unknown>(
url: string,
data: unknown,
config: RequestConfig & { responseType: 'arraybuffer' },
): Promise<HttpResponse<ArrayBuffer>>
put<T = unknown>(url: string, data?: undefined, config?: undefined): Promise<HttpResponse<T>>
put<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>
/**
* Makes a PATCH request. `data` is the request BODY (second argument) — never `{ data: body }`,
* which sends `{"data":body}`; a config is the THIRD argument.
*/
patch<T = unknown>(
url: string,
data: unknown,
config: RequestConfig & { responseType: 'text' },
): Promise<HttpResponse<string>>
patch<T = unknown>(
url: string,
data: unknown,
config: RequestConfig & { responseType: 'blob' },
): Promise<HttpResponse<Blob>>
patch<T = unknown>(
url: string,
data: unknown,
config: RequestConfig & { responseType: 'arraybuffer' },
): Promise<HttpResponse<ArrayBuffer>>
patch<T = unknown>(url: string, data?: undefined, config?: undefined): Promise<HttpResponse<T>>
patch<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>
/**
* Makes a DELETE request.
*/
delete<T = unknown>(
url: string,
config: RequestConfig & { responseType: 'text' },
): Promise<HttpResponse<string>>
delete<T = unknown>(
url: string,
config: RequestConfig & { responseType: 'blob' },
): Promise<HttpResponse<Blob>>
delete<T = unknown>(
url: string,
config: RequestConfig & { responseType: 'arraybuffer' },
): Promise<HttpResponse<ArrayBuffer>>
delete<T = unknown>(url: string, config?: undefined, unused?: undefined): Promise<HttpResponse<T>>
delete<T = unknown>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>
/**
* Adds a request interceptor.
* Returns a function to remove the interceptor.
*/
addRequestInterceptor(interceptor: RequestInterceptor): () => void
/**
* Adds a response interceptor.
* Returns a function to remove the interceptor.
*/
addResponseInterceptor(interceptor: ResponseInterceptor): () => void
/**
* Adds an error interceptor.
* Returns a function to remove the interceptor.
*/
addErrorInterceptor(interceptor: ErrorInterceptor): () => void
/**
* Sets the authorization token.
*
* A static token: every request carries it until the next `setAuthToken` call. A client
* that has a `resolver` (registered through
* `@molecule/app-http`'s `setAuthTokenSource`) ignores this value when it assembles a
* request — the resolver IS the session, and reading it per request is what keeps a
* session adopted without an auth event from sending the previous account's bearer. Once
* the resolver is removed, the static token is sent again.
*/
setAuthToken(token: string | null): void
/**
* Returns the token the client applies to its next request: the resolver's answer when one
* is registered, else the static token from `setAuthToken`.
*/
getAuthToken(): string | null
/** Registers the LIVE token resolver every request of this client reads, or clears it with `null`. */
setAuthTokenResolver?(resolver: AuthTokenResolver | null): void
/**
* Registers a handler for authentication errors (401), with the rejected request's metadata.
*
* @param handler - Receives the method, URL and credential state of the rejected request.
* @returns An unsubscribe function.
*/
onAuthError(handler: (event: HttpAuthErrorEvent) => void): () => void
}
(The longest member comment is shortened to its first sentence to fit; the full declaration is in node_modules/@molecule/app-http/dist/client.d.ts.)
HttpClientConfig
HTTP client configuration.
interface HttpClientConfig {
/**
* Base URL for all requests: a path (`/api`) or an absolute URL
* (`https://api.example.com/api`). A request path that already starts with the base's
* path (`/api/users`) is not prefixed again, and a URL with its own host (`https://…`,
* `//cdn…`) is requested as written.
*/
baseURL?: string
/**
* Default headers for all requests.
*/
defaultHeaders?: Record<string, string>
/**
* Default timeout in milliseconds.
*/
timeout?: number
/**
* Whether to include credentials by default.
*/
withCredentials?: boolean
/**
* The scheme of the `Authorization` header the auth token is sent with:
* `Bearer` (default), or what your API expects, e.g. `Token` for an API that
* reads `Authorization: Token <jwt>`. It is an option of the client's
* CONSTRUCTOR (`createFetchClient({ baseURL, authScheme: 'Token' })`, or the
* bonded provider's `createAxiosClient({ … })`), never a property of the client
* it returns.
*/
authScheme?: string
/**
* Default retry policy for every request this client makes, or `false` to
* disable retries. A request's own `retry` overrides it.
*/
retry?: RetryOptions | false
}
RequestConfig
HTTP request options (headers, query params, timeout, credentials, response type, abort signal).
interface RequestConfig {
/**
* Request headers.
*/
headers?: Record<string, string>
/**
* Query parameters.
*/
params?: Record<string, string | number | boolean | undefined>
/**
* Request timeout in milliseconds.
*/
timeout?: number
/**
* Whether to include credentials (cookies).
*/
withCredentials?: boolean
/**
* Send this request WITHOUT waiting for the session restore (the auth gate).
*
* For a custom HTTP restore, `setAuthGate({ client, restore })` provides a request function
* that applies this option automatically.
*
* A request the gate's restore issues from a LATER continuation must set this. The gate
* recognizes a request the settle function issues while it runs and never holds it — this
* package's client awaits the gate before anything else, so a `restoreSession()` that calls
* it directly does not deadlock. But a restore that awaits something else first
* (`await storage.get()`), then calls the client, reaches `awaitAuthGate` after that window:
* the gate then waits on itself, and every request on the page stalls until the gate times
* out (3 s) — so pass `skipAuthGate: true` on that request. A provider that awaits the gate
* inside a scheduled interceptor (the Axios bond) also reaches it after the window, so its
* restore's request needs the opt-out.
*
* ```typescript
* // the request the gate's restore makes, through the same client the gate holds back
* await http.get(currentUserEndpoint, { skipAuthGate: true })
* ```
*
* `@molecule/app-auth`'s own restore does not need it — it sends its request with
* `fetch`, not through this client. An app with its own session store that registers a
* gate with `setAuthGate` does not need it either when its restore calls the client
* directly.
*/
skipAuthGate?: boolean
/**
* Response type.
*/
responseType?: 'json' | 'text' | 'blob' | 'arraybuffer'
/**
* Abort signal for cancellation.
*/
signal?: AbortSignal
/**
* Keep the fetch client's configured timeout when a caller also supplies a signal.
* Use this when the signal represents cancellation independent of the timeout,
* such as a session change during a write.
*/
timeoutWithSignal?: boolean
/**
* Keep the request running when the page navigates away or closes before it finishes — fetch's
* own `keepalive`. The bundled clients already do this for a write (POST, PUT, PATCH, DELETE) to
* the page's own origin with a body of at most 8 KB, so a favorite or a save followed at once by
* a navigation still reaches the API; nothing needs to be set for that. Pass `true` for anything
* else that must outlive the page (a cross-origin write, a beacon on `pagehide`) and `false` to
* let a navigation cancel a write. The browser caps the bodies of in-flight keepalive requests
* at 64 KB in total, so not for uploads.
*
* ```typescript
* await http.post('https://events.example.com/v1/track', event, { keepalive: true })
* ```
*/
keepalive?: boolean
/**
* Request body data, as a field of THIS config object — the `request({ method, url, data })`
* form. The verb methods take the body as their SECOND argument: `post(url, data, config)`,
* `put(url, data, config)`, `patch(url, data, config)`. Never wrap it there as
* `{ data: body }`: that object is the body, so the wire sends `{"data":body}` and the API
* sees none of the fields at its top level.
*/
data?: unknown
/**
* Custom request options (implementation-specific).
*/
options?: Record<string, unknown>
/**
* Retry policy for this request, or `false` to disable retries.
*
* The default policy retries only IDEMPOTENT requests (`GET`/`HEAD`/`PUT`/
* `DELETE`/`OPTIONS`, and `POST`/`PATCH` carrying an `Idempotency-Key`) on
* `502`/`503`/`504` or a reset/refused connection, twice, with exponential
* backoff and jitter, honoring `Retry-After`. See `@molecule/app-http`'s
* `shouldRetryRequest`.
*/
retry?: RetryOptions | false
}
FullRequestConfig
Full request configuration including method and URL.
interface FullRequestConfig extends RequestConfig {
/**
* HTTP method.
*/
method: HttpMethod
/**
* Request URL (can be relative or absolute).
*/
url: string
/**
* Request body data — a field of this config object, never a second argument here.
*/
data?: unknown
}
HttpResponse
Parsed HTTP response with status code, headers, and typed body data.
interface HttpResponse<T = unknown> {
/**
* Response data.
*/
data: T
/**
* HTTP status code.
*/
status: number
/**
* HTTP status text.
*/
statusText: string
/**
* Response headers.
*/
headers: Record<string, string>
/**
* Original request config.
*/
config: FullRequestConfig
}
Types
AuthTokenResolver
A live access-token resolver: called once per request, it returns the token the session
holds NOW (null/undefined when the session carries no body token — a cookie-session API).
@molecule/app-http's setAuthTokenSource() installs one on every client it bonds, so the
app's auth bond decides the bearer and the client resolves it per request instead of holding
a copy that a session adopted mid-page can leave stale.
type AuthTokenResolver = () => string | null | undefined
RequestInterceptor
Intercepts outgoing requests to modify headers, URL, or body before the request is sent.
type RequestInterceptor = (
config: FullRequestConfig,
) => FullRequestConfig | Promise<FullRequestConfig>
config— The full request configuration to transform.
ResponseInterceptor
Intercepts incoming responses to transform data, check status, or perform side effects before the response reaches the caller.
type ResponseInterceptor<T = unknown> = (
response: HttpResponse<T>,
) => HttpResponse<T> | Promise<HttpResponse<T>>
ErrorInterceptor
Intercepts HTTP errors to transform, retry, or rethrow them. Throwing from this interceptor propagates the error to the caller.
type ErrorInterceptor = (error: HttpError) => HttpError | Promise<HttpError> | never
HttpMethod
HTTP request method.
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS'
Classes
HttpError
HTTP error with response details.
instanceof HttpError also recognizes compatible failures carrying the shared
Symbol.for('@molecule/app-http/HttpError') brand, including non-ok auth responses
and errors from duplicated package copies. Branded producers must provide this
class's instance fields. Subclasses retain ordinary prototype-based checks.
Request metadata on config and response.config is limited to method, a
credential-redacted URL, and the two boolean notification-suppression controls
(molSilentErrorNotification and molSilentRateLimitNotification). Request bodies,
headers, params and all other provider options are omitted, as are credential-bearing
response headers, because errors are routinely serialized by logging transports.
status is an HTTP status when the server answered, and 0 when it did not.
A 0 means NO response exists: the request was aborted (an AbortController, a
component's effect cleanup, a route change tearing the request down) or the
network failed before anything came back. It is not an HTTP status at all, so it
says nothing about the session — an app must never treat status < 500 as
"signed out", because every aborted request on a route change would clear the
session. The correct check is isAuthFailure (status === 401 || status === 403);
isNoResponse (status === 0) identifies this case.
class HttpError extends Error {
/**
* Recognizes the shared failed-request contract without widening subclass checks.
*
* @param value - Any value to test.
* @returns True for a prototype instance, or a compatible branded value checked against the base class.
*/
static [Symbol.hasInstance](value: unknown): boolean {
return (
Function.prototype[Symbol.hasInstance].call(this, value) ||
(this === HttpError &&
typeof value === 'object' &&
value !== null &&
(value as Record<symbol, unknown>)[HTTP_ERROR_BRAND] === true)
)
}
/**
* Redacts URL userinfo and credential query/fragment values while preserving the path and
* non-secret parameters. Shared by compatible HTTP and auth error/log views.
*/
static readonly redactUrlCredentials = redactUrlCredentials
/**
* Builds a failed-request error.
*
* @param message - Human-readable message (already translated where a key exists);
* credentials in any URL it contains are redacted.
* @param status - HTTP status, or `0` when no response was received (aborted / network failure).
* @param response - Parsed response when the server answered, otherwise undefined;
* its request config and headers are sanitized for safe logging.
* @param config - The request config that failed; only its method, redacted URL and
* boolean notification-suppression controls are retained.
* @param errorKey - Translation key for the failure (`http.error.aborted` for an
* aborted request, `http.error.networkError` for a real network failure).
* @param aborted - Whether the request was aborted before a response: an
* `AbortController` signal (component cleanup, a cancelled request) or the
* browser tearing the fetch down during a navigation. Distinguishes an abort
* from a network outage on the same `status === 0`, without parsing the message.
*/
constructor(
message: string,
public status: number,
public response?: HttpResponse,
public config?: FullRequestConfig,
public errorKey?: string,
public aborted = false,
) {
super(snapshotCredentials(message))
this.name = 'HttpError'
this.response = response ? errorResponse(response) : undefined
this.config = config ? errorRequestConfig(config) : undefined
Object.defineProperty(this, HTTP_ERROR_BRAND, { value: true })
}
}
Functions
createAxiosClient(config)
Creates an Axios-based HTTP client implementing the molecule HttpClient interface.
Supports request/response/error interceptors, auth token management, and 401 error handlers.
function createAxiosClient(config?: AxiosHttpClientConfig): HttpClient
config— Axios client configuration (base URL, headers, timeout, credentials, auth scheme, or an existing Axios instance).
Returns: An HttpClient backed by Axios with interceptor and auth token support.
Constants
provider
Default Axios-based HTTP client created with default options (no base URL, 30s timeout).
const provider: HttpClient
Core Interface
Implements @molecule/app-http interface.
Bond Wiring
Setup function to register this provider with the core interface:
import { setClient } from '@molecule/app-http'
import { provider } from '@molecule/app-http-axios'
export function setupHttpAxios(): void {
setClient(provider)
}
Injection Notes
Requirements
Peer dependencies:
@molecule/app-http^1.10.0axios^1.6.0
Runtime Dependencies
-
@molecule/app-http -
axios -
Call
setClient(...)at startup, BEFORE anyget/postfrom@molecule/app-httpruns. The core'sgetClient()lazily bonds its own fetch-based fallback on first use — requests issued beforesetClient()silently bypass the Axios client (no interceptors, no auth token). -
get()/post()/… resolve toHttpResponse<T>— read.datafor the body (const { data: users } = await get('/users')). -
client.onAuthError(handler)reports the rejected request's method, credential-redacted URL and whether it carried credentials or explicitly included cookies when it gets a 401. -
The exported
providerconstant is a pre-built default client (no baseURL, 30 s timeout); prefercreateAxiosClient({...})so baseURL/credentials are explicit. -
A bonded
baseURLapplies to every relative path (also on an existinginstance), and a path that already starts with the base's path is requested as written, never prefixed twice: withbaseURL: '/api'(or'https://api.example.com/api'), bothget('/articles')andget('/api/articles')request…/api/articles— the URLs@molecule/app-http's own fetch client requests. Leave the base URL off and both ask the PAGE origin, which answers its ownindex.htmlwith a 200. -
An API whose scheme is not
Bearer(Authorization: Token <jwt>) passesauthScheme: 'Token'to THIS package's constructor —setClient(createAxiosClient({ baseURL, authScheme: 'Token' }))— and the auth token is then sent with that scheme. It is NOT a property of the clientsetClient()/getClient()hands around: that is a builtHttpClient, andclient.authScheme = 'Token'is a type error that changes no header. The option name is the same one@molecule/app-http'sHttpClientConfigdeclares, so the config reads the same whichever transport is bonded. -
A small write to the page's own origin (at most 8 KB of body) is kept alive like on
@molecule/app-http's fetch client, so a navigation right after it cannot cancel it: XHR cannot outlive the page, so that request goes through Axios's fetch adapter withkeepaliveand the page's cookies. A customadapterfunction is never replaced, andkeepalive: falseon a request opts it out (RequestConfig.keepalive). -
instance/axiosConfigaccept raw Axios options — anything the app sets there couples it to Axios; keep app code on the@molecule/app-httpinterface so the bond stays swappable.