Next.js
Mount a Kizuna API on a Next.js App Router catch-all route.
@kizunajs/next serves a Kizuna API from a Next.js App Router application, through a single catch-all route file.
Requires Next.js ≥ 16 (App Router).
The adapter
nextAdapter() is what you name in kizuna.config.ts. Naming it is what gives every handler on this API Next's own NextRequest, typed.
Declare a route
Name Next in your config
Mount it at a catch-all route
mount hands back a handler per HTTP method, which Next dispatches for you:
Handler context
Each handler receives { params, query, body, headers, request }. The request is the native NextRequest, useful when you need to read cookies or anything else Next exposes.
Guards
A guard sits on the identity it authenticates, and receives the credential extracted and typed, alongside the native request. See the Authentication guide.
Middleware
For request-scoped values handlers need, use request context. For cache headers, declare a cache policy on the route. For other middleware, such as logging and rate limiting, use Next.js's own middleware, or the requestMiddleware option to run middleware after route matching. Authentication belongs in guards.
Options
| Option | Default | Description |
|---|---|---|
basePath | '' | Path prefix to strip when matching incoming paths |
requestMiddleware | [] | Middleware run after route matching, before the handler. See requestMiddleware |
responseValidation | false | Validate handler return values against response schemas. Enable in development. |
onError | none | Map a thrown error into a response (inbound migration seam); return an AdapterResult/Response to override the default 500. |
onError
Pass onError to nextAdapter(), or to mount, to send your own response:
requestMiddleware
requestMiddleware applies a flat array of middleware to every matched route, after route matching and before the handler. Each middleware receives (request, route), where route is the matched route's path and method, and may return a Response to short-circuit.
Type-safe request properties
Middleware that sets custom properties on the request, such as a request id, needs module augmentation to extend NextRequest so handlers reach them without casts. Authentication data does not: guards hand handlers typed context directly.
Both middleware and handlers are then typed:
Multipart / file uploads
Routes with contentType: 'multipart/form-data' work natively. The adapter calls request.formData() internally.
Streams
A stream response is returned as a NextResponse with a ReadableStream body. The generator's signal is request.signal.