3987baa11b
* Improve typing of client CORS helper * Take node-fetch as a dependency * Add explicit Firebase region into config * Add new Vercel proxy API routes that talk to backend * Call Vercel proxy routes from `api-call` module * Tweak import to try to get Vercel happy * Tidy up a tiny bit
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import { auth } from './users'
|
|
|
|
export class APIError extends Error {
|
|
code: number
|
|
constructor(code: number, message: string) {
|
|
super(message)
|
|
this.code = code
|
|
this.name = 'APIError'
|
|
}
|
|
}
|
|
|
|
export async function call(url: string, method: string, params: any) {
|
|
const user = auth.currentUser
|
|
if (user == null) {
|
|
throw new Error('Must be signed in to make API calls.')
|
|
}
|
|
const token = await user.getIdToken()
|
|
const req = new Request(url, {
|
|
headers: {
|
|
Authorization: `Bearer ${token}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
method: method,
|
|
body: JSON.stringify(params),
|
|
})
|
|
return await fetch(req).then(async (resp) => {
|
|
const json = (await resp.json()) as { [k: string]: any }
|
|
if (!resp.ok) {
|
|
throw new APIError(resp.status, json?.message)
|
|
}
|
|
return json
|
|
})
|
|
}
|
|
|
|
export function createContract(params: any) {
|
|
return call('/api/v0/market', 'POST', params)
|
|
}
|
|
|
|
export function placeBet(params: any) {
|
|
return call('/api/v0/bets', 'POST', params)
|
|
}
|