From 60bb892601c54b292a3622d11695e6ffd5f933dc Mon Sep 17 00:00:00 2001 From: Ben Congdon Date: Thu, 16 Jun 2022 15:14:59 -0700 Subject: [PATCH] Include free response answers in FullMarket API response (#519) --- docs/docs/api.md | 3 ++- web/lib/firebase/contracts.ts | 4 ++-- web/pages/api/v0/_types.ts | 2 ++ web/pages/api/v0/market/[id].ts | 8 +++++++- web/pages/api/v0/slug/[slug].ts | 8 +++++++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/docs/docs/api.md b/docs/docs/api.md index 487c43f1..04febe8c 100644 --- a/docs/docs/api.md +++ b/docs/docs/api.md @@ -354,10 +354,11 @@ Requires no authorization. - Response type: A `FullMarket` ```tsx - // A complete market, along with bets and comments + // A complete market, along with bets, comments, and answers (for free response markets) type FullMarket = LiteMarket & { bets: Bet[] comments: Comment[] + answers?: Answer[] } type Bet = { diff --git a/web/lib/firebase/contracts.ts b/web/lib/firebase/contracts.ts index 0e0ad57e..f63a14e9 100644 --- a/web/lib/firebase/contracts.ts +++ b/web/lib/firebase/contracts.ts @@ -18,7 +18,7 @@ import { range, sortBy, sum } from 'lodash' import { app } from './init' import { getValues, listenForValue, listenForValues } from './utils' -import { BinaryContract, Contract } from 'common/contract' +import { BinaryContract, Contract, FreeResponseContract } from 'common/contract' import { getDpmProbability } from 'common/calculate-dpm' import { createRNG, shuffle } from 'common/util/random' import { getCpmmProbability } from 'common/calculate-cpmm' @@ -28,7 +28,7 @@ import { MAX_FEED_CONTRACTS } from 'common/recommended-contracts' import { Bet } from 'common/bet' import { Comment } from 'common/comment' import { ENV_CONFIG } from 'common/envs/constants' -export type { Contract } +export type { Contract, FreeResponseContract } export function contractPath(contract: Contract) { return `/${contract.creatorUsername}/${contract.slug}` diff --git a/web/pages/api/v0/_types.ts b/web/pages/api/v0/_types.ts index 78fe3708..ab008111 100644 --- a/web/pages/api/v0/_types.ts +++ b/web/pages/api/v0/_types.ts @@ -1,4 +1,5 @@ import { Bet } from 'common/bet' +import { Answer } from 'common/answer' import { getProbability } from 'common/calculate' import { Comment } from 'common/comment' import { Contract } from 'common/contract' @@ -40,6 +41,7 @@ export type LiteMarket = { export type FullMarket = LiteMarket & { bets: Exclude[] comments: Comment[] + answers?: Answer[] } export type ApiError = { diff --git a/web/pages/api/v0/market/[id].ts b/web/pages/api/v0/market/[id].ts index 8f33276d..aef45db3 100644 --- a/web/pages/api/v0/market/[id].ts +++ b/web/pages/api/v0/market/[id].ts @@ -1,7 +1,10 @@ import { NextApiRequest, NextApiResponse } from 'next' import { Bet, listAllBets } from 'web/lib/firebase/bets' import { listAllComments } from 'web/lib/firebase/comments' -import { getContractFromId } from 'web/lib/firebase/contracts' +import { + getContractFromId, + FreeResponseContract, +} from 'web/lib/firebase/contracts' import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors' import { FullMarket, ApiError, toLiteMarket } from '../_types' @@ -25,6 +28,8 @@ export default async function handler( 'userId' >[] + const answers = (contract as FreeResponseContract).answers + if (!contract) { res.status(404).json({ error: 'Contract not found' }) return @@ -36,5 +41,6 @@ export default async function handler( ...toLiteMarket(contract), bets, comments, + answers, }) } diff --git a/web/pages/api/v0/slug/[slug].ts b/web/pages/api/v0/slug/[slug].ts index 119a688b..acea746e 100644 --- a/web/pages/api/v0/slug/[slug].ts +++ b/web/pages/api/v0/slug/[slug].ts @@ -2,7 +2,10 @@ import { NextApiRequest, NextApiResponse } from 'next' import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors' import { Bet, listAllBets } from 'web/lib/firebase/bets' import { listAllComments } from 'web/lib/firebase/comments' -import { getContractFromSlug } from 'web/lib/firebase/contracts' +import { + getContractFromSlug, + FreeResponseContract, +} from 'web/lib/firebase/contracts' import { FullMarket, ApiError, toLiteMarket } from '../_types' export default async function handler( @@ -24,6 +27,8 @@ export default async function handler( listAllComments(contract.id), ]) + const answers = (contract as FreeResponseContract).answers + // eslint-disable-next-line @typescript-eslint/no-unused-vars const bets = allBets.map(({ userId, ...bet }) => bet) as Exclude< Bet, @@ -36,5 +41,6 @@ export default async function handler( ...toLiteMarket(contract), bets, comments, + answers, }) }