Skip to content

@venturekit/runtime API

Creates a unified Lambda handler that wraps your business logic with auth, body parsing, status codes, middleware, and error handling.

function handler<TBody = unknown, TResult = unknown>(
fn: HandlerFn<TBody, TResult>,
config?: HandlerConfig
): (event: APIGatewayProxyEventV2, context: Context) => Promise<APIGatewayProxyResultV2>

Builds a RequestContext from an API Gateway event.

function buildContext(
event: APIGatewayProxyEventV2,
options: { supportedLocales: string[]; defaultLocale: string }
): RequestContext

Extracts user information from JWT claims in the API Gateway event.

function extractUserContext(event: APIGatewayProxyEventV2): UserContext | null

extractLocale(event, supportedLocales, defaultLocale)

Section titled “extractLocale(event, supportedLocales, defaultLocale)”

Extracts the locale from the Accept-Language header.

function extractLocale(event: APIGatewayProxyEventV2, supported: string[], fallback: string): string
FunctionStatusDescription
success(data, meta?)200Success response
created(data, meta?)201Created response
noContent(meta?)204No content response
error(statusCode, body)*Custom error response
errorResponse(error, meta?)*Format a VentureError
redirect(url, statusCode?)301/302Redirect response
FunctionDescription
compose(middlewares)Compose middleware into a single function
loggingMiddleware(logger)Request/response logging with timing
corsMiddleware(options)CORS headers and preflight handling
timeoutMiddleware(ms)Request timeout enforcement
errorBoundaryMiddleware(handler)Catch and format errors
ExportDescription
LoggerLogger class
createLogger(config?)Create a new logger instance
loggerDefault logger instance
type HandlerFn<TBody, TResult> = (
body: TBody,
ctx: RequestContext,
logger: Logger
) => Promise<TResult>
interface HandlerConfig {
scopes?: string[]
status?: 200 | 201 | 204
middleware?: Middleware[]
logLevel?: 'debug' | 'info' | 'warn' | 'error'
transactional?: boolean
}
interface RequestContext {
requestId: string
timestamp: Date
method: string
path: string
sourceIp: string
userAgent: string
user: UserContext | null
tenant: TenantContext | null
locale: string
queryParams?: Record<string, string | undefined>
tx?: unknown
intentOutputs?: Record<string, unknown>
rawEvent: APIGatewayProxyEventV2
}
interface UserContext {
id: string
email?: string
scopes: string[]
claims: Record<string, unknown>
}
interface TenantContext {
id: string
slug?: string
metadata: Record<string, unknown>
}
interface Middleware {
name: string
fn: MiddlewareFn
}
type MiddlewareFn = (
ctx: RequestContext,
next: () => Promise<APIGatewayProxyResultV2>
) => Promise<APIGatewayProxyResultV2>
ClassStatusCode
VentureError500Base class
BadRequestError400BAD_REQUEST
UnauthorizedError401UNAUTHORIZED
ForbiddenError403FORBIDDEN
NotFoundError404NOT_FOUND
ConflictError409CONFLICT
ValidationError422VALIDATION_ERROR
RateLimitError429RATE_LIMITED
InternalError500INTERNAL_ERROR
ServiceUnavailableError503SERVICE_UNAVAILABLE

Type guard to check if an error is a VentureError.

function isVentureError(error: unknown): error is VentureError
MethodDescription
save(connectionId)Save a new connection (unauthenticated)
authenticate(connectionId, metadata)Upgrade to authenticated
remove(connectionId)Remove a connection
get(connectionId)Get a connection record
getByUser(userId)Get all connections for a user
getByTenant(tenantId)Get all connections for a tenant
postToConnection(domain, stage, connId, data)Send to one connection
sendToUser(domain, stage, userId, data)Send to all user sessions
sendToTenant(domain, stage, tenantId, data)Send to all tenant connections
broadcast(domain, stage, data)Send to all connections