UNPKG

@opendoc/openapi-shaking

Version:

Remove the useless components and paths of openapi document.

48 lines (47 loc) 1.79 kB
import * as R from 'ramda'; import { isValidMethod } from './utils/is-valid-method'; import { openapiShakingOrphanedComponents } from './shaking-orphaned-components'; export 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 (!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 openapiShakingOrphanedComponents(doc); } export function openapiShakingSync(document, filter) { let doc = document; for (const path in document.paths) { for (const method in document.paths[path]) { if (!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 openapiShakingOrphanedComponents(doc); }