import { OperationRequestBodyContent, MediaType, HttpMethod, PathsWithMethod, RequiredKeysOf, Writable, IsOperationRequestBodyOptional, FilterKeys, Readable, SuccessResponse, ResponseObjectMap, ErrorResponse } from 'openapi-typescript-helpers'; /** Options for each client instance */ interface ClientOptions extends Omit { /** set the common root URL for all API requests */ baseUrl?: string; /** custom fetch (defaults to globalThis.fetch) */ fetch?: (input: Request) => Promise; /** custom Request (defaults to globalThis.Request) */ Request?: typeof Request; /** global querySerializer */ querySerializer?: QuerySerializer | QuerySerializerOptions; /** global bodySerializer */ bodySerializer?: BodySerializer; /** global pathSerializer */ pathSerializer?: PathSerializer; headers?: HeadersOptions; /** RequestInit extension object to pass as 2nd argument to fetch when supported (defaults to undefined) */ requestInitExt?: Record; } type HeadersOptions = | Required["headers"] | Record; type QuerySerializer = ( query: T extends { parameters: any } ? NonNullable : Record, ) => string; /** @see https://swagger.io/docs/specification/serialization/#query */ type QuerySerializerOptions = { /** Set serialization for arrays. @see https://swagger.io/docs/specification/serialization/#query */ array?: { /** default: "form" */ style: "form" | "spaceDelimited" | "pipeDelimited"; /** default: true */ explode: boolean; }; /** Set serialization for objects. @see https://swagger.io/docs/specification/serialization/#query */ object?: { /** default: "deepObject" */ style: "form" | "deepObject"; /** default: true */ explode: boolean; }; /** * The `allowReserved` keyword specifies whether the reserved characters * `:/?#[]@!$&'()*+,;=` in parameter values are allowed to be sent as they * are, or should be percent-encoded. By default, allowReserved is `false`, * and reserved characters are percent-encoded. * @see https://swagger.io/docs/specification/serialization/#query */ allowReserved?: boolean; }; type BodySerializer = (body: OperationRequestBodyContent) => any; type PathSerializer = (pathname: string, pathParams: Record) => string; type BodyType = { json: T; text: Awaited>; blob: Awaited>; arrayBuffer: Awaited>; stream: Response["body"]; }; type ParseAs = keyof BodyType; type ParseAsResponse = Options extends { parseAs: ParseAs; } ? BodyType[Options["parseAs"]] : T; interface DefaultParamsOption { params?: { query?: Record; }; } type ParamsOption = T extends { parameters: any; } ? RequiredKeysOf extends never ? { params?: T["parameters"] } : { params: T["parameters"] } : DefaultParamsOption; // Writable strips $Read markers (readOnly properties excluded from request body) type RequestBodyOption = Writable> extends never ? { body?: never } : IsOperationRequestBodyOptional extends true ? { body?: Writable> } : { body: Writable> }; type FetchOptions = RequestOptions & Omit; // Readable strips $Write markers (writeOnly properties excluded from response) type FetchResponse, Options, Media extends MediaType> = | { data: ParseAsResponse, Media>>, Options>; error?: never; response: Response; } | { data?: never; error: Readable, Media>>; response: Response; }; type RequestOptions = ParamsOption & RequestBodyOption & { baseUrl?: string; querySerializer?: QuerySerializer | QuerySerializerOptions; bodySerializer?: BodySerializer; pathSerializer?: PathSerializer; parseAs?: ParseAs; fetch?: ClientOptions["fetch"]; headers?: HeadersOptions; middleware?: Middleware[]; }; type MergedOptions = { baseUrl: string; parseAs: ParseAs; querySerializer: QuerySerializer; bodySerializer: BodySerializer; pathSerializer: PathSerializer; fetch: typeof globalThis.fetch; }; interface MiddlewareRequestParams { query?: Record; header?: Record; path?: Record; cookie?: Record; } interface MiddlewareCallbackParams { /** Current Request object */ request: Request; /** The original OpenAPI schema path (including curly braces) */ readonly schemaPath: string; /** OpenAPI parameters as provided from openapi-fetch */ readonly params: MiddlewareRequestParams; /** Unique ID for this request */ readonly id: string; /** createClient options (read-only) */ readonly options: MergedOptions; } type MiddlewareOnRequest = ( options: MiddlewareCallbackParams, ) => void | Request | Response | undefined | Promise; type MiddlewareOnResponse = ( options: MiddlewareCallbackParams & { response: Response }, ) => void | Response | undefined | Promise; type MiddlewareOnError = ( options: MiddlewareCallbackParams & { error: unknown }, ) => void | Response | Error | Promise; type Middleware = | { onRequest: MiddlewareOnRequest; onResponse?: MiddlewareOnResponse; onError?: MiddlewareOnError; } | { onRequest?: MiddlewareOnRequest; onResponse: MiddlewareOnResponse; onError?: MiddlewareOnError; } | { onRequest?: MiddlewareOnRequest; onResponse?: MiddlewareOnResponse; onError: MiddlewareOnError; }; /** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */ type MaybeOptionalInit = RequiredKeysOf>> extends never ? FetchOptions> | undefined : FetchOptions>; // The final init param to accept. // - Determines if the param is optional or not. // - Performs arbitrary [key: string] addition. // Note: the addition MUST happen after all the inference happens (otherwise TS can’t infer if init is required or not). type InitParam = RequiredKeysOf extends never ? [(Init & { [key: string]: unknown })?] : [Init & { [key: string]: unknown }]; type ClientMethod< Paths extends Record>, Method extends HttpMethod, Media extends MediaType, > = , Init extends MaybeOptionalInit>( url: Path, ...init: InitParam ) => Promise>; type ClientRequestMethod>, Media extends MediaType> = < Method extends HttpMethod, Path extends PathsWithMethod, Init extends MaybeOptionalInit, >( method: Method, url: Path, ...init: InitParam ) => Promise>; type ClientForPath, Media extends MediaType> = { [Method in keyof PathInfo as Uppercase]: >( ...init: InitParam ) => Promise>; }; interface Client { request: ClientRequestMethod; /** Call a GET endpoint */ GET: ClientMethod; /** Call a PUT endpoint */ PUT: ClientMethod; /** Call a POST endpoint */ POST: ClientMethod; /** Call a DELETE endpoint */ DELETE: ClientMethod; /** Call a OPTIONS endpoint */ OPTIONS: ClientMethod; /** Call a HEAD endpoint */ HEAD: ClientMethod; /** Call a PATCH endpoint */ PATCH: ClientMethod; /** Call a TRACE endpoint */ TRACE: ClientMethod; /** Register middleware */ use(...middleware: Middleware[]): void; /** Unregister middleware */ eject(...middleware: Middleware[]): void; } type ClientPathsWithMethod, Method extends HttpMethod> = CreatedClient extends Client ? PathsWithMethod : never; type MethodResponse< CreatedClient extends Client, Method extends HttpMethod, Path extends ClientPathsWithMethod, Options = {}, > = CreatedClient extends Client ? NonNullable["data"]> : never; declare function createClient( clientOptions?: ClientOptions, ): Client; type PathBasedClient, Media extends MediaType = MediaType> = { [Path in keyof Paths]: ClientForPath; }; declare function wrapAsPathBasedClient( client: Client, ): PathBasedClient; declare function createPathBasedClient( clientOptions?: ClientOptions, ): PathBasedClient; /** Serialize primitive params to string */ declare function serializePrimitiveParam( name: string, value: string, options?: { allowReserved?: boolean }, ): string; /** Serialize object param to string */ declare function serializeObjectParam( name: string, value: Record, options: { style: "simple" | "label" | "matrix" | "form" | "deepObject"; explode: boolean; allowReserved?: boolean; }, ): string; /** Serialize array param to string */ declare function serializeArrayParam( name: string, value: unknown[], options: { style: "simple" | "label" | "matrix" | "form" | "spaceDelimited" | "pipeDelimited"; explode: boolean; allowReserved?: boolean; }, ): string; /** Serialize query params to string */ declare function createQuerySerializer( options?: QuerySerializerOptions, ): (queryParams: T) => string; /** * Handle different OpenAPI 3.x serialization styles * @type {import("./index.js").defaultPathSerializer} * @see https://swagger.io/docs/specification/serialization/#path */ declare function defaultPathSerializer(pathname: string, pathParams: Record): string; /** Serialize body object to string */ declare function defaultBodySerializer(body: T): string; /** Construct URL string from baseUrl and handle path and query params */ declare function createFinalURL( pathname: string, options: { baseUrl: string; params: { query?: Record; path?: Record; }; querySerializer: QuerySerializer; pathSerializer: PathSerializer; }, ): string; /** Merge headers a and b, with b taking priority */ declare function mergeHeaders(...allHeaders: (HeadersOptions | undefined)[]): Headers; /** Remove trailing slash from url */ declare function removeTrailingSlash(url: string): string; // @ts-ignore export = createClient; export { createFinalURL, createPathBasedClient, createQuerySerializer, defaultBodySerializer, defaultPathSerializer, mergeHeaders, removeTrailingSlash, serializeArrayParam, serializeObjectParam, serializePrimitiveParam, wrapAsPathBasedClient }; export type { BodySerializer, Client, ClientForPath, ClientMethod, ClientOptions, ClientPathsWithMethod, ClientRequestMethod, DefaultParamsOption, FetchOptions, FetchResponse, HeadersOptions, MaybeOptionalInit, MergedOptions, MethodResponse, Middleware, MiddlewareCallbackParams, MiddlewareRequestParams, ParamsOption, ParseAs, ParseAsResponse, PathBasedClient, PathSerializer, QuerySerializer, QuerySerializerOptions, RequestBodyOption, RequestOptions };