2022-02-17 20:56:24 +00:00
|
|
|
// Next.js API route support: https://vercel.com/docs/concepts/functions/serverless-functions
|
|
|
|
import type { NextApiRequest, NextApiResponse } from 'next'
|
2022-05-08 20:33:58 +00:00
|
|
|
import { listAllContracts } from 'web/lib/firebase/contracts'
|
|
|
|
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
|
2022-02-17 23:22:51 +00:00
|
|
|
import { toLiteMarket } from './_types'
|
2022-02-17 20:56:24 +00:00
|
|
|
|
|
|
|
export default async function handler(
|
|
|
|
req: NextApiRequest,
|
2022-06-09 01:08:06 +00:00
|
|
|
res: NextApiResponse
|
2022-02-17 20:56:24 +00:00
|
|
|
) {
|
2022-04-30 20:30:49 +00:00
|
|
|
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
|
2022-06-09 01:08:06 +00:00
|
|
|
let before: string | undefined
|
|
|
|
let limit: number | undefined
|
|
|
|
if (req.query.before != null) {
|
|
|
|
if (typeof req.query.before !== 'string') {
|
|
|
|
res.status(400).json({ error: 'before must be null or a market ID.' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
before = req.query.before
|
|
|
|
}
|
|
|
|
if (req.query.limit != null) {
|
|
|
|
if (typeof req.query.limit !== 'string') {
|
|
|
|
res
|
|
|
|
.status(400)
|
|
|
|
.json({ error: 'limit must be null or a number of markets to return.' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
limit = parseInt(req.query.limit)
|
|
|
|
} else {
|
|
|
|
limit = 1000
|
|
|
|
}
|
|
|
|
if (limit < 1 || limit > 1000) {
|
|
|
|
res.status(400).json({ error: 'limit must be between 1 and 1000.' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const contracts = await listAllContracts(limit, before)
|
|
|
|
// 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.status(200).json(contracts.map(toLiteMarket))
|
|
|
|
} catch (e) {
|
|
|
|
res.status(400).json({
|
|
|
|
error:
|
|
|
|
'Failed to fetch markets (did you pass an invalid ID as the before parameter?)',
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-02-17 20:56:24 +00:00
|
|
|
}
|