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.

34 lines (33 loc) • 1.18 kB
import clone from 'es-toolkit/compat/clone'; import isNil from 'es-toolkit/compat/isNil'; import omit from 'es-toolkit/compat/omit'; import { addEnumSchema, isEnumDefined } from '../utils/enum.utils.js'; import { createParamDecorator, getTypeIsArrayTuple } from './helpers.js'; const defaultParamOptions = { name: '', required: true }; export function ApiParam(options) { const param = { name: isNil(options.name) ? defaultParamOptions.name : options.name, in: 'path', ...omit(options, ['enum', 'extensions']) }; const [type, isArray] = getTypeIsArrayTuple(options.type, undefined); if (isArray) { param.type = type; param.isArray = isArray; } if (isEnumDefined(options)) { addEnumSchema(param, options); } const extensions = options.extensions; if (extensions && typeof extensions === 'object') { const cloned = clone(extensions); for (const [key, value] of Object.entries(cloned)) { const extKey = key.startsWith('x-') ? key : `x-${key}`; param[extKey] = value; } } return createParamDecorator(param, defaultParamOptions); }