1.0.0 • Published 3 years ago

axios_sen v1.0.0

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

安装

npm install axios_sen

导入

const { Client } from 'axios_sen'

配置项

创建实例的第一个参数为:请求URL前缀
创建实例的第二个参数为:请求配置项

配置项内容:

// 发送请求的数据键名是否驼峰转下划线
underscoreRequestData: true
// 请求回来的数据是否下划线转驼峰
camelizeResponseData: true
// 发送请求的数据类型
dataType: "json",
// 是否将值为空字符串的项去除
removeEmptyValue:false
// 默认会被去除掉的值
defaultRemoveTypeArray = [null, undefined, NaN]

请求头携带 Authorization 字段

通过给接口传参时携带字段名为 Authorization 的项

示例

import { Client } from "axios_sen";
// 根据请求前缀对请求接口进行分类管理
export class MyAxios extends Client {
  constructor() {
    // 第二个参数是配置项 此处没传
    super("http://127.0.0.1:3007");
  }
  // 配置对应的请求
  login(options) {
    return this.post("/api/login", options);
  }
  getUserInfo(options) {
    return this.get("/my/userinfo",options);
  }
}


const loginObj = { email: "222test@qq.com", password: "222222" };
let token = "";

// 从外部引入 MyAxios 并实例化
import MyAxios from './xxx/xxx'
const myAxios = new MyAxios();
// 发送请求
myAxios.login(loginObj).then((res) => {
    console.log("res", res);
    token = res.token;
  })
  .catch((err) => {
    console.log(err);
  });

setTimeout(() => {
  myAxios.getUserInfo({ Authorization: token }).then((res) => {
      console.log("userInfo", res);
    })
    .catch((err) => {
      console.log(err);
    });
}, 1000);