import type { OperationError } from './OperationError.js'; /** * The `RequestFn` is the wrapper on top of the `fetch` function that simplifies * the process of making requests to the API using Qraft. * using OpenAPI Operation schemas generated by Qraft CLI. * * @param schema - The schema of the operation to be performed. It includes the OpenAPI path, HTTP method and other details. * @param requestInfo - The information required to make the request. It includes parameters, headers, body, etc. * @param options - The options for the request. It includes custom `urlSerializer`, `bodySerializer`, and fetch function. * * @returns Returns a promise that resolves with the response of the request. */ export interface RequestFn { ( schema: OperationSchema, requestInfo: RequestFnInfo, options?: RequestFnOptions ): Promise>; } export interface OperationSchema { /** * Operation path * @example /user/{id} */ readonly url: string; //todo::rename to `path` /** * Operation method */ readonly method: | 'get' | 'put' | 'post' | 'patch' | 'delete' | 'options' | 'head' | 'trace'; /** * Media type of the request body * @example application/json */ readonly mediaType?: string[]; readonly security?: string[]; } export interface RequestFnInfo extends Omit< RequestInit, 'headers' | 'method' | 'body' | 'signal' > { /** * Request headers * @example { 'X-Auth': '123' } */ readonly headers?: HeadersOptions; /** * Base URL to use for the request * @example 'https://api.example.com' */ readonly baseUrl?: string; /** * OpenAPI parameters * @example * ```ts * { path: {id: 1}, query: {search: 'hello'} } * ``` */ readonly parameters?: { readonly path?: Record; readonly header?: Record; readonly query?: Record; }; /** * Request body * @example { name: 'John' } */ readonly body?: BodyInit | Record | null; /** * TanStack Query Meta */ readonly meta?: Record; /** An AbortSignal to set request's signal. */ readonly signal?: AbortSignal | null; } /** * The `RequestFnResponse` is the response of the `RequestFn` function. */ export type RequestFnResponse = | { data: TData; response: Response; error?: undefined; } | { // server error data?: undefined; response: Response; error: OperationError; } | { // network error data?: undefined; response?: undefined; error: Error; }; export interface RequestFnOptions { urlSerializer?: UrlSerializer; bodySerializer?: BodySerializer; fetch?: typeof fetch; } type BodySerializer = ( schema: OperationSchema, body: RequestFnInfo['body'] ) => | { body: string | FormData | Blob; headers: HeadersOptions; } | undefined; type UrlSerializer = (schema: OperationSchema, info: RequestFnInfo) => string; // To have definitely typed headers without a conversion to stings export type HeadersOptions = | HeadersInit | Record;