UNPKG

@nestjs/swagger

Version:

OpenAPI for NestJS: generate the API document from controllers and decorators, serve Swagger UI, and derive request types with mapped types.

64 lines (63 loc) • 3.55 kB
import cloneDeep from 'es-toolkit/compat/cloneDeep'; import groupBy from 'es-toolkit/compat/groupBy'; import isEqual from 'es-toolkit/compat/isEqual'; import keyBy from 'es-toolkit/compat/keyBy'; import mapValues from 'es-toolkit/compat/mapValues'; import omit from 'es-toolkit/compat/omit'; import uniqWith from 'es-toolkit/compat/uniqWith'; import { sortObjectLexicographically } from './utils/sort-object-lexicographically.js'; export class SwaggerTransformer { normalizePaths(denormalizedDoc, globalSecurity = []) { 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) => this.normalizeOperations(routes, globalSecurity)); const webhookNameOf = ({ root }) => root.webhookName || root.path; const webhooksByName = groupBy(webhookRoots, webhookNameOf); const groupedByWebhookName = groupBy(webhookRoots, (doc) => { const name = webhookNameOf(doc); const { qualifiedWebhookName } = doc.root; const isShared = qualifiedWebhookName !== undefined && webhooksByName[name].some(({ root }) => root.qualifiedWebhookName !== qualifiedWebhookName); return isShared ? qualifiedWebhookName : name; }); const webhooks = mapValues(groupedByWebhookName, (routes) => this.normalizeOperations(routes, globalSecurity)); const groupedByWebhookPath = groupBy(webhookRoots, ({ root }) => root.path); const webhookPaths = mapValues(groupedByWebhookPath, (routes) => this.normalizeOperations(routes, globalSecurity)); return { paths, ...(Object.keys(webhooks).length > 0 ? { webhooks } : {}), ...(Object.keys(webhookPaths).length > 0 ? { webhookPaths } : {}) }; } normalizeOperations(routes, globalSecurity) { const keyByMethod = keyBy(routes, ({ root }) => root.method); return mapValues(keyByMethod, (route) => { const mergedDefinition = { ...omit(route, 'root'), ...omit(route.root, [ 'method', 'path', 'isWebhook', 'webhookName', 'qualifiedWebhookName' ]) }; const handlers = routes.filter(({ root }) => root.method === route.root.method); if (handlers.length > 1) { const declared = handlers.map((handler) => handler.root.security ?? handler.security); if (declared.some((security) => security !== undefined)) { const effective = declared.map((security) => security ?? cloneDeep(globalSecurity)); const isAnonymous = (requirement) => Object.keys(requirement).length === 0; const allowsAnonymous = effective.every((security) => security.length === 0 || security.some(isAnonymous)); const security = uniqWith(effective.flat(), isEqual).filter((requirement) => allowsAnonymous || !isAnonymous(requirement)); if (security.length > 0) { mergedDefinition.security = security; } } } return sortObjectLexicographically(mergedDefinition); }); } }