import type { BackoffOptions } from "./backoff.js"; import type { QueryParams } from "./query.js"; import { PersistMemoryError } from "./errors.js"; /** * The one place a request is made, retried, timed out and turned into an error. * * Every resource in this package goes through `request`. That is deliberate: * an SDK where each resource calls `fetch` for itself is an SDK with six * slightly different retry policies, and the differences only show up during * an outage, which is the moment nobody wants to be reading six files. * * `fetch` is injected rather than imported. It is what makes a test of this * incapable of opening a socket by accident, and it is the same reason every * provider client in this repo takes one. */ export type { QueryParams, QueryValue } from "./query.js"; export interface ClientOptions { /** * A `pm_live_...` API key, or a session JWT. * * Held privately and never returned, logged, stringified or put in an error. * See `#apiKey` below and the `toJSON` next to it. */ readonly apiKey: string; readonly baseUrl?: string; readonly fetch?: typeof globalThis.fetch; /** * Per-attempt deadline, not a budget for the whole call. * * Per attempt because a whole-call deadline interacts badly with backoff: a * request that spent nine seconds waiting between retries would get one * second to actually run, and the failure would look like a slow server. */ readonly timeoutMs?: number; /** Attempts, not retries. 3 means the original and two more. */ readonly maxAttempts?: number; readonly backoff?: BackoffOptions; /** Injected so tests do not spend real seconds asleep. */ readonly sleep?: (ms: number, signal?: AbortSignal) => Promise; /** Sent on every request, for support and for server-side triage. */ readonly userAgent?: string; } export interface RequestOptions { /** Cancels the call. A caller's abort is never retried - they asked it to stop. */ readonly signal?: AbortSignal; readonly timeoutMs?: number; readonly maxAttempts?: number; /** * Makes a POST safe to retry. * * The API deduplicates on this header, so a retry after a timeout finds the * first result rather than doing the work twice. Without one, this client * will NOT retry a POST that may already have been processed - see * `mayRetry`. * * Give it meaning: `remember:note-42`, not a fresh random value per call. A * random one makes every retry a new request, which is exactly what it * exists to prevent. */ readonly idempotencyKey?: string; } interface InternalRequest { readonly method: "GET" | "POST" | "PATCH" | "DELETE"; readonly path: string; readonly query?: QueryParams; readonly body?: unknown; /** * Bytes, for the endpoints that take a file. * * Separate from `body` rather than sniffed out of it: `JSON.stringify` of a * `Uint8Array` produces `{"0":137,"1":80,...}`, which is a valid request and * a corrupt file - and the failure shows up as "this PDF is not a PDF" a * long way from the line that caused it. */ readonly rawBody?: Uint8Array; readonly contentType?: string; /** Bytes back, for a download. JSON is parsed; a file is not. */ readonly rawResponse?: boolean; readonly expectedStatus?: number; readonly redirect?: RequestInit["redirect"]; readonly options?: RequestOptions; } export declare class HttpClient { #private; constructor(options: ClientOptions); /** * What this object looks like when something serialises it. * * Both hooks return the same key-free shape. `toJSON` covers * `JSON.stringify`, the inspect symbol covers `console.log` under Node, and * between them they cover how a credential actually escapes: not through a * deliberate log line, but through an object dumped into a bug report. */ toJSON(): Record; get(path: string, query?: QueryParams, options?: RequestOptions): Promise; post(path: string, body?: unknown, options?: RequestOptions): Promise; /** A queue mutation: exactly one attempt, no redirects, and a 202 acknowledgement. */ postAccepted(path: string, body: unknown, options?: RequestOptions): Promise; /** POST with a file as the body. The type describes the bytes, not JSON. */ postBytes(path: string, bytes: Uint8Array, contentType: string, query?: QueryParams, options?: RequestOptions): Promise; /** GET that returns bytes rather than JSON, for downloading a file. */ getBytes(path: string, query?: QueryParams, options?: RequestOptions): Promise<{ bytes: Uint8Array; contentType: string; filename?: string; }>; patch(path: string, body?: unknown, options?: RequestOptions): Promise; delete(path: string, body?: unknown, options?: RequestOptions): Promise; } /** * Whether this failure is worth another attempt. * * Two questions, and both have to say yes. The first is whether the failure * itself could clear - a 400 fails identically on every attempt, so retrying * spends two more calls to learn the same thing. The second is whether * repeating the REQUEST is safe, which is a different question and the one * that gets skipped. * * For a POST the honest answer is that we usually cannot tell. A connection * that died after the server accepted the request is indistinguishable from * one that died before, so retrying `remember` on a timeout would queue the * same note twice and the user would see it remembered twice. So a POST is * retried only when: * * an idempotency key was given the API deduplicates on it, so the retry * returns the first result rather than * repeating the work * * the status was 429 the request was refused BEFORE it was * processed. That is what a rate limit is, * and it is the one case where "already * succeeded" is not possible * * Every other retryable POST failure - a 500, a 503, a timeout, a reset - is * given back to the caller, who knows whether repeating it is safe and this * package does not. */ export declare function mayRetry(error: PersistMemoryError, request: InternalRequest): boolean;