Accept just one HTTP method per endpoint
This commit is contained in:
parent
b80fdd7480
commit
b04276f7d1
|
@ -99,12 +99,12 @@ export const validate = <T extends z.ZodTypeAny>(schema: T, val: unknown) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EndpointOptions extends HttpsOptions {
|
export interface EndpointOptions extends HttpsOptions {
|
||||||
methods?: string[]
|
method?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_OPTS = {
|
const DEFAULT_OPTS = {
|
||||||
methods: ['POST'],
|
method: 'POST',
|
||||||
minInstances: 1,
|
minInstances: 1,
|
||||||
concurrency: 100,
|
concurrency: 100,
|
||||||
memory: '2GiB',
|
memory: '2GiB',
|
||||||
|
@ -117,9 +117,8 @@ export const newEndpoint = (endpointOpts: EndpointOptions, fn: Handler) => {
|
||||||
return onRequest(opts, async (req, res) => {
|
return onRequest(opts, async (req, res) => {
|
||||||
log('Request processing started.')
|
log('Request processing started.')
|
||||||
try {
|
try {
|
||||||
if (!opts.methods.includes(req.method)) {
|
if (opts.method !== req.method) {
|
||||||
const allowed = opts.methods.join(', ')
|
throw new APIError(405, `This endpoint supports only ${opts.method}.`)
|
||||||
throw new APIError(405, `This endpoint supports only ${allowed}.`)
|
|
||||||
}
|
}
|
||||||
const authedUser = await lookupUser(await parseCredentials(req))
|
const authedUser = await lookupUser(await parseCredentials(req))
|
||||||
log('User credentials processed.')
|
log('User credentials processed.')
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { newEndpoint } from './api'
|
import { newEndpoint } from './api'
|
||||||
|
|
||||||
export const health = newEndpoint({ methods: ['GET'] }, async (_req, auth) => {
|
export const health = newEndpoint({ method: 'GET' }, async (_req, auth) => {
|
||||||
return {
|
return {
|
||||||
message: 'Server is working.',
|
message: 'Server is working.',
|
||||||
uid: auth.uid,
|
uid: auth.uid,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user