From 099a7540d0f70059f3ec8d42a82b54e00a0ba418 Mon Sep 17 00:00:00 2001 From: aizigao Date: Wed, 18 Nov 2020 10:10:18 +0800 Subject: [PATCH] feat: if mockerKey match proxyRegExp use mock process instead of proxy --- example/base/mocker/index.js | 14 ++++++++++++-- example/base/package.json | 3 ++- packages/core/src/index.ts | 5 ++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/example/base/mocker/index.js b/example/base/mocker/index.js index 95195817..16baa3a1 100644 --- a/example/base/mocker/index.js +++ b/example/base/mocker/index.js @@ -5,6 +5,16 @@ const user = require('./user.mock'); const noProxy = process.env.NO_PROXY === 'true'; const proxy = { + _proxy: { + proxy: { + "/(.*)": "https://api.github.com/", + }, + changeHost: true, + }, + // 注释它则走 默认代理 + "GET /repos/jaywcjlove/mocker-api": { + mock: true, + }, ...user, // 'GET /api/userinfo/:id': (req, res) => { // console.log('---->', req.params) @@ -90,5 +100,5 @@ const proxy = { res.send({ status: 'ok', message: '删除成功!' }); }, } -module.exports = (noProxy ? {} : delay(proxy, 1000)); -// module.exports = proxy; \ No newline at end of file +// module.exports = (noProxy ? {} : delay(proxy, 1000)); +module.exports = proxy; \ No newline at end of file diff --git a/example/base/package.json b/example/base/package.json index 5d27646a..ff4f10d6 100644 --- a/example/base/package.json +++ b/example/base/package.json @@ -4,7 +4,8 @@ "description": "", "private": true, "scripts": { - "api": "mocker ./mocker" + "api": "mocker ./mocker", + "apix": "node ../../packages/core/lib/bin/mocker ./mocker" }, "keywords": [], "mocker": { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index d55416da..b7fe4c18 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -306,7 +306,10 @@ export default function (app: Application, watchFile: string | string[] | Mocker return res.sendStatus(200); } - if (proxyKey && options.proxy[proxyKey]) { + // if mockerKey match proxyRegExp use mock process instead of proxy + const mockerKeyIsMatchProxyKey = new RegExp(proxyKey).test(mockerKey) + + if (mockerKeyIsMatchProxyKey && proxyKey && options.proxy[proxyKey]) { const currentProxy = options.proxy[proxyKey]; const url = URL.parse(currentProxy); if (options.changeHost) {