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.

43 lines (42 loc) • 2.04 kB
import omitBy from 'es-toolkit/compat/omitBy'; import { DECORATORS } from '../constants.js'; import { ApiOperation } from '../decorators/api-operation.decorator.js'; import { METADATA_FACTORY_NAME } from '../plugin/plugin-constants.js'; export const exploreApiOperationMetadata = (instance, prototype, method) => { applyMetadataFactory(prototype, instance); return Reflect.getMetadata(DECORATORS.API_OPERATION, method); }; function applyMetadataFactory(prototype, instance) { do { if (!prototype.constructor) { return; } if (!Object.prototype.hasOwnProperty.call(prototype.constructor, METADATA_FACTORY_NAME)) { continue; } const metadata = prototype.constructor[METADATA_FACTORY_NAME](); const methodKeys = Object.keys(metadata).filter((key) => typeof instance[key] === 'function' && Object.prototype.hasOwnProperty.call(prototype, key)); methodKeys.forEach((key) => { const operationMeta = {}; const { summary, deprecated, tags, description } = metadata[key]; applyIfNotNil(operationMeta, 'summary', summary); applyIfNotNil(operationMeta, 'deprecated', deprecated); applyIfNotNil(operationMeta, 'tags', tags); applyIfNotNil(operationMeta, 'description', description); if (Object.keys(operationMeta).length === 0) { return; } const descriptor = Object.getOwnPropertyDescriptor(prototype, key); const existing = omitBy(Reflect.getMetadata(DECORATORS.API_OPERATION, descriptor.value), (value, name) => name === 'summary' && value === ''); ApiOperation({ ...operationMeta, ...existing })(prototype, key, descriptor); }); } while ((prototype = Reflect.getPrototypeOf(prototype)) && prototype !== Object.prototype && prototype); } function applyIfNotNil(target, key, value) { if (value !== undefined && value !== null) { target[key] = value; } }