import type { StandardSchemaV1 } from '@standard-schema/spec' export type KeyOf = O extends unknown ? keyof O : never export type DistributiveOmit< TObject extends object, TKey extends KeyOf | (string & {}), > = TObject extends unknown ? Omit : never type IsNull = [T] extends [null] ? true : false type IsUnknown = unknown extends T // `T` can be `unknown` or `any` ? IsNull extends false // `any` can be `null`, but `unknown` can't be ? true : false : false export type MaybePromise = T | Promise type JsonPrimitive = string | number | boolean | null | undefined export type JsonifiableObject = Record export type JsonifiableArray = | Array | ReadonlyArray> | ReadonlyArray export type MinFetchFn = ( input: Request, options?: any, ctx?: any, ) => Promise type ParseResponse = ( response: Response, request: Request, ) => MaybePromise type ParseRejected = (response: Response, request: Request) => any type SerializeBody = (body: TRawBody) => BodyInit | null | undefined export type SerializeParams = (params: Params) => string export type Params = Record export type HeadersObject = Record type Method = | 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'HEAD' | (string & {}) type BaseOptions = DistributiveOmit< NonNullable[1]>, 'body' | 'headers' | 'method' > & {} type OnRetry = (context: { response: Response | undefined error: unknown request: Request attempt: number }) => MaybePromise type RetryWhen = (context: { response: Response | undefined error: unknown request: Request }) => MaybePromise type RetryAttempts = | number | ((context: { request: Request }) => MaybePromise) type RetryDelay = | number | ((context: { response: Response | undefined error: unknown request: Request attempt: number }) => MaybePromise) export type RetryOptions = { /** * The number of attempts to make before giving up */ attempts?: RetryAttempts /** * The delay before retrying */ delay?: RetryDelay /** * Function to determine if a retry attempt should be made */ when?: RetryWhen } export type ResponseStreamingEvent = { /** The last streamed chunk */ chunk: Uint8Array /** Total bytes, read from the Response "Content-Length" header */ totalBytes: number | undefined /** Transferred bytes */ transferredBytes: number } export type RequestStreamingEvent = { /** The last streamed chunk */ chunk: Uint8Array /** Total bytes, read from the Request "Content-Length" header or from the Request body */ totalBytes: number /** Transferred bytes */ transferredBytes: number } export type DefaultRawBody = BodyInit | JsonifiableObject | JsonifiableArray export type FallbackOptions = { parseRejected: ParseRejected parseResponse: ParseResponse reject: (response: Response) => MaybePromise retry: Required serializeParams: SerializeParams serializeBody: SerializeBody } export type GetDefaultParsedData = TDefaultOptions extends DefaultOptions ? U : never export type GetDefaultRawBody = TDefaultOptions extends DefaultOptions ? IsUnknown extends true ? DefaultRawBody : U : never /** * Default configuration options for the fetch client */ export type DefaultOptions< TFetchFn extends MinFetchFn, TDefaultParsedData, TDefaultRawBody, > = BaseOptions & { /** Base URL to prepend to all request URLs */ baseUrl?: string /** Request headers to be sent with each request */ headers?: HeadersInit | HeadersObject /** HTTP method to use for the request */ method?: Method /** Executed when the request fails */ onError?: (error: unknown, request: Request) => MaybePromise /** Executed before the request is made */ onRequest?: (request: Request) => MaybePromise /** Executed when a response is received, once all retries are completed. Not executed when the fetch itself fails (e.g. network error) */ onResponse?: (response: Response, request: Request) => MaybePromise /** Executed before each retry */ onRetry?: OnRetry /** Executed when the request succeeds */ onSuccess?: (data: any, request: Request) => MaybePromise /** URL parameters to be serialized and appended to the URL */ params?: Params /** Function to parse response errors */ parseRejected?: ParseRejected /** Function to parse the response data */ parseResponse?: ParseResponse /** Function to determine if a response should throw an error */ reject?: (response: Response) => MaybePromise /** The default retry options. Will be merged with the fetcher options */ retry?: RetryOptions /** Function to serialize request body. Restrict the valid `body` type by typing its first argument. */ serializeBody?: SerializeBody /** Function to serialize URL parameters */ serializeParams?: SerializeParams /** AbortSignal to cancel the request */ signal?: AbortSignal /** Request timeout in milliseconds */ timeout?: number } /** * Options for individual fetch requests */ export type FetcherOptions< TFetchFn extends MinFetchFn, TSchema extends StandardSchemaV1, TParsedData, TRawBody, > = BaseOptions & { /** Base URL to prepend to the request URL */ baseUrl?: string /** Request body data */ body?: NoInfer | null | undefined /** Request headers */ headers?: HeadersInit | HeadersObject /** HTTP method */ method?: Method /** Executed when the request fails */ onError?: (error: unknown, request: Request) => MaybePromise /** Executed before the request is made */ onRequest?: (request: Request) => MaybePromise /** Executed each time a chunk of the request stream is sent */ onRequestStreaming?: ( event: RequestStreamingEvent, request: Request, ) => MaybePromise /** Executed each time a chunk of the response stream is received */ onResponseStreaming?: ( event: ResponseStreamingEvent, response: Response, ) => MaybePromise /** Executed when a response is received, once all retries are completed. Not executed when the fetch itself fails (e.g. network error) */ onResponse?: (response: Response, request: Request) => MaybePromise /** Executed before each retry */ onRetry?: OnRetry /** Executed when the request succeeds */ onSuccess?: ( data: NoInfer, request: Request, ) => MaybePromise /** URL parameters */ params?: Params /** Function to parse response errors */ parseRejected?: ParseRejected /** Function to parse the response data */ parseResponse?: ParseResponse /** Function to determine if a response should throw an error */ reject?: (response: Response) => MaybePromise /** The fetch retry options. Merged with the default retry options */ retry?: RetryOptions /** JSON Schema for request/response validation */ schema?: TSchema /** Function to serialize request body. Restrict the valid `body` type by typing its first argument. */ serializeBody?: SerializeBody /** Function to serialize URL parameters */ serializeParams?: SerializeParams /** AbortSignal to cancel the request */ signal?: AbortSignal /** Request timeout in milliseconds */ timeout?: number } export type UpFetch< TFetchFn extends MinFetchFn = typeof fetch, TDefaultOptions extends DefaultOptions< MinFetchFn, any, any > = DefaultOptions, > = < TParsedData = GetDefaultParsedData, TSchema extends StandardSchemaV1< TParsedData, any > = StandardSchemaV1, TRawBody = GetDefaultRawBody, >( input: Parameters[0], options?: FetcherOptions, ctx?: Parameters[2], ) => Promise>