@nestjs/swagger
Version:
OpenAPI for NestJS: generate the API document from controllers and decorators, serve Swagger UI, and derive request types with mapped types.
200 lines (199 loc) • 6.45 kB
JavaScript
import { Logger } from '@nestjs/common';
import clone from 'es-toolkit/compat/clone';
import cloneDeep from 'es-toolkit/compat/cloneDeep';
import isString from 'es-toolkit/compat/isString';
import isUndefined from 'es-toolkit/compat/isUndefined';
import negate from 'es-toolkit/compat/negate';
import omit from 'es-toolkit/compat/omit';
import pickBy from 'es-toolkit/compat/pickBy';
import { getTypeIsArrayTuple } from './decorators/helpers.js';
import { buildDocumentBase } from './fixtures/document.base.js';
import { GLOBAL_PARAMETERS, GLOBAL_RESPONSES } from './interfaces/global-metadata.interface.js';
export class DocumentBuilder {
constructor() {
this.logger = new Logger(DocumentBuilder.name);
this.document = buildDocumentBase();
}
setTitle(title) {
this.document.info.title = title;
return this;
}
setDescription(description) {
this.document.info.description = description;
return this;
}
setVersion(version) {
this.document.info.version = version;
return this;
}
setTermsOfService(termsOfService) {
this.document.info.termsOfService = termsOfService;
return this;
}
setContact(name, url, email) {
this.document.info.contact = { name, url, email };
return this;
}
setLicense(name, url) {
this.document.info.license = { name, url };
return this;
}
setOpenAPIVersion(version) {
if (/^\d+\.\d+\.\d+$/.test(version)) {
this.document.openapi = version;
}
else {
this.logger.warn('The OpenApi version is invalid. Expecting format "x.x.x"');
}
return this;
}
addServer(url, description, variables, serverExtraProperties) {
const serverObjDef = {
url,
description,
variables
};
if (serverExtraProperties) {
const alreadyDefinedKeysForServerEntry = Object.keys(serverObjDef);
for (const key in serverExtraProperties) {
if (alreadyDefinedKeysForServerEntry.includes(key))
continue;
const extraFieldValue = serverExtraProperties[key];
serverObjDef[key] = extraFieldValue;
}
}
this.document.servers.push(serverObjDef);
return this;
}
setExternalDoc(description, url) {
this.document.externalDocs = { description, url };
return this;
}
setBasePath(path) {
this.logger.warn('The "setBasePath" method has been deprecated. Now, a global prefix is populated automatically. If you want to ignore it, take a look here: https://docs.nestjs.com/recipes/swagger#global-prefix. Alternatively, you can use "addServer" method to set up multiple different paths.');
return this;
}
addTag(name, description = '', externalDocs, options) {
this.document.tags = this.document.tags.concat(pickBy({
name,
summary: options?.summary,
description,
externalDocs,
parent: options?.parent,
kind: options?.kind
}, negate(isUndefined)));
return this;
}
addExtension(extensionKey, extensionProperties, location = 'root') {
if (!extensionKey.startsWith('x-')) {
throw new Error('Extension key is not prefixed. Please ensure you prefix it with `x-`.');
}
if (location === 'root') {
this.document[extensionKey] = clone(extensionProperties);
}
else {
this.document[location][extensionKey] = clone(extensionProperties);
}
return this;
}
addSecurity(name, options) {
this.document.components.securitySchemes = {
...(this.document.components.securitySchemes || {}),
[name]: options
};
return this;
}
addGlobalResponse(...respones) {
const groupedByStatus = respones.reduce((acc, response) => {
const { status = 'default' } = response;
const [type, isArray] = getTypeIsArrayTuple(response.type, response.isArray);
acc[status] = {
...omit(response, 'status'),
type,
isArray,
description: response.description ?? ''
};
return acc;
}, {});
this.document[GLOBAL_RESPONSES] = {
...this.document[GLOBAL_RESPONSES],
...groupedByStatus
};
return this;
}
addGlobalParameters(...parameters) {
this.document[GLOBAL_PARAMETERS] = [
...(this.document[GLOBAL_PARAMETERS] || []),
...parameters
];
return this;
}
addSecurityRequirements(name, requirements = []) {
let securityRequirement;
if (isString(name)) {
securityRequirement = { [name]: requirements };
}
else {
securityRequirement = name;
}
this.document.security = (this.document.security || []).concat({
...securityRequirement
});
return this;
}
addBearerAuth(options = {
type: 'http'
}, name = 'bearer') {
this.addSecurity(name, {
scheme: 'bearer',
bearerFormat: 'JWT',
...options
});
return this;
}
addOAuth2(options = {
type: 'oauth2'
}, name = 'oauth2') {
this.addSecurity(name, {
type: 'oauth2',
flows: {},
...options
});
return this;
}
addApiKey(options = {
type: 'apiKey'
}, name = 'api_key') {
this.addSecurity(name, {
type: 'apiKey',
in: 'header',
name,
...options
});
return this;
}
addBasicAuth(options = {
type: 'http'
}, name = 'basic') {
this.addSecurity(name, {
type: 'http',
scheme: 'basic',
...options
});
return this;
}
addCookieAuth(cookieName = 'connect.sid', options = {
type: 'apiKey'
}, securityName = 'cookie') {
this.addSecurity(securityName, {
type: 'apiKey',
in: 'cookie',
name: cookieName,
...options
});
return this;
}
build() {
return cloneDeep(this.document);
}
}