@edirect/rate-limit-module 11.0.48 → 11.0.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,80 +12,153 @@ npm install @edirect/rate-limit-module
12
12
 
13
13
  ---
14
14
 
15
- This module provides a flexible **rate-limiting system** where access to operations can be restricted based on dynamic **rule expressions** such as `count_quote( ) > 1`. If the condition evaluates to `true`, access is **blocked**.
15
+ ## 🚀 Quick Start
16
16
 
17
- ## Features
18
-
19
- - Define rate-limiting rules with custom expressions.
20
-
21
- - Dynamically inject context-aware functions for rule evaluation.
17
+ ### 1. Create a Global Database Connection Module
22
18
 
23
- - Easily integrate with any resource via a decorator.
19
+ **Important:** This module requires a MongoDB connection provider. Create a global module to provide it:
24
20
 
25
- - Built with extensibility and modularity in mind.
26
-
27
- ## 🧱 How It Works
21
+ ```ts
22
+ // db-provider.module.ts
23
+ import { Global, Module } from '@nestjs/common';
24
+ import { Connection } from 'mongoose';
25
+ import { MongoDBConnection } from './mongodb-connection';
26
+
27
+ export const dbProvider = [
28
+ {
29
+ provide: 'DATABASE_CONNECTION',
30
+ useFactory: async (): Promise<Connection> => {
31
+ return new MongoDBConnection().connect();
32
+ },
33
+ },
34
+ ];
28
35
 
29
- 1. You define a rule using a simple expression (e.g., `count_quote( ) > 1`).
36
+ @Global()
37
+ @Module({
38
+ providers: [...dbProvider],
39
+ exports: [...dbProvider],
40
+ })
41
+ export class DbProviderModule {}
42
+ ```
30
43
 
31
- 2. The rule is evaluated in runtime using data injected from the request context.
44
+ ### 2. Import Database Module in Root Module
32
45
 
33
- 3. If the expression is true, the action is blocked.
46
+ ```ts
47
+ // app.module.ts
48
+ import { Module } from '@nestjs/common';
49
+ import { DbProviderModule } from './db-provider.module';
34
50
 
35
- ## 🧩 Usage
51
+ @Module({
52
+ imports: [
53
+ DbProviderModule, // Must be imported first
54
+ // ... other modules
55
+ ],
56
+ })
57
+ export class AppModule {}
58
+ ```
36
59
 
37
- ### 1. Install the Module
60
+ ### 3. Import RateLimitingModule in Feature Module
38
61
 
39
62
  ```ts
63
+ import {
64
+ RateLimitingModule,
65
+ RuleEngineModule,
66
+ } from '@edirect/rate-limit-module';
67
+
40
68
  @Module({
41
69
  imports: [
42
- RateLimitingModule,
70
+ // Pass the token name of your existing MongoDB connection provider
71
+ // Default is 'DATABASE_CONNECTION'
72
+ RateLimitingModule.forRoot('DATABASE_CONNECTION'),
73
+
74
+ // Or use a custom token
75
+ RateLimitingModule.forRoot('MY_CUSTOM_DB_CONNECTION'),
43
76
 
77
+ // Or omit to use default
78
+ RateLimitingModule.forRoot(),
79
+
80
+ // Configure custom functions for rule evaluation
44
81
  RuleEngineModule.forFeatureAsync({
45
- useFactory: async (quoteService: QuoteService) => {
46
- return await quoteService.countQuote();
82
+ imports: [YourFunctionsModule],
83
+ useFactory: async (useCase: YourInjectFunctionsUseCase) => {
84
+ return await useCase.getFunctions();
47
85
  },
48
-
49
- inject: [QuoteService],
86
+ inject: [YourInjectFunctionsUseCase],
50
87
  }),
51
88
  ],
52
89
  })
53
90
  export class YourFeatureModule {}
54
91
  ```
55
92
 
56
- ## 2. Decorate Your Handlers
93
+ ### 2. Database Connection Requirement
57
94
 
58
- Use the `@RateLimitingRuleMetadata` decorator to attach metadata used during rule evaluation.
95
+ The module uses the global `DATABASE_CONNECTION` provider created in step 1. It references your connection using the token name, so it doesn't duplicate the connection.
96
+
97
+ ### 3. Apply Rate Limiting to Routes
98
+
99
+ Use the `@RateLimitingRuleMetadata` decorator and `@UseGuards(RateLimitingRuleGuard)` to protect your endpoints:
59
100
 
60
101
  ```ts
102
+ import { RateLimitingRuleGuard, RateLimitingRuleMetadata } from '@edirect/rate-limit-module';
61
103
 
62
- import { RateLimitingRuleMetadata } from 'path-to-rate-limiting-module';
104
+ @RateLimitingRuleMetadata({
105
+ action: 'create_quote',
106
+ partnerPath: 'partner.partnerId',
107
+ providerPath: 'provider',
108
+ productTypePath: 'productType',
109
+ marketPath: 'market',
110
+ })
111
+ @UseGuards(RateLimitingRuleGuard)
112
+ @Post('quotes')
113
+ createQuote(@Body() body: CreateQuoteDto) {
114
+ return this.quoteService.create(body);
115
+ }
116
+ ```
63
117
 
118
+ ## ✨ Features
64
119
 
120
+ - Define rate-limiting rules with custom expressions
121
+ - Dynamically inject context-aware functions for rule evaluation
122
+ - Easily integrate with any resource via decorators
123
+ - Uses existing MongoDB connection (no separate connection required)
124
+ - Global module - configure once, use everywhere
125
+ - Built with extensibility and modularity in mind
65
126
 
66
- @RateLimitingRuleMetadata({
127
+ ## 🧱 How It Works
67
128
 
68
- action: 'create_quote',
129
+ 1. You define a rule using a simple expression (e.g., `count_quote( ) > 1`)
130
+ 2. The rule is evaluated at runtime using data injected from the request context
131
+ 3. If the expression is true, the action is blocked
69
132
 
70
- partnerPath: 'partner.partnerId',
133
+ ## 🛠 Architecture
71
134
 
72
- providerPath: 'provider',
135
+ The module is designed to:
73
136
 
74
- productTypePath: 'productType',
137
+ - **Not create its own MongoDB connection** - it uses the application's existing connection
138
+ - **Be a global module** - providers are available throughout the application
139
+ - **Use custom model providers** - creates Mongoose models using the injected `DATABASE_CONNECTION`
140
+ - **Export Reflector** - ensures guards have access to metadata
75
141
 
76
- marketPath: 'market',
142
+ ## 📝 Configuration Details
77
143
 
78
- })
144
+ ### Module Structure
79
145
 
80
- @Post('quotes')
146
+ ```ts
147
+ RateLimitingModule.forRoot(dbProvider) // Global module, accepts DATABASE_CONNECTION provider
148
+ └─ RuleModule.register() // Provides RuleService and RuleRepository
149
+ └─ Custom Model Provider // Uses DATABASE_CONNECTION to create Mongoose models
150
+ ```
81
151
 
82
- createQuote(@Body() body: CreateQuoteDto) {
152
+ ### Required Providers
83
153
 
84
- return this.quoteService.create(body);
154
+ The module requires:
85
155
 
86
- }
156
+ 1. **DATABASE_CONNECTION**: Mongoose connection instance
157
+ 2. **Reflector**: For metadata reflection (provided by the module)
158
+ 3. **RuleService**: For accessing rate limiting rules (exported by RuleModule)
87
159
 
88
- ```
160
+ ````ts
161
+ import { RateLimitingRuleMetadata } from 'path-to-rate-limiting-module';
89
162
 
90
163
  - `action`: Name of the action to be controlled.
91
164
 
@@ -109,7 +182,7 @@ RuleEngineModule.forFeatureAsync({
109
182
 
110
183
  inject: [RateLimitingInjectFunctionsUseCase],
111
184
  });
112
- ```
185
+ ````
113
186
 
114
187
  ## 🧪 Example Rule
115
188
 
package/dist/README.md CHANGED
@@ -1,16 +1,4 @@
1
- # @edirect/rate-limit-module
2
-
3
- A flexible **rate-limiting system** where access to operations can be restricted based on dynamic **rule expressions** such as `count_quote( ) > 1`. If the condition evaluates to `true`, access is **blocked**.
4
-
5
- ## Installation
6
-
7
- ```sh
8
- pnpm add @edirect/rate-limit-module
9
- # or
10
- npm install @edirect/rate-limit-module
11
- ```
12
-
13
- ---
1
+ # 📈 Rate Limiting Module
14
2
 
15
3
  This module provides a flexible **rate-limiting system** where access to operations can be restricted based on dynamic **rule expressions** such as `count_quote( ) > 1`. If the condition evaluates to `true`, access is **blocked**.
16
4
 
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edirect/rate-limit-module",
3
- "version": "11.0.48",
3
+ "version": "11.0.46",
4
4
  "main": "./dist/src/index.js",
5
5
  "types": "./dist/src/index.d.ts",
6
6
  "exports": {
@@ -16,21 +16,21 @@
16
16
  "dist"
17
17
  ],
18
18
  "dependencies": {
19
- "@edirect/auth": "^11.0.48",
20
- "@edirect/logger": "^11.0.48",
19
+ "@edirect/auth": "^11.0.46",
20
+ "@edirect/logger": "^11.0.46",
21
21
  "@nestjs/cache-manager": "^3.1.0",
22
- "@nestjs/common": "^11.1.15",
23
- "@nestjs/config": "^4.0.3",
24
- "@nestjs/core": "^11.1.15",
22
+ "@nestjs/common": "^11.1.12",
23
+ "@nestjs/config": "^4.0.2",
24
+ "@nestjs/core": "^11.1.12",
25
25
  "@nestjs/mongoose": "^11.0.4",
26
- "@nestjs/swagger": "^11.2.6",
26
+ "@nestjs/swagger": "^11.2.5",
27
27
  "cache-manager-redis-store": "^3.0.1",
28
28
  "class-transformer": "^0.5.1",
29
- "class-validator": "^0.15.1",
29
+ "class-validator": "^0.14.3",
30
30
  "dayjs": "^1.11.19",
31
- "ioredis": "^5.10.0",
31
+ "ioredis": "^5.9.2",
32
32
  "jexl": "^2.3.0",
33
- "mongoose": "^9.2.4",
33
+ "mongoose": "^9.1.5",
34
34
  "tslib": "^2.8.1",
35
35
  "uuid": "^13.0.0"
36
36
  },
@@ -30,7 +30,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
30
30
  __v: number;
31
31
  }), any, RateLimitingRule>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
32
32
  id: string;
33
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
33
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
34
34
  _id: import("mongoose").Types.ObjectId;
35
35
  } & {
36
36
  __v: number;
@@ -39,7 +39,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
39
39
  }, {
40
40
  ruleId?: import("mongoose").SchemaDefinitionProperty<string, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
41
41
  id: string;
42
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
42
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
43
43
  _id: import("mongoose").Types.ObjectId;
44
44
  } & {
45
45
  __v: number;
@@ -48,7 +48,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
48
48
  }> | undefined;
49
49
  name?: import("mongoose").SchemaDefinitionProperty<string, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
50
50
  id: string;
51
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
51
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
52
52
  _id: import("mongoose").Types.ObjectId;
53
53
  } & {
54
54
  __v: number;
@@ -57,7 +57,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
57
57
  }> | undefined;
58
58
  targetAction?: import("mongoose").SchemaDefinitionProperty<string | undefined, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
59
59
  id: string;
60
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
60
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
61
61
  _id: import("mongoose").Types.ObjectId;
62
62
  } & {
63
63
  __v: number;
@@ -66,7 +66,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
66
66
  }> | undefined;
67
67
  expression?: import("mongoose").SchemaDefinitionProperty<string, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
68
68
  id: string;
69
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
69
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
70
70
  _id: import("mongoose").Types.ObjectId;
71
71
  } & {
72
72
  __v: number;
@@ -75,7 +75,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
75
75
  }> | undefined;
76
76
  enabled?: import("mongoose").SchemaDefinitionProperty<boolean, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
77
77
  id: string;
78
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
78
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
79
79
  _id: import("mongoose").Types.ObjectId;
80
80
  } & {
81
81
  __v: number;
@@ -84,7 +84,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
84
84
  }> | undefined;
85
85
  market?: import("mongoose").SchemaDefinitionProperty<string, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
86
86
  id: string;
87
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
87
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
88
88
  _id: import("mongoose").Types.ObjectId;
89
89
  } & {
90
90
  __v: number;
@@ -93,7 +93,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
93
93
  }> | undefined;
94
94
  errorMessage?: import("mongoose").SchemaDefinitionProperty<string | undefined, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
95
95
  id: string;
96
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
96
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
97
97
  _id: import("mongoose").Types.ObjectId;
98
98
  } & {
99
99
  __v: number;
@@ -102,7 +102,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
102
102
  }> | undefined;
103
103
  partner?: import("mongoose").SchemaDefinitionProperty<string, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
104
104
  id: string;
105
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
105
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
106
106
  _id: import("mongoose").Types.ObjectId;
107
107
  } & {
108
108
  __v: number;
@@ -111,7 +111,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
111
111
  }> | undefined;
112
112
  provider?: import("mongoose").SchemaDefinitionProperty<string | undefined, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
113
113
  id: string;
114
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
114
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
115
115
  _id: import("mongoose").Types.ObjectId;
116
116
  } & {
117
117
  __v: number;
@@ -120,7 +120,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
120
120
  }> | undefined;
121
121
  productType?: import("mongoose").SchemaDefinitionProperty<string | undefined, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
122
122
  id: string;
123
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
123
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
124
124
  _id: import("mongoose").Types.ObjectId;
125
125
  } & {
126
126
  __v: number;
@@ -129,7 +129,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
129
129
  }> | undefined;
130
130
  variables?: import("mongoose").SchemaDefinitionProperty<Record<string, string> | undefined, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
131
131
  id: string;
132
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
132
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
133
133
  _id: import("mongoose").Types.ObjectId;
134
134
  } & {
135
135
  __v: number;
@@ -138,7 +138,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
138
138
  }> | undefined;
139
139
  variablesRequired?: import("mongoose").SchemaDefinitionProperty<boolean | undefined, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
140
140
  id: string;
141
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
141
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
142
142
  _id: import("mongoose").Types.ObjectId;
143
143
  } & {
144
144
  __v: number;
@@ -147,7 +147,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
147
147
  }> | undefined;
148
148
  deletedAt?: import("mongoose").SchemaDefinitionProperty<Date | undefined, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
149
149
  id: string;
150
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
150
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
151
151
  _id: import("mongoose").Types.ObjectId;
152
152
  } & {
153
153
  __v: number;
@@ -156,7 +156,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
156
156
  }> | undefined;
157
157
  createdAt?: import("mongoose").SchemaDefinitionProperty<Date | undefined, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
158
158
  id: string;
159
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
159
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
160
160
  _id: import("mongoose").Types.ObjectId;
161
161
  } & {
162
162
  __v: number;
@@ -165,7 +165,7 @@ export declare const RateLimitingRuleSchema: import("mongoose").Schema<RateLimit
165
165
  }> | undefined;
166
166
  updatedAt?: import("mongoose").SchemaDefinitionProperty<Date | undefined, RateLimitingRule, Document<unknown, {}, RateLimitingRule, {
167
167
  id: string;
168
- }, import("mongoose").DefaultSchemaOptions> & Omit<RateLimitingRule & {
168
+ }, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & Omit<RateLimitingRule & {
169
169
  _id: import("mongoose").Types.ObjectId;
170
170
  } & {
171
171
  __v: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edirect/rate-limit-module",
3
- "version": "11.0.48",
3
+ "version": "11.0.50",
4
4
  "packageScope": "@edirect",
5
5
  "main": "./dist/src/index.js",
6
6
  "types": "./dist/src/index.d.ts",
@@ -18,22 +18,22 @@
18
18
  ],
19
19
  "dependencies": {
20
20
  "@nestjs/cache-manager": "^3.1.0",
21
- "@nestjs/common": "^11.1.15",
21
+ "@nestjs/common": "^11.1.16",
22
22
  "@nestjs/config": "^4.0.3",
23
- "@nestjs/core": "^11.1.15",
23
+ "@nestjs/core": "^11.1.16",
24
24
  "@nestjs/mongoose": "^11.0.4",
25
25
  "@nestjs/swagger": "^11.2.6",
26
26
  "cache-manager-redis-store": "^3.0.1",
27
27
  "class-transformer": "^0.5.1",
28
28
  "class-validator": "^0.15.1",
29
- "dayjs": "^1.11.19",
29
+ "dayjs": "^1.11.20",
30
30
  "ioredis": "^5.10.0",
31
31
  "jexl": "^2.3.0",
32
- "mongoose": "^9.2.4",
32
+ "mongoose": "^9.3.0",
33
33
  "tslib": "^2.8.1",
34
34
  "uuid": "^13.0.0",
35
- "@edirect/auth": "11.0.48",
36
- "@edirect/logger": "11.0.48"
35
+ "@edirect/auth": "11.0.50",
36
+ "@edirect/logger": "11.0.50"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/jexl": "^2.3.4"