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

feat(apply-decorators) #3113

Merged
merged 3 commits into from
Oct 31, 2019
Merged

feat(apply-decorators) #3113

merged 3 commits into from
Oct 31, 2019

Conversation

karlos1337
Copy link
Contributor

@karlos1337 karlos1337 commented Oct 6, 2019

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

A new helper method.

[ ] Bugfix
[X] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

Issue Number: #3104

What is the new behavior?

Could use helper method to combine multiple decorators at once.
For example:

// auth.decorator.ts
import { applyDecorators, NestCustomDecorator } from '@nestjs/core';

export function Auth(
  roles: UserRole | UserRole[],
): NestCustomDecorator {
  return applyDecorators(
    SetMetadata('roles', Array.isArray(roles) ? roles : [roles]),
    UseGuards(AuthGuard),
    ApiBearerAuth(),
    ApiUnauthorizedResponse({ description: 'Unauthorized' }),
  );
}

To be used as:

import { Auth } from '../auth/auth.decorator';
[...]
@Get('')
@Auth('admin')
@ApiOkResponse({ type: [User], description: 'Users fetched successfully' })
getUsers() { [...] }

Applying all four decorators at once and encapsulating all auth related ones.

Does this PR introduce a breaking change?

[ ] Yes
[X] No

@coveralls
Copy link

coveralls commented Oct 6, 2019

Pull Request Test Coverage Report for Build 4215

  • 5 of 5 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.005%) to 95.2%

Totals Coverage Status
Change from base Build 3710: 0.005%
Covered Lines: 4363
Relevant Lines: 4583

💛 - Coveralls

*/
export function applyDecorators(
...decorators: Array<(target: object, ...params: any[]) => any>
): (target: object, ...params: any[]) => any {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using any, can you refactor the code to use MethodDecorator / ClassDecorator type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I've also created an exported type to define the new decorator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@kamilmysliwiec kamilmysliwiec added this to the 6.9.0 milestone Oct 24, 2019
@kamilmysliwiec
Copy link
Member

Do you want to create a PR to the docs with this feature?

@karlos1337
Copy link
Contributor Author

Sure, but my English is so far from perfect, maybe will need a review, I'll refer docs PR here when done.

@kamilmysliwiec
Copy link
Member

Sure thing, no worries @karlos1337!

@kamilmysliwiec kamilmysliwiec changed the base branch from master to 6.9.0 October 31, 2019 12:19
@kamilmysliwiec kamilmysliwiec merged commit 038b55f into nestjs:6.9.0 Oct 31, 2019
@kamilmysliwiec kamilmysliwiec mentioned this pull request Nov 3, 2019
3 tasks
@karlos1337 karlos1337 deleted the feature/apply-decorators branch November 13, 2019 08:28
@lock
Copy link

lock bot commented Feb 11, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants