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

request-by-axios

v1.0.2

Published

简单封装axios

Downloads

4

Readme

初步封装 axios

详细的代码思路请参看博文

封装思路

  • 支持多模块多 baseUrl 配置,每个实例可以单独配置
  • 防重入策略
  • 参数处理策略
  • 校验业务状态是否为失败状态
  • 失败状态进行全局提醒
  • 提供一个方法取消当前正在进行的请求
  • 消息国际化 ✖未实装

安装

安装 request-by-axios

npm install request-by-axios --save
#or
yarn add request-by-axios --dev

配置axios的实例

import { create } from "request-by-axios";

const options = [
  {
    baseUrl: "/api/user",
    serverName: "user",
    log: true,
  },
  {
    baseUrl: "/api/order",
    serverName: "order",
    log: true,
    adapter: (config) => {
      return new Promise((resolve) => {
        const res = {
          data: {
            success: true,
            message: "Ok",
            data: [{ id: 1 }, { id: 2 }],
          },
          status: 200,
          statusText: "OK",
          config,
        };
        // 调用响应函数
        setTimeout(() => {
          resolve(res);
        }, 5000);
      });
    },
  },
];
const instance = create(options);

export default instance;

维护不同模块的后端服务接口

// src/api/module/order.js
import http from "request-by-axios";
const { order } = http;

export default {
  list: function (params) {
    return order.get("url", {
      params,
      cancelModel: 2,
    });
  },
};

// src/api/module/user.js
import http from "request-by-axios";
const { user } = http;

export default {
  login: function (data, config) {
    return user.post("login", data, config);
  },
};

Vue创建全局api

import "./config";
import user from "./module/user";
import order from "./module/order";

const apiList = Object.assign({}, user, order);

function install(Vue) {
  if (install.installed) {
    return console.warn("api重复注册");
  }
  Object.defineProperties(Vue.prototype, {
    $api: {
      get() {
        return apiList;
      },
    },
  });
  install.installed = true;
}

export default {
  install,
};

调用

<template>
  <div>
    <div>
      自定义adapter
      <button @click="sendOrder">发起order请求</button>
    </div>
    <div>
      不同模块不同配置
      <button @click="sendUser">发起user请求</button>
      <button @click="sendOrder">发起order请求</button>
    </div>
    <div>
      防重入
      <button @click="sendOrder2">连续发起两次order请求</button>
    </div>
    <div>
      自定义错误提醒
      <button @click="sendUser2">
        调用接口传入或全局配置, 进行500系列提醒
      </button>
    </div>
    <div>
      参数去空格
      <button @click="sendUser3">参数去空格</button>
    </div>
  </div>
</template>
export default {
  name: "Axios",
  methods: {
    sendUser() {
      this.$api.login();
    },
    sendOrder() {
      this.$api.list();
    },
    sendOrder2() {
      this.sendOrder();
      setTimeout(this.sendOrder, 2000);
    },
    sendUser2() {
      this.$api.login(
        { username: "", password: "" },
        {
          showErrorMsg: ({ response }) => {
            if (response.status === 404) {
              alert("接口未实现");
            }
          },
        }
      );
    },
    sendUser3() {
      const data = { username: " das f ", password: "zzzz " };
      this.$api.login(data, {
        beforeAxiosSend: function (config) {
          alert(
            "原数据:" +
              JSON.stringify(data) +
              "\n" +
              " 处理后数据:" +
              JSON.stringify(config.data)
          );
        },
        autoTrim: true,
      });
    },
  },
};