manifold/web/pages/api/v0/slug/[slug].ts
Austin Chen 51aa8df010
Set up a read API using Vercel serverless functions (#49)
* Set up read API using Vercel serverless functions

Featuring:
/api/v0/markets
/api/v0/market/[contractId]
/api/v0/slug/[contractSlug]

* Include tags in API
2022-02-17 12:56:24 -08:00

33 lines
939 B
TypeScript

import { NextApiRequest, NextApiResponse } from 'next'
import { listAllBets } from '../../../../lib/firebase/bets'
import { listAllComments } from '../../../../lib/firebase/comments'
import { getContractFromSlug } from '../../../../lib/firebase/contracts'
import { FullContract, ApiError, toLiteContract } from '../_types'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<FullContract | ApiError>
) {
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({
...toLiteContract(contract),
bets,
comments,
})
}