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

Version in routes #29

Closed
bovandersteene opened this issue Sep 27, 2018 · 7 comments
Closed

Version in routes #29

bovandersteene opened this issue Sep 27, 2018 · 7 comments
Labels
help wanted Extra attention is needed

Comments

@bovandersteene
Copy link

bovandersteene commented Sep 27, 2018

When I want to do multiple versions on my routes, and I reuse a controller for certain version, it only takes the latest version.

const routes: Routes = [
  {
    path: 'v1',
    children: [
      {
        path: Route.COMIC,
        module: ComicModule,
      },
      {
        path: Route.MY_COLLECTION,
        module: MyCollectionModule,
      },
    ],
  },
  {
    path: 'v2',
    children: [
      {
        path: Route.COMIC,
        module: ComicModule,
      },
      {
        path: Route.MY_COLLECTION,
        module: MyCollectionModule,
      },
    ],
  },
];

gives
image

So I miss, the v1 mapping

@shekohex
Copy link
Member

it the obvious result as any Controller or Module should have only a single route. with that have said,

expressjs supports routes alias as an array

so we can use

app.get(['/user/:id', '/profile/:id'], (req, res) => { ... });

as nest-router uses the build-in MODULE_PATH which is provided in nestjs/core

we cannot provide it as an array.
so we have a workaround here:

you can create another module with the same name and post-fix it with v2 or whatever,
and also do that for your controller, actually you would not have to re implement the same methods of that controller you can extend it and override the changed API/method of the old method or create new one, nest should get the old methods too.

to be clear i'll make it as an example:

we have module Users and in that module we have one controller UserPayment

...
@Controller('/payment')
export class UserPayment {
    @Get(':id')
    async getUserPayments(@Parm('id') userId: number) {
        ...
        // some complex sql calls
    }
   ...
   // another methods
}
...
@Module({
    controllers: [UserPayment]
})
export class Users {}

then we have updated our api and now we have api v2

we will create another module UsersV2 and another UserPaymentV2 controller

...
@Controller('/payment')
export class UserPaymentV2 extends UserPayment {
    @Get(':id')
    async getUserPayments(@Parm('id') userId: number) {
        ...
        // some complex sql calls
       // with update for v2
    }
   ...
   // another methods from extended class
}
...
@Module({
    controllers: [UserPaymentV2]
})
export class UsersV2 {}

then in our routes

const routes: Routes = [
  {
    path: 'v1',
    children: [{ path: 'users', module: Users }],
  },
  {
    path: 'v2',
    children: [{ path: 'users', module: UsersV2 }],
  },
];

PS: children array can be just an array of modules if they have the same path.

@shekohex shekohex added the help wanted Extra attention is needed label Sep 27, 2018
@shekohex shekohex closed this as completed Oct 9, 2018
@vahidvdn
Copy link

vahidvdn commented Dec 4, 2020

@shekohex Do you think this is a good idea? Let say we will have 3 versions. In the user folder, there will be 3 files for each user.module.ts and user.controller.ts
Which seems very hard to maintain. Any idea?

@shekohex
Copy link
Member

shekohex commented Dec 4, 2020

@vahidvdn so you have 3 versions of the same Controller?
The easiest way is that you every time you create a new version you extend the original Controller class and override/add any new methods you want.
And you version them in the routes file.

@vahidvdn
Copy link

vahidvdn commented Dec 4, 2020

@shekohex Thanks for the point.
What I mean was about managing files. Let say we will have module, moduleV2, and moduleV3. Also for controllers. It seems very messy.

@shekohex
Copy link
Member

shekohex commented Dec 4, 2020

Did you tried to use the same Module, but with 3 Different Controllers?
ModuleA: ControllerV1, ControllerV2, ControllerV3

Every one of these extends the previous one with new changes and with a new route in the @Controller()

Is that ok?

@vahidvdn
Copy link

vahidvdn commented Dec 4, 2020

@shekohex No, that's not. In your example:

const routes: Routes = [
  {
    path: 'v1',
    children: [{ path: 'users', module: Users }],
  },
  {
    path: 'v2',
    children: [{ path: 'users', module: UsersV2 }],
  },
];

In v2 path, you mentioned UsersV2 module. So, as I understand we can't have only one module. Am I right?

@shekohex
Copy link
Member

shekohex commented Dec 4, 2020

Yes, any module will only have one version, but as many Controllers you wish, so I guess you could instead of using this package you could add the versions to the Controllers itself.

If you don't need to versioning based on the modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants