forked from brocoders/nestjs-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
51 lines (46 loc) · 1.7 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import 'dotenv/config';
import {
ClassSerializerInterceptor,
ValidationPipe,
VersioningType,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NestFactory, Reflector } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { useContainer } from 'class-validator';
import { AppModule } from './app.module';
import validationOptions from './utils/validation-options';
import { AllConfigType } from './config/config.type';
import { ResolvePromisesInterceptor } from './utils/serializer.interceptor';
async function bootstrap() {
const app = await NestFactory.create(AppModule, { cors: true });
useContainer(app.select(AppModule), { fallbackOnErrors: true });
const configService = app.get(ConfigService<AllConfigType>);
app.enableShutdownHooks();
app.setGlobalPrefix(
configService.getOrThrow('app.apiPrefix', { infer: true }),
{
exclude: ['/'],
},
);
app.enableVersioning({
type: VersioningType.URI,
});
app.useGlobalPipes(new ValidationPipe(validationOptions));
app.useGlobalInterceptors(
// ResolvePromisesInterceptor is used to resolve promises in responses because class-transformer can't do it
// https://github.com/typestack/class-transformer/issues/549
new ResolvePromisesInterceptor(),
new ClassSerializerInterceptor(app.get(Reflector)),
);
const options = new DocumentBuilder()
.setTitle('API')
.setDescription('API docs')
.setVersion('1.0')
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('docs', app, document);
await app.listen(configService.getOrThrow('app.port', { infer: true }));
}
void bootstrap();