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.

80 lines (79 loc) • 4.09 kB
import { inheritPropertyInitializers, inheritTransformationMetadata, inheritValidationMetadata } from '@nestjs/mapped-types'; import { DECORATORS } from '../constants.js'; import { createApiPropertyDecorator } from '../decorators/api-property.decorator.js'; import { ApiProperty } from '../decorators/index.js'; import { MetadataLoader } from '../plugin/metadata-loader.js'; import { METADATA_FACTORY_NAME } from '../plugin/plugin-constants.js'; import { ModelPropertiesAccessor } from '../services/model-properties-accessor.js'; import { clonePluginMetadataFactory, inheritExplicitApiPropertyOptions } from './mapped-types.utils.js'; const modelPropertiesAccessor = new ModelPropertiesAccessor(); function isDecorated(prototype, propertyKey) { for (let current = prototype; current && current !== Object.prototype; current = Reflect.getPrototypeOf(current)) { if (Reflect.hasOwnMetadata(DECORATORS.API_MODEL_PROPERTY_EXPLICIT_OPTIONS, current, propertyKey)) { return true; } } return false; } function getPluginMetadata(classRef) { class PluginMetadataHolder { } clonePluginMetadataFactory(PluginMetadataHolder, classRef.prototype); return PluginMetadataHolder[METADATA_FACTORY_NAME]?.() ?? {}; } export function IntersectionType(...classRefs) { class IntersectionClassType { constructor() { classRefs.forEach((classRef) => { inheritPropertyInitializers(this, classRef); }); } } function replaceType(propertyKey, metadata) { const prototype = IntersectionClassType.prototype; if (!metadata || !Reflect.hasOwnMetadata(DECORATORS.API_MODEL_PROPERTIES, prototype, propertyKey)) { return false; } const decoratedOptions = modelPropertiesAccessor.getDecoratedOptions(prototype, propertyKey) ?? {}; Reflect.deleteMetadata(DECORATORS.API_MODEL_PROPERTIES, prototype, propertyKey); createApiPropertyDecorator({ ...metadata, ...decoratedOptions }, false)(prototype, propertyKey); Reflect.defineMetadata(DECORATORS.API_MODEL_PROPERTY_EXPLICIT_OPTIONS, Object.keys(decoratedOptions), prototype, propertyKey); return true; } classRefs.forEach((classRef) => { const fields = modelPropertiesAccessor.getModelProperties(classRef.prototype); inheritValidationMetadata(classRef, IntersectionClassType); class InheritedClassType { } inheritTransformationMetadata(classRef, InheritedClassType); inheritTransformationMetadata(InheritedClassType, IntersectionClassType, undefined, false); function applyFields(fields) { clonePluginMetadataFactory(IntersectionClassType, classRef.prototype); fields.forEach((propertyKey) => { const metadata = modelPropertiesAccessor.getPropertyMetadata(classRef.prototype, propertyKey); if (!isDecorated(classRef.prototype, propertyKey) && replaceType(propertyKey, metadata)) { return; } const decoratorFactory = ApiProperty(metadata); decoratorFactory(IntersectionClassType.prototype, propertyKey); inheritExplicitApiPropertyOptions(classRef.prototype, IntersectionClassType.prototype, propertyKey); }); const pluginMetadata = getPluginMetadata(classRef); Object.keys(pluginMetadata) .filter((propertyKey) => !fields.includes(propertyKey)) .forEach((propertyKey) => replaceType(propertyKey, pluginMetadata[propertyKey])); } applyFields(fields); MetadataLoader.addRefreshHook(() => { const fields = modelPropertiesAccessor.getModelProperties(classRef.prototype); applyFields(fields); }); }); Object.defineProperty(IntersectionClassType, 'name', { configurable: true, get: () => `Intersection${classRefs.reduce((prev, ref) => prev + ref.name, '')}` }); return IntersectionClassType; }