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

View File

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

View File

@ -1,7 +1,7 @@
// Next.js API route support: https://vercel.com/docs/concepts/functions/serverless-functions
import type { NextApiRequest, NextApiResponse } from 'next'
import { listAllContracts } from '../../../lib/firebase/contracts'
import { toLiteContract } from './_types'
import { toLiteMarket } from './_types'
type Data = any[]
@ -12,5 +12,5 @@ export default async function handler(
const contracts = await listAllContracts()
// 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.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 { listAllComments } from '../../../../lib/firebase/comments'
import { getContractFromSlug } from '../../../../lib/firebase/contracts'
import { FullContract, ApiError, toLiteContract } from '../_types'
import { FullMarket, ApiError, toLiteMarket } from '../_types'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<FullContract | ApiError>
res: NextApiResponse<FullMarket | ApiError>
) {
const { slug } = req.query
@ -25,7 +25,7 @@ export default async function handler(
// Cache on Vercel edge servers for 2min
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
return res.status(200).json({
...toLiteContract(contract),
...toLiteMarket(contract),
bets,
comments,
})