Context (ctx)

The Context object (ctx) is the central interface for handling HTTP requests in Jetpath. It provides:

Usage Examples

Basic Request

// Basic route handler
export const GET_user = async (ctx) => {
  // Access query parameters
  const page = ctx.query.page || '1';

// Get request headers const authHeader = ctx.get('Authorization');

// Send JSON response ctx.send({ message: 'Success', page: parseInt(page) }); }

File Upload with Validation

// Using ctx.parse() for file uploads with validation
export const POST_upload = async (ctx) => {
  const data = await ctx.parse({
    maxBodySize: 10 * 1024 * 1024, // 10MB limit
    contentType: 'multipart/form-data'
  });

// Validate file data if (!data.files || !data.files.file) { ctx.send( 'No file uploaded',400); return }

ctx.send({ message: 'Files uploaded successfully' });

}

WebSocket Connection

// WebSocket route handler
export const GET_live = async (ctx) => {
  // Upgrade to WebSocket
  ctx.upgrade();

// Access WebSocket connection const conn = ctx.connection!;

// Handle WebSocket events conn.addEventListener('message', (data) => { // Handle incoming messages });

conn.addEventListener('close', () => { // Handle connection close });

}

Best Practices

  1. Type Safety: Use generics to define expected data shapes
  2. Error Handling: Use ctx.send(error, <http status code>) for HTTP errors
  3. State Management: Use ctx.state for request-scoped data and attach user info if authenticated
  4. Security: Always validate input data and implement proper authentication
  5. Response Handling: Use appropriate methods for different response types and include request ID in headers
  6. Logging: Use ctx.plugins.logger for detailed request logging with request ID and duration
  7. Headers: Add standard response headers including X-Request-ID and X-Response-Time

Key Features

Request Data Access

Response Handling

Navigation

Headers & Cookies

WebSocket

Additional Properties

}
ctx.send(user);

}


### File Upload

```typescript
// Using ctx.parse() for file uploads
export const POST_upload = async (ctx) => {
    const data = await ctx.parse({
      maxBodySize: 10 * 1024 * 1024, // 10MB limit
      contentType: 'multipart/form-data'
    });
    
    // Handle uploaded files
    // data.files contains uploaded files
    ctx.send({ message: 'Files uploaded successfully' });
}

Best Practices

  1. Type Safety: Use generics to define expected data shapes
  2. State Management: Use ctx.state for request-scoped data
  3. Security: Always validate input data
  4. Response Handling: Use appropriate methods for different response types

Next Steps

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

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

ctx.sendStream(file name); ```

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