UNPKG

@kubb/plugin-client

Version:

API client generator plugin for Kubb, creating type-safe HTTP clients (Axios, Fetch) from OpenAPI specifications for making API requests.

1 lines 2.58 kB
{"version":3,"file":"axios.cjs","names":["_config: Partial<RequestConfig>"],"sources":["../../src/clients/axios.ts"],"sourcesContent":["import type { AxiosError, AxiosHeaders, AxiosRequestConfig, AxiosResponse } from 'axios'\nimport axios from 'axios'\n\ndeclare const AXIOS_BASE: string\ndeclare const AXIOS_HEADERS: string\n\n/**\n * Subset of AxiosRequestConfig\n */\nexport type RequestConfig<TData = unknown> = {\n baseURL?: string\n url?: string\n method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'\n params?: unknown\n data?: TData | FormData\n responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'\n signal?: AbortSignal\n validateStatus?: (status: number) => boolean\n headers?: AxiosRequestConfig['headers']\n paramsSerializer?: AxiosRequestConfig['paramsSerializer']\n}\n\n/**\n * Subset of AxiosResponse\n */\nexport type ResponseConfig<TData = unknown> = {\n data: TData\n status: number\n statusText: string\n headers: AxiosResponse['headers']\n}\n\nexport type ResponseErrorConfig<TError = unknown> = AxiosError<TError>\n\nlet _config: Partial<RequestConfig> = {\n baseURL: typeof AXIOS_BASE !== 'undefined' ? AXIOS_BASE : undefined,\n headers: typeof AXIOS_HEADERS !== 'undefined' ? (JSON.parse(AXIOS_HEADERS) as AxiosHeaders) : undefined,\n}\n\nexport const getConfig = () => _config\n\nexport const setConfig = (config: RequestConfig) => {\n _config = config\n return getConfig()\n}\n\nexport const axiosInstance = axios.create(getConfig())\n\nexport const client = async <TResponseData, TError = unknown, TRequestData = unknown>(\n config: RequestConfig<TRequestData>,\n): Promise<ResponseConfig<TResponseData>> => {\n const globalConfig = getConfig()\n\n return axiosInstance\n .request<TResponseData, ResponseConfig<TResponseData>>({\n ...globalConfig,\n ...config,\n headers: {\n ...globalConfig.headers,\n ...config.headers,\n },\n })\n .catch((e: AxiosError<TError>) => {\n throw e\n })\n}\n\nclient.getConfig = getConfig\nclient.setConfig = setConfig\n\nexport default client\n"],"mappings":";;;;;;AAkCA,IAAIA,UAAkC;CACpC,SAAS,OAAO,eAAe,cAAc,aAAa;CAC1D,SAAS,OAAO,kBAAkB,cAAe,KAAK,MAAM,cAAc,GAAoB;CAC/F;AAED,MAAa,kBAAkB;AAE/B,MAAa,aAAa,WAA0B;AAClD,WAAU;AACV,QAAO,WAAW;;AAGpB,MAAa,gBAAgB,cAAM,OAAO,WAAW,CAAC;AAEtD,MAAa,SAAS,OACpB,WAC2C;CAC3C,MAAM,eAAe,WAAW;AAEhC,QAAO,cACJ,QAAsD;EACrD,GAAG;EACH,GAAG;EACH,SAAS;GACP,GAAG,aAAa;GAChB,GAAG,OAAO;GACX;EACF,CAAC,CACD,OAAO,MAA0B;AAChC,QAAM;GACN;;AAGN,OAAO,YAAY;AACnB,OAAO,YAAY;AAEnB,oBAAe"}