Context object passed to middleware functions

Contains information about the current request, response, and shared state. Middleware can read and modify all properties except error (which should only be set on errors).

Example

api.use('custom', async (ctx, next) => {
// Access request info
console.log('URL:', ctx.request.url);
console.log('Method:', ctx.request.init.method);

// Store data in shared state
ctx.state.startTime = Date.now();

await next();

// Access response
if (ctx.response) {
console.log('Status:', ctx.response.status);
}

// Access shared state
const duration = Date.now() - (ctx.state.startTime as number);
console.log('Duration:', duration);
});
interface FejContext {
    request: {
        url: string;
        init: RequestInit;
    };
    response?: Response;
    state: Record<string, unknown>;
    error?: Error;
}

Properties

request: {
    url: string;
    init: RequestInit;
}

Request information

Type declaration

  • url: string

    The URL being requested

  • init: RequestInit

    Request initialization options

response?: Response

Response object (set after fetch completes)

state: Record<string, unknown>

Shared state between middleware

error?: Error

Error that occurred during request (set by middleware or fetch)