5
5
<h2 align =" center " >Rate Limiter Module for NestJS</h2 >
6
6
7
7
<p align =" center " >
8
- <a href =" https://www.codefactor.io/repository/github/ozkanonur/nestjs-rate-limiter " ><img src =" https://www.codefactor.io/repository/github/ozkanonur/nestjs-rate-limiter/badge?style=flat-square&sanitize=true " alt =" Code Quality " /></a >
9
- <a href =" https://www.npmjs.com/package/nestjs-rate-limiter " ><img src =" https://img.shields.io/npm/v/nestjs-rate-limiter.svg?style=flat-square&sanitize=true " alt =" NPM Version " /></a >
10
- <a href =" https://www.npmjs.com/package/nestjs-rate-limiter " ><img src =" https://img.shields.io/npm/dm/nestjs-rate-limiter.svg?style=flat-square&sanitize=true " alt =" NPM Downloads " /></a >
11
- <a href =" # " ><img src =" https://img.shields.io/npm/l/nestjs-rate-limiter.svg?colorB=black&label=LICENSE&style=flat-square&sanitize=true " alt =" License " /></a >
8
+ <a href =" https://www.codefactor.io/repository/github/ozkanonur/nestjs-rate-limiter " ><img src =" https://www.codefactor.io/repository/github/ozkanonur/nestjs-rate-limiter/badge?sanitize=true " alt =" Code Quality " /></a >
9
+ <a href =" https://www.npmjs.com/package/nestjs-rate-limiter " ><img src =" https://img.shields.io/npm/v/nestjs-rate-limiter.svg?sanitize=true " alt =" NPM Version " /></a >
10
+ <a href =" https://www.npmjs.com/package/nestjs-rate-limiter " ><img src =" https://img.shields.io/npm/dm/nestjs-rate-limiter.svg?sanitize=true " alt =" NPM Downloads " /></a >
11
+ <a href =" # " ><img src =" https://img.shields.io/npm/l/nestjs-rate-limiter.svg?colorB=black&label=LICENSE&sanitize=true " alt =" License " /></a >
12
+ <a href =" # " ><img src =" https://github.com/ozkanonur/nestjs-rate-limiter/actions/workflows/test.yml/badge.svg?branch=master " alt =" Test " /></a >
12
13
13
14
</p >
14
15
17
18
- [ Installation] ( https://github.com/ozkanonur/nestjs-rate-limiter#installation )
18
19
- [ Basic Usage] ( https://github.com/ozkanonur/nestjs-rate-limiter#basic-usage )
19
20
- [ Include Module] ( https://github.com/ozkanonur/nestjs-rate-limiter#include-module )
20
- - [ Using Interceptor ] ( https://github.com/ozkanonur/nestjs-rate-limiter#using-interceptor )
21
+ - [ Using Guard ] ( https://github.com/ozkanonur/nestjs-rate-limiter#using-guard )
21
22
- [ With Decorator] ( https://github.com/ozkanonur/nestjs-rate-limiter#with-decorator )
22
23
- [ With All Options] ( https://github.com/ozkanonur/nestjs-rate-limiter#with-all-options )
23
24
- [ Fastify based Graphql] ( https://github.com/ozkanonur/nestjs-rate-limiter#fastify-based-graphql )
50
51
- [ customResponseSchema] ( https://github.com/ozkanonur/nestjs-rate-limiter#-customResponseSchema )
51
52
- [ Benchmarks] ( https://github.com/ozkanonur/nestjs-rate-limiter#benchmarks )
52
53
- [ TODO List] ( https://github.com/ozkanonur/nestjs-rate-limiter#todo )
53
- - [ Examples] ( https://github.com/ozkanonur/nestjs-rate-limiter/examples/README.md )
54
54
55
55
# Description
56
56
@@ -83,44 +83,44 @@ First you need to import this module into your main application module:
83
83
> app.module.ts
84
84
85
85
``` ts
86
- import { RateLimiterModule } from ' nestjs-rate-limiter' ;
86
+ import { RateLimiterModule } from ' nestjs-rate-limiter'
87
87
88
88
@Module ({
89
89
imports: [RateLimiterModule ],
90
90
})
91
91
export class ApplicationModule {}
92
92
```
93
93
94
- ### Using Interceptor
94
+ ### Using Guard
95
95
96
- Now you need to register the interceptor . You can do this only on some routes:
96
+ Now you need to register the guard . You can do this only on some routes:
97
97
98
98
> app.controller.ts
99
99
100
100
``` ts
101
- import { RateLimiterInterceptor } from ' nestjs-rate-limiter' ;
101
+ import { RateLimiterGuard } from ' nestjs-rate-limiter'
102
102
103
- @UseInterceptors ( RateLimiterInterceptor )
103
+ @UseGuards ( RateLimiterGuard )
104
104
@Get (' /login' )
105
105
public async login () {
106
- console .log (' hello' );
106
+ console .log (' hello' )
107
107
}
108
108
```
109
109
110
- Or you can choose to register the interceptor globally:
110
+ Or you can choose to register the guard globally:
111
111
112
112
> app.module.ts
113
113
114
114
``` ts
115
- import { APP_INTERCEPTOR } from ' @nestjs/core' ;
116
- import { RateLimiterModule , RateLimiterInterceptor } from ' nestjs-rate-limiter' ;
115
+ import { APP_GUARD } from ' @nestjs/core'
116
+ import { RateLimiterModule , RateLimiterGuard } from ' nestjs-rate-limiter'
117
117
118
118
@Module ({
119
119
imports: [RateLimiterModule ],
120
120
providers: [
121
121
{
122
- provide: APP_INTERCEPTOR ,
123
- useClass: RateLimiterInterceptor ,
122
+ provide: APP_GUARD ,
123
+ useClass: RateLimiterGuard ,
124
124
},
125
125
],
126
126
})
@@ -135,19 +135,19 @@ route basis:
135
135
> app.controller.ts
136
136
137
137
``` ts
138
- import { RateLimit } from ' nestjs-rate-limiter' ;
138
+ import { RateLimit } from ' nestjs-rate-limiter'
139
139
140
140
@RateLimit ({ keyPrefix: ' sign-up' , points: 1 , duration: 60 , errorMessage: ' Accounts cannot be created more than once in per minute' })
141
141
@Get (' /signup' )
142
142
public async signUp () {
143
- console .log (' hello' );
143
+ console .log (' hello' )
144
144
}
145
145
```
146
146
147
147
### Dynamic Keyprefix
148
148
149
149
``` ts
150
- import { RateLimit } from ' nestjs-rate-limiter' ;
150
+ import { RateLimit } from ' nestjs-rate-limiter'
151
151
152
152
@RateLimit ({
153
153
keyPrefix : () => programmaticFuncThatReturnsValue (),
@@ -157,7 +157,7 @@ import { RateLimit } from 'nestjs-rate-limiter';
157
157
})
158
158
@Get (' /example' )
159
159
public async example () {
160
- console .log (' hello' );
160
+ console .log (' hello' )
161
161
}
162
162
```
163
163
@@ -201,8 +201,8 @@ The usage of the limiter options is as in the code block below. For an explanati
201
201
],
202
202
providers: [
203
203
{
204
- provide: APP_INTERCEPTOR ,
205
- useClass: RateLimiterInterceptor ,
204
+ provide: APP_GUARD ,
205
+ useClass: RateLimiterGuard ,
206
206
},
207
207
],
208
208
})
@@ -486,4 +486,3 @@ GraphQLModule.forRoot({
486
486
## TODO
487
487
- [ ] Support Websocket
488
488
- [ ] Support Rpc
489
- - [ ] Github Actions
0 commit comments