|
|
@@ -1,10 +1,11 @@
|
|
|
-const mockFile = require('./mock/index');
|
|
|
const pathToRegexp = require('path-to-regexp');
|
|
|
-const debug = console.log;
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
|
|
+const mockFile = require('./mock/index');
|
|
|
+
|
|
|
const BODY_PARSED_METHODS = ['post', 'put', 'patch'];
|
|
|
|
|
|
+const debug = console.log;
|
|
|
function parseKey(key) {
|
|
|
let method = 'get';
|
|
|
let path = key;
|
|
|
@@ -20,7 +21,14 @@ function parseKey(key) {
|
|
|
}
|
|
|
|
|
|
function createHandler(method, path, handler) {
|
|
|
- return function(req, res, next) {
|
|
|
+ return (req, res, next) => {
|
|
|
+ function sendData() {
|
|
|
+ if (typeof handler === 'function') {
|
|
|
+ handler(req, res, next);
|
|
|
+ } else {
|
|
|
+ res.json(handler);
|
|
|
+ }
|
|
|
+ }
|
|
|
if (BODY_PARSED_METHODS.includes(method)) {
|
|
|
bodyParser.json({ limit: '5mb', strict: false })(req, res, () => {
|
|
|
bodyParser.urlencoded({ limit: '5mb', extended: true })(req, res, () => {
|
|
|
@@ -30,14 +38,6 @@ function createHandler(method, path, handler) {
|
|
|
} else {
|
|
|
sendData();
|
|
|
}
|
|
|
-
|
|
|
- function sendData() {
|
|
|
- if (typeof handler === 'function') {
|
|
|
- handler(req, res, next);
|
|
|
- } else {
|
|
|
- res.json(handler);
|
|
|
- }
|
|
|
- }
|
|
|
};
|
|
|
}
|
|
|
|
|
|
@@ -63,6 +63,24 @@ const mockData = normalizeConfig(mockFile);
|
|
|
function matchMock(req) {
|
|
|
const { path: exceptPath } = req;
|
|
|
const exceptMethod = req.method.toLowerCase();
|
|
|
+ function decodeParam(val) {
|
|
|
+ if (typeof val !== 'string' || val.length === 0) {
|
|
|
+ return val;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ return decodeURIComponent(val);
|
|
|
+ } catch (err) {
|
|
|
+ if (err instanceof URIError) {
|
|
|
+ err.message = `Failed to decode param ' ${val} '`;
|
|
|
+ err.statusCode = 400;
|
|
|
+ err.status = 400;
|
|
|
+ }
|
|
|
+
|
|
|
+ throw err;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // eslint-disable-next-line no-restricted-syntax
|
|
|
for (const mock of mockData) {
|
|
|
const { method, re, keys } = mock;
|
|
|
if (method === exceptMethod) {
|
|
|
@@ -70,7 +88,7 @@ function matchMock(req) {
|
|
|
if (match) {
|
|
|
const params = {};
|
|
|
|
|
|
- for (let i = 1; i < match.length; i = i + 1) {
|
|
|
+ for (let i = 1; i < match.length; i += 1) {
|
|
|
const key = keys[i - 1];
|
|
|
const prop = key.name;
|
|
|
const val = decodeParam(match[i]);
|
|
|
@@ -85,33 +103,13 @@ function matchMock(req) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- function decodeParam(val) {
|
|
|
- if (typeof val !== 'string' || val.length === 0) {
|
|
|
- return val;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- return decodeURIComponent(val);
|
|
|
- } catch (err) {
|
|
|
- if (err instanceof URIError) {
|
|
|
- err.message = `Failed to decode param ' ${val} '`;
|
|
|
- err.status = err.statusCode = 400;
|
|
|
- }
|
|
|
-
|
|
|
- throw err;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return mockData.filter(({ method, re }) => {
|
|
|
- return method === exceptMethod && re.test(exceptPath);
|
|
|
- })[0];
|
|
|
+ return mockData.filter(({ method, re }) => method === exceptMethod && re.test(exceptPath))[0];
|
|
|
}
|
|
|
module.exports = (req, res, next) => {
|
|
|
const match = matchMock(req);
|
|
|
if (match) {
|
|
|
debug(`mock matched: [${match.method}] ${match.path}`);
|
|
|
return match.handler(req, res, next);
|
|
|
- } else {
|
|
|
- return next();
|
|
|
}
|
|
|
+ return next();
|
|
|
};
|