darraghoriordan / eslint-plugin-nestjs-typed

Some eslint rules for working with NestJs projects
http://www.darraghoriordan.com
171 stars 34 forks source link

Add custom decorators to api-method-should-specify-api-response #47

Closed elliotaplant closed 1 year ago

elliotaplant commented 1 year ago

We use this lint rule to ensure all of our API methods define their response types. We also have an additional decorator we want the linter to recognize: @ApiPaginatedResponse(SomeDataType) that defines a paginated response of some data type. What do you think about adding some way to include custom decorators in this linter?

darraghoriordan commented 1 year ago

Hey, yup this sounds good. there are a couple of examples of rule configurations that accept an array of custom decorators if anyone wants to take a crack at a PR 👍

I'll keep custom decorator requirement in mind the next time there is a new rule added to library where custom decorators might need to be configured. Seems to be a fairly consistent ask for these rules

elliotaplant commented 1 year ago

We ended up solving this by making the routes that return paginated DTOs extend an API decorated base PageDto. Contrary to my initial post, I think that creating decorators like ApiPaginatedResponse may actually be an anti-pattern because it duplicates the responsibility of type annotations on network DTOs.

For example, routes decorated by @ApiPaginatedResponse(SomeDataType) would have to decorate methods that return a value of PageDto<SomeDataType>. Both ApiPaginatedResponse and PageDto would have to provide the same information to Swagger, which invites an opportunity to have contradicting information. Better to let PageDto be the source of truth and simply use @ApiOkResponse({ type: PageDto }).

What other asks have you seen for custom rules? I'm curious to see if they might fall into the same pattern we saw in our project.

darraghoriordan commented 1 year ago

Hey yea that sounds like a nice solution with the object properties handling it

This link is an example of a rule has a configuration option for telling the rule a list of custom "class-validator" decorators. But this is a very common use case where people create custom validators or combine multiple validators into one for their domain.

https://github.dev/darraghoriordan/eslint-plugin-nestjs-typed/blob/1533ee31981e0a2c0992845d704eaa6a09516fd5/src/rules/validateNonPrimitvesNeedsTypeDecorator/validateNonPrimitiveNeedsDecorators.ts#L45

i will close this issue, it sounds like it's resolved 👍

Tsugami commented 6 months ago

Hey yea that sounds like a nice solution with the object properties handling it

This link is an example of a rule has a configuration option for telling the rule a list of custom "class-validator" decorators. But this is a very common use case where people create custom validators or combine multiple validators into one for their domain.

https://github.dev/darraghoriordan/eslint-plugin-nestjs-typed/blob/1533ee31981e0a2c0992845d704eaa6a09516fd5/src/rules/validateNonPrimitvesNeedsTypeDecorator/validateNonPrimitiveNeedsDecorators.ts#L45

i will close this issue, it sounds like it's resolved 👍

This not works for me, there are different rules

Error: image

Eslint config: image

ApiPaginatedResponse is my custom decorator

artemnesterenko commented 5 months ago

Being able to configure custom decorators for this rule would be really great. In my project, I have custom decorators that apply multiple decorators including ApiResponse. At the moment, I have to disable the rule for endpoints using those decorators.