Routing-controllers: question: how to set optional @Param ?

Created on 28 Nov 2017  路  7Comments  路  Source: typestack/routing-controllers

Hi,

How i can set optionnal parameter with @Param ?

Documentation say :
@Param(name: string, options?: ParamOptions)

But in fact, @Param have only one argument :
export function Param(name: string): Function

@Get('/dev/:test?')
@OnUndefined(200)
public async devRoute(@Param('test') test: string): Promise<void> {
    // Some stuff
    console.log('test : ', test);
}

Maybe i miss something ?

Thanks.

question

Most helpful comment

I'd also love to have this feature since express supports this kind of optional route parameters

All 7 comments

But in fact, @Param have only one argument

This has been fixed in #315 but not has been released yet.

params are always required, because if they are missing router will not match the route
https://github.com/typestack/routing-controllers/blob/master/src/decorator/Param.ts#L16

This has been fixed in #315 but not has been released yet.

Nope, just @Params(). Single param is required by design as it's stated up here.

Ok thanks.
It's too bad because express router can match route with optionnal parameters :
use('/test/:id?') instead of use('/test/:id')
It can be very usefull.

I'm keen on this too.
I know it's been closed but that would be helpful for doing something like
/resource:options/whatever options being optional
Here it's to have sorting for instance with {sort: {field: ASC}}

I'd also love to have this feature since express supports this kind of optional route parameters

Well, @Params aren't that bad. You can even pass a default value like this:

@Get('/dev/:test?')
@OnUndefined(200)
public async devRoute(@Params() { id = 'no-id' }: { id: string }`): Promise<void> {
    // Some stuff
    console.log('test : ', test);
}

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GBeushausen picture GBeushausen  路  5Comments

szabobar picture szabobar  路  4Comments

humbertowoody picture humbertowoody  路  4Comments

impzero picture impzero  路  4Comments

circy picture circy  路  3Comments