diff --git a/docs/docs/api.md b/docs/docs/api.md index 1cea6027..667c68b8 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -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 diff --git a/web/pages/api/v0/user/[username]/index.ts b/web/pages/api/v0/user/[username]/index.ts new file mode 100644 index 00000000..58daffcd --- /dev/null +++ b/web/pages/api/v0/user/[username]/index.ts @@ -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 +) { + 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)) +} diff --git a/web/pages/api/v0/user/by-id/[id].ts b/web/pages/api/v0/user/by-id/[id].ts new file mode 100644 index 00000000..6ed67d1c --- /dev/null +++ b/web/pages/api/v0/user/by-id/[id].ts @@ -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 +) { + 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)) +}