Controller Internals — Series 2

Preview — 3 of 10 questions

What happens to ParseUUIDPipe here?

javascript
export const CurrentUser = createParamDecorator(
  (data: keyof JwtUser | undefined, ctx: ExecutionContext) => {
    const user = ctx.switchToHttp().getRequest().user as JwtUser;
    return data ? user?.[data] : user;
  },
);

@Get('me')
me(@CurrentUser('id', ParseUUIDPipe) id: string) {}
AIt runs on the value the decorator's factory returned — custom parameter decorators accept pipes exactly like the built-in ones, because both register the same kind of route-argument metadata
BIt is ignored; pipes only apply to @Body(), @Param() and @Query()
CIt runs before the factory, validating the raw request object
DIt causes a startup error, since a custom decorator cannot take extra arguments

How many routes does this register, and in what shape?

javascript
app.setGlobalPrefix('api');
app.enableVersioning({ type: VersioningType.URI });

@Controller({ path: ['cats', 'kittens'], version: ['1', '2'] })
export class CatsController {
  @Get([':id', ':id/details'])
  findOne() {}
}
AOne route, using the first entry of each array and ignoring the rest
BTwo routes, pairing the arrays element-wise
CEight — the path factory produces the cartesian product of versions, controller paths and handler paths, each prefixed globally: /api/v1/cats/:id, /api/v1/cats/:id/details, /api/v1/kittens/:id, and so on through v2
DFour, because handler-level path arrays are flattened into a single regular expression

Why does this request hang, and what does Nest look at to decide?

javascript
@Get('a')
a(@Res() res: Response) {
  // forgot res.send(...)
}
AIt does not hang; Nest sends the handler's return value as usual
BInjecting the response object is recorded in the route's argument metadata, which switches the route into library-specific mode — Nest then stops managing the response, so nothing is ever sent unless your code sends it
CIt hangs because @Res() opens a persistent connection intended for Server-Sent Events
DIt hangs because the handler is not async

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.