Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
chore(corsConfig.ts): whitelist values can be string or regex
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCharrier committed Jan 22, 2024
1 parent eb4ae23 commit 4c10ed1
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/utils/corsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ var whitelist = config.CORS_ORIGIN;

const corsOptions = {
origin: function (origin, callback) {
if (
whitelist.indexOf(origin) !== -1 ||
process.env.NODE_ENV === 'test' ||
!origin
) {
const isAllowed = whitelist.some((allowedOrigin: RegExp | string) => {
if (typeof allowedOrigin === 'string') {
return origin === allowedOrigin;
} else if (allowedOrigin instanceof RegExp) {
return allowedOrigin.test(origin);
}
return false;
});

if (isAllowed || process.env.NODE_ENV === 'test' || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
Expand Down

0 comments on commit 4c10ed1

Please sign in to comment.