@azm-library/api-js
TypeScript icon, indicating that this package has built-in type declarations

2.0.3 • Public • Published

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;

Readme

Keywords

none

Package Sidebar

Install

npm i @azm-library/api-js

Weekly Downloads

1

Version

2.0.3

License

ISC

Unpacked Size

54.7 kB

Total Files

8

Last publish

Collaborators

  • aaron-zm