manifold/web/pages/api/v0/user/[username]/index.ts
Marshall Polaris 38c26f8b5c
Add API endpoints for fetching user info by username and ID (#652)
* Add an API endpoint for fetching user info by username

* Add endpoint for querying users by ID, too

* Add very simple docs about user APIs
2022-07-15 14:03:34 -07:00

20 lines
683 B
TypeScript

import { NextApiRequest, NextApiResponse } from 'next'
import { getUserByUsername } from 'web/lib/firebase/users'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { LiteUser, ApiError, toLiteUser } from '../../_types'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<LiteUser | 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
}
res.setHeader('Cache-Control', 'no-cache')
return res.status(200).json(toLiteUser(user))
}