Skip to content

Commit 7025458

Browse files
committed
refine the code
1 parent da32c3f commit 7025458

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ build/Release
2828
node_modules
2929
.lock-wscript
3030
temp
31-
dist
31+
dist

config.demo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ module.exports = {
2020
})
2121
})
2222
}
23-
}
23+
};

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
"dependencies": {
3030
"co": "^4.6.0",
3131
"is-plain-object": "^2.0.3",
32-
"mime-db": "~1.21.0",
33-
"mime-type": "^3.0.5",
32+
"mime-types": "^2.1.17",
3433
"url": "^0.11.0"
3534
}
3635
}

rule.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const getSubPathExcludePattern = (reqUrl, exclude) => {
2929
const getMockResponse = function * (requestDetail, matchedProxy) {
3030
return new Promise((resolve, reject) => {
3131
const callback = (status, header, responseData) => {
32-
console.info('==> resolve in mock response, ', responseData);
32+
// console.info('==> resolve in mock response, ', responseData);
3333
resolve({
3434
statusCode: status,
3535
header,
@@ -44,12 +44,19 @@ const getMockResponse = function * (requestDetail, matchedProxy) {
4444
const { value: proxyOption } = matchedProxy;
4545

4646
if (typeof proxyOption === 'function') {
47-
proxyOption(requestDetail, getRes(requestDetail, callback));
47+
const req = {
48+
url: requestDetail.url,
49+
protocol: requestDetail.protocol,
50+
headers: requestDetail.requestOptions.headers,
51+
body: requestDetail.requestData
52+
};
53+
54+
proxyOption(req, getRes(requestDetail, callback));
4855
}
4956

5057
// Handle with local file
5158
if (typeof proxyOption === 'string' && !isRemote(proxyOption)) {
52-
getRes(requestDetail, callback).end(fs.readFileSync(proxyOption), 'utf-8');
59+
getRes(requestDetail, callback).end(fs.readFileSync(proxyOption, 'utf-8'));
5360
}
5461
});
5562
}
@@ -90,7 +97,6 @@ module.exports = {
9097
handleInverseProxy(requestDetail, matchedProxy);
9198
resolve(requestDetail);
9299
} else {
93-
// TODO local mock handler
94100
co(function * () {
95101
return yield getMockResponse(requestDetail, matchedProxy);
96102
})

util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { parse: parseUrl } = require('url');
22
const isPlainObject = require('is-plain-object');
3-
const mime = require('mime-type/with-db');
3+
const mime = require('mime-types');
44

55
function decodeParam(val) {
66
if (typeof val !== 'string' || val.length === 0) {

0 commit comments

Comments
 (0)