//#region src/core/security/upstream.d.ts /** How long an upstream call may take before it's abandoned. */ declare const UPSTREAM_TIMEOUT_MS = 10000; interface UpstreamFetchInit extends RequestInit { /** Who is being called—`"Square"`, `"Stripe"`. Names the error. */ provider: string; /** Abandon the call after this long. Default {@link UPSTREAM_TIMEOUT_MS}. */ timeoutMs?: number; } interface UpstreamErrorInit { /** The provider's own error code (`"NOT_FOUND"`, `"card_declined"`). Safe to * show and to branch on; anything that isn't code-shaped is dropped. */ code?: string | null; /** What the provider said, verbatim. For logs—never for a response. */ detail?: string | null; /** Method and path (never the query, which can carry a token). For logs. */ operation?: string | null; cause?: unknown; } /** * A call to a third-party API that failed. `message` is safe to put in front of * a user—provider, status, and the provider's code, nothing it wrote. The * provider's own words are on `detail`, non-enumerable so `{ ...err }`, * `JSON.stringify(err)` and a logger copying fields all leave it behind. * * Log with {@link upstreamLogLine}, which includes it. Show `message`, or map * `code` to your own copy. */ declare class UpstreamError extends Error { readonly provider: string; /** HTTP status, or `0` when no response arrived (timeout, network). */ readonly status: number; readonly code: string | null; /** Worth retrying: rate limited, a server fault, or no response at all. A * 4xx means the request itself is wrong and will stay wrong. */ readonly retryable: boolean; readonly detail: string | null; readonly operation: string | null; constructor(provider: string, status: number, init?: UpstreamErrorInit); /** What `JSON.stringify` sees: the fields that are safe to send. */ toJSON(): { name: string; provider: string; status: number; code: string | null; message: string; }; } /** * One log line for any error, with everything an upstream failure knows: * operation, status, code, and what the provider said. For logs only—the * point of `UpstreamError` is that its `message` alone is what users see. */ declare function upstreamLogLine(err: unknown): string; /** * `fetch` for a third-party API. Adds what every provider call needs and none * of them had: * * - **A timeout** (default 10 s), combined with the caller's own `signal`. * - **`redirect: "manual"`.** Provider APIs don't redirect; a 3xx comes back as * a non-ok response rather than being followed somewhere nobody chose. * - **A safe failure.** No response at all becomes an {@link UpstreamError} * with status `0` and code `timeout` or `network`. * * A non-2xx is returned, not thrown: each provider reads its own error shape. * Read the body with {@link readUpstreamBody} and throw an `UpstreamError`. * A caller that aborts through its own `signal` gets its own abort back. */ declare function upstreamFetch(input: string | URL, init: UpstreamFetchInit): Promise; /** * Read a provider's response body without letting it into an error message: * `json` when it parses, and always the raw `text`. `res.json()` on an HTML * error page throws a `SyntaxError` that quotes the page, which is the same * leak by another route. */ declare function readUpstreamBody(res: Response): Promise<{ json: unknown; text: string; }>; //#endregion export { readUpstreamBody as a, UpstreamFetchInit as i, UpstreamError as n, upstreamFetch as o, UpstreamErrorInit as r, upstreamLogLine as s, UPSTREAM_TIMEOUT_MS as t };