@aiwheels/node-extra
v0.0.2
Published
> TODO: node 工具扩展
Downloads
3
Readme
api-js 工具
安装
# 通过 npm 安装
npm i @aiwheels/axios-api-js -S
# 通过 yarn 安装
yarn add @aiwheels/axios-api-js -S
# 通过 pnpm 安装
pnpm add @aiwheels/axios-api-js -S
methods
示例代码
import { createApiService } from '@aiwheels/axios-api-js';
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;