@azm-library/api-js
v2.0.3
Published
> TODO: description
Downloads
10
Readme
api-js 工具
安装
# 通过 npm 安装
npm i @azm-library/api-js -S
# 通过 yarn 安装
yarn add @azm-library/api-js -S
# 通过 pnpm 安装
pnpm add @azm-library/api-js -S
methods
示例代码
import { createApiService } from '@pagoda/client-helper';
import axios from 'axios';
const http = axios.create();
const createApi = createApiService(http);
createApi({
url: 'xxx',
method: 'get',
}).request({
body: {},
});
API
createApiService
interface ApiConfig {
url: string | ((path: ApiRequestConfig['path']) => string);
method: Method;
cache: boolean;
cacheTime: number;
headers: AxiosRequestConfig['headers'];
axiosConfig: AxiosRequestConfig;
}
interface ApiRequestConfig<T = any> {
headers?: AxiosRequestConfig<T>['headers'];
path?: Record<string, string>;
query?: Record<string, string>;
body?: AxiosRequestConfig<T>['data'];
axiosConfig?: AxiosRequestConfig<T>;
}
class Api {
http: AxiosInstance;
config: ApiConfig;
cache: Record<
string,
{
promise: Promise<any>;
expireTime: number | null;
}
>;
constructor(http: AxiosInstance, config: ApiConfig): Api;
request<T = never, R = AxiosResponse<T>>(requestConfig?: ApiRequestConfig<T>): Promise<R>;
clearCache(): void;
}
function createApiService(http: AxiosInstance): (apiConfig: ApiConfig) => Api;