@nestjs/swagger
Version:
OpenAPI for NestJS: generate the API document from controllers and decorators, serve Swagger UI, and derive request types with mapped types.
119 lines (118 loc) • 5.82 kB
JavaScript
import isEmpty from 'es-toolkit/compat/isEmpty';
import isFunction from 'es-toolkit/compat/isFunction';
import omit from 'es-toolkit/compat/omit';
import pick from 'es-toolkit/compat/pick';
import { DECORATORS } from '../constants.js';
import { isBuiltInType } from '../utils/is-built-in-type.util.js';
import { isDateCtor } from '../utils/is-date-ctor.util.js';
import { MimetypeContentWrapper } from './mimetype-content-wrapper.js';
import { ModelPropertiesAccessor } from './model-properties-accessor.js';
import { ResponseObjectMapper } from './response-object-mapper.js';
import { SchemaObjectFactory } from './schema-object-factory.js';
import { StandardSchemaOpenApiConverter } from './standard-schema-openapi.converter.js';
import { SwaggerTypesMapper } from './swagger-types-mapper.js';
export class ResponseObjectFactory {
constructor(standardSchemaConverter) {
this.standardSchemaConverter = standardSchemaConverter;
this.mimetypeContentWrapper = new MimetypeContentWrapper();
this.modelPropertiesAccessor = new ModelPropertiesAccessor();
this.swaggerTypesMapper = new SwaggerTypesMapper();
this.standardSchemaOpenApiConverter = new StandardSchemaOpenApiConverter(this.standardSchemaConverter);
this.schemaObjectFactory = new SchemaObjectFactory(this.modelPropertiesAccessor, this.swaggerTypesMapper, this.standardSchemaConverter);
this.responseObjectMapper = new ResponseObjectMapper();
}
create(response, produces, schemas, factories) {
const { type, isArray } = response;
const schemaOverride = this.getSchemaOverride(response, schemas);
response = omit(response, ['isArray', 'standardSchema']);
if (schemaOverride) {
return this.responseObjectMapper.wrapSchemaWithContent({
...omit(response, ['type']),
schema: isArray
? { type: 'array', items: schemaOverride }
: schemaOverride
}, produces, schemas);
}
if (!type) {
return this.responseObjectMapper.wrapSchemaWithContent(response, produces, schemas);
}
if (isBuiltInType(type) ||
isDateCtor(type) ||
type === BigInt) {
const itemSchema = this.getBuiltInTypeSchema(type);
const exampleKeys = ['example', 'examples'];
const baseSchema = isArray
? { type: 'array', items: itemSchema }
: itemSchema;
const schema = response.nullable
? { ...baseSchema, nullable: true }
: baseSchema;
const content = this.mimetypeContentWrapper.wrap(produces, {
schema,
...pick(response, exampleKeys)
});
return {
...omit(response, [...exampleKeys, 'nullable']),
...content
};
}
const name = this.schemaObjectFactory.exploreModelSchema(type, schemas);
if (isFunction(type) && type.prototype) {
const { prototype } = type;
const links = {};
const properties = this.modelPropertiesAccessor.getModelProperties(prototype);
const generateLink = (controllerPrototype, method, parameter, field) => {
if (!factories) {
return;
}
const linkName = factories.linkName(controllerPrototype.constructor.name, method.name, field);
const { operationId } = Reflect.getMetadata(DECORATORS.API_OPERATION, method) ?? {};
links[linkName] = {
operationId: operationId ??
factories.operationId(controllerPrototype.constructor.name, method.name),
parameters: {
[parameter]: `$response.body#/${field}`
}
};
};
for (const key of properties) {
const metadata = Reflect.getMetadata(DECORATORS.API_MODEL_PROPERTIES, prototype, key) ?? {};
if (!metadata.link) {
continue;
}
const linkedType = metadata.link();
const linkedGetterInfo = Reflect.getMetadata(DECORATORS.API_DEFAULT_GETTER, linkedType.prototype);
if (!linkedGetterInfo) {
continue;
}
const { getter, parameter, prototype: controllerPrototype } = linkedGetterInfo;
generateLink(controllerPrototype, getter, parameter, key);
}
const customLinks = Reflect.getMetadata(DECORATORS.API_LINK, prototype);
for (const customLink of customLinks ?? []) {
const { method, parameter, field, prototype: controllerPrototype } = customLink;
generateLink(controllerPrototype, method, parameter, field);
}
if (!isEmpty(links)) {
response.links = Object.assign(links, response.links);
}
}
if (isArray) {
return this.responseObjectMapper.toArrayRefObject(response, name, produces);
}
return this.responseObjectMapper.toRefObject(response, name, produces);
}
getBuiltInTypeSchema(type) {
if (isDateCtor(type)) {
return { type: 'string', format: 'date-time' };
}
if (type === BigInt) {
return { type: 'integer', format: 'int64' };
}
const typeName = isFunction(type) ? type.name : type;
return { type: this.swaggerTypesMapper.mapTypeToOpenAPIType(typeName) };
}
getSchemaOverride(response, schemas) {
return this.standardSchemaOpenApiConverter.convertInto(response.standardSchema, schemas, 'output');
}
}