1- import fs from 'fs' ;
2-
31import { ValidationPipe } from '@nestjs/common' ;
42import { HttpAdapterHost , NestFactory } from '@nestjs/core' ;
5- import { DocumentBuilder , SwaggerModule } from '@nestjs/swagger' ;
63import compression from 'compression' ;
7- import { SHA256 } from 'crypto-js' ;
84import dayjs from 'dayjs' ;
95import isoWeek from 'dayjs/plugin/isoWeek' ;
106import minMax from 'dayjs/plugin/minMax' ;
@@ -13,85 +9,35 @@ import * as sourceMapSupport from 'source-map-support';
139import { MongoErrorsInterceptor } from 'src/mongo' ;
1410
1511import { AppModule } from './app.module' ;
16- import { GetAuthorizerQuery } from './auth' ;
17- import { ListCaptchasQuery } from './captcha' ;
1812import { AllExceptionsFilter } from './common/all-exceptions.filter' ;
1913import { exceptionFactory } from './common/exception-factory' ;
2014import { port , prefix } from './config/config' ;
21- import { ListEmailRecordsQuery } from './email' ;
22- import { ListGroupsQuery } from './group' ;
23- import { ListIndustriesQuery } from './industry' ;
24- import { ListNamespacesQuery } from './namespace' ;
25- import { ListRolesQuery } from './role' ;
26- import { ListSessionsQuery } from './session' ;
27- import { ListSmsRecordsQuery } from './sms' ;
28- import { ListThirdPartyQuery } from './third-party' ;
29- import { ListUsersQuery } from './user' ;
15+ import { writeOpenapi } from './swagger' ;
3016
3117dayjs . extend ( isoWeek ) ;
3218dayjs . extend ( minMax ) ;
3319
34- const openapiPath = `openapi.json` ;
35-
3620async function bootstrap ( ) {
3721 sourceMapSupport . install ( ) ;
3822 const app = await NestFactory . create ( AppModule , {
3923 cors : { exposedHeaders : [ 'Link' , 'X-Total-Count' ] } ,
4024 } ) ;
4125
42- let swaggerPrefix = 'openapi' ;
4326 if ( prefix ) {
4427 app . setGlobalPrefix ( prefix ) ;
45- swaggerPrefix = `${ prefix } /${ swaggerPrefix } ` ;
4628 }
4729
48- const swaggerConfig = new DocumentBuilder ( )
49- . setTitle ( 'Auth API Server' )
50- . setDescription ( 'Auth API for auth service' )
51- . setVersion ( '2.0' )
52- . addApiKey (
53- {
54- in : 'header' ,
55- name : 'x-api-key' ,
56- type : 'apiKey' ,
57- } ,
58- 'ApiKey'
59- )
60- . build ( ) ;
61- const document = SwaggerModule . createDocument ( app , swaggerConfig , {
62- extraModels : [
63- ListUsersQuery ,
64- ListNamespacesQuery ,
65- GetAuthorizerQuery ,
66- ListCaptchasQuery ,
67- ListEmailRecordsQuery ,
68- ListGroupsQuery ,
69- ListIndustriesQuery ,
70- ListRolesQuery ,
71- ListSessionsQuery ,
72- ListSmsRecordsQuery ,
73- ListThirdPartyQuery ,
74- ] ,
75- } ) ;
76- SwaggerModule . setup ( swaggerPrefix , app , document ) ;
77-
7830 app . use ( compression ( ) ) ;
7931 app . useGlobalPipes ( new ValidationPipe ( { whitelist : true , transform : true , exceptionFactory } ) ) ;
8032
8133 const { httpAdapter } = app . get ( HttpAdapterHost ) ;
8234 app . useGlobalFilters ( new AllExceptionsFilter ( httpAdapter ) ) ;
8335 app . useGlobalInterceptors ( new MongoErrorsInterceptor ( ) ) ;
8436
85- await app . listen ( port ) ;
37+ // setup swagger
38+ writeOpenapi ( app , prefix ) ;
8639
87- // write openapi.json
88- if ( process . env . NODE_ENV === 'development' ) {
89- const documentWithSha = {
90- hash : SHA256 ( JSON . stringify ( document , null , 2 ) ) . toString ( ) ,
91- ...document ,
92- } ;
93- fs . writeFileSync ( openapiPath , JSON . stringify ( documentWithSha , null , 2 ) ) ;
94- }
40+ await app . listen ( port ) ;
9541}
9642
9743bootstrap ( ) ;
0 commit comments