Nest: [Feature Request] Support url parameters without router positions

Created on 18 May 2018  路  6Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

It's only possible to use parameters if they're route parameters.

For example, to access name and age, a route must be set up as:

:name/:age

Expected behavior

It should be possible to use url parameters as well:

E.g. http://localhost:3000/cats?name=gary&age=1.57

Minimal reproduction of the problem with instructions

@Get('cats')
async getByNameAndAge(@Param('name') name: string, @Param('age'): number): Promise<object> {
   return Promise.resolve({"name": name, "age": age, "success": true})
}

What is the motivation / use case for changing the behavior?

Url parameters being parsed in any order is better for long lists of parameters and the potential for optional parameters compared to positional route parameters.

Environment


Nest version: 4.6.6


type

Most helpful comment

I'll provide an example in the docs.

All 6 comments

@natejgardner you can use @Query('name') or @Query('age') as well.

@chanlito +1 , exactly

@Query() is what's you are looking for.
And it also accepts an optional query string name.

Query is imported from @nestjs/common

I'll provide an example in the docs.

Is there any way to have different controller methods with a hardcoded query parameter value? Something like:

@Get("/aggregate/sum?field=myField")

or do we have to check the field name using a controller method like this:

  @Get("/aggregate/sum")
  async findAggregateBySum(@Query("field")field:string){
    if(field==="myField"){
     //etc
    }
  }

This thread 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

anyx picture anyx  路  3Comments

JulianBiermann picture JulianBiermann  路  3Comments

yanshuf0 picture yanshuf0  路  3Comments

tronginc picture tronginc  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments