A simple CORS module for use with Vercel Serverless Functions. Also compatible with Next.js API Routes.
yarn add vercel-cors | npm i vercel-cors
Create your CORS configuration in a separated file:
config/cors.js
import cors from "vercel-cors"
export default cors({
origin: '*' | string | RegExp | Array<string | RegExp>,
headers: string[],
methods: Array<'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'UPDATE' | 'DELETE' | 'OPTIONS'>,
expose: string[],
maxAge: number | undefined,
credentials: boolean
})
Then export as default a Higher Order Function passing your handler as parameter:
api/index.js
import withCors from "config/cors"
async function handler (req, res) {
// do something
}
export default withCors(handler)
export const defaults = {
origin: '*',
headers: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept'],
methods: ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'UPDATE', 'OPTIONS', 'DELETE'],
expose: [],
maxAge: undefined,
credentials: false
}
import cors from "vercel-cors"
export default cors({
origin: /^https:\/\/vercelproject-[a-z0-9]+-organization.vercel.app$/,
methods: ['GET', 'POST', 'PUT', 'PATCH', 'UPDATE', 'DELETE'],
credentials: true
})
Just export differents cors()
functions from differents files:
config/cors
├── dev.js
└── prod.js
diegoulloao · 2022