All files / src/config index.ts

0% Statements 0/13
0% Branches 0/14
0% Functions 0/3
0% Lines 0/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                 
import * as dotenv from 'dotenv';
 
dotenv.config();
 
export const config = {
  port: process.env.PORT || 8080,
  defaultStorage: process.env.DEFAULT_STORAGE || 'https://dev3.4nkweb.com/storage',
  appHost: process.env.APP_HOST || 'http://localhost:3000',
 
  // CORS dynamique: autorise une liste d'origines connues
  cors: {
    origin: (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) => {
      const defaultAllowed = ['http://localhost:3000', 'http://local.4nkweb.com:3000', 'https://dev4.4nkweb.com'];
      const envAllowed = (process.env.CORS_ALLOWED_ORIGINS || '')
        .split(',')
        .map((s) => s.trim())
        .filter((s) => s.length > 0);
      const allowedOrigins = envAllowed.length > 0 ? envAllowed : defaultAllowed;
 
      if (!origin) {
        return callback(null, true);
      }
      if (allowedOrigins.includes(origin)) {
        return callback(null, true);
      }
      return callback(new Error(`CORS origin not allowed: ${origin}`));
    },
    methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    allowedHeaders: ['Content-Type', 'x-session-id', 'Authorization'],
    credentials: true
  }
};