UNPKG

serverless-utilities

Version:

Handy 🛠️ utility functions for Vercel ⚡ serverless functions.

41 lines (40 loc) 1.38 kB
import type { ZodObject } from 'zod'; import { VercelRequest, VercelResponse } from '@vercel/node'; export type RequestMethod = "get" | "head" | "post" | "put" | "delete" | "connect" | "options" | "trace" | "patch"; export interface RouteConfig { handler: (req: VercelRequest, res: VercelResponse) => Promise<VercelResponse> | VercelResponse; validation?: { params?: ZodObject<any>; query?: ZodObject<any>; body?: ZodObject<any>; }; } export interface FunctionConfig { methods: { get?: RouteConfig; put?: RouteConfig; head?: RouteConfig; post?: RouteConfig; trace?: RouteConfig; patch?: RouteConfig; delete?: RouteConfig; connect?: RouteConfig; options?: RouteConfig; }; cors?: { allowedOrigin?: string; exposeHeaders?: string[]; allowedHeaders?: string[]; allowCredentials?: boolean; allowedMethods?: RequestMethod[]; }; caching?: { sharedCacheSeconds?: number; }; } export type CORSConfig = FunctionConfig['cors']; export type CachingConfig = FunctionConfig['caching']; /** A helpful utility function that wraps around your * serverless function to make it easy to use. */ export declare function func(config: FunctionConfig): (req: VercelRequest, res: VercelResponse) => Promise<VercelResponse>;