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: skip option for skipping by logic. #970

Closed
wants to merge 6 commits into from

Conversation

linhx
Copy link

@linhx linhx commented May 23, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • 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?

There is no option to skip by logic. ignoreUserAgents is not safe because the user agent string can be spoofed.

What is the new behavior?

Add the option skip to skip by logic:

@Module({
  imports: [
    ThrottlerModule.forRoot({
      ttl: 60,
      limit: 10,
      skip(context, req, res) {
        return req.ip === 'google-bot-ip';
      }
    }),
  ],
})

Override by @SkipThrottle

@Controller()
@SkipThrottle((context, req, res) => req.ip === 'another-ip')
class Controller {
  @Get() // skip when req.ip === 'another-ip'
  async index() {
    return '';
  }

  @Get('root-setting')
  @SkipThrottle(false) // skip when req.ip === 'google-bot-ip'
  async useDefault() {
    return '';
  }

  @Get('method-setting')
  @SkipThrottle((context, req, res) => req.ip === 'another-ip-1') // skip when req.ip === 'another-ip-1'
  async useMethod() {
    return '';
  }

  @Get('dont-skip-at-all')
  @SkipThrottle(null) // don't skip at all
  async dontSkipAtAll() {
    return '';
  }
}

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@linhx linhx force-pushed the feature/skip-method branch from 29f75b6 to e52aa6d Compare May 23, 2022 06:21
for the skip option and the @SkipThrottle
@linhx linhx force-pushed the feature/skip-method branch from e52aa6d to 36300b9 Compare May 23, 2022 06:57
src/throttler.guard.ts Outdated Show resolved Hide resolved
import { ModuleMetadata, Type } from '@nestjs/common/interfaces';

export type SkipMethod = (context: ExecutionContext, ...param: any) => boolean | Promise<boolean>;
Copy link
Member

Choose a reason for hiding this comment

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

I guess we can improve the ...param: any part somehow

Copy link
Author

@linhx linhx May 23, 2022

Choose a reason for hiding this comment

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

@micalevisk at first, I defined the Skip method like this:

export type SkipMethod = (
  context: ExecutionContext,
  req: Record<string, any>,
  res: Record<string, any>,
) => boolean | Promise<boolean>;

but the problem is when someone want to override the handleRequest method (for example Working with Websockets), the param now is not request and response.
So, I define the method params as ...param: any so others can use their custom params.

README.md Outdated Show resolved Hide resolved
linhx and others added 2 commits May 23, 2022 18:10
`isSkip` to `shouldSkip`

Co-authored-by: Micael Levi L. Cavalcante <[email protected]>
`isSkip` to `shouldSkip`

Co-authored-by: Micael Levi L. Cavalcante <[email protected]>
@jmcdo29
Copy link
Member

jmcdo29 commented May 23, 2022

I think rather than making this an option we pass in configuration it should be a method inside the guard that can be overwritten. Do you think you could update the code to be like that?

@linhx
Copy link
Author

linhx commented May 23, 2022

I think rather than making this an option we pass in configuration it should be a method inside the guard that can be overwritten. Do you think you could update the code to be like that?

I understand but I don't find that better.
With the current Guard we were able to override the method handleRequest and put the skipping logic there.
I think making it an optional option helps developer don't have to create a new guard.

@harshmehta813
Copy link

@jmcdo29 @micalevisk when can we have this feature?

Are there any other way to use @SkipThrottle() conditionally?

@jmcdo29
Copy link
Member

jmcdo29 commented Mar 10, 2023

@jmcdo29 @micalevisk when can we have this feature?

Are there any other way to use @SkipThrottle() conditionally?

I still feel like a method for the class and using a new class is a cleaner approach and that I'd prefer to take that route. Maybe some method like shouldThrottle(context: ExecutionContext): boolean, though that could cause issues when each route has its own condition, so maybe an option via decorator is a better approach here.

I'm working on the next major version of this package where a few things will be changed regarding the @Throttle() decorator and the configuration passed to it, so I think I'll probably update this as well to align with that

jmcdo29 added a commit that referenced this pull request Jul 6, 2023
@jmcdo29
Copy link
Member

jmcdo29 commented Jul 6, 2023

Implemented in #1565

@jmcdo29 jmcdo29 closed this Jul 6, 2023
jmcdo29 added a commit that referenced this pull request Jul 7, 2023
jmcdo29 added a commit that referenced this pull request Sep 4, 2023
jmcdo29 added a commit that referenced this pull request Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants