0.2.2 • Published 4 years ago

@szgc/plt-request v0.2.2

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

@szgc/plt-request

安装

npm i @szgc/plt-request -S

使用

引入

import request from "@szgc/plt-socket";

初始化

调用 request.init 进行 request 初始化

/**
 * request对象初始化
 * @param {object} options
 * @param {object} options.config 可用属性参照AxiosRequestConfig
 * @param {function} options.reqFilter AxiosInstance.interceptors.request配置
 * @param {function} options.resFilter AxiosInstance.interceptors.response配置
 * @param {function} options.onFetchStart 请求开始的统一回调
 * @param {function} options.onFetchEnd 请求结束的统一回调
**/
request.init(
  (options = {
    config: {
      baseURL : '/',
      timeout : 1000,
      ... // 其他属性,参参照AxiosRequestConfig
    },
    reqFilter(config) {
      return config;
    },
    resFilter(res) {
      return res.data;
    },
    onFetchStart() {
      // do something
    },
    onFetchEnd() {
      // do something
    }
  }) : void;
);

接口调用

  • 单接口调用
const result = await request({
  url : 'xxx/xxxx/action.do',
  method : 'get',
  ...
  // 参照AxiosRequestCofing
});
  • 批量接口调用

批量接口调用时,将多接口参数按照多参数的形式传入 request,返回结果将以入参的顺序以数组的形式返回。当某一个接口抛出异常时,整个批量请求被放弃,被返回异常。

try {
  const [res1, res2, res3] = await request(
    {
      url: "aaa/get-data.do" // 返回结果 res1
    },
    {
      url: "aaa/get-data.do" // 返回结果 res2
    },
    {
      url: "aaa/get-data.do" // 返回结果 res3
    }
  );
  // do something
} catch (e) {
  // do something
}
0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago