/** * Minimal `fetch`-based HTTP client used by {@link BaseApi}. * * Replaces the previously-used `mande` package (now abandoned). The behavior * here intentionally mirrors what `mande` did for the request shapes this SDK * relies on, so swapping it in is transparent to callers: * - joins the base URL and endpoint, normalizing the slash between them * - sends `Accept`/`Content-Type: application/json` by default (overridable) * - JSON-stringifies the body of POST/PUT/PATCH requests * - parses the response per {@link ResponseAs}; a 204 (or parse failure) * resolves to `null` * - throws on any non-2xx status, attaching the {@link Response} and the parsed * body to the error * * An optional {@link HttpErrorTransformer} lets consumers observe or replace the * error before it propagates. It runs for both non-2xx responses and network * rejections, always after the {@link HttpError} (with its parsed body) has been * built. When absent, the original error is thrown unchanged. */ /** How a response body should be returned from a request. */ export type ResponseAs = "json" | "text" | "response"; /** HTTP verbs the SDK issues. */ export type HttpRequestMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; /** * Minimal, side-effect-free description of the request that produced an error. * * Intentionally excludes the request body and headers (including any * `Authorization` header) so an {@link HttpErrorTransformer} cannot leak * credentials or payloads. */ export interface HttpRequestContext { /** The HTTP verb used for the request. */ method: HttpRequestMethod; /** The fully-resolved request URL (base URL joined with the endpoint). */ url: string; } /** * Hook invoked when a request fails, either with an {@link HttpError} (non-2xx) * or a native fetch/network rejection. * * Return the value to throw in place of the original error. Returning the * original `error` preserves its identity. Returning `undefined` means "rethrow * the original error unchanged" — the same as having no transformer. If the * transformer itself throws (or rejects), that error propagates; the transformer * is never invoked a second time for the same request. */ export type HttpErrorTransformer = (error: unknown, context: HttpRequestContext) => unknown | Promise; /** Options for constructing an {@link HttpClient}. */ export interface HttpClientOptions { /** Absolute base URL that every request is resolved against. */ baseUrl: string; /** Headers applied to every request. */ headers?: Record; /** How to interpret response bodies. Defaults to `"json"`. */ responseAs?: ResponseAs; /** * Optional hook to observe or transform request failures. See * {@link HttpErrorTransformer}. */ errorTransformer?: HttpErrorTransformer; /** * Optional `fetch` implementation. Defaults to `globalThis.fetch`, resolved * at call time so the ambient global is honored when this is omitted. */ fetch?: typeof globalThis.fetch; } /** * Error thrown for any non-2xx response. Carries the originating * {@link Response} and the parsed body so callers can inspect the failure. */ export declare class HttpError extends Error { readonly response: Response; readonly body: unknown; constructor(response: Response, body: unknown); } export declare class HttpClient { private readonly baseUrl; private readonly headers; private readonly responseAs; private readonly errorTransformer?; private readonly fetchImpl?; constructor(options: HttpClientOptions); get(url: string): Promise; post(url: string, data?: unknown): Promise; put(url: string, data?: unknown): Promise; patch(url: string, data?: unknown): Promise; delete(url: string): Promise; private request; /** * Performs the request and parses the response. Throws {@link HttpError} on a * non-2xx status; native fetch/network failures reject as they always have. * All failures are funneled through {@link transformError} by {@link request}. */ private send; /** * Runs the configured {@link HttpErrorTransformer}, if any, and returns the * error to throw. Without a transformer, or when the transformer returns * `undefined`, the original error is returned unchanged. A transformer that * throws/rejects propagates its own error (and is not retried). */ private transformError; }