UNPKG

openapi-modifier

Version:

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

81 lines (80 loc) 4.72 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 refs_1 = require("../common/utils/refs"); const get_operation_schema_1 = require("../common/utils/get-operation-schema"); 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(), contentType: zod_1.z.string().optional(), correction: config_1.correctionConfigSchema.optional(), patchMethod: config_1.patchMethodConfigSchema.optional(), schemaDiff: config_1.openAPISchemaConfigSchema.optional(), }); exports.configSchema = configSchema; const processor = { configSchema, defaultConfig: { patchMethod: 'merge', }, processDocument: (openAPIFile, config, logger, ruleMeta) => { var _a; const { patchMethod, schemaDiff, contentType, correction, endpointDescriptor } = config; if (!endpointDescriptor) { logger.errorMessage(factory_1.messagesFactory.ruleNotApply.requiredConfigField(ruleMeta, 'endpointDescriptor')); 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; } if (!patchMethod) { logger.errorMessage(factory_1.messagesFactory.ruleNotApply.requiredConfigField(ruleMeta, 'patchMethod')); return openAPIFile; } if (!schemaDiff) { logger.errorMessage(factory_1.messagesFactory.ruleNotApply.requiredConfigField(ruleMeta, 'schemaDiff')); return openAPIFile; } const operationSchema = (0, get_operation_schema_1.getOperationSchema)(openAPIFile, parsedEndpointDescriptor.path, parsedEndpointDescriptor.method); if (!operationSchema) { logger.warning(factory_1.messagesFactory.ruleNotApply.withReason(ruleMeta, `Not found endpoint (same path) with descriptor: ${JSON.stringify(parsedEndpointDescriptor)}!`)); return openAPIFile; } const requestBodySchema = operationSchema === null || operationSchema === void 0 ? void 0 : operationSchema.requestBody; if ((0, refs_1.checkIsRefSchema)(requestBodySchema)) { logger.warning(factory_1.messagesFactory.ruleNotApply.withReason(ruleMeta, `requestBodySchema is ref: ${JSON.stringify(endpointDescriptor)}!`)); return openAPIFile; } const targetRequestBodyContentSchema = ((_a = requestBodySchema === null || requestBodySchema === void 0 ? void 0 : requestBodySchema.content) === null || _a === void 0 ? void 0 : _a[contentType]) || null; if (contentType && !targetRequestBodyContentSchema) { logger.warning(factory_1.messagesFactory.ruleNotApply.withReason(ruleMeta, `Not found endpoint (same requestBody) with contentType: "${contentType}"!`)); return openAPIFile; } const requestBodyContentSchemas = targetRequestBodyContentSchema ? [ targetRequestBodyContentSchema ] : Object.values((requestBodySchema === null || requestBodySchema === void 0 ? void 0 : requestBodySchema.content) || {}); requestBodyContentSchemas.forEach((requestBodyContentSchema) => { var _a; if (!requestBodyContentSchema) { return; } const parsedCorrection = ((_a = (0, parse_simple_descriptor_1.parseSimpleDescriptor)(correction, { isContainsName: false })) === null || _a === void 0 ? void 0 : _a.correction) || null; if (parsedCorrection) { (0, object_path_1.setObjectProp)(requestBodyContentSchema.schema, parsedCorrection, (0, patch_1.patchSchema)(logger, (0, object_path_1.getObjectPath)(requestBodyContentSchema.schema, parsedCorrection), patchMethod, schemaDiff)); } else { requestBodyContentSchema.schema = (0, patch_1.patchSchema)(logger, requestBodyContentSchema.schema, patchMethod, schemaDiff); } }); return openAPIFile; }, }; exports.default = processor;