Migrate placeBet and createContract to v2 functions (#432)

This commit is contained in:
Marshall Polaris 2022-06-06 12:34:58 -07:00 committed by GitHub
parent 44b3579cc7
commit 13826b5759
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 11 deletions

View File

@ -1,5 +1,8 @@
import * as admin from 'firebase-admin'
import * as functions from 'firebase-functions'
import { Response } from 'express'
import { logger } from 'firebase-functions/v2'
import { onRequest, Request } from 'firebase-functions/v2/https'
import * as Cors from 'cors'
import { z } from 'zod'
@ -10,8 +13,6 @@ import {
} from '../../common/envs/constants'
type Output = Record<string, unknown>
type Request = functions.https.Request
type Response = functions.Response
type AuthedUser = [User, PrivateUser]
type Handler = (req: Request, user: AuthedUser) => Promise<Output>
type JwtCredentials = { kind: 'jwt'; data: admin.auth.DecodedIdToken }
@ -47,7 +48,7 @@ export const parseCredentials = async (req: Request): Promise<Credentials> => {
return { kind: 'jwt', data: jwt }
} catch (err) {
// This is somewhat suspicious, so get it into the firebase console
functions.logger.error('Error verifying Firebase JWT: ', err)
logger.error('Error verifying Firebase JWT: ', err)
throw new APIError(403, 'Error validating token.')
}
case 'Key':
@ -135,7 +136,7 @@ export const validate = <T extends z.ZodTypeAny>(schema: T, val: unknown) => {
}
export const newEndpoint = (methods: [string], fn: Handler) =>
functions.runWith({ minInstances: 1 }).https.onRequest(async (req, res) => {
onRequest({ minInstances: 1 }, async (req, res) => {
try {
await applyCors(req, res, {
origin: [CORS_ORIGIN_MANIFOLD, CORS_ORIGIN_LOCALHOST],
@ -155,7 +156,7 @@ export const newEndpoint = (methods: [string], fn: Handler) =>
}
res.status(e.code).json(output)
} else {
functions.logger.error(e)
logger.error(e)
res.status(500).json({ message: 'An unknown error occurred.' })
}
}

View File

@ -48,7 +48,7 @@ const numericSchema = z.object({
max: z.number(),
})
export const createContract = newEndpoint(['POST'], async (req, [user, _]) => {
export const createmarket = newEndpoint(['POST'], async (req, [user, _]) => {
const { question, description, tags, closeTime, outcomeType } = validate(
bodySchema,
req.body

View File

@ -2,15 +2,13 @@ import * as admin from 'firebase-admin'
admin.initializeApp()
// v1
// export * from './keep-awake'
export * from './health'
export * from './transact'
export * from './place-bet'
export * from './resolve-market'
export * from './stripe'
export * from './sell-bet'
export * from './sell-shares'
export * from './create-contract'
export * from './create-user'
export * from './create-fold'
export * from './create-answer'
@ -31,3 +29,8 @@ export * from './add-liquidity'
export * from './on-create-answer'
export * from './on-update-contract'
export * from './on-follow-user'
// v2
export * from './health'
export * from './place-bet'
export * from './create-contract'

View File

@ -32,7 +32,7 @@ const numericSchema = z.object({
value: z.number(),
})
export const placeBet = newEndpoint(['POST'], async (req, [bettor, _]) => {
export const placebet = newEndpoint(['POST'], async (req, [bettor, _]) => {
const { amount, contractId } = validate(bodySchema, req.body)
const result = await firestore.runTransaction(async (trans) => {