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
This commit is contained in:
parent
feba0b58ee
commit
38c26f8b5c
|
@ -34,6 +34,18 @@ response was a 4xx or 5xx.)
|
|||
|
||||
## Endpoints
|
||||
|
||||
### `GET /v0/user/[username]`
|
||||
|
||||
Gets a user by their username. Remember that usernames may change.
|
||||
|
||||
Requires no authorization.
|
||||
|
||||
### `GET /v0/user/by-id/[id]`
|
||||
|
||||
Gets a user by their unique ID. Many other API endpoints return this as the `userId`.
|
||||
|
||||
Requires no authorization.
|
||||
|
||||
### `GET /v0/markets`
|
||||
|
||||
Lists all markets, ordered by creation date descending.
|
||||
|
@ -627,6 +639,7 @@ Requires no authorization.
|
|||
|
||||
## Changelog
|
||||
|
||||
- 2022-07-15: Add user by username and user by ID APIs
|
||||
- 2022-06-08: Add paging to markets endpoint
|
||||
- 2022-06-05: Add new authorized write endpoints
|
||||
- 2022-02-28: Add `resolutionTime` to markets, change `closeTime` definition
|
||||
|
|
19
web/pages/api/v0/user/[username]/index.ts
Normal file
19
web/pages/api/v0/user/[username]/index.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
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))
|
||||
}
|
19
web/pages/api/v0/user/by-id/[id].ts
Normal file
19
web/pages/api/v0/user/by-id/[id].ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { getUser } 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 { id } = req.query
|
||||
const user = await getUser(id 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))
|
||||
}
|
Loading…
Reference in New Issue
Block a user