diff --git a/functions/src/api.ts b/functions/src/api.ts index 9fc989f3..aee95532 100644 --- a/functions/src/api.ts +++ b/functions/src/api.ts @@ -115,15 +115,13 @@ export const newEndpoint = (methods: [string], fn: Handler) => const allowed = methods.join(', ') throw new APIError(405, `This endpoint supports only ${allowed}.`) } - const data = await fn(req, res) - data.status = 'success' - res.status(200).json({ data: data }) + res.status(200).json(await fn(req, res)) } catch (e) { if (e instanceof APIError) { // Emit a 200 anyway here for now, for backwards compatibility - res.status(200).json({ data: { status: 'error', message: e.msg } }) + res.status(e.code).json({ message: e.msg }) } else { - res.status(500).json({ data: { status: 'error', message: '???' } }) + res.status(500).json({ message: 'An unknown error occurred.' }) } } }) diff --git a/functions/src/create-contract.ts b/functions/src/create-contract.ts index 4046df5c..5feb32c0 100644 --- a/functions/src/create-contract.ts +++ b/functions/src/create-contract.ts @@ -44,7 +44,7 @@ export const createContract = newEndpoint(['POST'], async (req, _res) => { min, max, manaLimitPerUser, - } = req.body.data || {} + } = req.body || {} if (!question || typeof question != 'string') throw new APIError(400, 'Missing or invalid question field') @@ -205,7 +205,7 @@ export const createContract = newEndpoint(['POST'], async (req, _res) => { await anteBetDoc.set(anteBet) } - return { contract: contract } + return contract }) const getSlug = async (question: string) => { diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index a95b46de..4de53496 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -16,7 +16,7 @@ import { Fees } from '../../common/fees' export const placeBet = newEndpoint(['POST'], async (req, _res) => { const [bettor, _privateUser] = await lookupUser(await parseCredentials(req)) - const { amount, outcome, contractId, value } = req.body.data || {} + const { amount, outcome, contractId, value } = req.body || {} if (amount <= 0 || isNaN(amount) || !isFinite(amount)) throw new APIError(400, 'Invalid amount') diff --git a/web/components/numeric-resolution-panel.tsx b/web/components/numeric-resolution-panel.tsx index cbda7f9c..f05a1c0a 100644 --- a/web/components/numeric-resolution-panel.tsx +++ b/web/components/numeric-resolution-panel.tsx @@ -17,7 +17,7 @@ export function NumericResolutionPanel(props: { }) { useEffect(() => { // warm up cloud function - resolveMarket({} as any).catch() + resolveMarket({} as any).catch(() => {}) }, []) const { contract, className } = props diff --git a/web/lib/firebase/api-call.ts b/web/lib/firebase/api-call.ts index ebc90d20..61a32021 100644 --- a/web/lib/firebase/api-call.ts +++ b/web/lib/firebase/api-call.ts @@ -25,14 +25,14 @@ export async function call(name: string, method: string, params: any) { 'Content-Type': 'application/json', }, method: method, - body: JSON.stringify({ data: params }), + body: JSON.stringify(params), }) return await fetch(req).then(async (resp) => { const json = (await resp.json()) as { [k: string]: any } - if (json.data.status == 'error') { - throw new APIError(resp.status, json.data.message) + if (!resp.ok) { + throw new APIError(resp.status, json?.message) } - return json.data + return json }) } diff --git a/web/pages/create.tsx b/web/pages/create.tsx index b68bde82..caf4927f 100644 --- a/web/pages/create.tsx +++ b/web/pages/create.tsx @@ -138,7 +138,7 @@ export function NewContract(props: { question: string; tag?: string }) { max, }) ) - await router.push(contractPath(result.contract as Contract)) + await router.push(contractPath(result as Contract)) } catch (e) { console.log('error creating contract', e) } diff --git a/web/pages/make-predictions.tsx b/web/pages/make-predictions.tsx index 081b29b7..55fba2ce 100644 --- a/web/pages/make-predictions.tsx +++ b/web/pages/make-predictions.tsx @@ -148,14 +148,14 @@ ${TEST_VALUE} } setIsSubmitting(true) for (const prediction of predictions) { - const contract = await createContract({ + const contract = (await createContract({ question: prediction.question, description: prediction.description, initialProb: prediction.initialProb, ante, closeTime, tags: parseWordsAsTags(tags), - }).then((r) => r.contract) + })) as FullContract setCreatedContracts((prev) => [...prev, contract]) }