Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 29x 3x 26x 29x 29x 26x 26x | /**
* Context creation for route handlers
* Core logic - runtime agnostic
*/
import type { SocketEmitter } from '../shared/vitek-app.js';
export interface VitekContext {
url: string;
method: string;
path: string;
query: Record<string, string | string[]>;
params: Record<string, string>;
headers: Record<string, string>;
body?: any;
/** When trustProxy is true, client IP from X-Forwarded-For or socket.remoteAddress. */
clientIp?: string;
/** When the app runs with shared context (dev, preview, serve), use to broadcast to WebSocket clients. */
sockets?: SocketEmitter;
}
export interface VitekRequest {
url: string;
method: string;
headers: Record<string, string>;
body?: any;
}
/**
* Response object that allows control over status code and headers
* If a handler returns a plain object, it will be treated as status 200 with JSON content-type
*/
export interface VitekResponse {
status?: number;
headers?: Record<string, string>;
body?: any;
}
/**
* Type guard to check if a value is a VitekResponse
* A VitekResponse is identified by having 'status' (number) or 'headers' (object) properties
* Plain objects without these properties are treated as regular JSON responses (backward compatibility)
*/
export function isVitekResponse(value: any): value is VitekResponse {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
return false;
}
// It's a VitekResponse if it has 'status' (number) or 'headers' (object) properties
// These are clear indicators of a response object, not a data object
const hasStatus = 'status' in value && typeof value.status === 'number';
const hasHeaders = 'headers' in value && typeof value.headers === 'object' && value.headers !== null && !Array.isArray(value.headers);
return hasStatus || hasHeaders;
}
/**
* Creates a context from a request and extracted parameters
*/
export function createContext(
request: VitekRequest,
params: Record<string, string> = {},
query: Record<string, string | string[]> = {}
): VitekContext {
const url = new URL(request.url, 'http://localhost');
return {
url: request.url,
method: request.method.toLowerCase(),
path: url.pathname,
query,
params,
headers: request.headers,
body: request.body,
};
}
|