import type { AxiosInstance, AxiosResponse, AxiosRequestConfig, ResponseType } from 'axios'; import qs from 'qs'; import type { ConditionalKeys, Opaque } from 'type-fest'; import { interpolateParams } from './interpolate-params'; import urljoin from 'url-join'; // @NOTE: [never] instead of never, see https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887 type EverOr = [A] extends [never] ? B : A; type IsRequired = undefined extends T ? false : true; type RequiredProperties = { [P in keyof T]-?: undefined extends T[P] ? never : P; }[keyof T]; type HasRequiredProperties = RequiredProperties extends never ? false : true; // === // // @NOTE: Mostly follows OpenAPI and Axios supported methods // @TODO: What about TRACE? It is supported by OpenAPI, but not by Axios type Method = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'PATCH'; // @NOTE: Mostly follows OpenAPI names for these things type Field = 'query' | 'params' | 'body' | 'response'; // @NOTE: Follows Axios signatures // @TODO: What about TRACE? It is supported by OpenAPI, but not by Axios type HasBody = TMethod extends 'GET' | 'HEAD' | 'DELETE' | 'OPTIONS' ? false : true; type Scheme = { version: '1'; routes: { [route in string]: { [method in Method]?: { [field in Field]?: unknown; }; }; }; }; type AvailableRoutes = ConditionalKeys< TApi['routes'], { [method in TMethod]: unknown } >; type FieldLut< TApi extends Scheme, TMethod extends Method, TRoute extends AvailableRoutes, TField extends Field, > = TRoute extends keyof TApi['routes'] ? TMethod extends keyof TApi['routes'][TRoute] ? TField extends keyof TApi['routes'][TRoute][TMethod] ? TApi['routes'][TRoute][TMethod][TField] : never : never : never; type RouteLut< TApi extends Scheme, TMethod extends Method, TRoute extends AvailableRoutes, > = TRoute extends keyof TApi['routes'] ? TMethod extends keyof TApi['routes'][TRoute] ? TApi['routes'][TRoute][TMethod] : never : never; export type Config> = EverOr< Pick, keyof RouteLut & ('params' | 'query' | 'responseType')>, undefined >; export type Body> = EverOr< FieldLut, undefined >; export type Response< TApi extends Scheme, TMethod extends Method, TRoute extends AvailableRoutes, TStrict, > = TStrict extends true ? EverOr, undefined> : any; type Url> = Opaque< string, [TApi, TMethod, TRoute] >; type LaxConfig = { params?: { [key in string]: any }; query?: { [key in string]: any }; responseType?: ResponseType; axios?: AxiosRequestConfig; qs?: qs.IStringifyOptions; }; type RequestConfig> = Config< TApi, TMethod, TRoute > & { axios?: AxiosRequestConfig; qs?: qs.IStringifyOptions }; type LaxRequestConfig = LaxConfig & { axios?: AxiosRequestConfig }; type InferredRouteArg = TStrict extends true ? TRoute : TRoute | string; type RequestArgs< TApi extends Scheme, TMethod extends Method, TRoute extends AvailableRoutes, > = HasBody extends true ? HasRequiredProperties> extends true ? Parameters<(body: Body, config: RequestConfig) => void> : IsRequired> extends true ? Parameters<(body: Body, config?: RequestConfig) => void> : Parameters<(body?: Body, config?: RequestConfig) => void> : HasRequiredProperties> extends true ? Parameters<(config: RequestConfig) => void> : Parameters<(config?: RequestConfig) => void>; type LaxRequestArgs = HasBody extends true ? Parameters<(body?: any, config?: LaxRequestConfig) => void> : Parameters<(config?: LaxRequestConfig) => void>; type InferredRequestArgs< TApi extends Scheme, TMethod extends Method, TRoute extends AvailableRoutes, TStrict extends boolean, > = TStrict extends true ? RequestArgs : LaxRequestArgs; type UrlArgs< TApi extends Scheme, TMethod extends Method, TRoute extends AvailableRoutes, > = HasRequiredProperties> extends true ? Parameters<(config: RequestConfig) => void> : Parameters<(config?: RequestConfig) => void>; type LaxUrlArgs = Parameters<(config?: LaxConfig) => void>; type InferredUrlArgs< TApi extends Scheme, TMethod extends Method, TRoute extends AvailableRoutes, TStrict extends boolean, > = TStrict extends true ? UrlArgs : LaxUrlArgs; export type TaxiosOptions = { qs?: qs.IStringifyOptions; }; export class Taxios { private axios: AxiosInstance; private qs?: qs.IStringifyOptions; constructor(axios: AxiosInstance, opts?: TaxiosOptions) { this.axios = axios; this.qs = opts?.qs; } get unsafe(): Taxios { return this; } url>( method: TMethod, route: InferredRouteArg, ...args: InferredUrlArgs ): Url { let [url] = this.prepare(method, route, ...args); const baseUrl = this.axios.defaults.baseURL; if (baseUrl) { url = urljoin(baseUrl, url) as Opaque; } return url; } // @TODO: Cover with tests // @TODO: https://swagger.io/docs/specification/serialization/ protected prepare>( _method: TMethod, route: InferredRouteArg, ...args: InferredUrlArgs ): [Url, AxiosRequestConfig] { let urlString = route as string; let axiosConfig: AxiosRequestConfig = {}; // const [rawConfig] = args as [LaxConfig]; if (rawConfig) { const rawParams = rawConfig.params; if (rawParams) { urlString = interpolateParams(urlString, rawParams); } const rawQuery = rawConfig.query; if (rawQuery) { urlString += '?' + qs.stringify(rawQuery, rawConfig.qs || this.qs); } const rawAxiosConfig = rawConfig.axios; if (rawAxiosConfig) { axiosConfig = { ...rawAxiosConfig }; } const rawResponseType = rawConfig.responseType; if (rawResponseType) { axiosConfig.responseType = rawResponseType; } } const url = urlString as Opaque; return [url, axiosConfig]; } async get>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise>> { const [url, axiosConfig] = this.prepare('GET', route, ...args); return await this.axios.get(url, axiosConfig); } async $get>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise> { const [url, axiosConfig] = this.prepare('GET', route, ...args); return await this.axios .get(url, axiosConfig) .then((response) => response.data as Response); } async head>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise>> { const [url, axiosConfig] = this.prepare('HEAD', route, ...args); return await this.axios.head(url, axiosConfig); } async $head>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise> { const [url, axiosConfig] = this.prepare('HEAD', route, ...args); return await this.axios .head(url, axiosConfig) .then((response) => response.data as Response); } async post>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise>> { const [body, config] = args; const prepareArgs = [config] as InferredUrlArgs; const [url, axiosConfig] = this.prepare('POST', route, ...prepareArgs); return await this.axios.post(url, body, axiosConfig); } async $post>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise> { const [body, config] = args; const prepareArgs = [config] as InferredUrlArgs; const [url, axiosConfig] = this.prepare('POST', route, ...prepareArgs); return await this.axios .post(url, body, axiosConfig) .then((response) => response.data as Response); } async put>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise>> { const [body, config] = args; const prepareArgs = [config] as InferredUrlArgs; const [url, axiosConfig] = this.prepare('PUT', route, ...prepareArgs); return await this.axios.put(url, body, axiosConfig); } async $put>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise> { const [body, config] = args; const prepareArgs = [config] as InferredUrlArgs; const [url, axiosConfig] = this.prepare('PUT', route, ...prepareArgs); return await this.axios .put(url, body, axiosConfig) .then((response) => response.data as Response); } async delete>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise>> { const [url, axiosConfig] = this.prepare('DELETE', route, ...args); return await this.axios.delete(url, axiosConfig); } async $delete>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise> { const [url, axiosConfig] = this.prepare('DELETE', route, ...args); return await this.axios .delete(url, axiosConfig) .then((response) => response.data as Response); } async options>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise>> { const [url, axiosConfig] = this.prepare('OPTIONS', route, ...args); return await this.axios.options(url, axiosConfig); } async $options>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise> { const [url, axiosConfig] = this.prepare('OPTIONS', route, ...args); return await this.axios .options(url, axiosConfig) .then((response) => response.data as Response); } async patch>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise>> { const [body, config] = args; const prepareArgs = [config] as InferredUrlArgs; const [url, axiosConfig] = this.prepare('PATCH', route, ...prepareArgs); return await this.axios.patch(url, body, axiosConfig); } async $patch>( route: InferredRouteArg, ...args: InferredRequestArgs ): Promise> { const [body, config] = args; const prepareArgs = [config] as InferredUrlArgs; const [url, axiosConfig] = this.prepare('PATCH', route, ...prepareArgs); return await this.axios .patch(url, body, axiosConfig) .then((response) => response.data as Response); } }