Skip to content

Commit

Permalink
Make it easier to test in local
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Aug 22, 2024
1 parent 7e89f89 commit fd11836
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions apps/api/src/app/plugins/cors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import cors from "@fastify/cors";
import fp from "fastify-plugin";
import cors, { FastifyCorsOptions } from '@fastify/cors';
import fp from 'fastify-plugin';

export default fp(async (fastify, opts) => {
const options = {
const options: FastifyCorsOptions = {
...opts,
origin: false,
delegator: (req, callback) => {
const corsOptions = {
origin: false,
};

// do not include CORS headers for requests from localhost
const origin = req.headers.origin;
console.log('delegator', origin);
if (origin && /^http:\/\/localhost/.test(origin)) {
corsOptions.origin = true;
}

// callback expects two parameters: error and options
callback(null, corsOptions);
},
};
fastify.register(cors, options);
});

0 comments on commit fd11836

Please sign in to comment.