npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mlz/axios

v1.2.0

Published

mlz-axios

Downloads

11

Readme

Build Status codecov

介绍

对 axios 做了二次封装,统一 axios 的配置

!!!!!!请求/响应拦截放在实例化之前!!!!!! !!!!!!请求/响应拦截放在实例化之前!!!!!! !!!!!!请求/响应拦截放在实例化之前!!!!!!

安装

使用 npm

npm install @mlz/axios

使用 yarn

yarn add @mlz/axios

默认 axios 配置

timeout: 5000;
withCredentials: true;
validateStatus: status => status >= 200 && status < 599;

实例化

实例化可以传入 baseUrl 和默认配置

import Http from 'mlz-axios'
const httpIns = new Http(baseUrl:string, config:AxiosRequestConfig)

静态方法

获取对应的实例

getInstances(key:string)

示例:

import Http from "mlz-axios";
Http.getInstances("https://www.example.com");

全局设置token与tokenType

setGlobalTokenConfig({
  authorizationTypeKey?: string;
  authorizationTokenKey?: string;
  authorizationTypeValue?: number;
  authorizationTokenValue?: string;
})

示例:

import Http from "mlz-axios";
Http.setGlobalTokenConfig({
  authorizationTypeKey: 'authorization_type',
  authorizationTokenKey: 'Authorization',
  authorizationTypeValue: 1
  authorizationTokenValue: 'token'
});

设置请求拦截器

setReqInterceptor (resolve?:(config:AxiosRequestConfig) => AxiosRequestConfig, reject?:(error:any) => void, url?:string)

设置全局请求拦截器

import Http from "mlz-axios";
Http.setReqInterceptor(
  config => {
    console.log(config);
  },
  err => {
    console.log(err);
  }
);

设置单个实例请求拦截器

Http.setReqInterceptor(
  config => {
    console.log("请求前config", config);
    return config;
  },
  err => {
    console.log("请求前err", err);
    return Promise.reject(err);
  },
  "https://xxx.xxx.com"
);

设置响应拦截器

setResInterceptor (resolve?:(res:AxiosResponse) => AxiosResponse, reject?:(error) => any, url?:string)

设置全局响应拦截器

Http.setResInterceptor(
  res => {
    console.log("请求后res", res);
    return res;
  },
  err => {
    console.log("请求后err", err);
    return Promise.reject(err);
  }
);

设置单个实例响应拦截器

Http.setResInterceptor(
  res => {
    console.log("请求后res", res);
    return res;
  },
  err => {
    console.log("请求后err", err);
    return Promise.reject(err);
  },
  "https://xxx.xxx.com"
);

实例方法

设置实例的 authorizationTokenToken 和 authorizationType

setInstanceTokenConfig({
  authorizationTypeKey?: string;
  authorizationTokenKey?: string;
  authorizationTypeValue?: number;
  authorizationTokenValue?: string;
})

示例:

import Http from "mlz-axios";
const httpIns = new Http('https://xxx.xxx.com')
httpIns.setInstanceTokenConfig({
  authorizationTypeKey: 'authorization_type',
  authorizationTokenKey: 'Authorization',
  authorizationTypeValue: 1
  authorizationTokenValue: 'token'
});

get(url[, config])

import Http from "mlz-axios";
const httpIns = new Http("https://www.example.com");
httpIns
  .get("/user")
  .then(function(response) {
    // handle success
    console.log(response);
  })
  .catch(function(error) {
    // handle error
    console.log(error);
  })
  .finally(function() {
    // always executed
  });

delete(url[, config])

import Http from "mlz-axios";
const httpIns = new Http("https://www.example.com");
httpIns
  .delete("/user")
  .then(function(response) {
    // handle success
    console.log(response);
  })
  .catch(function(error) {
    // handle error
    console.log(error);
  })
  .finally(function() {
    // always executed
  });

post(url[, data[, config]])

import Http from "mlz-axios";
const httpIns = new Http("https://www.example.com");
httpIns
  .post("/user", {
    firstName: "Fred",
    lastName: "Flintstone"
  })
  .then(function(response) {
    // handle success
    console.log(response);
  })
  .catch(function(error) {
    // handle error
    console.log(error);
  })
  .finally(function() {
    // always executed
  });

put(url[, data[, config]])

import Http from "mlz-axios";
const httpIns = new Http("https://www.example.com");
httpIns
  .put("/user", {
    firstName: "Fred",
    lastName: "Flintstone"
  })
  .then(function(response) {
    // handle success
    console.log(response);
  })
  .catch(function(error) {
    // handle error
    console.log(error);
  })
  .finally(function() {
    // always executed
  });

patch(url[, data[, config]])

import Http from "mlz-axios";
const httpIns = new Http("https://www.example.com");
httpIns
  .patch("/user", {
    firstName: "Fred",
    lastName: "Flintstone"
  })
  .then(function(response) {
    // handle success
    console.log(response);
  })
  .catch(function(error) {
    // handle error
    console.log(error);
  })
  .finally(function() {
    // always executed
  });

例子

import Http from "@mlz/axios";

const token = localStorage.getItem('authorization')
const AUTHORIZATION_TYPE = 3

// 全局设置token与tokenType
Http.setGlobalTokenConfig({
  authorizationTypeKey: 'Authorization',
  authorizationTokenKey: 'authorization_type',
  authorizationTypeValue: AUTHORIZATION_TYPE,
  authorizationTokenValue: token
})

const httpIns = new Http('https://xxx.aaa.com');
const httpIns2 = new Http('https://xxx.bbb.com');
const httpIns3 = new Http('https://xxx.ccc.com');

const httpIns2Token = localStorage.getItem('authorization_httpIns2')
const AUTHORIZATION_HTTPINS_2_TYPE = 4

// 设置单个实例的token与tokenType
httpIns2.setInstanceTokenConfig({
  authorizationTypeKey: 'Authorization',
  authorizationTokenKey: 'authorization_type',
  authorizationTypeValue: AUTHORIZATION_HTTPINS_2_TYPE,
  authorizationTokenValue: httpIns2Token
})

//对单个实例设置请求拦截器
Http.setReqInterceptor(
  config => {
    console.log(config)
  },
  err => {
    return Promise.reject(err);
  },
  'https://xxx.aaa.com'
);

httpIns
  .post("/xxx/xxx/xxx", {
    id: "xxx",
  })
  .then(res => {})
  .catch(err => {
    console.log(err);
  });

httpIns2
  .get("/xxx/xxx/xxx", { params: { id: 1 } })
  .then(res => {});

httpIns3
  .get("/xxx/xxxx")
  .then(res => {
    if (/^2/.test(res.status)) {
      console.log(res.data);
    } else {
      console.log(res.data.error_code);
    }
  })
  .catch(err => {
    console.log(err);
  });