API: Rename to LiteMarket/FullMarket; include creator avatar url

This commit is contained in:
Austin Chen 2022-02-17 15:22:51 -08:00
parent 24e646640a
commit 8f40350a75
4 changed files with 21 additions and 12 deletions

View File

@ -3,16 +3,23 @@ import { getProbability } from '../../../../common/calculate'
import { Comment } from '../../../../common/comment' import { Comment } from '../../../../common/comment'
import { Contract } from '../../../../common/contract' import { Contract } from '../../../../common/contract'
export type LiteContract = { export type LiteMarket = {
// Unique identifer for this market
id: string id: string
// Attributes about the creator
creatorUsername: string creatorUsername: string
creatorName: string creatorName: string
createdTime: number createdTime: number
creatorAvatarUrl?: string
// Market attributes. All times are in milliseconds since epoch
closeTime?: number closeTime?: number
question: string question: string
description: string description: string
tags: string[] tags: string[]
url: string url: string
pool: number pool: number
probability: number probability: number
volume7Days: number volume7Days: number
@ -21,7 +28,7 @@ export type LiteContract = {
resolution?: string resolution?: string
} }
export type FullContract = LiteContract & { export type FullMarket = LiteMarket & {
bets: Bet[] bets: Bet[]
comments: Comment[] comments: Comment[]
} }
@ -30,11 +37,12 @@ export type ApiError = {
error: string error: string
} }
export function toLiteContract({ export function toLiteMarket({
id, id,
creatorUsername, creatorUsername,
creatorName, creatorName,
createdTime, createdTime,
creatorAvatarUrl,
closeTime, closeTime,
question, question,
description, description,
@ -46,12 +54,13 @@ export function toLiteContract({
volume24Hours, volume24Hours,
isResolved, isResolved,
resolution, resolution,
}: Contract): LiteContract { }: Contract): LiteMarket {
return { return {
id, id,
creatorUsername, creatorUsername,
creatorName, creatorName,
createdTime, createdTime,
creatorAvatarUrl,
closeTime, closeTime,
question, question,
description, description,

View File

@ -2,11 +2,11 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { listAllBets } from '../../../../lib/firebase/bets' import { listAllBets } from '../../../../lib/firebase/bets'
import { listAllComments } from '../../../../lib/firebase/comments' import { listAllComments } from '../../../../lib/firebase/comments'
import { getContractFromId } from '../../../../lib/firebase/contracts' import { getContractFromId } from '../../../../lib/firebase/contracts'
import { FullContract, ApiError, toLiteContract } from '../_types' import { FullMarket, ApiError, toLiteMarket } from '../_types'
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<FullContract | ApiError> res: NextApiResponse<FullMarket | ApiError>
) { ) {
const { id } = req.query const { id } = req.query
const contractId = id as string const contractId = id as string
@ -25,7 +25,7 @@ export default async function handler(
// Cache on Vercel edge servers for 2min // Cache on Vercel edge servers for 2min
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120') res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
return res.status(200).json({ return res.status(200).json({
...toLiteContract(contract), ...toLiteMarket(contract),
bets, bets,
comments, comments,
}) })

View File

@ -1,7 +1,7 @@
// Next.js API route support: https://vercel.com/docs/concepts/functions/serverless-functions // Next.js API route support: https://vercel.com/docs/concepts/functions/serverless-functions
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
import { listAllContracts } from '../../../lib/firebase/contracts' import { listAllContracts } from '../../../lib/firebase/contracts'
import { toLiteContract } from './_types' import { toLiteMarket } from './_types'
type Data = any[] type Data = any[]
@ -12,5 +12,5 @@ export default async function handler(
const contracts = await listAllContracts() const contracts = await listAllContracts()
// Serve from Vercel cache, then update. see https://vercel.com/docs/concepts/functions/edge-caching // Serve from Vercel cache, then update. see https://vercel.com/docs/concepts/functions/edge-caching
res.setHeader('Cache-Control', 's-maxage=1, stale-while-revalidate') res.setHeader('Cache-Control', 's-maxage=1, stale-while-revalidate')
res.status(200).json(contracts.map(toLiteContract)) res.status(200).json(contracts.map(toLiteMarket))
} }

View File

@ -2,11 +2,11 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { listAllBets } from '../../../../lib/firebase/bets' import { listAllBets } from '../../../../lib/firebase/bets'
import { listAllComments } from '../../../../lib/firebase/comments' import { listAllComments } from '../../../../lib/firebase/comments'
import { getContractFromSlug } from '../../../../lib/firebase/contracts' import { getContractFromSlug } from '../../../../lib/firebase/contracts'
import { FullContract, ApiError, toLiteContract } from '../_types' import { FullMarket, ApiError, toLiteMarket } from '../_types'
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse<FullContract | ApiError> res: NextApiResponse<FullMarket | ApiError>
) { ) {
const { slug } = req.query const { slug } = req.query
@ -25,7 +25,7 @@ export default async function handler(
// Cache on Vercel edge servers for 2min // Cache on Vercel edge servers for 2min
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120') res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
return res.status(200).json({ return res.status(200).json({
...toLiteContract(contract), ...toLiteMarket(contract),
bets, bets,
comments, comments,
}) })