import { Logger } from '@nestjs/common' ; import { isString , isUndefined , negate , pickBy } from 'lodash' ; import { buildDocumentBase } from './fixtures/document.base' ; import { OpenAPIObject } from './interfaces' ; import { ExternalDocumentationObject , SecurityRequirementObject , SecuritySchemeObject , ServerVariableObject , TagObject } from './interfaces/open-api-spec.interface' ; export class DocumentBuilder { private readonly logger = new Logger ( DocumentBuilder . name ) ; private readonly document : Omit < OpenAPIObject , 'paths' > = buildDocumentBase ( ) ; public setTitle ( title ) { this . document . info . title = title ; return this ; } public setDescription ( description ) { this . document . info . description = description ; return this ; } public setVersion ( version ) { this . document . info . version = version ; return this ; } public setTermsOfService ( termsOfService ) { this . document . info . termsOfService = termsOfService ; return this ; } public setContact ( name , url , email ) { this . document . info . contact = { name , url , email } ; return this ; } public setLicense ( name , url ) { this . document . info . license = { name , url } ; return this ; } public addServer ( url , description ? , variables ? ) { this . document . servers . push ( { url , description , variables } ) ; return this ; } public setExternalDoc ( description , url ) { this . document . externalDocs = { description , url } ; return this ; } public 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 ; } public addTag ( name , description = '' , externalDocs ? ) { this . document . tags = this . document . tags . concat ( pickBy ( { name , description , externalDocs } , negate ( isUndefined ) ) as TagObject ) ; return this ; } public addSecurity ( name , options ) { this . document . components . securitySchemes = { ... ( this . document . components . securitySchemes || { } ) , [ name ] : options } ; return this ; } public addSecurityRequirements ( name , requirements = [ ] ) { let securityRequirement ; if ( isString ( name ) ) { securityRequirement = { [ name ] : requirements } ; } else { securityRequirement = name ; } this . document . security = ( this . document . security || [ ] ) . concat ( { ... securityRequirement } ) ; return this ; } public addBearerAuth ( options = { type : 'http' } , name = 'bearer' ) { this . addSecurity ( name , { scheme : 'bearer' , bearerFormat : 'JWT' , ... options } ) ; return this ; } public addOAuth2 ( options = { type : 'oauth2' } , name = 'oauth2' ) { this . addSecurity ( name , { type : 'oauth2' , flows : { } , ... options } ) ; return this ; } public addApiKey ( options = { type : 'apiKey' } , name = 'api_key' ) { this . addSecurity ( name , { type : 'apiKey' , in : 'header' , name , ... options } ) ; return this ; } public addBasicAuth ( options = { type : 'http' } , name = 'basic' ) { this . addSecurity ( name , { type : 'http' , scheme : 'basic' , ... options } ) ; return this ; } public addCookieAuth ( cookieName = 'connect.sid' , options = { type : 'apiKey' } , securityName = 'cookie' ) { this . addSecurity ( securityName , { type : 'apiKey' , in : 'cookie' , name : cookieName , ... options } ) ; return this ; } public build ( ) { return this . document ; } }