import{NestFactory}from'@nestjs/core';import{SwaggerModule,DocumentBuilder}from'@nestjs/swagger';import{AppModule}from'./app.module';asyncfunctionbootstrap(){constapp=awaitNestFactory.create(AppModule);constconfig=newDocumentBuilder().setTitle('Cats example').setDescription('The cats API description').setVersion('1.0').addTag('cats').build();constdocument=SwaggerModule.createDocument(app,config);SwaggerModule.setup('api',app,document);awaitapp.listen(3000);}bootstrap();
app.register(helmet,{contentSecurityPolicy:{directives:{defaultSrc:[`'self'`],styleSrc:[`'self'`,`'unsafe-inline'`],imgSrc:[`'self'`,'data:','validator.swagger.io'],scriptSrc:[`'self'`,`https: 'unsafe-inline'`],},},});// If you are not going to use CSP at all, you can use this:app.register(helmet,{contentSecurityPolicy:false,});
exportinterfaceSwaggerDocumentOptions{/** * List of modules to include in the specification */include?:Function[];/** * Additional, extra models that should be inspected and included in the specification */extraModels?:Function[];/** * If `true`, swagger will ignore the global prefix set through `setGlobalPrefix()` method */ignoreGlobalPrefix?:boolean;/** * If `true`, swagger will also load routes from the modules imported by `include` modules */deepScanRoutes?:boolean;/** * Custom operationIdFactory that will be used to generate the `operationId` * based on the `controllerKey` and `methodKey` * @default () => controllerKey_methodKey */operationIdFactory?:(controllerKey:string,methodKey:string)=>string;}
constcustomOptions:SwaggerCustomOptions={swaggerOptions:{persistAuthorization:true,},customSiteTitle:'My API Docs',};SwaggerModule.setup('docs',app,document,customOptions);