import type { AxiosError, AxiosInstance, AxiosRequestHeaders, AxiosResponse, AxiosStatic, CreateAxiosDefaults, } from 'axios'; import type { Auth } from '../core/auth'; import type { ServerSentEventsOptions, ServerSentEventsResult, } from '../core/serverSentEvents'; import type { Client as CoreClient, Config as CoreConfig, } from '../core/types'; export interface Config extends Omit, CoreConfig { /** * Axios implementation. You can use this option to provide either an * `AxiosStatic` or an `AxiosInstance`. * * @default axios */ axios?: AxiosStatic | AxiosInstance; /** * Base URL for all requests made by this client. */ baseURL?: T['baseURL']; /** * An object containing any HTTP headers that you want to pre-populate your * `Headers` object with. * * {@link https://developer.mozilla.org/docs/Web/API/Headers/Headers#init See more} */ headers?: | AxiosRequestHeaders | Record< string, | string | number | boolean | (string | number | boolean)[] | null | undefined | unknown >; /** * Throw an error instead of returning it in the response? * * @default false */ throwOnError?: T['throwOnError']; } export interface RequestOptions< TData = unknown, ThrowOnError extends boolean = boolean, Url extends string = string, > extends Config<{ throwOnError: ThrowOnError; }>, Pick< ServerSentEventsOptions, | 'onSseError' | 'onSseEvent' | 'sseDefaultRetryDelay' | 'sseMaxRetryAttempts' | 'sseMaxRetryDelay' > { /** * Any body that you want to add to your request. * * {@link https://developer.mozilla.org/docs/Web/API/fetch#body} */ body?: unknown; path?: Record; query?: Record; /** * Security mechanism(s) to use for the request. */ security?: ReadonlyArray; url: Url; } export interface ClientOptions { baseURL?: string; throwOnError?: boolean; } export type RequestResult< TData = unknown, TError = unknown, ThrowOnError extends boolean = boolean, > = ThrowOnError extends true ? Promise< AxiosResponse< TData extends Record ? TData[keyof TData] : TData > > : Promise< | (AxiosResponse< TData extends Record ? TData[keyof TData] : TData > & { error: undefined }) | (AxiosError< TError extends Record ? TError[keyof TError] : TError > & { data: undefined; error: TError extends Record ? TError[keyof TError] : TError; }) >; type MethodFn = < TData = unknown, TError = unknown, ThrowOnError extends boolean = false, >( options: Omit, 'method'>, ) => RequestResult; type SseFn = < TData = unknown, TError = unknown, ThrowOnError extends boolean = false, >( options: Omit, 'method'>, ) => Promise>; type RequestFn = < TData = unknown, TError = unknown, ThrowOnError extends boolean = false, >( options: Omit, 'method'> & Pick>, 'method'>, ) => RequestResult; type BuildUrlFn = < TData extends { body?: unknown; path?: Record; query?: Record; url: string; }, >( options: TData & Options, ) => string; export type Client = CoreClient< RequestFn, Config, MethodFn, BuildUrlFn, SseFn > & { instance: AxiosInstance; }; /** * The `createClientConfig()` function will be called on client initialization * and the returned object will become the client's initial configuration. * * You may want to initialize your client this way instead of calling * `setConfig()`. This is useful for example if you're using Next.js * to ensure your client always has the correct values. */ export type CreateClientConfig = ( override?: Config, ) => Config & T>; export interface TDataShape { body?: unknown; headers?: unknown; path?: unknown; query?: unknown; url: string; } type OmitKeys = Pick>; export type Options< TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponse = unknown, > = OmitKeys< RequestOptions, 'body' | 'path' | 'query' | 'url' > & ([TData] extends [never] ? unknown : Omit);