Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: if mockerKey match proxyRegExp use mock process instead of proxy #152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions example/base/mocker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -90,5 +100,5 @@ const proxy = {
res.send({ status: 'ok', message: '删除成功!' });
},
}
module.exports = (noProxy ? {} : delay(proxy, 1000));
// module.exports = proxy;
// module.exports = (noProxy ? {} : delay(proxy, 1000));
module.exports = proxy;
3 changes: 2 additions & 1 deletion example/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"private": true,
"scripts": {
"api": "mocker ./mocker"
"api": "mocker ./mocker",
"apix": "node ../../packages/core/lib/bin/mocker ./mocker"
},
"keywords": [],
"mocker": {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down