#NestJS DTO

14 messages · Page 1 of 1 (latest)

sand flicker
#

I have an endpoint, that receives a UUID as param, and some pagination properties as query params, like page=1&take=10.It is possible to create a single DTO that map both route param and query?
My current code is something like:

@Get(':searchId')
async myMethod(
  @Param('searchId') searchId:string
  @Query('page') page:string
  @Query('take') take:string
){
...
}
onyx juniper
#

Oh, yeah, totally make a single class DTO for that. And if you do, you can even do validation on it

#

@Query() { searchId, page, take }: SearchQueryDTO

#

Oh, crap, just saw that searchId is part of the @Param()

sand flicker
#

Yep

onyx juniper
#

It's still possible, but you need a custom decorator solution

#
export const QueryAndParam = createParamDecorator((data: unknown, context: ExecutionContext) => {
  const req = context.switchToHttp().getRequest();
  return { ...req.param, ...req.query };
});
sand flicker
#

Got it, thanks @onyx juniper

#

@onyx juniper with that example, both req.param and req.query will be available inside the req.body?

sand flicker
#

@onyx juniper it worked! Thank you

rose path
#

be careful not to name params and queries the same because they might overwrite one another with that CB_pika_think

sand flicker
#

One of you guys know why those params doesn't show on my Swagger anymore?

onyx juniper
#

Yep. Nest reads for the @Query() and @Param() decorators. You no longer use those, so Nest can'tautomatically detect them