fix api products

This commit is contained in:
2026-03-29 21:12:55 +03:00
parent b2d81446b7
commit 1661f210b0
2 changed files with 20 additions and 3 deletions

View File

@@ -2,7 +2,7 @@ export default () => ({
app: {
port: parseInt(process.env.PORT ?? '3000', 10),
nodeEnv: process.env.NODE_ENV ?? 'development',
corsOrigins: (process.env.CORS_ORIGINS ?? 'http://localhost:3000')
corsOrigins: (process.env.CORS_ORIGINS ?? '*')
.split(',')
.map((origin) => origin.trim())
.filter(Boolean),

View File

@@ -11,13 +11,30 @@ async function bootstrap() {
const reflector = app.get(Reflector);
const configService = app.get(ConfigService);
const corsOrigins = configService.get<string[]>('app.corsOrigins', []);
const allowAllOrigins = corsOrigins.includes('*');
app.setGlobalPrefix('api');
app.enableCors({
origin: corsOrigins,
origin: (origin, callback) => {
if (!origin || allowAllOrigins || corsOrigins.includes(origin)) {
callback(null, true);
return;
}
callback(new Error(`Origin ${origin} is not allowed by CORS`), false);
},
credentials: true,
methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'x-product-type'],
allowedHeaders: [
'Content-Type',
'Authorization',
'x-product-type',
'Accept',
'Origin',
'X-Requested-With',
],
exposedHeaders: ['Content-Disposition'],
optionsSuccessStatus: 204,
});
app.useGlobalPipes(
new ValidationPipe({