Apply permissive CORS headers to API (#115)

* Take cors package as dependency

* Apply permissive CORS headers to all API routes
This commit is contained in:
Marshall Polaris 2022-04-30 13:30:49 -07:00 committed by GitHub
parent a3311bd5aa
commit 731e5d5b7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 0 deletions

20
web/lib/api/cors.ts Normal file
View File

@ -0,0 +1,20 @@
import Cors from 'cors'
import { NextApiRequest, NextApiResponse } from 'next'
export function applyCorsHeaders(
req: NextApiRequest,
res: NextApiResponse,
params: object
) {
// This cors module is made as express.js middleware, so it's easier to promisify it for ourselves.
return new Promise((resolve, reject) => {
Cors(params)(req, res, (result) => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
}
export const CORS_UNRESTRICTED = {}

View File

@ -22,6 +22,7 @@
"@nivo/core": "0.74.0", "@nivo/core": "0.74.0",
"@nivo/line": "0.74.0", "@nivo/line": "0.74.0",
"clsx": "1.1.1", "clsx": "1.1.1",
"cors": "^2.8.5",
"daisyui": "1.16.4", "daisyui": "1.16.4",
"dayjs": "1.10.7", "dayjs": "1.10.7",
"firebase": "9.6.0", "firebase": "9.6.0",

View File

@ -3,11 +3,13 @@ import { Bet, listAllBets } from '../../../../lib/firebase/bets'
import { listAllComments } from '../../../../lib/firebase/comments' import { listAllComments } from '../../../../lib/firebase/comments'
import { getContractFromId } from '../../../../lib/firebase/contracts' import { getContractFromId } from '../../../../lib/firebase/contracts'
import { FullMarket, ApiError, toLiteMarket } from '../_types' import { FullMarket, ApiError, toLiteMarket } from '../_types'
import { applyCorsHeaders, CORS_UNRESTRICTED } from '../../../../lib/api/cors'
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<FullMarket | ApiError> res: NextApiResponse<FullMarket | ApiError>
) { ) {
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
const { id } = req.query const { id } = req.query
const contractId = id as string const contractId = id as string

View File

@ -2,6 +2,7 @@
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
import { listAllContracts } from '../../../lib/firebase/contracts' import { listAllContracts } from '../../../lib/firebase/contracts'
import { toLiteMarket } from './_types' import { toLiteMarket } from './_types'
import { applyCorsHeaders, CORS_UNRESTRICTED } from '../../../lib/api/cors'
type Data = any[] type Data = any[]
@ -9,6 +10,7 @@ export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<Data> res: NextApiResponse<Data>
) { ) {
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
const contracts = await listAllContracts() const contracts = await listAllContracts()
// Serve from Vercel cache, then update. see https://vercel.com/docs/concepts/functions/edge-caching // Serve from Vercel cache, then update. see https://vercel.com/docs/concepts/functions/edge-caching
res.setHeader('Cache-Control', 's-maxage=1, stale-while-revalidate') res.setHeader('Cache-Control', 's-maxage=1, stale-while-revalidate')

View File

@ -3,11 +3,13 @@ import { Bet, listAllBets } from '../../../../lib/firebase/bets'
import { listAllComments } from '../../../../lib/firebase/comments' import { listAllComments } from '../../../../lib/firebase/comments'
import { getContractFromSlug } from '../../../../lib/firebase/contracts' import { getContractFromSlug } from '../../../../lib/firebase/contracts'
import { FullMarket, ApiError, toLiteMarket } from '../_types' import { FullMarket, ApiError, toLiteMarket } from '../_types'
import { applyCorsHeaders, CORS_UNRESTRICTED } from '../../../../lib/api/cors'
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<FullMarket | ApiError> res: NextApiResponse<FullMarket | ApiError>
) { ) {
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
const { slug } = req.query const { slug } = req.query
const contract = await getContractFromSlug(slug as string) const contract = await getContractFromSlug(slug as string)