UNPKG

openapi-modifier

Version:

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

143 lines (142 loc) 8.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.configSchema = void 0; const zod_1 = require("zod"); const patch_1 = require("../common/utils/patch"); const config_1 = require("../common/config"); const get_operation_schema_1 = require("../common/utils/get-operation-schema"); const find_parameter_index_1 = require("../common/utils/find-parameter-index"); const refs_1 = require("../common/utils/refs"); const object_path_1 = require("../common/utils/object-path"); const factory_1 = require("../../logger/messages/factory"); const parse_endpoint_descriptor_1 = require("../common/utils/config/parse-endpoint-descriptor"); const parse_simple_descriptor_1 = require("../common/utils/config/parse-simple-descriptor"); const configSchema = zod_1.z .object({ endpointDescriptor: config_1.anyEndpointDescriptorConfigSchema.optional(), parameterDescriptor: config_1.endpointParameterWithCorrectionDescriptorConfigSchema.optional(), patchMethod: config_1.patchMethodConfigSchema.optional(), schemaDiff: config_1.openAPISchemaConfigSchema.optional(), objectDiff: zod_1.z .object({ name: zod_1.z.string().optional(), in: config_1.parameterInConfigSchema.optional(), required: zod_1.z.boolean().optional(), }) .strict() .optional(), }) .strict(); exports.configSchema = configSchema; const processor = { configSchema, defaultConfig: { patchMethod: 'merge' }, processDocument: (openAPIFile, config, logger, ruleMeta) => { var _a, _b, _c, _d, _e; const { endpointDescriptor, parameterDescriptor, patchMethod, schemaDiff, objectDiff } = config; if (!patchMethod) { logger.errorMessage(factory_1.messagesFactory.ruleNotApply.requiredConfigField(ruleMeta, 'patchMethod')); return openAPIFile; } if (!parameterDescriptor) { logger.errorMessage(factory_1.messagesFactory.ruleNotApply.requiredConfigField(ruleMeta, 'parameterDescriptor')); return openAPIFile; } if (!endpointDescriptor) { logger.trace(`Empty descriptor: ${JSON.stringify(endpointDescriptor)}`); const componentParameterSchemas = ((_b = (_a = openAPIFile === null || openAPIFile === void 0 ? void 0 : openAPIFile.document) === null || _a === void 0 ? void 0 : _a.components) === null || _b === void 0 ? void 0 : _b.parameters) || {}; const targetComponentParameterKey = Object.keys(componentParameterSchemas || {}).find((key) => { const componentParameterSchema = componentParameterSchemas[key]; if ((0, refs_1.checkIsRefSchema)(componentParameterSchema)) { return false; } if (componentParameterSchema.name === parameterDescriptor.name && componentParameterSchema.in === parameterDescriptor.in) { return true; } }); if (!targetComponentParameterKey) { logger.warning(`Rule not apply: not found parameter with descriptor ${parameterDescriptor} !`); return openAPIFile; } const targetComponentParameter = componentParameterSchemas[targetComponentParameterKey]; if (!targetComponentParameter || (0, refs_1.checkIsRefSchema)(targetComponentParameter)) { return openAPIFile; } if (schemaDiff) { logger.trace(`Apply schemaDiff: ${schemaDiff}`); const rawCorrection = (parameterDescriptor === null || parameterDescriptor === void 0 ? void 0 : parameterDescriptor.correction) || null; const parsedCorrection = (_c = (0, parse_simple_descriptor_1.parseSimpleDescriptor)(rawCorrection, { isContainsName: false })) === null || _c === void 0 ? void 0 : _c.correction; if (parsedCorrection) { (0, object_path_1.setObjectProp)(targetComponentParameter.schema, parsedCorrection, (0, patch_1.patchSchema)(logger, (0, object_path_1.getObjectPath)(targetComponentParameter.schema, parsedCorrection), patchMethod, schemaDiff)); } else { targetComponentParameter.schema = (0, patch_1.patchSchema)(logger, targetComponentParameter.schema, patchMethod, schemaDiff); } } if (objectDiff) { logger.trace(`Apply objectDiff: ${objectDiff}`); if ((objectDiff === null || objectDiff === void 0 ? void 0 : objectDiff.name) !== undefined) { targetComponentParameter.name = objectDiff.name; } if ((objectDiff === null || objectDiff === void 0 ? void 0 : objectDiff.in) !== undefined) { targetComponentParameter.in = objectDiff.in; } if ((objectDiff === null || objectDiff === void 0 ? void 0 : objectDiff.required) !== undefined) { targetComponentParameter.required = objectDiff.required; } } return openAPIFile; } const parsedEndpointDescriptor = (0, parse_endpoint_descriptor_1.parseAnyEndpointDescriptor)(endpointDescriptor, logger); if (!parsedEndpointDescriptor) { logger.errorMessage(factory_1.messagesFactory.ruleNotApply.failedToParseDescriptor(ruleMeta, 'endpointDescriptor')); return openAPIFile; } const operationSchema = (0, get_operation_schema_1.getOperationSchema)(openAPIFile, parsedEndpointDescriptor.path, parsedEndpointDescriptor.method); if (!operationSchema) { logger.warning(`Empty operationSchema, not found endpoint: ${JSON.stringify(parsedEndpointDescriptor)}`); return openAPIFile; } const targetParameterIndex = (0, find_parameter_index_1.findParameterIndex)(operationSchema.parameters, parameterDescriptor.name, parameterDescriptor.in); if (targetParameterIndex === null) { logger.warning(`Not found parameter: ${JSON.stringify(parsedEndpointDescriptor)}`); return openAPIFile; } const targetParameterSchema = (_d = operationSchema.parameters) === null || _d === void 0 ? void 0 : _d[targetParameterIndex]; if (!targetParameterSchema) { logger.warning(`Not found parameter: ${JSON.stringify(parsedEndpointDescriptor)}`); return openAPIFile; } if ((0, refs_1.checkIsRefSchema)(targetParameterSchema)) { logger.warning(`Descriptor should not refer to links: ${JSON.stringify(parsedEndpointDescriptor)}`); return openAPIFile; } if (schemaDiff) { logger.trace(`Apply schemaDiff: ${schemaDiff}`); const rawCorrection = (parameterDescriptor === null || parameterDescriptor === void 0 ? void 0 : parameterDescriptor.correction) || null; const parsedCorrection = (_e = (0, parse_simple_descriptor_1.parseSimpleDescriptor)(rawCorrection, { isContainsName: false })) === null || _e === void 0 ? void 0 : _e.correction; if (parsedCorrection) { (0, object_path_1.setObjectProp)(targetParameterSchema.schema, parsedCorrection, (0, patch_1.patchSchema)(logger, (0, object_path_1.getObjectPath)(targetParameterSchema.schema, parsedCorrection), patchMethod, schemaDiff)); } else { targetParameterSchema.schema = (0, patch_1.patchSchema)(logger, targetParameterSchema.schema, patchMethod, schemaDiff); } } if (objectDiff) { logger.trace(`Apply objectDiff: ${objectDiff}`); if ((objectDiff === null || objectDiff === void 0 ? void 0 : objectDiff.name) !== undefined) { targetParameterSchema.name = objectDiff.name; } if ((objectDiff === null || objectDiff === void 0 ? void 0 : objectDiff.in) !== undefined) { targetParameterSchema.in = objectDiff.in; } if ((objectDiff === null || objectDiff === void 0 ? void 0 : objectDiff.required) !== undefined) { targetParameterSchema.required = objectDiff.required; } } return openAPIFile; }, }; exports.default = processor;