/** biome-ignore-all lint/suspicious/noConfusingVoidType: its fine */ import type { StandardSchemaV1 } from '@standard-schema/spec' import type { RetryContext } from 'p-retry' import type { Jsonifiable } from 'type-fest' export type RequestInput = URL | string export interface PollOptions { /** * The delay between polling attempts in milliseconds. * * @default 1000 */ interval?: number | ((context: PollContext) => number | Promise) /** * The maximum number of pollable responses before returning the last response. * * @default 10 */ limit?: number /** * The HTTP status codes that should trigger polling. * Status codes in the range 200-299. * * @default [202] */ statusCodes?: number[] /** * Called after built-in checks pass, before polling continues. * * Returning false stops polling and returns the current response. * * @param context - The context of the poll * @returns - Whether to continue polling */ shouldPoll?: (context: PollContext) => boolean | Promise } export interface PollContext { attempt: number response: Response request: Request options: RequestOptions } export interface RetryOptions { /** * The HTTP status codes to retry on. * Status codes in the range 400-599. * * @default [408, 413, 429, 500, 502, 503, 504] */ statusCodes?: number[] /** * The status codes to retry after * * Request will wait until the date, timeout, or timestamp given in the Retry-After header has passed to retry the request. If Retry-After is missing, the non-standard RateLimit-Reset header is used in its place as a fallback. If the provided status code is not in the list, the Retry-After header will be ignored. * * @default [413, 429, 503] */ afterStatusCodes?: number[] /** * The methods to retry * * @default ['get', 'put', 'head', 'delete', 'options', 'trace'] */ methods?: string[] /** * Called after built-in checks pass, before retrying. Return false to stop retrying. * * @param context - The context of the retry * @returns - Whether to retry the request */ shouldRetry?: (context: RetryContext) => boolean | Promise /** * Whether to [unref](https://nodejs.org/api/timers.html#timers_unref) the setTimeout's. * @default false */ unref?: boolean | undefined /** * The maximum time (in milliseconds) that the retried operation is allowed to run. * @default Infinity */ maxRetryTime?: number | undefined /** * The maximum amount of times to retry the operation. * @default 2 */ retries?: number | undefined /** * The exponential factor to use. * @default 2 */ factor?: number | undefined /** * The number of milliseconds before starting the first retry. * @default 1000 */ minTimeout?: number | undefined /** * The maximum number of milliseconds between two retries. * @default Infinity */ maxTimeout?: number | undefined /** * Randomizes the timeouts by multiplying a factor between 1-2. * @default false */ randomize?: boolean | undefined } export interface RequestOptions { fetch?: typeof globalThis.fetch redirect?: RequestRedirect body?: BodyInit | null method?: string headers?: HeadersInit signal?: AbortSignal keepalive?: boolean /** * Timeout in milliseconds for the request, `false` to disable timeout * * @default 5000 */ timeout?: number | false /** * Retry failed requests. * * Set to `true` to use the default retry options. * * @default false */ retry?: RetryOptions | boolean /** * Poll responses that match the configured status codes. * * Set to `true` to use the default polling options. * * @default false */ poll?: PollOptions | boolean json?: Jsonifiable onResponse?: ( response: Response, request: Request ) => void | Response | Promise } export interface JSONRequestOptions { fetch?: typeof globalThis.fetch redirect?: RequestRedirect body?: Jsonifiable | null method?: string headers?: HeadersInit signal?: AbortSignal keepalive?: boolean /** * Timeout in milliseconds for the request, `false` to disable timeout * * @default 5000 */ timeout?: number | false /** * Retry failed requests. * * Set to `true` to use the default retry options. * * @default false */ retry?: RetryOptions | boolean /** * Poll responses that match the configured status codes. * * Set to `true` to use the default polling options. * * @default false */ poll?: PollOptions | boolean schema?: StandardSchemaV1 onResponse?: ( response: Response, request: Request ) => void | Response | Promise } /** * Generic result with error */ export type MaybeResult = | { error: ErrorType result?: undefined } | { result: ResultType error?: undefined }