UNPKG

openapi-modifier

Version:

This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules

82 lines (81 loc) 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.configSchema = void 0; const zod_1 = require("zod"); const each_operation_1 = require("../common/utils/iterators/each-operation"); const factory_1 = require("../../logger/messages/factory"); const configSchema = zod_1.z .object({ map: zod_1.z.record(zod_1.z.string(), zod_1.z.string()), ignoreOperationCollisions: zod_1.z.boolean().optional(), }) .strict(); exports.configSchema = configSchema; const getOperationHash = ({ method, path }) => `[${method}] - ${path}`; const changeBasePath = (sourcePath, fromPrefix, toPrefix) => { return sourcePath.replace(fromPrefix, toPrefix); }; const processor = { configSchema, defaultConfig: { map: {}, ignoreOperationCollisions: false, }, processDocument: (openAPIFile, config, logger) => { var _a; const { map, ignoreOperationCollisions } = config; logger.trace(`Check paths ..`); if (!ignoreOperationCollisions) { const sourceOperationHashes = []; const collisionOperationHashes = []; (0, each_operation_1.forEachOperation)(openAPIFile, ({ path, method }) => { const operationHash = getOperationHash({ method, path }); sourceOperationHashes.push(operationHash); }); (0, each_operation_1.forEachOperation)(openAPIFile, ({ path, method }) => { Object.keys(map).forEach((fromPrefix) => { const toPrefix = map[fromPrefix] || ''; if (path.startsWith(fromPrefix)) { const preparedPath = changeBasePath(path, fromPrefix, toPrefix); const preparedOperationHash = getOperationHash({ method, path: preparedPath }); if (sourceOperationHashes.includes(preparedOperationHash)) { collisionOperationHashes.push(preparedOperationHash); } } }); }); if (collisionOperationHashes === null || collisionOperationHashes === void 0 ? void 0 : collisionOperationHashes.length) { const errorMessage = `Failed to change endpoint basepath's, operaion conflicts: \n ${collisionOperationHashes.join('\n')}`; logger.errorMessage(errorMessage); return openAPIFile; } } const pathsSchema = (_a = openAPIFile.document) === null || _a === void 0 ? void 0 : _a.paths; let usageCount = {}; const increaseUsageCount = (fromPrefix) => { usageCount[fromPrefix] = (usageCount[fromPrefix] || 0) + 1; }; Object.keys(pathsSchema || {}).forEach((pathKey) => { Object.keys(map).forEach((fromPrefix) => { const toPrefix = map[fromPrefix] || ''; if (pathKey.startsWith(fromPrefix)) { logger.trace(`Matching "${pathKey}" with "${fromPrefix}"`); increaseUsageCount(fromPrefix); const preparedPath = changeBasePath(pathKey, fromPrefix, toPrefix); if (pathsSchema) { pathsSchema[preparedPath] = pathsSchema[pathKey]; delete pathsSchema[pathKey]; } } }); }); logger.trace(`Usage count: ${JSON.stringify(usageCount)}`); Object.keys(map).forEach((fromPrefix) => { if (!usageCount[fromPrefix]) { logger.warning(factory_1.messagesFactory.configField.notUsaged(`map.${fromPrefix}`, `Not found endpoints with prefix "${fromPrefix}"`)); } }); return openAPIFile; }, }; exports.default = processor;