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).
error
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);}); Copy
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);});
Request information
The URL being requested
Request initialization options
Optional
Response object (set after fetch completes)
Shared state between middleware
Error that occurred during request (set by middleware or fetch)
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