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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@142vip/egg-axios

v0.0.1-alpha.2

Published

Egg.js框架下使用@142vip/axios的插件,简化Http请求配置

Readme

@142vip/egg-axios

NPM version

Egg.js框架下使用@142vip/axios的插件,简化Http请求配置

安装

# npm
npm i @142vip/axios
# pnpm
pnpm i @142vip/egg-axios

配置

单实例

// config/config.xxx.js
module.exports = {
  axios: {
    client: {
      headers: {
        common: {
          'Content-Type': 'application/json; charset=UTF-8',
        },
      },
      timeout: 6000,
    },
  }
}

多个实例

// config/config.xxx.js
module.exports = {
  axios: {
    clients: {
      axios1: {
        headers: {
          common: {
            'Content-Type': 'application/json; charset=UTF-8',
          },
        },
        timeout: 7000,
        instanceName: 'axios1',
      },
      axios2: {
        headers: {
          common: {
            'Content-Type': 'application/json; charset=UTF-8',
          },
        },
        timeout: 8000,
        instanceName: 'axios2',
      },
    },
    // 所有实例的默认配置,可选
    default: {
      timeout: 5000,
    },
  }
}

使用

plugin.js文件中,加载@142vip/egg-axios插件:

// config/plugin.js
module.exports = {
  axios: {
    enable: true,
    package: '@142vip/egg-axios',
  },
}

使用egg-axios插件时,会在app上挂载axios对象,该对象除了提供自带的构造函数、方法外,还另外挂载了三个方法:

  • getInstance: (name?: string) => T | undefined
  • getInstances: () => Record<string, T>
  • getInstanceNames: () => string[]
export interface EggPluginInstance<T> {
  /**
   * 获取实例对象
   * @param name 实例名称
   * - 多实例,默认获取第一个实例
   * @returns 实例
   */
  getInstance: (name?: string) => T | undefined
  /**
   * 获取所有实例
   * - 默认实例和单实例,对象中对应Key为`default`
   * - 多实例,对象中对应Key为实例名称
   * @returns 所有实例配置key与实例对象组成的键值对
   */
  getInstances: () => Record<string, T>
  /**
   * 获取所有实例名称
   * - 默认实例和单实例,返回: ['default']
   * - 多实例,返回: 实例名称数组,例如: ['axios1','axios2']
   * @returns 所有实例名称
   */
  getInstanceNames: () => string[]
}

默认实例

egg项目中,如果只加载了@142vip/egg-axios插件,没有在config.xx.js配置文件中增加axios关键字配置,会加载插件默认配置:

const { defaultRequestInterceptor, defaultResponseInterceptor } = require('@142vip/axios')
const { defaultPluginConfig } = require('@142vip/egg')
const { name: pkgName } = require('../package.json')

module.exports = {
  axios: defaultPluginConfig(pkgName, {
    default: {
      requestInterceptorsHandler: defaultRequestInterceptor,
      responseInterceptorsHandler: defaultResponseInterceptor,
      timeout: 5 * 1000,
    },
    client: {},
  }),
}

此时,在app上挂载的axios对象,就是默认实例。获取默认实例:

// 直接获取
this.app.axios
// 通过方法获取
this.app.axios.getInstance()

单实例

单实例配置:

// config/config.xxx.js
module.exports = {
  axios: {
    client: {
      headers: {
        common: {
          'Content-Type': 'application/json; charset=UTF-8',
        },
      },
      timeout: 6000,
    },
  }
}

因为只挂载一个实例,可以像默认配置那样,基于axios对象或者getInstance方法获取实例:

class EggAxiosController extends Controller {
  /**
   * 获取挂载的单实例
   */
  async getSimpleInstance() {
    const { app } = this

    // 直接获取
    this.app.axios
    // 通过方法获取
    this.app.axios.getInstance()
    await app.axios.getInstance()
  }
}

多实例

多实例配置:

module.exports = {
  axios: {
    clients: {
      axios1: {
        headers: {
          common: {
            'Content-Type': 'application/json; charset=UTF-8',
          },
        },
        timeout: 7000,
        instanceName: 'axios1',
      },
      axios2: {
        headers: {
          common: {
            'Content-Type': 'application/json; charset=UTF-8',
          },
        },
        timeout: 8000,
        instanceName: 'axios2',
      },
    },
  }
}

由于插件挂载了多个axios实例,在获取实例时,只能通过实例名称获取。

class EggAxiosController extends Controller {
  /**
   * 获取挂载的多个实例
   */
  async getMultipleInstance() {
    const { app } = this
    const axios1Instance = await app.axios.getInstance('axios1')
    const axios2Instance = await app.axios.getInstance('axios2')
    // xxx...
  }
}

单元测试

参考

证书

MIT

Copyright (c) 2019-present, @142vip 储凡