Router context
A router's context is what it gives the handler of every endpoint under it: path parameters, providers and per-request values. You declare each once, in the router's context, with the options an endpoint has for them: params, inject and injectOnRequest.
src/endpoints/projects/
├── router.ts context: inject, injectOnRequest
├── projects.service.ts
├── list.endpoint.ts
└── $projectId/
├── router.ts context: params (projectId)
├── view.endpoint.ts
└── tasks.$taskId.complete.endpoint.tsNo endpoint in that tree declares projectId, the service or the current user. Each handler receives all three, typed.
On a router
// src/endpoints/projects/router.ts
export default EndpointRouter.create({
providers: [ProjectsService],
context: {
inject: { projects: ProjectsService },
injectOnRequest: { user: decorated<User>(CurrentUser()) },
},
});create() returns the router with an endpoint builder of its own, as NestjsEndpoints.forRoot() does for the root router. An endpoint built with it receives the router's context along with what it declares itself:
// src/endpoints/projects/list.endpoint.ts
import router from './router.js';
export default router.endpoint.query({
output: z.array(z.object({ id: z.string(), name: z.string() })),
handler: ({ projects, user }) => projects.listFor(user.id),
});Only endpoints built with the router's endpoint receive its context, not one built with the package's own endpoint, and not the router itself, which has its own inject.
A router file can also re-export the builder, with export const { endpoint } = router;, so its endpoints import endpoint from ./router.js.
Path parameters
A router's context.params are the path parameters of a folder: a plain z.object() whose keys are :params in the URL of each of its endpoints. With file-based routing, put the router in the folder that names the parameter:
// src/endpoints/projects/$projectId/router.ts
import projects from '../router.js';
export default projects.child({
context: { params: z.object({ projectId: z.uuid() }) },
});// src/endpoints/projects/$projectId/view.endpoint.ts
import router from './router.js';
export default router.endpoint.query({
output: z.object({ id: z.string(), name: z.string() }),
handler: ({ params, projects, user }) => { return projects.find(params.projectId, user.id);
},
});An endpoint declares only its own parameters, and the handler receives both:
// src/endpoints/projects/$projectId/tasks.$taskId.complete.endpoint.ts
import router from './router.js';
export default router.endpoint.mutation({
params: z.object({ taskId: z.coerce.number() }),
output: z.object({ done: z.boolean() }),
handler: ({ params, projects, user }) => { return projects.completeTask(
params.projectId,
params.taskId,
user.id,
);
},
});The router's parameters are validated with the endpoint's own, as one schema, and appear in the endpoint's invoke(), its OpenAPI operation and the generated clients' path group like its own.
A rule on the whole object, such as a router's z.strictObject() or .refine(), applies only to the endpoints that add no parameters. One that adds its own is validated by its own object's rules, with each of the router's keys still checked by its schema. Put a rule on a key's schema to keep it everywhere.
Child routers
A router made with its parent's child() has its parent's context too, through every level. See What flows down.
When two levels declare the same key
The most specific declaration wins: an endpoint's over its router's, and a child router's over its parent's.
// The router's `user` is the full record;
// this endpoint only needs the id.
export default router.endpoint.query({
injectOnRequest: { user: decorated<number>(CurrentUserId()) },
handler: ({ user }) => {
// user: number
},
});A key in inject or injectOnRequest replaces an outer level's key of that name, whichever of the two it was in. A path parameter an endpoint declares again is read with the endpoint's schema.
See the router options reference for every option.