Migrate placeBet and createContract to v2 functions (#432)
This commit is contained in:
parent
44b3579cc7
commit
13826b5759
|
@ -1,5 +1,8 @@
|
||||||
import * as admin from 'firebase-admin'
|
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 * as Cors from 'cors'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
|
@ -10,8 +13,6 @@ import {
|
||||||
} from '../../common/envs/constants'
|
} from '../../common/envs/constants'
|
||||||
|
|
||||||
type Output = Record<string, unknown>
|
type Output = Record<string, unknown>
|
||||||
type Request = functions.https.Request
|
|
||||||
type Response = functions.Response
|
|
||||||
type AuthedUser = [User, PrivateUser]
|
type AuthedUser = [User, PrivateUser]
|
||||||
type Handler = (req: Request, user: AuthedUser) => Promise<Output>
|
type Handler = (req: Request, user: AuthedUser) => Promise<Output>
|
||||||
type JwtCredentials = { kind: 'jwt'; data: admin.auth.DecodedIdToken }
|
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 }
|
return { kind: 'jwt', data: jwt }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// This is somewhat suspicious, so get it into the firebase console
|
// 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.')
|
throw new APIError(403, 'Error validating token.')
|
||||||
}
|
}
|
||||||
case 'Key':
|
case 'Key':
|
||||||
|
@ -135,7 +136,7 @@ export const validate = <T extends z.ZodTypeAny>(schema: T, val: unknown) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const newEndpoint = (methods: [string], fn: Handler) =>
|
export const newEndpoint = (methods: [string], fn: Handler) =>
|
||||||
functions.runWith({ minInstances: 1 }).https.onRequest(async (req, res) => {
|
onRequest({ minInstances: 1 }, async (req, res) => {
|
||||||
try {
|
try {
|
||||||
await applyCors(req, res, {
|
await applyCors(req, res, {
|
||||||
origin: [CORS_ORIGIN_MANIFOLD, CORS_ORIGIN_LOCALHOST],
|
origin: [CORS_ORIGIN_MANIFOLD, CORS_ORIGIN_LOCALHOST],
|
||||||
|
@ -155,7 +156,7 @@ export const newEndpoint = (methods: [string], fn: Handler) =>
|
||||||
}
|
}
|
||||||
res.status(e.code).json(output)
|
res.status(e.code).json(output)
|
||||||
} else {
|
} else {
|
||||||
functions.logger.error(e)
|
logger.error(e)
|
||||||
res.status(500).json({ message: 'An unknown error occurred.' })
|
res.status(500).json({ message: 'An unknown error occurred.' })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ const numericSchema = z.object({
|
||||||
max: z.number(),
|
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(
|
const { question, description, tags, closeTime, outcomeType } = validate(
|
||||||
bodySchema,
|
bodySchema,
|
||||||
req.body
|
req.body
|
||||||
|
|
|
@ -2,15 +2,13 @@ import * as admin from 'firebase-admin'
|
||||||
|
|
||||||
admin.initializeApp()
|
admin.initializeApp()
|
||||||
|
|
||||||
|
// v1
|
||||||
// export * from './keep-awake'
|
// export * from './keep-awake'
|
||||||
export * from './health'
|
|
||||||
export * from './transact'
|
export * from './transact'
|
||||||
export * from './place-bet'
|
|
||||||
export * from './resolve-market'
|
export * from './resolve-market'
|
||||||
export * from './stripe'
|
export * from './stripe'
|
||||||
export * from './sell-bet'
|
export * from './sell-bet'
|
||||||
export * from './sell-shares'
|
export * from './sell-shares'
|
||||||
export * from './create-contract'
|
|
||||||
export * from './create-user'
|
export * from './create-user'
|
||||||
export * from './create-fold'
|
export * from './create-fold'
|
||||||
export * from './create-answer'
|
export * from './create-answer'
|
||||||
|
@ -31,3 +29,8 @@ export * from './add-liquidity'
|
||||||
export * from './on-create-answer'
|
export * from './on-create-answer'
|
||||||
export * from './on-update-contract'
|
export * from './on-update-contract'
|
||||||
export * from './on-follow-user'
|
export * from './on-follow-user'
|
||||||
|
|
||||||
|
// v2
|
||||||
|
export * from './health'
|
||||||
|
export * from './place-bet'
|
||||||
|
export * from './create-contract'
|
||||||
|
|
|
@ -32,7 +32,7 @@ const numericSchema = z.object({
|
||||||
value: z.number(),
|
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 { amount, contractId } = validate(bodySchema, req.body)
|
||||||
|
|
||||||
const result = await firestore.runTransaction(async (trans) => {
|
const result = await firestore.runTransaction(async (trans) => {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user