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

@forchange/apis

v0.0.20

Published

基于 axios 封装的接口管理方案

Downloads

78

Readme

apis

npm version license

基于 axios 封装的接口管理方案,@forchange/apis

Features

  • 接口统一管理
  • 可配置多个接口服务
  • 支持 restful 接口
  • 支持命名空间

Installing

$ npm install @forchange/apis

Syntax

new Apis(serverMap, apiMap);

Parameters

serverMap

  • serverMap 是服务配置的 map 对象
  • 服务的参数配置同 axios 中的 config 部分
  • default 为自定义属性,当 default 为 true 时,api 会使用它做为默认服务配置
{
  "baseServer": {
    "default": true,
    "baseUrl": "https://base.apis.com"
  }
}

apiMap

  • apiMap 是接口配置的 map 对象
  • 接口的参数配置同 axios 中的 config 部分,会覆盖服务配置中的参数
  • server 为自定义属性,表示使用哪个服务配置,当 server 为 null 时,表示使用默认服务配置
{
  "getBaseInfo": {
    "method": "get",
    "url": "/info"
  }
}

Custom

rest:restful 参数

当接口中需要传递 restful 参数时,按如下配置

配置时用:标记:

{
  "getBaseInfoId": {
    "method": "get",
    "url": "/info/:id"
  }
}

调用时参数中添加 rest 参数:

apis.getBaseInfoId({
  rest: {
    id: 1
  }
});

Namespace

^0.0.16 起支持

apiMap 的 key 中出现的 / 会解析为对应的命名空间路径,不需要命名空间时,不加 / 即可

e.g: auth/user/getInfo => auth.user.getInfo()
{
  'user/getInfo':{
    method: "get"
    server: "baseServer"
    url: "/user/info"
  }
}

=> apis.user.getInfo()

Interceptors

Apis 通过useReq,useRes两个接口对请求做拦截,可以多次调用,添加多个 middleware

Apis.useReq(middleware)

axios.interceptors.request.use

Apis.useReq(function(config) {
  config.headers.Authorization = "Bearer";
  return config;
});

Apis.useRes(middleware)

axios.interceptors.response.use

Apis.useRes(function(res) {
  res.msg = "ok";
  return res;
});

Usage

const apis = new Apis(serverMap, apiMap);

apis.getTest({
  params: {
    color: "green"
  }
});

License

MIT