From fc9fa8701d8e603520dc50aef22a50bc9b8cb001 Mon Sep 17 00:00:00 2001 From: openhands Date: Tue, 21 Jan 2025 03:26:37 +0000 Subject: [PATCH] fix: rename proxy config to eggProxy to avoid conflict with egg.js built-in config --- README.md | 4 ++-- README.zh_CN.md | 9 +++++++++ app.js | 6 +++--- config/config.default.js | 4 ++-- index.d.ts | 2 +- .../apps/egg-proxy-multiple/config/config.unittest.js | 2 +- .../apps/egg-proxy-single/config/config.unittest.js | 2 +- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4679370..9cfaa77 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Single: ```js // {app_root}/config/config.default.js -exports.proxy = { +exports.eggProxy = { host: 'http://localhost:9000', // target host that matched path will be proxy to match: /\/assets/ // path pattern. }; @@ -55,7 +55,7 @@ exports.proxy = { Multiple: ```js // {app_root}/config/config.default.js -exports.proxy = [{ +exports.eggProxy = [{ host: 'host1', match: /\/assets1/ }, { diff --git a/README.zh_CN.md b/README.zh_CN.md index 6629aa0..19290b0 100644 --- a/README.zh_CN.md +++ b/README.zh_CN.md @@ -63,6 +63,15 @@ exports.proxy = { 请到 [config/config.default.js](config/config.default.js) 查看详细配置项说明。 +配置示例: +```js +// {app_root}/config/config.default.js +exports.eggProxy = { + host: 'http://localhost:9000', // 代理目标地址 + match: /\/assets/ // 路径匹配规则 +}; +``` + ## 单元测试 diff --git a/app.js b/app.js index 2aabb31..3b4ba4c 100644 --- a/app.js +++ b/app.js @@ -1,14 +1,14 @@ 'use strict'; -const proxy = require('koa-proxy'); +const proxyMiddleware = require('koa-proxy'); module.exports = app => { - let proxyConfs = app.config.proxy || []; + let proxyConfs = app.config.eggProxy || []; if (!Array.isArray(proxyConfs)) { proxyConfs = [].concat(proxyConfs); } proxyConfs.forEach(config => { - app.use(proxy(config)); + app.use(proxyMiddleware(config)); }); }; diff --git a/config/config.default.js b/config/config.default.js index 9d3034d..5908c6b 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -2,11 +2,11 @@ /** * egg-proxy default config - * @member Config#proxy + * @member Config#eggProxy * @property {String} SOME_KEY - some description */ -exports.proxy = { +exports.eggProxy = { host: 'http://localhost:7001', match: /^\/assets/, }; diff --git a/index.d.ts b/index.d.ts index 92b51e0..4ccf89d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,6 @@ declare module 'egg' { interface EggAppConfig { - proxy: IProxy | IProxy[]; + eggProxy: IProxy | IProxy[]; } interface IProxy { host?: string; diff --git a/test/fixtures/apps/egg-proxy-multiple/config/config.unittest.js b/test/fixtures/apps/egg-proxy-multiple/config/config.unittest.js index 875db4c..79d70f8 100644 --- a/test/fixtures/apps/egg-proxy-multiple/config/config.unittest.js +++ b/test/fixtures/apps/egg-proxy-multiple/config/config.unittest.js @@ -2,7 +2,7 @@ exports.keys = '123456'; -exports.proxy = [ +exports.eggProxy = [ { host: 'http://127.0.0.1:1234', match: /^\/proxy/, diff --git a/test/fixtures/apps/egg-proxy-single/config/config.unittest.js b/test/fixtures/apps/egg-proxy-single/config/config.unittest.js index 8a1c40e..095b663 100644 --- a/test/fixtures/apps/egg-proxy-single/config/config.unittest.js +++ b/test/fixtures/apps/egg-proxy-single/config/config.unittest.js @@ -2,7 +2,7 @@ exports.keys = '123456'; -exports.proxy = { +exports.eggProxy = { host: 'http://127.0.0.1:1234', match: /^\/proxy/, };