2022-02-17 20:56:24 +00:00
|
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
2022-05-08 20:33:58 +00:00
|
|
|
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
|
2022-06-17 07:35:38 +00:00
|
|
|
import { listAllBets } from 'web/lib/firebase/bets'
|
2022-05-08 20:33:58 +00:00
|
|
|
import { listAllComments } from 'web/lib/firebase/comments'
|
2022-06-17 07:20:43 +00:00
|
|
|
import { getContractFromSlug } from 'web/lib/firebase/contracts'
|
|
|
|
import { FullMarket, ApiError, toFullMarket } from '../_types'
|
2022-02-17 20:56:24 +00:00
|
|
|
|
|
|
|
export default async function handler(
|
|
|
|
req: NextApiRequest,
|
2022-02-17 23:22:51 +00:00
|
|
|
res: NextApiResponse<FullMarket | ApiError>
|
2022-02-17 20:56:24 +00:00
|
|
|
) {
|
2022-04-30 20:30:49 +00:00
|
|
|
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
|
2022-02-17 20:56:24 +00:00
|
|
|
const { slug } = req.query
|
|
|
|
|
|
|
|
const contract = await getContractFromSlug(slug as string)
|
|
|
|
|
|
|
|
if (!contract) {
|
|
|
|
res.status(404).json({ error: 'Contract not found' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-17 07:35:38 +00:00
|
|
|
const [bets, comments] = await Promise.all([
|
2022-02-17 20:56:24 +00:00
|
|
|
listAllBets(contract.id),
|
|
|
|
listAllComments(contract.id),
|
|
|
|
])
|
|
|
|
|
|
|
|
// Cache on Vercel edge servers for 2min
|
|
|
|
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
|
2022-06-17 07:20:43 +00:00
|
|
|
return res.status(200).json(toFullMarket(contract, comments, bets))
|
2022-02-17 20:56:24 +00:00
|
|
|
}
|