manifold/web/lib/firebase/api-call.ts
Ian Philips 3b3717d307
Groups (#510)
* Folds=>groups

* Show groups on user profile

* Allow group creation from /create

* Refactoring to groups

* Convert folds to groups

* Add new add to group notification

* Fix user profile tab bug

* Add groups nav and tab for my groups

* Remove bad profile pages

* remove comments

* Add group list dropdown to sidebar

* remove unused

* group cards ui

* Messages=>Comments, v2, groupDetails

* Discussion time

* Cleaning up some code

* Remove follow count

* Fix pool scoring for cpmm

* Fix imports

* Simplify rules, add GroupUser collection

* Fix group cards

* Refactor

* Refactor

* Small fixes

* Remove string

* Add api error detail handling

* Clear name field

* Componentize

* Spacing

* Undo userpage memo

* Member groups are already in my tab

* Remove active contracts reference for now

* Remove unused

* Refactoring

* Allow adding old questions to a group

* Rename

* Wording

* Throw standard v2 APIError

* Hide input for non-members, add about under title

* Multiple names to & # more

* Move comments firestore rules to appropriate subpaths

* Group membership, pool=>volume

* Cleanup, useEvent

* Raise state to parent

* Eliminate unused

* Cleaning up

* Clean code

* Revert tags input deletion

* Cleaning code

* Stylling

* Limit members to display

* Array cleanup

* Add categories back in

* Private=>closed

* Unused vars
2022-06-22 11:35:50 -05:00

67 lines
1.9 KiB
TypeScript

import { auth } from './users'
import { ENV_CONFIG } from 'common/envs/constants'
import { V2CloudFunction } from 'common/envs/prod'
export class APIError extends Error {
code: number
details?: string
constructor(code: number, message: string, details?: string) {
super(message)
this.code = code
this.name = 'APIError'
this.details = details
}
}
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, json?.details)
}
return json
})
}
// Our users access the API through the Vercel proxy routes at /api/v0/blah,
// but right now at least until we get performance under control let's have the
// app just hit the cloud functions directly -- there's no difference and it's
// one less hop
export function getFunctionUrl(name: V2CloudFunction) {
return ENV_CONFIG.functionEndpoints[name]
}
export function createMarket(params: any) {
return call(getFunctionUrl('createmarket'), 'POST', params)
}
export function placeBet(params: any) {
return call(getFunctionUrl('placebet'), 'POST', params)
}
export function sellShares(params: any) {
return call(getFunctionUrl('sellshares'), 'POST', params)
}
export function sellBet(params: any) {
return call(getFunctionUrl('sellbet'), 'POST', params)
}
export function createGroup(params: any) {
return call(getFunctionUrl('creategroup'), 'POST', params)
}