Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/middlewares/cors.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import cors from 'cors';
import cors, { CorsOptions } from 'cors';
import { RequestHandler } from 'express';
import { appConfig } from '../config';

export const corsMiddleware = () =>
cors({
// origin: appConfig.allowedOrigins,
origin: appConfig.allowedOrigins,
credentials: true,
});
const defaultCorsOptions: CorsOptions = {
origin: appConfig.allowedOrigins,
credentials: true,
};

/**
* Global CORS middleware applied to all routes via app.use().
* Uses the allowed origins defined in appConfig.
*/
export const corsMiddleware = (): RequestHandler => cors(defaultCorsOptions);

/**
* Per-endpoint CORS override. Merges the provided options on top of the
* global defaults so only the fields you specify are changed.
*
* Keep secure defaults (allowedOrigins, credentials: true) unless you have
* an explicit reason to override them.
*
* @example
* // Allow a specific third-party origin on one public webhook route
* router.post('/webhook', corsOverride({ origin: 'https://partner.example.com', credentials: false }), handler);
*
* @example
* // Expose extra headers on a single endpoint
* router.get('/export', corsOverride({ exposedHeaders: ['Content-Disposition'] }), exportHandler);
*/
export function corsOverride(options: CorsOptions): RequestHandler {
return cors({ ...defaultCorsOptions, ...options });
}
Loading