Public API improvements (#266)
* Remove needless wrappers from API requests and responses * Return real HTTP status codes instead of status field * More robustly handle API errors * Fix broken error handling in NumericResolutionPanel warmup
This commit is contained in:
parent
03a13248a4
commit
540476f65a
|
@ -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.' })
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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) => {
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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<DPM | CPMM, Binary>
|
||||
|
||||
setCreatedContracts((prev) => [...prev, contract])
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user