samchon / nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
https://nestia.io/
MIT License
1.76k stars 91 forks source link

Optional `@TypedParam` Decorator #817

Closed 8471919 closed 6 months ago

8471919 commented 6 months ago

I want to use @TypedParam decorator as optional in this case. for example.

@Get([
  '/user/:userId/:nickname',
  '/user/:userId'
])
async getUserInfo(
  @TypedParam('userId') userId: number,
  @TypedParam('nickname') nickname: string | undefined
  @TypedQuery() query: QueryType,
) {
  ...
}

but, this doesn't work.

this is the error message when I run e2e test. image In above picture, I want to make relationType to optional. but It's impossible.

and also, I can't set default value. image

@Param works well both of situation.

In case of @Param decorator, It works.

@Get([
  '/user/:userId/:nickname',
  '/user/:userId'
])
async getUserInfo(
  @TypedParam('userId') userId: number,
  @Param('nickname') nickname: string | undefined
  @TypedQuery() query: QueryType,
) {
  ...
}

/// ------------------
@Get([
  '/user/:userId/:nickname',
  '/user/:userId'
])
async getUserInfo(
  @TypedParam('userId') userId: number,
  @Param('nickname') nickname: string | undefined = undefined
  @TypedQuery() query: QueryType,
) {
  ...
}
samchon commented 6 months ago

Make it nullable instead of optional.

It is the right decision, because path parameter can't be skipped.

https://nestia.io/docs/core/TypedParam/#restriction

8471919 commented 6 months ago

@samchon Can't I use 2 endpoint in one function of controller? like @Get(['/:a/:b', '/:a']) That's so sad news... thank you for your reply.

samchon commented 6 months ago

Have not considered that case.

Just define two methods please.

8471919 commented 6 months ago

Okay. thank you!