mihawk
Version:
A tiny & simple mock server tool, support json,js,cjs,ts(typescript).
49 lines (48 loc) • 2.45 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const color_cc_1 = __importDefault(require("color-cc"));
const print_1 = require("../utils/print");
const str_1 = require("../utils/str");
const consts_1 = require("../consts");
const path_1 = require("../utils/path");
function default_1(routes) {
var _a;
const needCheckRoutes = ((_a = Object.keys(routes)) === null || _a === void 0 ? void 0 : _a.length) > 0;
const routeKvEntries = needCheckRoutes ? Object.entries(routes) : [];
return function (ctx, next) {
return __awaiter(this, void 0, void 0, function* () {
const { skipDefaultMock, mockRelPath, routePath, path } = ctx;
if (skipDefaultMock || !needCheckRoutes) {
yield next();
}
else {
let matched = null;
for (const [k, v] of routeKvEntries) {
if ((0, str_1.isMatchPatterns)(path, k) || (0, str_1.isMatchPatterns)(routePath, k)) {
matched = { route: k, mockFile: v };
break;
}
}
if (matched && matched.mockFile) {
const newMockRelPath = (0, path_1.unixifyPath)(matched.mockFile);
print_1.Printer.log('mdw-routes:', `Reset route-logic: ${color_cc_1.default.cyan(mockRelPath)} ${consts_1.LOG_ARROW} ${color_cc_1.default.green(newMockRelPath)} ↩️`);
ctx.mockRelPath = newMockRelPath;
}
yield next();
}
});
};
}
exports.default = default_1;