fix api products
This commit is contained in:
@@ -2,7 +2,7 @@ export default () => ({
|
|||||||
app: {
|
app: {
|
||||||
port: parseInt(process.env.PORT ?? '3000', 10),
|
port: parseInt(process.env.PORT ?? '3000', 10),
|
||||||
nodeEnv: process.env.NODE_ENV ?? 'development',
|
nodeEnv: process.env.NODE_ENV ?? 'development',
|
||||||
corsOrigins: (process.env.CORS_ORIGINS ?? 'http://localhost:3000')
|
corsOrigins: (process.env.CORS_ORIGINS ?? '*')
|
||||||
.split(',')
|
.split(',')
|
||||||
.map((origin) => origin.trim())
|
.map((origin) => origin.trim())
|
||||||
.filter(Boolean),
|
.filter(Boolean),
|
||||||
|
|||||||
21
src/main.ts
21
src/main.ts
@@ -11,13 +11,30 @@ async function bootstrap() {
|
|||||||
const reflector = app.get(Reflector);
|
const reflector = app.get(Reflector);
|
||||||
const configService = app.get(ConfigService);
|
const configService = app.get(ConfigService);
|
||||||
const corsOrigins = configService.get<string[]>('app.corsOrigins', []);
|
const corsOrigins = configService.get<string[]>('app.corsOrigins', []);
|
||||||
|
const allowAllOrigins = corsOrigins.includes('*');
|
||||||
|
|
||||||
app.setGlobalPrefix('api');
|
app.setGlobalPrefix('api');
|
||||||
app.enableCors({
|
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,
|
credentials: true,
|
||||||
methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'],
|
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(
|
app.useGlobalPipes(
|
||||||
new ValidationPipe({
|
new ValidationPipe({
|
||||||
|
|||||||
Reference in New Issue
Block a user