import Cors from 'cors' import { NextApiRequest, NextApiResponse } from 'next' import { SSR_CONFIG } from '@SSR_CONFIG' // Helper method to wait for a middleware to execute before continuing // And to throw an error when an error happens in a middleware const initMiddleware = (middleware: any) => (req: NextApiRequest, res: NextApiResponse) => new Promise((resolve, reject) => { middleware(req, res, (result: any) => { if (result instanceof Error) { return reject(result) } return resolve(result) }) }) export const corsMiddleware = initMiddleware( Cors({ // Only allow requests with GET, POST and OPTIONS methods: ['GET', 'OPTIONS', 'POST'], origin: [ ...SSR_CONFIG.corsWhitelistDomains, ...['http://localhost:6006', 'http://localhost:3000'] ] }) )