Routes

A route is an explicit contract. Nitro does the routing; Fellow enforces the contract.

import { defineRoute, z } from '@fellowframework/core'

export default defineRoute({
  id: 'records.documents.get',
  method: 'GET',
  path: '/api/records/documents/:id',
  input: { params: z.object({ id: z.uuid() }) },
  output: documentSchema,
  access: { permission: 'records.document.read' },
  tenant: 'required',
  errors: ['records.document.not_found'],
  handler: ({ input }) => getDocument(input.params.id),
})

Pipeline

For every request, in this order:

  1. Identity: Authorization: Bearer <token> (HS256, FELLOW_SECRET) first, then the session cookie (see auth).
  2. Execution context is created (request id, identity, tenant, session).
  3. Middleware: the application’s global middleware (app.ts), then the route’s middleware.
  4. access: 'public', 'authenticated' (any identity) or { permission }. No identity → 401 auth.unauthenticated. Missing permission → 403 auth.forbidden.
  5. tenant: 'required' takes the tenant from the verified identity → otherwise 403 tenant.required. The tenant is never read from headers, query or body.
  6. CSRF: requests authenticated by a session cookie with a method other than GET/HEAD/OPTIONS need x-csrf-token → otherwise 403 auth.csrf_mismatch. Bearer tokens are exempt.
  7. input.params, input.query and input.body are parsed → otherwise 422 validation.failed. Bodies are JSON or multipart/form-data (files arrive as File; use z.file()). Bodies over maxBodyBytes (default 1 MiB for JSON, 10 MiB for multipart) → 413.
  8. The handler runs inside the execution context (useTenant(), useIdentity(), sql).
  9. The result is parsed through output. Fields not in the schema are dropped; a mismatch is a 500 contract.output_invalid. status: 204 sends no body.

Every response carries x-request-id and the security headers (x-content-type-options: nosniff, x-frame-options: SAMEORIGIN, referrer-policy: strict-origin-when-cross-origin).

Middleware

import { defineMiddleware, rateLimit } from '@fellowframework/core'

export const audit = defineMiddleware('records.audit', async ({ request, identity }, next) => {
  const response = await next()
  console.info(request.method, identity?.subject, response.status)
  return response
})

export default defineRoute({ /* ... */ middleware: [rateLimit({ limit: 10, window: 60 }), audit] })

rateLimit({ limit, window, by?, name? }) is a fixed window on the cache store (Redis in production), keyed by client IP by default; exceeding it gives 429 rate_limit.exceeded with Retry-After. The client IP comes from the socket; x-forwarded-for is honoured only with FELLOW_TRUST_PROXY=true.

Errors

Every error response has the same shape:

{ "error": { "code": "records.document.not_found", "message": "Document not found.", "requestId": "..." } }

Throw domain errors with errors.notFound(code, message), errors.conflict(code, message) or new FellowError(code, message, { status }). Unexpected errors are logged with the request id and returned as a generic internal.error; internal details never reach the client.

Permissions

Granted permissions support wildcards: * and records.document.*. Every permission used by a route must be declared in the module manifest.

Limitations

  • Routes are registered when the server starts: restart fellow dev after adding a route file.