Make it so you can specify runtime opts for v2 functions

This commit is contained in:
Marshall Polaris 2022-06-29 16:21:12 -07:00
parent d5800dfe87
commit f647797037
7 changed files with 18 additions and 11 deletions

View File

@ -108,7 +108,12 @@ export const validate = <T extends z.ZodTypeAny>(schema: T, val: unknown) => {
}
}
const DEFAULT_OPTS: HttpsOptions = {
interface EndpointOptions extends HttpsOptions {
methods?: string[]
}
const DEFAULT_OPTS = {
methods: ['POST'],
minInstances: 1,
concurrency: 100,
memory: '2GiB',
@ -116,12 +121,13 @@ const DEFAULT_OPTS: HttpsOptions = {
cors: [CORS_ORIGIN_MANIFOLD, CORS_ORIGIN_LOCALHOST],
}
export const newEndpoint = (methods: [string], fn: Handler) =>
onRequest(DEFAULT_OPTS, async (req, res) => {
export const newEndpoint = (endpointOpts: EndpointOptions, fn: Handler) => {
const opts = Object.assign(endpointOpts, DEFAULT_OPTS)
return onRequest(opts, async (req, res) => {
log('Request processing started.')
try {
if (!methods.includes(req.method)) {
const allowed = methods.join(', ')
if (!opts.methods.includes(req.method)) {
const allowed = opts.methods.join(', ')
throw new APIError(405, `This endpoint supports only ${allowed}.`)
}
const authedUser = await lookupUser(await parseCredentials(req))
@ -140,3 +146,4 @@ export const newEndpoint = (methods: [string], fn: Handler) =>
}
}
})
}

View File

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

View File

@ -20,7 +20,7 @@ const bodySchema = z.object({
about: z.string().min(1).max(MAX_ABOUT_LENGTH).optional(),
})
export const creategroup = newEndpoint(['POST'], async (req, auth) => {
export const creategroup = newEndpoint({}, async (req, auth) => {
const { name, about, memberIds, anyoneCanJoin } = validate(
bodySchema,
req.body

View File

@ -1,6 +1,6 @@
import { newEndpoint } from './api'
export const health = newEndpoint(['GET'], async (_req, auth) => {
export const health = newEndpoint({ methods: ['GET'] }, async (_req, auth) => {
return {
message: 'Server is working.',
uid: auth.uid,

View File

@ -33,7 +33,7 @@ const numericSchema = z.object({
value: z.number(),
})
export const placebet = newEndpoint(['POST'], async (req, auth) => {
export const placebet = newEndpoint({}, async (req, auth) => {
log('Inside endpoint handler.')
const { amount, contractId } = validate(bodySchema, req.body)

View File

@ -13,7 +13,7 @@ const bodySchema = z.object({
betId: z.string(),
})
export const sellbet = newEndpoint(['POST'], async (req, auth) => {
export const sellbet = newEndpoint({}, async (req, auth) => {
const { contractId, betId } = validate(bodySchema, req.body)
// run as transaction to prevent race conditions

View File

@ -16,7 +16,7 @@ const bodySchema = z.object({
outcome: z.enum(['YES', 'NO']),
})
export const sellshares = newEndpoint(['POST'], async (req, auth) => {
export const sellshares = newEndpoint({}, async (req, auth) => {
const { contractId, shares, outcome } = validate(bodySchema, req.body)
// Run as transaction to prevent race conditions.