From fa86f5e89a1b1a6478a29d94dc93152eac273edc Mon Sep 17 00:00:00 2001 From: Justin <21313833+wasabipesto@users.noreply.github.com> Date: Sat, 25 Jun 2022 19:28:01 -0400 Subject: [PATCH] Add Users API endpoint (#547) * add users endpoint to API * docs, url * tweak docs --- docs/docs/api.md | 59 +++++++++++++++++++++++++++++++++++++ web/pages/api/v0/_types.ts | 60 ++++++++++++++++++++++++++++++++++++++ web/pages/api/v0/users.ts | 17 +++++++++++ 3 files changed, 136 insertions(+) create mode 100644 web/pages/api/v0/users.ts diff --git a/docs/docs/api.md b/docs/docs/api.md index b7420b52..ffdaa65f 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -398,6 +398,65 @@ Requires no authorization. ``` - Response type: A `FullMarket` ; same as above. +### `GET /v0/users` + +Lists all users. + +Requires no authorization. + +- Example request + ``` + https://manifold.markets/api/v0/users + ``` +- Example response + ```json + [ + { + "id":"igi2zGXsfxYPgB0DJTXVJVmwCOr2", + "createdTime":1639011767273, + "name":"Austin", + "username":"Austin", + "url":"https://manifold.markets/Austin", + "avatarUrl":"https://lh3.googleusercontent.com/a-/AOh14GiZyl1lBehuBMGyJYJhZd-N-mstaUtgE4xdI22lLw=s96-c", + "bio":"I build Manifold! Always happy to chat; reach out on Discord or find a time on https://calendly.com/austinchen/manifold!", + "bannerUrl":"https://images.unsplash.com/photo-1501523460185-2aa5d2a0f981?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1531&q=80", + "website":"https://blog.austn.io", + "twitterHandle":"akrolsmir", + "discordHandle":"akrolsmir#4125", + "balance":9122.607163564959, + "totalDeposits":10339.004780544328, + "totalPnLCached":9376.601262721899, + "creatorVolumeCached":76078.46984199001 + } + ``` +- Response type: Array of `LiteUser` + + ```tsx + // Basic information about a user + type LiteUser = { + id: string // user's unique id + createdTime: number + + name: string // display name, may contain spaces + username: string // username, used in urls + url: string // link to user's profile + avatarUrl?: string + + bio?: string + bannerUrl?: string + website?: string + twitterHandle?: string + discordHandle?: string + + // Note: the following are here for convenience only and may be removed in the future. + balance: number + totalDeposits: number + totalPnLCached: number + creatorVolumeCached: number + } + ``` + + ### `POST /v0/bet` Places a new bet on behalf of the authorized user. diff --git a/web/pages/api/v0/_types.ts b/web/pages/api/v0/_types.ts index d3a74053..f9a66fa2 100644 --- a/web/pages/api/v0/_types.ts +++ b/web/pages/api/v0/_types.ts @@ -3,7 +3,9 @@ import { Answer } from 'common/answer' import { getOutcomeProbability, getProbability } from 'common/calculate' import { Comment } from 'common/comment' import { Contract } from 'common/contract' +import { User } from 'common/user' import { removeUndefinedProps } from 'common/util/object' +import { ENV_CONFIG } from 'common/envs/constants' export type LiteMarket = { // Unique identifer for this market @@ -143,3 +145,61 @@ function augmentAnswerWithProbability( probability, } } + +export type LiteUser = { + id: string + createdTime: number + + name: string + username: string + url: string + avatarUrl?: string + + bio?: string + bannerUrl?: string + website?: string + twitterHandle?: string + discordHandle?: string + + balance: number + totalDeposits: number + totalPnLCached: number + creatorVolumeCached: number +} + +export function toLiteUser(user: User): LiteUser { + const { + id, + createdTime, + name, + username, + avatarUrl, + bio, + bannerUrl, + website, + twitterHandle, + discordHandle, + balance, + totalDeposits, + totalPnLCached, + creatorVolumeCached, + } = user + + return removeUndefinedProps({ + id, + createdTime, + name, + username, + url: `https://${ENV_CONFIG.domain}/${username}`, + avatarUrl, + bio, + bannerUrl, + website, + twitterHandle, + discordHandle, + balance, + totalDeposits, + totalPnLCached, + creatorVolumeCached, + }) +} diff --git a/web/pages/api/v0/users.ts b/web/pages/api/v0/users.ts new file mode 100644 index 00000000..8c62b601 --- /dev/null +++ b/web/pages/api/v0/users.ts @@ -0,0 +1,17 @@ +// Next.js API route support: https://vercel.com/docs/concepts/functions/serverless-functions +import type { NextApiRequest, NextApiResponse } from 'next' +import { listAllUsers } from 'web/lib/firebase/users' +import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors' +import { toLiteUser } from './_types' + +type Data = any[] + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + await applyCorsHeaders(req, res, CORS_UNRESTRICTED) + const users = await listAllUsers() + res.setHeader('Cache-Control', 'max-age=0') + res.status(200).json(users.map(toLiteUser)) +}