manifold/web/pages/api/v0/slug/[slug].ts
Marshall Polaris 732a474cc9
Add userId to bets in API (#530)
* add userid to bets

* allBets -> bets

* revert one

Co-authored-by: wasabipesto <21313833+wasabipesto@users.noreply.github.com>
2022-06-17 00:35:38 -07:00

31 lines
1010 B
TypeScript

import { NextApiRequest, NextApiResponse } from 'next'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { listAllBets } from 'web/lib/firebase/bets'
import { listAllComments } from 'web/lib/firebase/comments'
import { getContractFromSlug } from 'web/lib/firebase/contracts'
import { FullMarket, ApiError, toFullMarket } from '../_types'
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
}
const [bets, comments] = await Promise.all([
listAllBets(contract.id),
listAllComments(contract.id),
])
// Cache on Vercel edge servers for 2min
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
return res.status(200).json(toFullMarket(contract, comments, bets))
}