manifold/web/pages/api/v0/market/[id].ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

import { NextApiRequest, NextApiResponse } from 'next'
import { Bet, listAllBets } from 'web/lib/firebase/bets'
import { listAllComments } from 'web/lib/firebase/comments'
import {
getContractFromId,
FreeResponseContract,
} from 'web/lib/firebase/contracts'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { FullMarket, ApiError, toLiteMarket } from '../_types'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<FullMarket | ApiError>
) {
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
const { id } = req.query
const contractId = id as string
2022-02-20 04:51:51 +00:00
const [contract, allBets, comments] = await Promise.all([
getContractFromId(contractId),
listAllBets(contractId),
listAllComments(contractId),
])
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2022-02-20 04:51:51 +00:00
const bets = allBets.map(({ userId, ...bet }) => bet) as Exclude<
Bet,
'userId'
>[]
const answers = (contract as FreeResponseContract).answers
if (!contract) {
res.status(404).json({ error: 'Contract not found' })
return
}
// 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,
answers,
})
}