84 lines
3.1 KiB
TypeScript
84 lines
3.1 KiB
TypeScript
import { VersioningType } from '@nestjs/common'
|
|
import { NestFactory, Reflector } from '@nestjs/core'
|
|
import { JwtService } from '@nestjs/jwt'
|
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
|
|
import { AppModule } from './app.module'
|
|
import { PrismaExceptionFilter } from './common/filters/prisma-exception.filter'
|
|
import { ValidationExceptionFilter } from './common/filters/validation-exception.filter'
|
|
import { LoggingInterceptor } from './common/interceptors/logging.interceptor'
|
|
import { ResponseMappingInterceptor } from './common/interceptors/response-mapping.interceptor'
|
|
import { JwtAuthGuard } from './modules/auth/jwt-auth.guard'
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create(AppModule)
|
|
|
|
const cookieParser = (await import('cookie-parser')).default
|
|
app.use(cookieParser())
|
|
|
|
const config = new DocumentBuilder()
|
|
.setTitle('Pos')
|
|
.setDescription('The Pos API description')
|
|
.setVersion('1.0')
|
|
.addTag('pos')
|
|
.addBearerAuth({
|
|
type: 'http',
|
|
scheme: 'bearer',
|
|
bearerFormat: 'JWT',
|
|
name: 'Authorization',
|
|
description: 'Enter JWT token',
|
|
in: 'header',
|
|
})
|
|
.build()
|
|
const documentFactory = () => SwaggerModule.createDocument(app, config)
|
|
SwaggerModule.setup('swagger', app, documentFactory, {
|
|
swaggerOptions: {
|
|
persistAuthorization: true,
|
|
requestInterceptor: req => {
|
|
const _tokenStorage = localStorage.getItem('authorized')
|
|
if (_tokenStorage) {
|
|
const tokenStorage = JSON.parse(_tokenStorage)
|
|
req.headers['Authorization'] = `Bearer ${tokenStorage?.bearer?.value}`
|
|
}
|
|
return req
|
|
},
|
|
},
|
|
})
|
|
|
|
// Set API prefix and enable URI versioning so alls routes live under /api/v1/*
|
|
app.setGlobalPrefix('api')
|
|
app.enableVersioning({
|
|
type: VersioningType.URI,
|
|
defaultVersion: '1',
|
|
})
|
|
|
|
// Enable CORS. You can set `CORS_ORIGINS` to a comma-separated list of allowed origins.
|
|
// Defaults include common localhost origins used by front-end dev servers.
|
|
const defaultOrigins = ['*', 'http://localhost:3001']
|
|
const envOrigins = process.env.CORS_ORIGINS
|
|
? process.env.CORS_ORIGINS.split(',')
|
|
.map(s => s.trim())
|
|
.filter(Boolean)
|
|
: []
|
|
const allowedOrigins = envOrigins.length ? envOrigins : defaultOrigins
|
|
app.enableCors({
|
|
origin: allowedOrigins,
|
|
credentials: true,
|
|
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
|
|
allowedHeaders: 'Content-Type, Accept, Authorization, X-Requested-With, X-CSRF-Token',
|
|
})
|
|
|
|
// Register global logging and response mapping interceptors
|
|
app.useGlobalInterceptors(new LoggingInterceptor(), new ResponseMappingInterceptor())
|
|
|
|
// Enable request validation and transformation globally
|
|
// app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }))
|
|
|
|
// Register global exception filters: validation errors and Prisma errors
|
|
app.useGlobalFilters(new ValidationExceptionFilter(), new PrismaExceptionFilter())
|
|
|
|
app.useGlobalGuards(new JwtAuthGuard(app.get(JwtService), app.get(Reflector)))
|
|
|
|
await app.listen(process.env.PORT ?? 5002)
|
|
}
|
|
bootstrap()
|