{"_id":"@142vip/egg-axios","_rev":"3-01ba630161254152f6aa9365750a2866","name":"@142vip/egg-axios","dist-tags":{"latest":"0.0.1-alpha.2"},"versions":{"0.0.1-alpha.0":{"name":"@142vip/egg-axios","version":"0.0.1-alpha.0","keywords":["公众号搜：储凡"],"author":{"name":"mmdapl","email":"mmdapl@163.com"},"license":"MIT","_id":"@142vip/egg-axios@0.0.1-alpha.0","maintainers":[{"name":"mmdapl","email":"2237221210@qq.com"}],"dist":{"shasum":"dffb26480b192d5e3a59f5c0668ff00c52e0961d","tarball":"https://registry.npmjs.org/@142vip/egg-axios/-/egg-axios-0.0.1-alpha.0.tgz","fileCount":12,"integrity":"sha512-6CSG3EkcfXJUWyx00E0jzpNZIsUBXMMc/JMR3OvHBC51acsiJ/TbHxCaLYsnQVexKKv71Z3Pbg8+ifX6Y9V6Dw==","signatures":[{"sig":"MEQCICcqR04i59ZfTHoGFoxMZKxdp9ELoz99bSBXUkXKaLSGAiBjTjsz/IV0hS+fUAKQm3QEKbaVRxpDfAJYbNC+oaR/ow==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":3113},"_from":"file:142vip-egg-axios-0.0.1-alpha.0.tgz","private":false,"scripts":{"build":"unbuild","typecheck":"tsc --noEmit"},"_npmUser":{"name":"mmdapl","email":"2237221210@qq.com"},"_resolved":"/private/var/folders/93/44x7xwkd2vz0pdy1z9s29m4r0000gn/T/718ee6f2ae64e61be32ec86540d9ebd8/142vip-egg-axios-0.0.1-alpha.0.tgz","_integrity":"sha512-6CSG3EkcfXJUWyx00E0jzpNZIsUBXMMc/JMR3OvHBC51acsiJ/TbHxCaLYsnQVexKKv71Z3Pbg8+ifX6Y9V6Dw==","_npmVersion":"10.8.1","description":"公众号搜：储凡","directories":{},"_nodeVersion":"20.16.0","dependencies":{"dayjs":"^1.11.11"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/egg-axios_0.0.1-alpha.0_1722504176902_0.01658386290416014","host":"s3://npm-registry-packages"}},"0.0.1-alpha.1":{"name":"@142vip/egg-axios","version":"0.0.1-alpha.1","keywords":["公众号搜：储凡","142vip","@142vip","@142vip/egg-axios","axios","egg-plugin","eggPlugin","egg"],"author":{"name":"mmdapl","email":"mmdapl@163.com"},"license":"MIT","_id":"@142vip/egg-axios@0.0.1-alpha.1","maintainers":[{"name":"mmdapl","email":"2237221210@qq.com"}],"homepage":"https://142vip.github.io/core-x/packages/egg-axios/","bugs":{"url":"https://github.com/142vip/core-x/issues"},"dist":{"shasum":"e899f486b98f6e88f1542efcb749afd7415f2c92","tarball":"https://registry.npmjs.org/@142vip/egg-axios/-/egg-axios-0.0.1-alpha.1.tgz","fileCount":7,"integrity":"sha512-Kj8tJQAvozQupgwUvDUWGWfL1PZt1csyhhem12Q5s6G/Bi1+sRlp416od9PDCpijAg7vsLhqHLSEHdEEl1n93w==","signatures":[{"sig":"MEUCIHYBJQbIXxut2IFMNi8o3lbS8lx+DdmJrdXHz8X+bUKRAiEA4Ag9SX/GPBAiOuFaoNs7cU8qh/UBswOUkST18w2lQc0=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":4769},"_from":"file:142vip-egg-axios-0.0.1-alpha.1.tgz","private":false,"_npmUser":{"name":"mmdapl","email":"2237221210@qq.com"},"_resolved":"/tmp/4fc2051051b30dae5189c75424168963/142vip-egg-axios-0.0.1-alpha.1.tgz","eggPlugin":{"name":"axios"},"_integrity":"sha512-Kj8tJQAvozQupgwUvDUWGWfL1PZt1csyhhem12Q5s6G/Bi1+sRlp416od9PDCpijAg7vsLhqHLSEHdEEl1n93w==","repository":{"url":"git+https://github.com/142vip/core-x.git","type":"git","directory":"packages/egg-axios"},"_npmVersion":"10.5.0","description":"公众号搜：储凡","directories":{},"_nodeVersion":"20.12.2","dependencies":{"axios":"1.7.9","@142vip/egg":"0.0.1-alpha.1","@142vip/utils":"0.0.1-alpha.13"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/egg-axios_0.0.1-alpha.1_1736165473946_0.9178021859282799","host":"s3://npm-registry-packages-npm-production"}},"0.0.1-alpha.2":{"name":"@142vip/egg-axios","version":"0.0.1-alpha.2","private":false,"description":"Egg.js框架下使用@142vip/axios的插件，简化Http请求配置","author":{"name":"mmdapl","email":"mmdapl@163.com"},"license":"MIT","homepage":"https://142vip.github.io/core-x/packages/egg-axios/","repository":{"type":"git","url":"git+https://github.com/142vip/core-x.git","directory":"packages/egg-axios"},"eggPlugin":{"name":"axios"},"keywords":["公众号搜：储凡","142vip","@142vip","@142vip/egg-axios","@142vip/axios","@142vip/egg","egg-plugin","eggPlugin","egg"],"authorInfo":{"name":"微信公众号：储凡","email":"fairy_vip@2925.com","url":"https://github.com/142vip","homePage":"https://142vip.cn"},"dependencies":{"@142vip/axios":"0.0.1-alpha.7","@142vip/egg":"0.0.1-alpha.3"},"publishConfig":{"access":"public","registry":"https://registry.npmjs.org"},"bugs":{"url":"https://github.com/142vip/core-x/issues"},"_id":"@142vip/egg-axios@0.0.1-alpha.2","_integrity":"sha512-sFH8yj1n45Ji3cYFQ+pX08CUR03W0J5k57b/4A1GIFui+bICjBrvn2wLNshxhjQsYQrSmc6GDktrz7bwgRuJiA==","_resolved":"/tmp/ff56f74d0ebd79d1303da43ad310b8b3/142vip-egg-axios-0.0.1-alpha.2.tgz","_from":"file:142vip-egg-axios-0.0.1-alpha.2.tgz","_nodeVersion":"20.12.2","_npmVersion":"10.5.0","dist":{"integrity":"sha512-sFH8yj1n45Ji3cYFQ+pX08CUR03W0J5k57b/4A1GIFui+bICjBrvn2wLNshxhjQsYQrSmc6GDktrz7bwgRuJiA==","shasum":"146e8d7ccbbaf0ebb6a764cd9bd8c6e3d551d7c7","tarball":"https://registry.npmjs.org/@142vip/egg-axios/-/egg-axios-0.0.1-alpha.2.tgz","fileCount":7,"unpackedSize":10472,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDur54VebrTep7qEU2pfBm2Y3s4+k2ngMqkxCokmJWbWQIhALbSukbC0UL1h9HXhnBfp0jMzsqJSFynHkwlO1f3oPhy"}]},"_npmUser":{"name":"mmdapl","email":"2237221210@qq.com"},"directories":{},"maintainers":[{"name":"mmdapl","email":"2237221210@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/egg-axios_0.0.1-alpha.2_1757474301015_0.8331853165922221"},"_hasShrinkwrap":false}},"time":{"created":"2024-08-01T09:22:56.763Z","modified":"2025-09-10T03:18:21.392Z","0.0.1-alpha.0":"2024-08-01T09:22:57.075Z","0.0.1-alpha.1":"2025-01-06T12:11:14.255Z","0.0.1-alpha.2":"2025-09-10T03:18:21.202Z"},"bugs":{"url":"https://github.com/142vip/core-x/issues"},"author":{"name":"mmdapl","email":"mmdapl@163.com"},"license":"MIT","homepage":"https://142vip.github.io/core-x/packages/egg-axios/","keywords":["公众号搜：储凡","142vip","@142vip","@142vip/egg-axios","@142vip/axios","@142vip/egg","egg-plugin","eggPlugin","egg"],"repository":{"type":"git","url":"git+https://github.com/142vip/core-x.git","directory":"packages/egg-axios"},"description":"Egg.js框架下使用@142vip/axios的插件，简化Http请求配置","maintainers":[{"name":"mmdapl","email":"2237221210@qq.com"}],"readme":"# @142vip/egg-axios\n\n[![NPM version](https://img.shields.io/npm/v/@142vip/egg-axios?labelColor=0b3d52&color=1da469&label=version)](https://www.npmjs.com/package/@142vip/egg-axios)\n\n`Egg.js`框架下使用`@142vip/axios`的插件，简化`Http`请求配置\n\n## 安装\n\n```bash\n# npm\nnpm i @142vip/axios\n# pnpm\npnpm i @142vip/egg-axios\n```\n\n## 配置\n\n### 单实例\n```js\n// config/config.xxx.js\nmodule.exports = {\n  axios: {\n    client: {\n      headers: {\n        common: {\n          'Content-Type': 'application/json; charset=UTF-8',\n        },\n      },\n      timeout: 6000,\n    },\n  }\n}\n```\n\n### 多个实例\n\n```js\n// config/config.xxx.js\nmodule.exports = {\n  axios: {\n    clients: {\n      axios1: {\n        headers: {\n          common: {\n            'Content-Type': 'application/json; charset=UTF-8',\n          },\n        },\n        timeout: 7000,\n        instanceName: 'axios1',\n      },\n      axios2: {\n        headers: {\n          common: {\n            'Content-Type': 'application/json; charset=UTF-8',\n          },\n        },\n        timeout: 8000,\n        instanceName: 'axios2',\n      },\n    },\n    // 所有实例的默认配置，可选\n    default: {\n      timeout: 5000,\n    },\n  }\n}\n```\n\n## 使用\n\n在`plugin.js`文件中，加载`@142vip/egg-axios`插件：\n\n```js\n// config/plugin.js\nmodule.exports = {\n  axios: {\n    enable: true,\n    package: '@142vip/egg-axios',\n  },\n}\n```\n\n使用`egg-axios`插件时，会在`app`上挂载`axios`对象，该对象除了提供自带的构造函数、方法外，还另外挂载了三个方法：\n\n- `getInstance: (name?: string) => T | undefined`\n- `getInstances: () => Record<string, T>`\n- `getInstanceNames: () => string[]`\n\n```ts\nexport interface EggPluginInstance<T> {\n  /**\n   * 获取实例对象\n   * @param name 实例名称\n   * - 多实例，默认获取第一个实例\n   * @returns 实例\n   */\n  getInstance: (name?: string) => T | undefined\n  /**\n   * 获取所有实例\n   * - 默认实例和单实例，对象中对应Key为`default`\n   * - 多实例，对象中对应Key为实例名称\n   * @returns 所有实例配置key与实例对象组成的键值对\n   */\n  getInstances: () => Record<string, T>\n  /**\n   * 获取所有实例名称\n   * - 默认实例和单实例，返回: ['default']\n   * - 多实例，返回: 实例名称数组，例如: ['axios1','axios2']\n   * @returns 所有实例名称\n   */\n  getInstanceNames: () => string[]\n}\n```\n### 默认实例\n\n在`egg`项目中，如果只加载了`@142vip/egg-axios`插件，没有在`config.xx.js`配置文件中增加`axios`关键字配置，会加载插件默认配置：\n\n```js\nconst { defaultRequestInterceptor, defaultResponseInterceptor } = require('@142vip/axios')\nconst { defaultPluginConfig } = require('@142vip/egg')\nconst { name: pkgName } = require('../package.json')\n\nmodule.exports = {\n  axios: defaultPluginConfig(pkgName, {\n    default: {\n      requestInterceptorsHandler: defaultRequestInterceptor,\n      responseInterceptorsHandler: defaultResponseInterceptor,\n      timeout: 5 * 1000,\n    },\n    client: {},\n  }),\n}\n```\n此时，在`app`上挂载的`axios`对象，就是默认实例。获取默认实例：\n\n```js\n// 直接获取\nthis.app.axios\n// 通过方法获取\nthis.app.axios.getInstance()\n```\n\n### 单实例\n\n单实例配置：\n\n```js\n// config/config.xxx.js\nmodule.exports = {\n  axios: {\n    client: {\n      headers: {\n        common: {\n          'Content-Type': 'application/json; charset=UTF-8',\n        },\n      },\n      timeout: 6000,\n    },\n  }\n}\n```\n\n因为只挂载一个实例，可以像默认配置那样，基于`axios`对象或者`getInstance`方法获取实例：\n\n```js\nclass EggAxiosController extends Controller {\n  /**\n   * 获取挂载的单实例\n   */\n  async getSimpleInstance() {\n    const { app } = this\n\n    // 直接获取\n    this.app.axios\n    // 通过方法获取\n    this.app.axios.getInstance()\n    await app.axios.getInstance()\n  }\n}\n```\n\n### 多实例\n\n多实例配置：\n\n```js\nmodule.exports = {\n  axios: {\n    clients: {\n      axios1: {\n        headers: {\n          common: {\n            'Content-Type': 'application/json; charset=UTF-8',\n          },\n        },\n        timeout: 7000,\n        instanceName: 'axios1',\n      },\n      axios2: {\n        headers: {\n          common: {\n            'Content-Type': 'application/json; charset=UTF-8',\n          },\n        },\n        timeout: 8000,\n        instanceName: 'axios2',\n      },\n    },\n  }\n}\n```\n\n由于插件挂载了多个`axios`实例，在获取实例时，只能通过实例名称获取。\n\n```js\nclass EggAxiosController extends Controller {\n  /**\n   * 获取挂载的多个实例\n   */\n  async getMultipleInstance() {\n    const { app } = this\n    const axios1Instance = await app.axios.getInstance('axios1')\n    const axios2Instance = await app.axios.getInstance('axios2')\n    // xxx...\n  }\n}\n```\n\n## 单元测试\n\n- [默认配置测试](../../apps/egg-demo/test/egg-axios/config.spec.ts)\n- [单实例配置测试](../../apps/egg-demo/test/egg-axios/simple-instance.spec.ts)\n- [多实例配置测试](../../apps/egg-demo/test/egg-axios/multiple-instance.spec.ts)\n- [测试单实例](../../apps/egg-demo/test/egg-axios/simple-instance.spec.ts)\n- [测试多实例](../../apps/egg-demo/test/egg-axios/multiple-instance.spec.ts)\n\n## 参考\n\n- [@142vip/axios 仓库](https://github.com/142vip/axios)\n- [axios Github仓库](https://github.com/axios/axios)\n- [axios NPM仓库](https://www.npmjs.com/package/axios)\n- [axios 中文文档](https://www.axios-http.cn/docs/intro)\n\n## 证书\n\n[MIT](https://opensource.org/license/MIT)\n\nCopyright (c) 2019-present, @142vip 储凡\n","readmeFilename":"README.md"}