openapi-modifier
Version:
This package allows you to automate the process of modifying OpenAPI specifications by applying a set of predefined rules
69 lines (68 loc) • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseSimpleDescriptor = void 0;
const ROOT_ARRAY_PLACEHOLDER = '[]';
const checkIsArray = (rawPart) => {
return !!rawPart && /\[\]$/.test(rawPart);
};
const checkIsOneOff = (rawPart) => {
if (!rawPart) {
return false;
}
return /^oneOf\[\d+\]$/.test(rawPart);
};
const checkIsAllOf = (rawPart) => {
if (!rawPart) {
return false;
}
return /^allOf\[\d+\]$/.test(rawPart);
};
const checkIsAnyOf = (rawPart) => {
if (!rawPart) {
return false;
}
return /^anyOf\[\d+\]$/.test(rawPart);
};
const clearArrayPostfix = (rawPart) => rawPart.replace(/\[\]$/, '');
const parseSimpleDescriptor = (descriptor, options) => {
if (!descriptor) {
return null;
}
const clearDescriptor = descriptor.trim();
if (!clearDescriptor) {
return null;
}
const parts = clearDescriptor.split('.').map(value => {
return value.trim();
}).filter(value => !!value);
if (!(parts === null || parts === void 0 ? void 0 : parts.length)) {
return null;
}
const rawComponentName = (options === null || options === void 0 ? void 0 : options.isContainsName) ? parts[0] : null;
const rawCorrection = (options === null || options === void 0 ? void 0 : options.isContainsName) ? parts.slice(1) : parts;
const correctionParts = rawCorrection.reduce((acc, rawPart) => {
if (checkIsOneOff(rawPart) || checkIsAllOf(rawPart) || checkIsAnyOf(rawPart)) {
acc.push(rawPart);
return acc;
}
if (ROOT_ARRAY_PLACEHOLDER !== rawPart) {
acc.push('properties');
}
if (checkIsArray(rawPart)) {
if (ROOT_ARRAY_PLACEHOLDER !== rawPart) {
acc.push(clearArrayPostfix(rawPart));
}
acc.push('items');
}
else {
acc.push(rawPart);
}
return acc;
}, checkIsArray(rawComponentName) ? ['items'] : []);
const correction = correctionParts.join('.');
return {
name: rawComponentName ? clearArrayPostfix(rawComponentName) : null,
correction: correction || undefined,
};
};
exports.parseSimpleDescriptor = parseSimpleDescriptor;