Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining routes as both nested and top level #74

Open
danielcornock opened this issue Jan 3, 2020 · 3 comments
Open

Defining routes as both nested and top level #74

danielcornock opened this issue Jan 3, 2020 · 3 comments
Labels
discussion 🔥 question Further information is requested

Comments

@danielcornock
Copy link

In my application I have an entity 'stories', that I want to be able to reach from the top level of routes (e.g. api/v1/stories) but also to access it nested so that it can have parameters for another entity, e.g. api/v1/boards/:boardId/stories.

Is this possible with the current build? I've tried out different ways of organising them but it seems that multiple declarations of a module will overwrite any previous ones.

For example one setup that I tried was:

const routes: Routes = [
  {
    path: '/stories',
    module: StoryModule
  },
  {
    path: '/boards',
    module: BoardModule,
    children: [
      {
        path: '/:boardId/stories',
        module: StoryModule
      }
    ]
  }
];

In this case the boards route and nested stories route works, but the top level stories route does not. If i place the top level stories route after the nested route, that one works and the nested one doesn't.

@shekohex
Copy link
Member

shekohex commented Jan 3, 2020

well, in this case, I'd recommend using regex to match both paths, since I think that the only way we can do it since Nestjs is already supporting path regex:
The best regex here should be: ^boards\/(?:([^\/]+?))\/stories|^stories(?=\/|$)

I know it seems complicated, but can we do better?

See it in action here: https://regex101.com/r/R7IxYM/2

this is helpful too: http://forbeslindesay.github.io/express-route-tester/

@frankmoto
Copy link

I ran into a similar issue and I got around it by doing a bit more leg work. What you could do for the time being is create a new Module called BoardStoryModule and extend your story module, then import the BoardStoryModule into your routing document and use it.

@shekohex shekohex added discussion 🔥 question Further information is requested labels May 20, 2021
@Discountrobot
Copy link

We have a similar issue, with dynamic modules.

When trying to setup the router on two different modules utilising the same shared module dynamically initialised.

Setup as follows:

const AnimalModuleInstance1: DynamicModule = {
    module: SharedModuleWithRoutes,
};

const AnimalModuleInstance2: DynamicModule = {
    module: SharedModuleWithRoutes,
};

const DOG_ROUTES: Routes = [
    {
        path: '/1/',
        module: DogModule,
        children: [
            AnimalModuleInstance1.module,
        ]
    }
];

const CAT_ROUTES: Routes = [
    {
        path: '/2/',
        module: CatModule,
        children: [
            AnimalModuleInstance2.module,
        ]
    }
];

We run into issues as the router module tries to reference the module based on the nestModule.metatype which will be "SharedModuleWithRoutes"

const modulePath: string = Reflect.getMetadata(MODULE_PATH, nestModule.metatype);

setting the base path to either /2/ or /1/ to both modules depending on which module is initialised last.

changing the reflector to use the module object reference rather than the metatype seems to resolve our issue - however will not work if string based module references are allowed in children property interface.

- const modulePath: string = Reflect.getMetadata(MODULE_PATH, nestModule.metatype);
+ const modulePath: string = Reflect.getMetadata(MODULE_PATH, nestModule);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion 🔥 question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants