Skip to content

Feature: Support nesting Router within applyDecorators  #50

@mrosnerr

Description

@mrosnerr

Issue

Consuming application can't nest Router in their own applyDecorators calls to allow for more complex configurations. This is currently not supported due to the getCallerFilePath function always expecting the router path to be a constant number (i.e. 2) levels down on the call stack.

i.e. in the example down below, it's assumed that authedRoute.decorator.ts is the router instead of dog.route.ts because it sits one step further down on the call stack which results in:

[Nest] 91798  - 12/06/2024, 4:25:52 PM    WARN TRPC Generator encountered an error.
[Nest] 91798  - 12/06/2024, 4:25:52 PM    WARN Error: Could not find router DogRouter class declaration.

Proposal

  1. Turn the path property on ROUTER_METADATA_KEY from a string of a single path to an array of the path hierarchy.
  2. Update serializeRouterProcedures to work down the list of paths to find the first one with a classDeclaration and use that.

If this looks good, let me know and I'd be happy to kick up a PR!

Example Use Case

authedRoute.decorator.ts

import { AuthnMiddleware } from '@/common/middlewares/authn.middleware';
import { AuthzMiddleware } from '@/common/middlewares/authz.middleware';
import { ResponseTimeMiddleware } from '@/common/middlewares/responseTime.middleware';
import { applyDecorators } from '@nestjs/common';
import { UseMiddlewares, Router } from 'nestjs-trpc';

export const AuthedRoute = (options: { alias: string }) => {
  return applyDecorators(
    Router({ alias: options.alias }),
    UseMiddlewares(ResponseTimeMiddleware, AuthnMiddleware, AuthzMiddleware),
  );
};

dog.route.ts

@Injectable()
@AuthedRoute({ alias: 'dog' })
export class DogRouter {

  @Query()
  async find() {
    return {};
  }
}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions