Core Concepts: Context (ctx)

The Context object, universally referred to as ctx in Jetpath, is the central nervous system for handling individual HTTP requests within your application. It acts as a container for request data, a toolkit for crafting responses, an access point for framework features like plugins and validation, and a temporary state holder for the duration of a single request-response cycle. Understanding ctx is key to mastering Jetpath.


Overview

ctx provides a consistent, runtime-agnostic interface whether you’re running on Node.js, Deno, or Bun. It simplifies common web development tasks and integrates seamlessly with TypeScript for enhanced type safety.

Its type signature reveals its capabilities:

interface ContextType<
  // Defines expected shapes for body, params, query
  JetData extends {
    body?: Record<string, any>;
    params?: Record<string, any>;
    query?: Record<string, any>;
  },
  // Defines types for registered plugins
  JetPluginTypes extends Record<string, unknown>[]
> {
  // ... properties and methods detailed below
}

Properties

app: {}

plugins: UnionToIntersection<JetPluginTypes[number]> & Record<string, any>

body: JetData["body"]

query: JetData["query"]

params: JetData["params"]

connection: jet_socket

request: Request

code: number

path: string


Methods

Methods returning never indicate they terminate the request flow.

eject(): never

validate(dataOrSchema?: any): YourValidatedType

sendStream(stream: ReadableStream | any, ContentType: string): never

sendResponse(response?: Response): never

send(data: unknown, ContentType?: string): never

throw(codeOrData?: number | string | Record<string, any> | unknown, message?: string | Record<string, any>): never

redirect(url: string, code: number = 302): never (Assuming optional code)

get(field: string): string | undefined

set(field: string, value: string): void

json(): Promise<Record<string, any>>


Next Steps