@opendoc/openapi-shaking
Version:
Remove the useless components and paths of openapi document.
63 lines (62 loc) • 2.75 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "ramda", "./utils/is-valid-method", "./shaking-orphaned-components"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.openapiShakingSync = exports.openapiShaking = void 0;
const R = require("ramda");
const is_valid_method_1 = require("./utils/is-valid-method");
const shaking_orphaned_components_1 = require("./shaking-orphaned-components");
async function openapiShaking(document, filter) {
let doc = document;
const pairs = await Promise.all(Object.entries(document.paths).map(async ([path, methods]) => {
if (!methods)
return [path, methods];
const pairs = await Promise.all(Object.entries(methods || {})
.map(async ([method, operation]) => {
if (!(0, is_valid_method_1.isValidMethod)(method))
return [method, operation];
if (await filter(path, method, operation)) {
return [method, operation];
}
}));
const pairsWithoutNil = pairs.filter(R.isNotNil);
if (!pairsWithoutNil.length)
return undefined;
return [
path,
R.fromPairs(pairsWithoutNil),
];
}));
const paths = R.fromPairs(pairs.filter(R.isNotNil));
doc = R.assoc('paths', paths, doc);
return (0, shaking_orphaned_components_1.openapiShakingOrphanedComponents)(doc);
}
exports.openapiShaking = openapiShaking;
function openapiShakingSync(document, filter) {
let doc = document;
for (const path in document.paths) {
for (const method in document.paths[path]) {
if (!(0, is_valid_method_1.isValidMethod)(method))
continue;
const operation = R.path(['paths', path, method], document);
if (!operation)
continue;
if (!filter(path, method, operation)) {
doc = R.dissocPath(['paths', path, method], doc);
}
}
if (R.isEmpty(R.path(['paths', path], doc))) {
doc = R.dissocPath(['paths', path], doc);
}
}
return (0, shaking_orphaned_components_1.openapiShakingOrphanedComponents)(doc);
}
exports.openapiShakingSync = openapiShakingSync;
});