openapi-modifier
Version:
This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules
66 lines (65 loc) • 3.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.configSchema = void 0;
const zod_1 = require("zod");
const config_1 = require("../common/config");
const get_operation_schema_1 = require("../common/utils/get-operation-schema");
const refs_1 = require("../common/utils/refs");
const parse_endpoint_descriptor_1 = require("../common/utils/config/parse-endpoint-descriptor");
const factory_1 = require("../../logger/messages/factory");
const configSchema = zod_1.z
.object({
endpointDescriptor: config_1.anyEndpointDescriptorConfigSchema.optional(),
parameterDescriptor: config_1.endpointParameterDescriptorConfigSchema.optional(),
})
.strict();
exports.configSchema = configSchema;
const processor = {
configSchema,
defaultConfig: {},
processDocument: (openAPIFile, config, logger, ruleMeta) => {
var _a, _b, _c, _d;
const { endpointDescriptor, parameterDescriptor } = config;
if (!endpointDescriptor) {
logger.trace(`Empty endpointDescriptor: ${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 === null || parameterDescriptor === void 0 ? void 0 : parameterDescriptor.name) && componentParameterSchema.in === (parameterDescriptor === null || parameterDescriptor === void 0 ? void 0 : parameterDescriptor.in)) {
return true;
}
});
if (targetComponentParameterKey) {
delete componentParameterSchemas[targetComponentParameterKey];
}
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(`Not found operation: ${JSON.stringify(endpointDescriptor)}`);
return openAPIFile;
}
if (!parameterDescriptor) {
logger.warning(`Empty parameterDescriptor: ${JSON.stringify(parameterDescriptor)}`);
return openAPIFile;
}
const targetParameterIndex = (_c = operationSchema.parameters) === null || _c === void 0 ? void 0 : _c.findIndex((parameter) => {
return 'name' in parameter && parameter.name === (parameterDescriptor === null || parameterDescriptor === void 0 ? void 0 : parameterDescriptor.name) && parameter.in === (parameterDescriptor === null || parameterDescriptor === void 0 ? void 0 : parameterDescriptor.in);
});
if (targetParameterIndex === -1 || typeof targetParameterIndex !== 'number') {
logger.warning(`Not found parameter: ${JSON.stringify(endpointDescriptor)}`);
return openAPIFile;
}
(_d = operationSchema.parameters) === null || _d === void 0 ? void 0 : _d.splice(targetParameterIndex, 1);
return openAPIFile;
},
};
exports.default = processor;