mimus-mocker
Version:
MimusMocker is a unified API mocking middleware which can be used for providing API mocks quickly i.e even before writing a single line of actual api into your [Routers](https://expressjs.com/en/guide/routing.html).
69 lines (53 loc) • 1.69 kB
JavaScript
/**
* Created by ajay.meher on 28/06/17.
*/
;
const matchPattern = require('url-matcher').matchPattern;
module.exports = function (options) {
return function _mimusMocker(req, res, next) {
if (!options
|| !options.isEligible
|| (options.mocks && !Array.isArray(options.mocks)))
return next();
let shouldWait = false;
for (let i = 0; i < options.mocks.length; i++) {
let result;
const mock = options.mocks[i];
if (!(mock && mock.isEligible
&& req.method === mock.method)) {
continue;
}
let matchedResult = matchPattern(mock.url, req.originalUrl);
if (!matchedResult) {
continue;
}
if (matchedResult.paramNames && matchedResult.paramNames.length > 0) {
_embedParams(req, matchedResult);
}
result = mock.responseData || {};
if (!mock.additionalValidator) {
return res.status(200).json(result);
}
shouldWait = true;
mock.additionalValidator(req, res, function (overrideResult, updatedRes) {
return overrideResult ? updatedRes : res.status(200).json(result);
});
break;
}
if (!shouldWait) next();
}
};
// embed params as user might have applied Mimus directly.
function _embedParams(req, matchedResult) {
if (!matchedResult) {
return;
}
if (matchedResult.paramNames && matchedResult.paramNames.length > 0) {
for (let i = 0; i < matchedResult.paramNames.length; i++) {
req.params[matchedResult.paramNames[i] + ""] = matchedResult.paramValues[i];
}
}
if (matchedResult.remainingPathname) {
req.remainingPath = matchedResult.remainingPathname;
}
}