2026-02-16 20:51:34 +03:30
|
|
|
import { VersioningType } from '@nestjs/common'
|
2026-02-12 20:31:04 +03:30
|
|
|
import { NestFactory, Reflector } from '@nestjs/core'
|
|
|
|
|
import { JwtService } from '@nestjs/jwt'
|
2025-12-04 21:05:57 +03:30
|
|
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
|
|
|
|
|
import { AppModule } from './app.module'
|
2025-12-06 17:48:10 +03:30
|
|
|
import { PrismaExceptionFilter } from './common/filters/prisma-exception.filter'
|
2026-02-16 20:51:34 +03:30
|
|
|
import { ValidationExceptionFilter } from './common/filters/validation-exception.filter'
|
2026-03-07 11:25:11 +03:30
|
|
|
import { JwtAuthGuard } from './common/guards/jwt-auth.guard'
|
2026-03-29 18:06:41 +03:30
|
|
|
import { PosGuard } from './common/guards/pos.gaurd'
|
2025-12-04 21:05:57 +03:30
|
|
|
import { LoggingInterceptor } from './common/interceptors/logging.interceptor'
|
|
|
|
|
import { ResponseMappingInterceptor } from './common/interceptors/response-mapping.interceptor'
|
2026-03-29 18:06:41 +03:30
|
|
|
import { PrismaService } from './prisma/prisma.service'
|
|
|
|
|
const cookieParser = require('cookie-parser')
|
2025-12-04 21:05:57 +03:30
|
|
|
|
|
|
|
|
async function bootstrap() {
|
|
|
|
|
const app = await NestFactory.create(AppModule)
|
|
|
|
|
|
2026-02-12 20:31:04 +03:30
|
|
|
app.use(cookieParser())
|
|
|
|
|
|
2025-12-04 21:05:57 +03:30
|
|
|
const config = new DocumentBuilder()
|
|
|
|
|
.setTitle('Pos')
|
|
|
|
|
.setDescription('The Pos API description')
|
|
|
|
|
.setVersion('1.0')
|
|
|
|
|
.addTag('pos')
|
2026-02-12 20:31:04 +03:30
|
|
|
.addBearerAuth({
|
|
|
|
|
type: 'http',
|
|
|
|
|
scheme: 'bearer',
|
|
|
|
|
bearerFormat: 'JWT',
|
|
|
|
|
name: 'Authorization',
|
|
|
|
|
description: 'Enter JWT token',
|
|
|
|
|
in: 'header',
|
|
|
|
|
})
|
2025-12-04 21:05:57 +03:30
|
|
|
.build()
|
|
|
|
|
const documentFactory = () => SwaggerModule.createDocument(app, config)
|
2026-02-17 20:16:36 +03:30
|
|
|
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
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
2025-12-04 21:05:57 +03:30
|
|
|
|
2026-02-12 20:31:04 +03:30
|
|
|
// Set API prefix and enable URI versioning so alls routes live under /api/v1/*
|
2025-12-04 21:05:57 +03:30
|
|
|
app.setGlobalPrefix('api')
|
|
|
|
|
app.enableVersioning({
|
|
|
|
|
type: VersioningType.URI,
|
|
|
|
|
defaultVersion: '1',
|
|
|
|
|
})
|
|
|
|
|
|
2025-12-05 00:01:44 +03:30
|
|
|
// 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.
|
2026-03-29 18:06:41 +03:30
|
|
|
const defaultOrigins = ['http://localhost:5000', 'http://127.0.0.1:5000']
|
2025-12-05 00:01:44 +03:30
|
|
|
const envOrigins = process.env.CORS_ORIGINS
|
|
|
|
|
? process.env.CORS_ORIGINS.split(',')
|
|
|
|
|
.map(s => s.trim())
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
: []
|
2026-03-29 18:06:41 +03:30
|
|
|
const allowedOrigins = (envOrigins.length ? envOrigins : defaultOrigins).filter(
|
|
|
|
|
origin => origin !== '*',
|
|
|
|
|
)
|
2025-12-05 00:01:44 +03:30
|
|
|
app.enableCors({
|
|
|
|
|
origin: allowedOrigins,
|
|
|
|
|
credentials: true,
|
|
|
|
|
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
|
2026-03-29 18:06:41 +03:30
|
|
|
allowedHeaders:
|
|
|
|
|
'Content-Type, Accept, Authorization, X-Requested-With, X-CSRF-Token, pos_id',
|
2025-12-05 00:01:44 +03:30
|
|
|
})
|
|
|
|
|
|
2025-12-04 21:05:57 +03:30
|
|
|
// Register global logging and response mapping interceptors
|
|
|
|
|
app.useGlobalInterceptors(new LoggingInterceptor(), new ResponseMappingInterceptor())
|
|
|
|
|
|
2025-12-06 17:48:10 +03:30
|
|
|
// Enable request validation and transformation globally
|
2026-02-16 20:51:34 +03:30
|
|
|
// app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }))
|
2025-12-06 17:48:10 +03:30
|
|
|
|
2026-02-16 20:51:34 +03:30
|
|
|
// Register global exception filters: validation errors and Prisma errors
|
|
|
|
|
app.useGlobalFilters(new ValidationExceptionFilter(), new PrismaExceptionFilter())
|
2025-12-06 17:48:10 +03:30
|
|
|
|
2026-02-12 20:31:04 +03:30
|
|
|
app.useGlobalGuards(new JwtAuthGuard(app.get(JwtService), app.get(Reflector)))
|
2026-03-29 18:06:41 +03:30
|
|
|
app.useGlobalGuards(
|
|
|
|
|
new PosGuard(app.get(JwtService), app.get(Reflector), app.get(PrismaService)),
|
|
|
|
|
)
|
2026-02-12 20:31:04 +03:30
|
|
|
|
|
|
|
|
await app.listen(process.env.PORT ?? 5002)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
bootstrap()
|