Skip to content

Commit

Permalink
feat(@142vip/egg-axios): 引入@142vip/axios模块,优化拦截器和axios对象创建
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl committed Jan 7, 2025
1 parent 7c6ec19 commit 34c92b0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
6 changes: 3 additions & 3 deletions packages/egg-axios/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { defaultPluginConfig } = require('@142vip/egg')
const { defaultRequestInterceptor, defaultResponseInterceptor } = require('@142vip/axios')
const { name: pkgName } = require('../package.json')
const { requestInterceptorsHandler, responseInterceptorsHandler } = require('../core/interceptors')

module.exports = {
axios: defaultPluginConfig(pkgName, {
default: {
requestInterceptorsHandler,
responseInterceptorsHandler,
requestInterceptorsHandler: defaultRequestInterceptor,
responseInterceptorsHandler: defaultResponseInterceptor,
timeout: 5 * 1000,
},
client: {
Expand Down
24 changes: 15 additions & 9 deletions packages/egg-axios/core/axios.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const { VipEggPluginLogger } = require('@142vip/egg')
const axios = require('axios')
const { VipLodash } = require('@142vip/utils')
const { responseInterceptorsHandler, requestInterceptorsHandler } = require('./interceptors')
const {
createAxiosInstance,
defaultRequestInterceptor,
defaultResponseInterceptor,
} = require('@142vip/axios')

/**
* 创建egg-axios实例
*/
function createEggAxiosInstance(pluginConfig, app) {
const pluginLogger = VipEggPluginLogger.getInstance(pluginConfig, app)

Expand All @@ -16,16 +22,16 @@ function createEggAxiosInstance(pluginConfig, app) {
// default 5 seconds timeout
timeout: 5000,
}
// 获取插件用户自定义配置
// const userAxiosConfig = app.config.axios
// 合并插件用户自定义配置、axios默认配置
const config = VipLodash.merge(defaultConfig, pluginConfig)

// axios对象初始化配置合并
axios.defaults = VipLodash.merge(axios.defaults, defaultConfig, pluginConfig)
pluginLogger.debug(`default configs: ${JSON.stringify(axios.defaults)}`)
pluginLogger.debug(`default configs: ${JSON.stringify(config)}`)

const axios = createAxiosInstance(config)

// 添加请求拦截器
axios.interceptors.request.use(
pluginConfig.requestInterceptorsHandler ?? requestInterceptorsHandler,
pluginConfig.requestInterceptorsHandler ?? defaultRequestInterceptor,
(error) => {
pluginLogger.error(`send request error in interceptors, ${error.message}`)
return Promise.reject(error)
Expand All @@ -34,7 +40,7 @@ function createEggAxiosInstance(pluginConfig, app) {

// 响应拦截器
axios.interceptors.response.use(
pluginConfig.responseInterceptorsHandler ?? responseInterceptorsHandler,
pluginConfig.responseInterceptorsHandler ?? defaultResponseInterceptor,
(error) => {
pluginLogger.error(`receive response error in interceptors, ${error.message}`)
return Promise.reject(error)
Expand Down
18 changes: 0 additions & 18 deletions packages/egg-axios/core/interceptors.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/egg-axios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dist"
],
"dependencies": {
"@142vip/axios": "workspace:*",
"@142vip/egg": "workspace:*",
"@142vip/utils": "workspace:*",
"axios": "1.7.9"
Expand Down

0 comments on commit 34c92b0

Please sign in to comment.