manifold/web/pages/api/v0/slug/[slug].ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

import { NextApiRequest, NextApiResponse } from 'next'
2022-02-20 04:51:51 +00:00
import { Bet, listAllBets } from '../../../../lib/firebase/bets'
import { listAllComments } from '../../../../lib/firebase/comments'
import { getContractFromSlug } from '../../../../lib/firebase/contracts'
import { FullMarket, ApiError, toLiteMarket } from '../_types'
import { applyCorsHeaders, CORS_UNRESTRICTED } from '../../../../lib/api/cors'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<FullMarket | ApiError>
) {
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
const { slug } = req.query
const contract = await getContractFromSlug(slug as string)
if (!contract) {
res.status(404).json({ error: 'Contract not found' })
return
}
2022-02-20 04:51:51 +00:00
const [allBets, comments] = await Promise.all([
listAllBets(contract.id),
listAllComments(contract.id),
])
2022-02-20 04:51:51 +00:00
const bets = allBets.map(({ userId, ...bet }) => bet) as Exclude<
Bet,
'userId'
>[]
// Cache on Vercel edge servers for 2min
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
return res.status(200).json({
...toLiteMarket(contract),
bets,
comments,
})
}