Fix up a little stuff I broke
This commit is contained in:
parent
cf17095ab9
commit
bf98e4e7a9
|
@ -46,7 +46,7 @@ import { unsubscribe } from './unsubscribe'
|
||||||
import { stripewebhook, createcheckoutsession } from './stripe'
|
import { stripewebhook, createcheckoutsession } from './stripe'
|
||||||
|
|
||||||
const toCloudFunction = ({ opts, handler }: EndpointDefinition) => {
|
const toCloudFunction = ({ opts, handler }: EndpointDefinition) => {
|
||||||
onRequest(opts, handler as any)
|
return onRequest(opts, handler as any)
|
||||||
}
|
}
|
||||||
const healthFunction = toCloudFunction(health)
|
const healthFunction = toCloudFunction(health)
|
||||||
const transactFunction = toCloudFunction(transact)
|
const transactFunction = toCloudFunction(transact)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import * as cors from 'cors'
|
import * as cors from 'cors'
|
||||||
import * as express from 'express'
|
import * as express from 'express'
|
||||||
import { Express } from 'express'
|
import { Express, Request, Response, NextFunction } from 'express'
|
||||||
import { EndpointDefinition } from './api'
|
import { EndpointDefinition } from './api'
|
||||||
|
|
||||||
const PORT = 8088
|
const PORT = 8088
|
||||||
|
@ -26,34 +26,43 @@ import { resolvemarket } from './resolve-market'
|
||||||
import { unsubscribe } from './unsubscribe'
|
import { unsubscribe } from './unsubscribe'
|
||||||
import { stripewebhook, createcheckoutsession } from './stripe'
|
import { stripewebhook, createcheckoutsession } from './stripe'
|
||||||
|
|
||||||
|
type Middleware = (req: Request, res: Response, next: NextFunction) => void
|
||||||
const app = express()
|
const app = express()
|
||||||
|
|
||||||
const addEndpointRoute = (name: string, endpoint: EndpointDefinition) => {
|
const addEndpointRoute = (
|
||||||
|
path: string,
|
||||||
|
endpoint: EndpointDefinition,
|
||||||
|
...middlewares: Middleware[]
|
||||||
|
) => {
|
||||||
const method = endpoint.opts.method.toLowerCase() as keyof Express
|
const method = endpoint.opts.method.toLowerCase() as keyof Express
|
||||||
const corsMiddleware = cors({ origin: endpoint.opts.cors })
|
const corsMiddleware = cors({ origin: endpoint.opts.cors })
|
||||||
const middleware = [express.json(), corsMiddleware]
|
const allMiddleware = [...middlewares, corsMiddleware]
|
||||||
app.options(name, corsMiddleware) // preflight requests
|
app.options(path, corsMiddleware) // preflight requests
|
||||||
app[method](name, ...middleware, endpoint.handler)
|
app[method](path, ...allMiddleware, endpoint.handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
const addJsonEndpointRoute = (name: string, endpoint: EndpointDefinition) => {
|
||||||
|
addEndpointRoute(name, endpoint, express.json())
|
||||||
}
|
}
|
||||||
|
|
||||||
addEndpointRoute('/health', health)
|
addEndpointRoute('/health', health)
|
||||||
addEndpointRoute('/transact', transact)
|
addJsonEndpointRoute('/transact', transact)
|
||||||
addEndpointRoute('/changeuserinfo', changeuserinfo)
|
addJsonEndpointRoute('/changeuserinfo', changeuserinfo)
|
||||||
addEndpointRoute('/createuser', createuser)
|
addJsonEndpointRoute('/createuser', createuser)
|
||||||
addEndpointRoute('/createanswer', createanswer)
|
addJsonEndpointRoute('/createanswer', createanswer)
|
||||||
addEndpointRoute('/placebet', placebet)
|
addJsonEndpointRoute('/placebet', placebet)
|
||||||
addEndpointRoute('/cancelbet', cancelbet)
|
addJsonEndpointRoute('/cancelbet', cancelbet)
|
||||||
addEndpointRoute('/sellbet', sellbet)
|
addJsonEndpointRoute('/sellbet', sellbet)
|
||||||
addEndpointRoute('/sellshares', sellshares)
|
addJsonEndpointRoute('/sellshares', sellshares)
|
||||||
addEndpointRoute('/claimmanalink', claimmanalink)
|
addJsonEndpointRoute('/claimmanalink', claimmanalink)
|
||||||
addEndpointRoute('/createmarket', createmarket)
|
addJsonEndpointRoute('/createmarket', createmarket)
|
||||||
addEndpointRoute('/addliquidity', addliquidity)
|
addJsonEndpointRoute('/addliquidity', addliquidity)
|
||||||
addEndpointRoute('/withdrawliquidity', withdrawliquidity)
|
addJsonEndpointRoute('/withdrawliquidity', withdrawliquidity)
|
||||||
addEndpointRoute('/creategroup', creategroup)
|
addJsonEndpointRoute('/creategroup', creategroup)
|
||||||
addEndpointRoute('/resolvemarket', resolvemarket)
|
addJsonEndpointRoute('/resolvemarket', resolvemarket)
|
||||||
addEndpointRoute('/unsubscribe', unsubscribe)
|
addJsonEndpointRoute('/unsubscribe', unsubscribe)
|
||||||
addEndpointRoute('/stripewebhook', stripewebhook)
|
addJsonEndpointRoute('/createcheckoutsession', createcheckoutsession)
|
||||||
addEndpointRoute('/createcheckoutsession', createcheckoutsession)
|
addEndpointRoute('/stripewebhook', stripewebhook, express.raw())
|
||||||
|
|
||||||
app.listen(PORT)
|
app.listen(PORT)
|
||||||
console.log(`Serving functions on port ${PORT}.`)
|
console.log(`Serving functions on port ${PORT}.`)
|
||||||
|
|
|
@ -100,9 +100,10 @@ export const stripewebhook: EndpointDefinition = {
|
||||||
let event
|
let event
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(typeof req.body, req.body)
|
// Cloud Functions jam the raw body into a special `rawBody` property
|
||||||
|
const rawBody = (req as any).rawBody ?? req.body
|
||||||
event = stripe.webhooks.constructEvent(
|
event = stripe.webhooks.constructEvent(
|
||||||
req.body,
|
rawBody,
|
||||||
req.headers['stripe-signature'] as string,
|
req.headers['stripe-signature'] as string,
|
||||||
process.env.STRIPE_WEBHOOKSECRET as string
|
process.env.STRIPE_WEBHOOKSECRET as string
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user