manifold/web/pages/api/v0/user/[username]/bets/index.ts
Ben Congdon c3bc25a4b9
Add API route for listing a bets by user (#567)
* Add API route for getting a user's bets

* Refactor bets API to use /bets

* Update /markets to use zod validation

* Update docs
2022-07-07 15:36:02 -07:00

26 lines
784 B
TypeScript

import { NextApiRequest, NextApiResponse } from 'next'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { Bet, getUserBets } from 'web/lib/firebase/bets'
import { getUserByUsername } from 'web/lib/firebase/users'
import { ApiError } from '../../../_types'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Bet[] | ApiError>
) {
await applyCorsHeaders(req, res, CORS_UNRESTRICTED)
const { username } = req.query
const user = await getUserByUsername(username as string)
if (!user) {
res.status(404).json({ error: 'User not found' })
return
}
const bets = await getUserBets(user.id, { includeRedemptions: false })
res.setHeader('Cache-Control', 'max-age=0')
return res.status(200).json(bets)
}