From 13826b5759d1e4357f2ae88ab024d39bf9d4bf37 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Mon, 6 Jun 2022 12:34:58 -0700 Subject: [PATCH] Migrate placeBet and createContract to v2 functions (#432) --- functions/src/api.ts | 13 +++++++------ functions/src/create-contract.ts | 2 +- functions/src/index.ts | 9 ++++++--- functions/src/place-bet.ts | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/functions/src/api.ts b/functions/src/api.ts index a305197e..81c2ce76 100644 --- a/functions/src/api.ts +++ b/functions/src/api.ts @@ -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 -type Request = functions.https.Request -type Response = functions.Response type AuthedUser = [User, PrivateUser] type Handler = (req: Request, user: AuthedUser) => Promise type JwtCredentials = { kind: 'jwt'; data: admin.auth.DecodedIdToken } @@ -47,7 +48,7 @@ export const parseCredentials = async (req: Request): Promise => { 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 = (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.' }) } } diff --git a/functions/src/create-contract.ts b/functions/src/create-contract.ts index 805e4f6a..4d7df7d8 100644 --- a/functions/src/create-contract.ts +++ b/functions/src/create-contract.ts @@ -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 diff --git a/functions/src/index.ts b/functions/src/index.ts index 910bd937..043b860f 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -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' diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index 184ee2df..3de47aef 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -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) => {