Skip to content

Commit

Permalink
fix(guard): guard now binds globally without the use of @UseGuards()
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed May 31, 2020
1 parent 86cf6d2 commit 4022447
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
9 changes: 6 additions & 3 deletions src/throttle.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ function setThrottlerMetadata(
Reflect.defineMetadata(THROTTLER_TTL, ttl, target);
}

export const Throttle = (limit = 20, ttl = 60) => {
export const Throttle = (
limit = 20,
ttl = 60,
): MethodDecorator & ClassDecorator => {
return (
target: any,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<any>,
propertyKey?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>,
) => {
if (descriptor) {
setThrottlerMetadata(descriptor.value, limit, ttl);
Expand Down
14 changes: 13 additions & 1 deletion src/throttler-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ import { createConfigurableDynamicRootModule } from '@golevelup/nestjs-modules';
import { Module } from '@nestjs/common';
import { THROTTLER_OPTIONS } from './throttler.constants';
import { ThrottlerOptions } from './throttler.interface';
import { ThrottlerStorageService } from './throttler.service';
import { APP_GUARD } from '@nestjs/core';
import { ThrottlerGuard } from './throttler.guard';

@Module({})
export class ThrottlerCoreModule extends createConfigurableDynamicRootModule<
ThrottlerCoreModule,
ThrottlerOptions
>(THROTTLER_OPTIONS) {}
>(THROTTLER_OPTIONS, {
providers: [
ThrottlerStorageService,
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
exports: [ThrottlerStorageService],
}) {}
16 changes: 1 addition & 15 deletions src/throttler.module.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import { AsyncModuleConfig } from '@golevelup/nestjs-modules';
import { Global, Module } from '@nestjs/common';
import { APP_GUARD } from '@nestjs/core';
import { ThrottlerCoreModule } from './throttler-core.module';
import { ThrottlerGuard } from './throttler.guard';
import { ThrottlerOptions } from './throttler.interface';
import { ThrottlerStorageService } from './throttler.service';

@Global()
@Module({
imports: [ThrottlerStorageService],
providers: [
ThrottlerStorageService,
{
provide: APP_GUARD,
useClass: ThrottlerGuard,
},
],
exports: [ThrottlerStorageService, ThrottlerGuard],
})
@Module({})
export class ThrottlerModule {
static forRoot(options?: ThrottlerOptions) {
return ThrottlerCoreModule.forRoot(ThrottlerCoreModule, options);
Expand Down
3 changes: 1 addition & 2 deletions test/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { Throttle, ThrottlerGuard } from '../../src';

@Controller()
export class AppController {
@Get()
@Throttle(2, 10)
@UseGuards(ThrottlerGuard)
@Get()
async test() {
return 'test';
}
Expand Down
4 changes: 2 additions & 2 deletions test/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Module } from '@nestjs/common';
import { ThrottlerModule, ThrottlerStorageService } from '../../src';
import { ThrottlerModule } from '../../src';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
imports: [ThrottlerModule.forRoot()],
controllers: [AppController],
providers: [AppService, ThrottlerStorageService],
providers: [AppService],
})
export class AppModule {}

0 comments on commit 4022447

Please sign in to comment.