1.0.2 • Published 3 years ago

pp-axios v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

接口请求工具

介绍

基于axios封装,提供拦截器配置、暴露原生axios等功能

使用场景

一般在h5、pc等web开发场景下,涉及http接口请求、多域名接口请求时使用,通过集中传入配置,统一进行业务请求的管理。

最新版本

v1.0.2

在项目中引入

npm包形式

// ESModule 规范
import Request from 'pp-axios';

// commonJs 规范
const Request = require('pp-axios').default;

// axios初始化:
axiosEnhance = new Request({
  domain: {
    xxxx: "https://xxxx",
  },
  interceptors: {
    xxxx: {
      reqInterceptors: {
        success: (config) => {
          return config
        },
        error: (err) => {
          return err
        }
      },
      resInterceptors: {
        success: (config) => {
          return config
        },
        error: (err) => {
          return err
        }
      },
      needGlobalInterceptors: true
    },
    glob: {
      reqInterceptors: {
        success: (config) => {
          return config
        },
        error: (err) => {
          return err
        }
      },

      resInterceptors: {
        success: (config) => {
          return config
        },
        error: (err) => {
          return err
        }
      },
    }
  },
})

初始化参数列表

参数类型是否必填说明默认值
domainobject所有域名集合
interceptorsobject全局拦截器与单个域名拦截器
paramsFilterboolean发送请求时过滤null、空字符串、undefinedtrue

interceptors

参数类型是否必填说明默认值
xxxxobject单个域名拦截器,参数名与domain 对象里的属性对应。
globobject全局拦截器配置全局拦截器与单个域名拦截器

reqInterceptors 与 resInterceptors 参数一致

参数类型是否必填说明默认值
successfunction拦截器成功回调
errorfunction拦截器失败回调

API

参数类型
requestsfunction
optionsfunction
getfunction
deletefunction
headfunction
postfunction
putfunction
patchfunction
axiosfunction
// requests 方法对axios的request方法进行封装
// 创建一个请求函数,参数继承axios的请求配置,并且追加了business 与 resType。
// 这里只列举 axios 几个常用的字段,更多字段请查阅axios 文档 http://www.axios-js.com/zh-cn/docs/#axios-request-config
// method 默认为get
axiosEnhance.request({
  business: "xxxx",
  resType: 0,
  url: "/xxx",
});

// get、post 等等方法对axios原来的方法进行封装,options追加了business 字段,可以自动对应damain里的baseurl,拦截器等配置
// 如果不在options里 传business,则会使用axios默认的实例,并且其他配置需要自己配置
// 使用方法与参数与axios原生提供一致 http://www.axios-js.com/zh-cn/docs/#axios-get-url-config-1
axiosEnhance.get(url: "/xxx", {
  business: "xxxx",
  params: {}
});

requests 参数列表

参数类型是否必填说明默认值
businessFunction请求域名key,与domain 中属性名对应
resTypenumber0:返回服务器响应以及https状态码等等 1:返回服务器响应 2:返回服务器响应中的data字段,没有的话返回所有字段1
resSuccessCodenumber服务端请求成功的code,只有resType为 2 时需要200

版本记录

npm模块

@ppRequest1.0.1
 - 正式1.0.1版本

拦截器

  • 全局拦截器:所有域名都会执行的拦截器函数,与axios 内置的拦截器对应。
  • 局部拦截器:只有指定的域名才会执行的拦截器。

  • 执行顺序:先执行全局拦截器,再执行局部拦截器

  • 如果某个域名不需要执行全局拦截器,只需要执行 needGlobalInterceptors 参数为false 即可,便不会执行

原生axios

const axios = Request.axios //可使用原生axios提供的all 等等方法
axios.all()

demo

// axios初始化:
const axiosEnhance = new Request({
  domain: {
    testDomain: "https://xxx.cn",
  },
  interceptors: {
    testDomain: {
      reqInterceptors: {
        success: (config) => {
          //单域名拦截器header设置,由于headers 与全局拦截器共享,所以配置需要自行合并,否则会覆盖全局拦截器设置的header
          config.headers = { ...config.headers } 
          return config
        },
      },
    },
    //全域名拦截器
    glob: {
      reqInterceptors: {
        success: (config) => {
          config.headers = { ...config.headers } 
          return config
        },
      },
    }
  },
})

axiosEnhance.request({
  business: "testDomain",
  url: "/v1/countries",
});

// get 
axiosEnhance.get("https://xxx.cn/v1/countries");

axiosEnhance.get("/v1/countries", {
  business: "testDomain",
});

//post
axiosEnhance.post("https://xxx.cn/v1/countries", {
  data: 'data',
});

axiosEnhance.post("v1/countries", {
  data: 'data',
}, {
  business: "testDomain",
});
1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago