import { FetchError, FetchOptions } from "ofetch"; import { ErrorResponse, GetResponseContent, IsOperationRequestBodyOptional, MediaType, OperationRequestBodyContent, ResponseObjectMap, SuccessResponse } from "openapi-typescript-helpers"; //#region src/openapi/types.d.ts type FetchResponseData> = SuccessResponse, MediaType>; type FetchResponseError> = FetchError, MediaType>>; type MethodOption = "get" extends keyof P ? { method?: M; } : { method: M; }; type ParamsOption = T extends { parameters?: any; query?: any; } ? Omit & { headers?: T["parameters"]["header"] | HeadersInit; } : Record; type RequestBodyOption = OperationRequestBodyContent extends never ? { body?: never; } : IsOperationRequestBodyOptional extends true ? { body?: OperationRequestBodyContent; } : { body: OperationRequestBodyContent; }; type FilterMethods = { [K in keyof Omit as T[K] extends never | undefined ? never : K]: T[K]; }; type OpenAPIFetchOptions = MethodOption & ParamsOption & RequestBodyOption & Omit; /** * Methods the path declares. `openapi-typescript` gives a path item a key for all * eight verbs and sets the ones the schema leaves out to an optional `never`, so * `keyof` alone would answer with every verb there is. */ type OpenAPIPathMethods = keyof FilterMethods; /** Path or query parameters of an operation, or `never` where it declares none. */ type OpenAPIParameters = Operation extends { parameters: infer Parameters; } ? Kind extends keyof Parameters ? Exclude : never : never; /** * Every type one operation carries, keyed for extraction – `Service<'/pet/{petId}', 'get'>['response']`. * `request` and `response` reach for the same helpers `OpenAPIClient` reaches for, so * both report the same type; a test pins that against a call through an actual client. */ interface OpenAPIEndpoint { path: OpenAPIParameters; query: OpenAPIParameters; /** Request body, `undefined` where the operation declares none or declares it optional. */ request: OperationRequestBodyContent; /** Body of the successful response, for whichever 2xx status and media type the operation declares. */ response: Paths[Path][Method] extends Record ? FetchResponseData : never; /** Response bodies keyed by status code, `undefined` where a status carries no content. */ responses: ResponseObjectMap extends (infer Responses extends Record) ? { [Status in keyof Responses]: GetResponseContent; } : never; /** Path literal including its parameter placeholders, for route builders to consume. */ fullPath: Path; method: Method; /** Raw operation object, carrying metadata such as tags and security requirements. */ operation: Paths[Path][Method]; } type OpenAPIClient = , Methods extends FilterMethods, Method extends Extract | Uppercase>, LowercasedMethod extends Lowercase extends keyof Methods ? Lowercase : never, DefaultMethod extends "get" extends LowercasedMethod ? "get" : LowercasedMethod, ResT = Methods[DefaultMethod] extends Record ? FetchResponseData : never>(url: ReqT, options?: OpenAPIFetchOptions) => Promise; //#endregion export { FetchResponseData, FetchResponseError, FilterMethods, MethodOption, OpenAPIClient, OpenAPIEndpoint, OpenAPIFetchOptions, OpenAPIParameters, OpenAPIPathMethods, ParamsOption, RequestBodyOption };