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

question: how to set optional @Param ? #335

Closed
StephenBeg opened this issue Nov 28, 2017 · 7 comments
Closed

question: how to set optional @Param ? #335

StephenBeg opened this issue Nov 28, 2017 · 7 comments
Labels
type: question Questions about the usage of the library.

Comments

@StephenBeg
Copy link

StephenBeg commented Nov 28, 2017

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.

@NoNameProvided
Copy link
Member

But in fact, @param have only one argument

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

@NoNameProvided NoNameProvided added the type: question Questions about the usage of the library. label Nov 28, 2017
@MichalLytek
Copy link
Contributor

MichalLytek commented Nov 28, 2017

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.

@StephenBeg
Copy link
Author

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.

@nolazybits
Copy link
Contributor

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}}

@trayhem
Copy link

trayhem commented Dec 12, 2018

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

@gerkirill
Copy link

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);
}

@NoNameProvided NoNameProvided changed the title [Question] How to set optional @Param ? question: how to set optional @Param ? Aug 9, 2020
@typestack typestack locked as resolved and limited conversation to collaborators Aug 9, 2020
@NoNameProvided
Copy link
Member

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

6 participants