@spec2ts/openapi-client
Version:
Utility to convert OpenAPI v3 specifications to Typescript HTTP client using TypeScript native compiler
82 lines (81 loc) • 3.79 kB
TypeScript
export declare const defaults: RequestOptions;
export declare const servers: {};
export type RequestOptions = {
baseUrl?: string;
fetch?: typeof fetch;
headers?: Record<string, string | undefined>;
} & Omit<RequestInit, "body" | "headers">;
export type ApiResponse<T> = {
status: number;
statusText: string;
headers: Record<string, string>;
data: T;
};
type Encoders = Array<(s: string) => string>;
type TagFunction = (strings: TemplateStringsArray, ...values: any[]) => string;
type FetchRequestOptions = RequestOptions & {
body?: string | FormData;
};
type JsonRequestOptions = RequestOptions & {
body: unknown;
};
type FormRequestOptions<T extends Record<string, unknown>> = RequestOptions & {
body: T;
};
type MultipartRequestOptions = RequestOptions & {
body: Record<string, any>;
};
/** Utilities functions */
export declare const _: {
encodeReserved: (typeof encodeURI)[];
allowReserved: (typeof encodeURI)[];
/** Deeply remove all properties with undefined values. */
stripUndefined<T extends Record<string, U | undefined>, U>(obj?: T | undefined): Record<string, U> | undefined;
isEmpty(v: unknown): boolean;
/** Creates a tag-function to encode template strings with the given encoders. */
encode(encoders: Encoders, delimiter?: string): TagFunction;
/** Separate array values by the given delimiter. */
delimited(delimiter?: string): (params: Record<string, any>, encoders?: Encoders) => string;
/** Join URLs parts. */
joinUrl(...parts: Array<string | undefined>): string;
};
/** Functions to serialize query parameters in different styles. */
export declare const QS: {
/** Join params using an ampersand and prepends a questionmark if not empty. */
query(...params: string[]): string;
/**
* Serializes nested objects according to the `deepObject` style specified in
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#style-values
*/
deep(params: Record<string, any>, [k, v]?: (typeof encodeURI)[]): string;
/**
* Property values of type array or object generate separate parameters
* for each value of the array, or key-value-pair of the map.
* For other types of properties this property has no effect.
* See https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#encoding-object
*/
explode(params: Record<string, any>, encoders?: (typeof encodeURI)[]): string;
form: (params: Record<string, any>, encoders?: Encoders) => string;
pipe: (params: Record<string, any>, encoders?: Encoders) => string;
space: (params: Record<string, any>, encoders?: Encoders) => string;
};
/** Http request base methods. */
export declare const http: {
fetch(url: string, req?: FetchRequestOptions): Promise<ApiResponse<string | undefined>>;
fetchJson(url: string, req?: FetchRequestOptions): Promise<ApiResponse<any>>;
fetchVoid(url: string, req?: FetchRequestOptions): Promise<ApiResponse<undefined>>;
json({ body, headers, ...req }: JsonRequestOptions): FetchRequestOptions;
form<T extends Record<string, unknown>>({ body, headers, ...req }: FormRequestOptions<T>): FetchRequestOptions;
multipart({ body, ...req }: MultipartRequestOptions): FetchRequestOptions;
headers(headers: Headers): Record<string, string>;
};
export declare class HttpError extends Error {
status: number;
statusText: string;
headers: Record<string, string>;
data?: Record<string, unknown>;
constructor(status: number, statusText: string, url: string, headers: Headers, text?: string);
}
/** Utility Type to extract returns type from a method. */
export type ApiResult<Fn> = Fn extends (...args: any) => Promise<ApiResponse<infer T>> ? T : never;
export {};