@nestjs/swagger
Version:
OpenAPI for NestJS: generate the API document from controllers and decorators, serve Swagger UI, and derive request types with mapped types.
40 lines (39 loc) • 1.8 kB
JavaScript
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;
}
ApiOperation(operationMeta, { overrideExisting: false })(prototype, key, Object.getOwnPropertyDescriptor(prototype, key));
});
} while ((prototype = Reflect.getPrototypeOf(prototype)) &&
prototype !== Object.prototype &&
prototype);
}
function applyIfNotNil(target, key, value) {
if (value !== undefined && value !== null) {
target[key] = value;
}
}