openapi-modifier
Version:
This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules
88 lines (87 loc) • 5.12 kB
JavaScript
;
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(),
code: zod_1.z.string().optional(),
contentType: zod_1.z.string().optional(),
correction: config_1.correctionConfigSchema.optional(),
patchMethod: config_1.patchMethodConfigSchema.optional(),
schemaDiff: config_1.openAPISchemaConfigSchema.optional(),
}).strict();
exports.configSchema = configSchema;
const getDefaultCode = (codes) => {
return (codes === null || codes === void 0 ? void 0 : codes.find(code => /^2/.test(code))) || null;
};
const processor = {
configSchema,
defaultConfig: {
patchMethod: 'merge',
},
processDocument: (openAPIFile, config, logger, ruleMeta) => {
var _a, _b;
const { patchMethod, schemaDiff, contentType, code, correction, endpointDescriptor } = config;
if (!endpointDescriptor) {
logger.errorMessage(factory_1.messagesFactory.ruleNotApply.requiredConfigField(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 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.errorMessage(factory_1.messagesFactory.ruleNotApply.withReason(ruleMeta, `Not found endpoint (same method) with descriptor: ${JSON.stringify(parsedEndpointDescriptor)}!`));
return openAPIFile;
}
const targetResponseCode = code || getDefaultCode(Object.keys(operationSchema.responses || {}));
if (!targetResponseCode) {
logger.errorMessage(factory_1.messagesFactory.ruleNotApply.withReason(ruleMeta, `Not found any endpoint code with descriptor: ${JSON.stringify(parsedEndpointDescriptor)}!`));
return openAPIFile;
}
const responseSchema = (_a = operationSchema.responses) === null || _a === void 0 ? void 0 : _a[targetResponseCode];
if ((0, refs_1.checkIsRefSchema)(responseSchema)) {
logger.warning(`responseSchema is ref: ${JSON.stringify(code)}!`);
return openAPIFile;
}
const targetResponseContentSchema = ((_b = responseSchema === null || responseSchema === void 0 ? void 0 : responseSchema.content) === null || _b === void 0 ? void 0 : _b[contentType]) || null;
if (contentType && !targetResponseContentSchema) {
logger.errorMessage(factory_1.messagesFactory.ruleNotApply.withReason(ruleMeta, `Not found endpoint (same code and contentType): ${JSON.stringify(parsedEndpointDescriptor)} ${contentType}`));
return openAPIFile;
}
const responseContentSchemas = targetResponseContentSchema ? [
targetResponseContentSchema
] : Object.values((responseSchema === null || responseSchema === void 0 ? void 0 : responseSchema.content) || {});
responseContentSchemas.forEach((responseContentSchema) => {
var _a;
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)(responseContentSchema.schema, parsedCorrection, (0, patch_1.patchSchema)(logger, (0, object_path_1.getObjectPath)(responseContentSchema.schema, parsedCorrection), patchMethod, schemaDiff));
}
else {
responseContentSchema.schema = (0, patch_1.patchSchema)(logger, responseContentSchema.schema, patchMethod, schemaDiff);
}
});
return openAPIFile;
},
};
exports.default = processor;