openapi-route-expander
Version:
A utility for resolving $ref in OpenAPI YAML routes, generating new YAML files without $ref, ready for OpenAPI processing.
108 lines • 4.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractFileName = extractFileName;
exports.replaceRelativeToAbsolutePath = replaceRelativeToAbsolutePath;
exports.fixTrailingSlash = fixTrailingSlash;
exports.mergeNestedPaths = mergeNestedPaths;
exports.fixOpenApiAbsoluteRoute = fixOpenApiAbsoluteRoute;
exports.fixOpenApiYamlStringIssue = fixOpenApiYamlStringIssue;
const path_1 = __importDefault(require("path"));
const file_util_1 = require("./util/file.util");
function extractFileName(filePath) {
return filePath.split('/').pop();
}
function replaceRelativeToAbsolutePath(json, basePath, fileName) {
if (json instanceof Array) {
return json.map((each) => replaceRelativeToAbsolutePath(each, basePath, fileName));
}
if (typeof json !== 'object') {
return json;
}
return Object.keys(json).reduce((result, key) => {
if (key === '$ref' && json[key][0] === '#') {
return { ...result, [key]: `${basePath}/${fileName}/${json[key]}` };
}
if (key === '$ref' && json[key][0] === '.') {
return { ...result, [key]: `${basePath}/${json[key]}` };
}
if (typeof json[key] === 'object') {
return { ...result, [key]: replaceRelativeToAbsolutePath(json[key], basePath, fileName) };
}
return result;
}, json);
}
function extractSubPath(json, paths) {
if (!paths.length) {
return json;
}
const item = paths.shift();
return extractSubPath(item === '' ? json : json[item], paths);
}
function fixTrailingSlash(paths) {
return Object.keys(paths).reduce((result, key) => {
const keyWithoutEndingSlash = key === '/' || key[key.length - 1] !== '/'
? key
: key.substring(0, key.length - 1);
return { ...result, [keyWithoutEndingSlash]: paths[key] };
}, {});
}
function mergeNestedPaths(paths, filePath, pathPrefix = '') {
const filePathSplit = filePath.split('/');
const currentFolder = filePathSplit.slice(0, filePathSplit.length - 1).join('/');
return Object.keys(paths).reduce((result, key) => {
if (['get', 'post', 'put', 'delete'].includes(key)) {
const fileName = extractFileName(filePath);
return {
...result,
[pathPrefix]: {
...(result[pathPrefix] || {}),
[key]: replaceRelativeToAbsolutePath(paths[key], currentFolder, fileName),
},
};
}
if (key === '$ref') {
const [nextYamlFileRelativePath, nextYamlSubPath = '/'] = paths[key].split('#');
const nextYamlFileAbsolutePath = nextYamlFileRelativePath[0] === '/'
? nextYamlFileRelativePath
: `${currentFolder}/${nextYamlFileRelativePath}`;
const nextYamlJSON = file_util_1.FileUtil.readYaml(nextYamlFileAbsolutePath);
const nextYamlSubPaths = nextYamlSubPath.split('/');
const nextPaths = extractSubPath(nextYamlJSON, nextYamlSubPaths);
const mergedPaths = mergeNestedPaths(nextPaths, nextYamlFileAbsolutePath, pathPrefix);
return { ...result, ...mergedPaths };
}
const newPathPrefix = `${pathPrefix}${key}`.replace(/\/\//g, '/');
const mergedPaths = mergeNestedPaths(paths[key], filePath, newPathPrefix);
return { ...result, ...mergedPaths };
}, {});
}
function fixOpenApiAbsoluteRoute(json, basePath) {
if (json instanceof Array) {
return json.map((each) => fixOpenApiAbsoluteRoute(each, basePath));
}
if (typeof json !== 'object') {
return json;
}
return Object.keys(json).reduce((result, key) => {
if (key === '$ref' && json[key][0] === '/') {
const [filePath, componentReference] = json[key].split('#');
return { ...result, [key]: `${path_1.default.relative(basePath, filePath)}${componentReference ? `#${componentReference}` : ''}` };
}
if (typeof json[key] === 'object') {
return { ...result, [key]: fixOpenApiAbsoluteRoute(json[key], basePath) };
}
return result;
}, json);
}
function fixOpenApiYamlStringIssue(yaml) {
return yaml.split('\n').map((each) => {
if (each.includes('$ref')) {
return `${each.replace('$ref: ', '$ref: "')}"`;
}
return each;
}).join('\n');
}
//# sourceMappingURL=openapi-json.js.map