UNPKG

@paultaku/node-mock-server

Version:

A TypeScript-based mock server with automatic Swagger-based mock file generation

133 lines (121 loc) 4.32 kB
/******/ (() => { // webpackBootstrap /******/ "use strict"; /******/ var __webpack_modules__ = ({ /***/ 537: /***/ (function(__unused_webpack_module, exports, __webpack_require__) { var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.getStatusJsonPath = getStatusJsonPath; exports.readStatusJson = readStatusJson; exports.writeStatusJson = writeStatusJson; exports.loadAllStatusJson = loadAllStatusJson; const fs_extra_1 = __importDefault(__webpack_require__(652)); const path_1 = __importDefault(__webpack_require__(928)); const DEFAULT_MOCK_FILE = "successful-operation-200.json"; /** * 获取 status.json 路径 * @param mockRoot mock 根目录 * @param endpointPath 形如 /pet/{petId} * @param method HTTP 方法(GET/POST/...) */ function getStatusJsonPath(mockRoot, endpointPath, method) { return path_1.default.join(mockRoot, ...endpointPath.replace(/^\//, "").split("/"), method.toUpperCase(), "status.json"); } /** * 读取 status.json * @param statusPath status.json 文件路径 * @returns StatusJson 对象或 null */ async function readStatusJson(statusPath) { try { const data = await fs_extra_1.default.readJson(statusPath); if (typeof data.selected === "string" && data.selected.endsWith(".json")) { return data; } return null; } catch { return null; } } /** * 写入 status.json(原子写入) * @param statusPath status.json 文件路径 * @param selected 选中的 mock 文件名 */ async function writeStatusJson(statusPath, selected) { await fs_extra_1.default.writeJson(statusPath, { selected }, { spaces: 2 }); } /** * 初始化所有 endpoint 的 mock 选择状态 * @param mockRoot mock 根目录 * @param templates 所有 endpoint 模板数组 * @returns Map<stateKey, selectedMockFile> */ async function loadAllStatusJson(mockRoot, templates) { const stateMap = new Map(); for (const template of templates) { const method = template[template.length - 1] || ""; const endpointPath = "/" + template.slice(0, -1).join("/"); if (!method) continue; // 跳过无效 method const statusPath = getStatusJsonPath(mockRoot, endpointPath, method); const status = await readStatusJson(statusPath); if (status && status.selected) { stateMap.set(`${method.toUpperCase()}:${endpointPath}`, status.selected); } else { // fallback stateMap.set(`${method.toUpperCase()}:${endpointPath}`, DEFAULT_MOCK_FILE); } } return stateMap; } /***/ }), /***/ 652: /***/ ((module) => { module.exports = require("fs-extra"); /***/ }), /***/ 928: /***/ ((module) => { module.exports = require("path"); /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /******/ // startup /******/ // Load entry module and return exports /******/ // This entry module is referenced by other modules so it can't be inlined /******/ var __webpack_exports__ = __webpack_require__(537); /******/ module.exports = __webpack_exports__; /******/ /******/ })() ; //# sourceMappingURL=status-manager.js.map