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.

126 lines (125 loc) • 5.6 kB
import { isFunction, isString } from '@nestjs/common/utils/shared.utils.js'; import pick from 'es-toolkit/compat/pick'; import 'reflect-metadata'; import { DECORATORS } from '../constants.js'; import { createApiPropertyDecorator } from '../decorators/api-property.decorator.js'; import { METADATA_FACTORY_NAME } from '../plugin/plugin-constants.js'; export class ModelPropertiesAccessor { getModelProperties(prototype) { const properties = Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES_ARRAY, prototype) || []; return properties .filter(isString) .filter((key) => key.charAt(0) === ':' && !isFunction(prototype[key])) .map((key) => key.slice(1)) .filter((key) => !this.isHiddenProperty(prototype, key)); } isHiddenField(prototype, field) { const [name] = String(field).split('/'); const keys = (Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES_ARRAY, prototype) || []) .filter(isString) .map((key) => key.slice(1)) .filter((key) => (Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, prototype, key) ?.name || key) === name); return keys.length > 0 ? keys.every((key) => this.isHiddenProperty(prototype, key)) : this.isHiddenProperty(prototype, name); } isHiddenProperty(prototype, key, stopAt) { let current = prototype; while (current && current !== Object.prototype) { if (Reflect.getOwnMetadata(DECORATORS.API_MODEL_PROPERTY_HIDDEN, current, key)) { return true; } if (current === stopAt || Reflect.getOwnMetadata(DECORATORS.API_MODEL_PROPERTIES, current, key)) { return false; } current = Reflect.getPrototypeOf(current); } return false; } keepDecoratedInheritedOptions(prototype, key) { if (Reflect.hasOwnMetadata(DECORATORS.API_MODEL_PROPERTIES, prototype, key)) { return; } const decoratedOptions = this.getDecoratedInheritedOptions(prototype, key); if (!decoratedOptions) { return; } Reflect.defineMetadata(DECORATORS.API_MODEL_PROPERTIES, decoratedOptions, prototype, key); } getDecoratedInheritedOptions(prototype, key) { return this.pickDecoratedOptions(prototype, Reflect.getPrototypeOf(prototype), key); } getDecoratedOptions(prototype, key) { return this.pickDecoratedOptions(prototype, prototype, key); } pickDecoratedOptions(prototype, decoratedFrom, key) { const metadata = Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, prototype, key); if (!metadata) { return undefined; } const decoratedOptions = new Set(); for (let current = decoratedFrom; current && current !== Object.prototype; current = Reflect.getPrototypeOf(current)) { (Reflect.getOwnMetadata(DECORATORS.API_MODEL_PROPERTY_EXPLICIT_OPTIONS, current, key) ?? []).forEach((option) => decoratedOptions.add(option)); } decoratedOptions.delete('type'); decoratedOptions.delete('isArray'); return pick(metadata, [...decoratedOptions]); } getPropertyMetadata(prototype, key) { for (let current = prototype; current && current !== Object.prototype; current = Reflect.getPrototypeOf(current)) { if (Reflect.hasOwnMetadata(DECORATORS.API_MODEL_PROPERTIES, current, key)) { break; } const pluginMetadata = current.constructor && Object.prototype.hasOwnProperty.call(current.constructor, METADATA_FACTORY_NAME) ? current.constructor[METADATA_FACTORY_NAME]()[key] : undefined; if (pluginMetadata) { return { ...pluginMetadata, ...this.getDecoratedInheritedOptions(current, key) }; } } return Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, prototype, key); } hasOwnMetadataUpTo(prototype, key, stopAt) { for (let current = prototype; current && current !== Object.prototype; current = Reflect.getPrototypeOf(current)) { if (Reflect.hasOwnMetadata(DECORATORS.API_MODEL_PROPERTIES, current, key)) { return true; } if (current === stopAt) { return false; } } return false; } applyMetadataFactory(prototype) { const classPrototype = prototype; do { if (!prototype.constructor) { return; } if (!Object.prototype.hasOwnProperty.call(prototype.constructor, METADATA_FACTORY_NAME)) { continue; } const metadata = prototype.constructor[METADATA_FACTORY_NAME](); const properties = Object.keys(metadata); properties.forEach((key) => { if (this.isHiddenProperty(classPrototype, key, prototype)) { return; } if (!this.hasOwnMetadataUpTo(classPrototype, key, prototype)) { this.keepDecoratedInheritedOptions(classPrototype, key); } createApiPropertyDecorator(metadata[key], false)(classPrototype, key); }); } while ((prototype = Reflect.getPrototypeOf(prototype)) && prototype !== Object.prototype && prototype); } }