Skip to content

What flows down ​

A router gives what it has to everything under it: the routers made with its child(), and their endpoints. This page follows one folder, with a router, a child router and one endpoint:

src/endpoints/teams/
├── router.ts               teams
└── $teamId/
    ├── router.ts           $teamId, made with teams' child()
    └── invite.endpoint.ts  invite
ts
export default EndpointRouter.create({
  imports: [DbModule],
  context: { inject: { db: DbService } },
  middleware: [RequestIdMiddleware],
  guards: [SignedInGuard],
  interceptors: [TimingInterceptor],
});
ts
import teamsRouter from '../router.js';

export default teamsRouter.child({
  context: { params: z.object({ teamId: z.uuid() }) },
  middleware: [LoadTeamMiddleware],
  guards: [TeamMemberGuard],
  interceptors: [AuditInterceptor],
});
ts
// src/endpoints/teams/$teamId/invite.endpoint.ts
import teamRouter from './router.js';

export default teamRouter.endpoint.mutation({
  input: z.object({ email: z.email() }),
  guards: [TeamAdminGuard],
  handler: ({ params, input, db }) =>
    db
      .insert(invites)
      .values({ teamId: params.teamId, email: input.email }),
});

In the diagrams below: teams is the router, $teamId its child, and invite the endpoint.

Context and providers ​

The handler receives what every router above it declares in context:

In the handlerDeclared by
dbteams context.inject
params.teamId$teamId context.params
inputinvite input

$teamId also has the imports and providers of teams, so it lists only its own.

Lifecycle hooks ​

Each of the three can declare NestJS's lifecycle hooks in lifecycle. At each lifecycle event, NestJS calls that hook on all three, in this order:

Startup

  • onModuleInit()
    1. teams router
    2. $teamId router
    3. invite endpoint
  • onApplicationBootstrap()
    1. teams router
    2. $teamId router
    3. invite endpoint

Shutdown

  • onModuleDestroy()
    1. invite endpoint
    2. $teamId router
    3. teams router
  • beforeApplicationShutdown()
    1. invite endpoint
    2. $teamId router
    3. teams router
  • onApplicationShutdown()
    1. invite endpoint
    2. $teamId router
    3. teams router

Shutdown runs on app.close(), or on a signal once the app calls enableShutdownHooks().

Guards and interceptors ​

A request for the endpoint passes each one once, from the outer router in:

POST /teams/42/invite

  • Guards
    1. teams SignedInGuard
    2. $teamId TeamMemberGuard
    3. invite TeamAdminGuard
  • Interceptors, before the handler
    1. teams TimingInterceptor
    2. $teamId AuditInterceptor
  • Handler
    1. invite handler()

The response

  • Interceptors, after the handler
    1. $teamId AuditInterceptor
    2. teams TimingInterceptor

An endpoint's own interceptors run after those of its routers too.

Middleware ​

Middleware runs first, before any guard, from the outer router in:

POST /teams/42/invite

  • Middleware
    1. teams RequestIdMiddleware
    2. $teamId LoadTeamMiddleware
  • Then the guards, the interceptors and the handler

One instance each ​

Each provider, guard and interceptor of a router is one instance, for every endpoint and router under it: SignedInGuard is made once, with what teams provides and imports.

A child router that lists one again has an instance of its own. So does an endpoint, for a guard or an interceptor. Both instances run, the parent's first:

ts
// src/endpoints/teams/$teamId/router.ts
export default teamsRouter.child({
  // A second SignedInGuard, for this folder.
  guards: [SignedInGuard, TeamMemberGuard],
});

A nested router made with create() ​

It starts a context and providers of its own. The middleware, guards and interceptors of the routers above it still apply to its folder.

child()create()
context✓
imports and providers✓
middleware, guards and interceptors✓✓
Lifecycle order✓✓

A router listed in routers ​

A router another lists in its routers has everything a router made with that router's child() has, the context too, though it was made with create() in a package of its own.

Released under the MIT License.