UNPKG

@nestjs/swagger

Version:

Nest - modern, fast, powerful node.js web framework (@swagger)

48 lines (47 loc) 2.4 kB
import { groupBy, keyBy, mapValues, omit } from 'es-toolkit/compat'; import { sortObjectLexicographically } from './utils/sort-object-lexicographically.js'; export class SwaggerTransformer { normalizePaths(denormalizedDoc) { const roots = denormalizedDoc.filter((doc) => Boolean(doc.root)); const webhookRoots = roots.filter(({ root }) => Boolean(root.isWebhook)); const pathRoots = roots.filter(({ root }) => !root.isWebhook); const groupedByPath = groupBy(pathRoots, ({ root }) => root.path); const paths = mapValues(groupedByPath, (routes) => { const keyByMethod = keyBy(routes, ({ root }) => root.method); return mapValues(keyByMethod, (route) => { const mergedDefinition = { ...omit(route, 'root'), ...omit(route.root, ['method', 'path', 'isWebhook', 'webhookName']) }; return sortObjectLexicographically(mergedDefinition); }); }); const groupedByWebhookName = groupBy(webhookRoots, ({ root }) => root.webhookName || root.path); const webhooks = mapValues(groupedByWebhookName, (routes) => { const keyByMethod = keyBy(routes, ({ root }) => root.method); return mapValues(keyByMethod, (route) => { const mergedDefinition = { ...omit(route, 'root'), ...omit(route.root, ['method', 'path', 'isWebhook', 'webhookName']) }; return sortObjectLexicographically(mergedDefinition); }); }); const groupedByWebhookPath = groupBy(webhookRoots, ({ root }) => root.path); const webhookPaths = mapValues(groupedByWebhookPath, (routes) => { const keyByMethod = keyBy(routes, ({ root }) => root.method); return mapValues(keyByMethod, (route) => { const mergedDefinition = { ...omit(route, 'root'), ...omit(route.root, ['method', 'path', 'isWebhook', 'webhookName']) }; return sortObjectLexicographically(mergedDefinition); }); }); return { paths, ...(Object.keys(webhooks).length > 0 ? { webhooks } : {}), ...(Object.keys(webhookPaths).length > 0 ? { webhookPaths } : {}) }; } }