2022-05-19 22:04:34 +00:00
|
|
|
import { auth } from './users'
|
|
|
|
import { app, functions } from './init'
|
|
|
|
|
|
|
|
export class APIError extends Error {
|
|
|
|
code: number
|
|
|
|
constructor(code: number, message: string) {
|
|
|
|
super(message)
|
|
|
|
this.code = code
|
|
|
|
this.name = 'APIError'
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
2022-05-19 22:04:34 +00:00
|
|
|
}
|
2022-01-05 18:23:58 +00:00
|
|
|
|
2022-05-19 22:04:34 +00:00
|
|
|
export async function call(name: string, method: string, params: any) {
|
|
|
|
const user = auth.currentUser
|
|
|
|
if (user == null) {
|
|
|
|
throw new Error('Must be signed in to make API calls.')
|
2022-01-19 03:36:46 +00:00
|
|
|
}
|
2022-05-19 22:04:34 +00:00
|
|
|
const token = await user.getIdToken()
|
|
|
|
const region = functions.region
|
|
|
|
const projectId = app.options.projectId
|
|
|
|
const url = `https://${region}-${projectId}.cloudfunctions.net/${name}`
|
|
|
|
const req = new Request(url, {
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method: method,
|
2022-05-20 21:58:14 +00:00
|
|
|
body: JSON.stringify(params),
|
2022-05-19 22:04:34 +00:00
|
|
|
})
|
|
|
|
return await fetch(req).then(async (resp) => {
|
|
|
|
const json = (await resp.json()) as { [k: string]: any }
|
2022-05-20 21:58:14 +00:00
|
|
|
if (!resp.ok) {
|
|
|
|
throw new APIError(resp.status, json?.message)
|
2022-05-19 22:04:34 +00:00
|
|
|
}
|
2022-05-20 21:58:14 +00:00
|
|
|
return json
|
2022-05-19 22:04:34 +00:00
|
|
|
})
|
2022-01-19 03:36:46 +00:00
|
|
|
}
|
2022-02-04 03:04:56 +00:00
|
|
|
|
2022-05-19 22:04:34 +00:00
|
|
|
export function createContract(params: any) {
|
|
|
|
return call('createContract', 'POST', params)
|
2022-02-04 03:04:56 +00:00
|
|
|
}
|
2022-04-21 17:58:12 +00:00
|
|
|
|
2022-05-19 22:04:34 +00:00
|
|
|
export function placeBet(params: any) {
|
|
|
|
return call('placeBet', 'POST', params)
|
2022-04-21 17:58:12 +00:00
|
|
|
}
|