mihawk
Version:
A tiny & simple mock server tool, support json,js,cjs,ts(typescript).
48 lines (47 loc) • 2.62 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 path_1 = require("path");
const free_dedupe_1 = __importDefault(require("free-dedupe"));
const print_1 = require("../utils/print");
const str_1 = require("../utils/str");
const consts_1 = require("../consts");
const path_2 = require("../utils/path");
function default_1(options) {
print_1.Debugger.log('mdw-com: init...', options);
const { logConfig } = options || {};
let { ignoreRoutes } = logConfig || {};
ignoreRoutes = (0, free_dedupe_1.default)(ignoreRoutes || []).map(ignRt => ignRt.trim().replace(/\s+/g, ' '));
const needCheckIgnore = (ignoreRoutes === null || ignoreRoutes === void 0 ? void 0 : ignoreRoutes.length) > 0;
return function common(ctx, next) {
return __awaiter(this, void 0, void 0, function* () {
const { method, path } = ctx;
const routePath = `${method.toUpperCase()} ${path}`;
const disableLogPrint = needCheckIgnore && ((0, str_1.isMatchPatterns)(routePath, ignoreRoutes) || (0, str_1.isMatchPatterns)(path, ignoreRoutes));
!disableLogPrint && console.log();
print_1.Debugger.log('mdw-com: >>', routePath);
ctx.mockRelPath = (0, path_2.unixifyPath)((0, path_1.join)(method, path));
ctx.routePath = routePath;
ctx.disableLogPrint = disableLogPrint;
const startTime = Date.now();
yield next();
const keepTime = `${Date.now() - startTime}ms`;
ctx.set('X-Mock-Time', keepTime);
ctx.set('Server-Timing', `inner;dur=${keepTime}`);
ctx.set('X-Powered-By', consts_1.PKG_NAME);
print_1.Debugger.log(`mdw-com: << ${ctx.type}`, routePath);
});
};
}
exports.default = default_1;