@edirect/rate-limit-module 2.0.9 โ†’ 11.0.40

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.
Files changed (158) hide show
  1. package/README.md +88 -172
  2. package/dist/README.md +583 -0
  3. package/dist/package.json +41 -0
  4. package/dist/src/decorators/rate-limiting-rule.decorator.d.ts +1 -0
  5. package/dist/src/decorators/rate-limiting-rule.decorator.d.ts.map +1 -0
  6. package/dist/src/decorators/rate-limiting-rule.decorator.js +0 -1
  7. package/dist/src/guards/rate-limiting.guard.d.ts +1 -0
  8. package/dist/src/guards/rate-limiting.guard.d.ts.map +1 -0
  9. package/dist/src/guards/rate-limiting.guard.js +43 -36
  10. package/dist/src/index.d.ts +1 -0
  11. package/dist/src/index.d.ts.map +1 -0
  12. package/dist/src/index.js +9 -23
  13. package/dist/src/main.d.ts +1 -0
  14. package/dist/src/main.d.ts.map +1 -0
  15. package/dist/src/main.js +0 -1
  16. package/dist/src/object.utils.d.ts +1 -0
  17. package/dist/src/object.utils.d.ts.map +1 -0
  18. package/dist/src/object.utils.js +0 -1
  19. package/dist/src/rate-limiting.module.d.ts +1 -0
  20. package/dist/src/rate-limiting.module.d.ts.map +1 -0
  21. package/dist/src/rate-limiting.module.js +2 -8
  22. package/dist/src/rules/dto/pagination.dto.d.ts +1 -0
  23. package/dist/src/rules/dto/pagination.dto.d.ts.map +1 -0
  24. package/dist/src/rules/dto/pagination.dto.js +7 -16
  25. package/dist/src/rules/dto/rule.dto.d.ts +3 -2
  26. package/dist/src/rules/dto/rule.dto.d.ts.map +1 -0
  27. package/dist/src/rules/dto/rule.dto.js +35 -44
  28. package/dist/src/rules/interface/pagination.interface.d.ts +1 -0
  29. package/dist/src/rules/interface/pagination.interface.d.ts.map +1 -0
  30. package/dist/src/rules/interface/pagination.interface.js +0 -1
  31. package/dist/src/rules/interface/rule.interface.d.ts +1 -0
  32. package/dist/src/rules/interface/rule.interface.d.ts.map +1 -0
  33. package/dist/src/rules/interface/rule.interface.js +0 -1
  34. package/dist/src/rules/rule.controller.d.ts +1 -0
  35. package/dist/src/rules/rule.controller.d.ts.map +1 -0
  36. package/dist/src/rules/rule.controller.js +29 -41
  37. package/dist/src/rules/rule.module.d.ts +1 -0
  38. package/dist/src/rules/rule.module.d.ts.map +1 -0
  39. package/dist/src/rules/rule.module.js +2 -8
  40. package/dist/src/rules/rule.repository.d.ts +1 -0
  41. package/dist/src/rules/rule.repository.d.ts.map +1 -0
  42. package/dist/src/rules/rule.repository.js +4 -16
  43. package/dist/src/rules/rule.schema.d.ts +150 -3
  44. package/dist/src/rules/rule.schema.d.ts.map +1 -0
  45. package/dist/src/rules/rule.schema.js +49 -56
  46. package/dist/src/rules/rule.service.d.ts +3 -1
  47. package/dist/src/rules/rule.service.d.ts.map +1 -0
  48. package/dist/src/rules/rule.service.js +16 -22
  49. package/dist/src/shared/cache/cache.service.d.ts +2 -1
  50. package/dist/src/shared/cache/cache.service.d.ts.map +1 -0
  51. package/dist/src/shared/cache/cache.service.js +3 -12
  52. package/dist/src/shared/dsl/dsl.interface.d.ts +1 -0
  53. package/dist/src/shared/dsl/dsl.interface.d.ts.map +1 -0
  54. package/dist/src/shared/dsl/dsl.interface.js +0 -1
  55. package/dist/src/shared/dsl/dsl.service.d.ts +2 -1
  56. package/dist/src/shared/dsl/dsl.service.d.ts.map +1 -0
  57. package/dist/src/shared/dsl/dsl.service.js +38 -28
  58. package/dist/src/shared/redis/redis.module.d.ts +1 -0
  59. package/dist/src/shared/redis/redis.module.d.ts.map +1 -0
  60. package/dist/src/shared/redis/redis.module.js +3 -9
  61. package/dist/src/shared/redis/redis.service.d.ts +2 -1
  62. package/dist/src/shared/redis/redis.service.d.ts.map +1 -0
  63. package/dist/src/shared/redis/redis.service.js +5 -17
  64. package/dist/src/shared/rule-engine/interface/rule-engine.interface.d.ts +2 -1
  65. package/dist/src/shared/rule-engine/interface/rule-engine.interface.d.ts.map +1 -0
  66. package/dist/src/shared/rule-engine/interface/rule-engine.interface.js +0 -1
  67. package/dist/src/shared/rule-engine/rule-engine.module.d.ts +4 -3
  68. package/dist/src/shared/rule-engine/rule-engine.module.d.ts.map +1 -0
  69. package/dist/src/shared/rule-engine/rule-engine.module.js +4 -11
  70. package/dist/src/shared/rule-engine/rule-engine.service.d.ts +7 -6
  71. package/dist/src/shared/rule-engine/rule-engine.service.d.ts.map +1 -0
  72. package/dist/src/shared/rule-engine/rule-engine.service.js +14 -12
  73. package/dist/src/shared/shared.module.d.ts +1 -0
  74. package/dist/src/shared/shared.module.d.ts.map +1 -0
  75. package/dist/src/shared/shared.module.js +2 -8
  76. package/dist/src/validators/rule.constraint.d.ts +3 -2
  77. package/dist/src/validators/rule.constraint.d.ts.map +1 -0
  78. package/dist/src/validators/rule.constraint.js +13 -16
  79. package/dist/src/validators/rule.validator.d.ts +2 -1
  80. package/dist/src/validators/rule.validator.d.ts.map +1 -0
  81. package/dist/src/validators/rule.validator.js +0 -1
  82. package/dist/tsconfig.lib.tsbuildinfo +1 -0
  83. package/package.json +43 -37
  84. package/dist/src/decorators/rate-limiting-rule.decorator.js.map +0 -1
  85. package/dist/src/guards/rate-limiting.guard.js.map +0 -1
  86. package/dist/src/index.js.map +0 -1
  87. package/dist/src/main.js.map +0 -1
  88. package/dist/src/mocks-modules/rate-limiting.module.d.ts +0 -21
  89. package/dist/src/mocks-modules/rate-limiting.module.js +0 -54
  90. package/dist/src/mocks-modules/rate-limiting.module.js.map +0 -1
  91. package/dist/src/mocks-modules/rule.module.d.ts +0 -5
  92. package/dist/src/mocks-modules/rule.module.js +0 -38
  93. package/dist/src/mocks-modules/rule.module.js.map +0 -1
  94. package/dist/src/mocks-modules/shared-module.mocks.d.ts +0 -10
  95. package/dist/src/mocks-modules/shared-module.mocks.js +0 -32
  96. package/dist/src/mocks-modules/shared-module.mocks.js.map +0 -1
  97. package/dist/src/object.utils.js.map +0 -1
  98. package/dist/src/rate-limiting.module.js.map +0 -1
  99. package/dist/src/rules/dto/pagination.dto.js.map +0 -1
  100. package/dist/src/rules/dto/rule.dto.js.map +0 -1
  101. package/dist/src/rules/interface/pagination.interface.js.map +0 -1
  102. package/dist/src/rules/interface/rule.interface.js.map +0 -1
  103. package/dist/src/rules/rule.controller.js.map +0 -1
  104. package/dist/src/rules/rule.module.js.map +0 -1
  105. package/dist/src/rules/rule.repository.js.map +0 -1
  106. package/dist/src/rules/rule.schema.js.map +0 -1
  107. package/dist/src/rules/rule.service.js.map +0 -1
  108. package/dist/src/shared/cache/cache.service.js.map +0 -1
  109. package/dist/src/shared/dls/dsl.interface.d.ts +0 -42
  110. package/dist/src/shared/dls/dsl.interface.js +0 -10
  111. package/dist/src/shared/dls/dsl.interface.js.map +0 -1
  112. package/dist/src/shared/dls/dsl.utils.d.ts +0 -3
  113. package/dist/src/shared/dls/dsl.utils.js +0 -73
  114. package/dist/src/shared/dls/dsl.utils.js.map +0 -1
  115. package/dist/src/shared/dsl/dsl.interface.js.map +0 -1
  116. package/dist/src/shared/dsl/dsl.service.js.map +0 -1
  117. package/dist/src/shared/dsl/dsl.utils.d.ts +0 -3
  118. package/dist/src/shared/dsl/dsl.utils.js +0 -73
  119. package/dist/src/shared/dsl/dsl.utils.js.map +0 -1
  120. package/dist/src/shared/redis/redis.module.js.map +0 -1
  121. package/dist/src/shared/redis/redis.service.js.map +0 -1
  122. package/dist/src/shared/rule-engine/interface/rule-engine.interface.js.map +0 -1
  123. package/dist/src/shared/rule-engine/rule-engine.module.js.map +0 -1
  124. package/dist/src/shared/rule-engine/rule-engine.service.js.map +0 -1
  125. package/dist/src/shared/shared.module.js.map +0 -1
  126. package/dist/src/validators/rule.constraint.js.map +0 -1
  127. package/dist/src/validators/rule.validator.js.map +0 -1
  128. package/dist/test/guards/rate-limiting.guard.spec.d.ts +0 -1
  129. package/dist/test/guards/rate-limiting.guard.spec.js +0 -93
  130. package/dist/test/guards/rate-limiting.guard.spec.js.map +0 -1
  131. package/dist/test/mocks/rate-limiting-module.mocks.d.ts +0 -2
  132. package/dist/test/mocks/rate-limiting-module.mocks.js +0 -60
  133. package/dist/test/mocks/rate-limiting-module.mocks.js.map +0 -1
  134. package/dist/test/mocks/rate-limiting.mocks.d.ts +0 -17
  135. package/dist/test/mocks/rate-limiting.mocks.js +0 -21
  136. package/dist/test/mocks/rate-limiting.mocks.js.map +0 -1
  137. package/dist/test/mocks/rule-engine.mocks.d.ts +0 -7
  138. package/dist/test/mocks/rule-engine.mocks.js +0 -11
  139. package/dist/test/mocks/rule-engine.mocks.js.map +0 -1
  140. package/dist/test/mocks/rule.mocks.d.ts +0 -32
  141. package/dist/test/mocks/rule.mocks.js +0 -58
  142. package/dist/test/mocks/rule.mocks.js.map +0 -1
  143. package/dist/test/mocks/shared-module.mocks.d.ts +0 -10
  144. package/dist/test/mocks/shared-module.mocks.js +0 -32
  145. package/dist/test/mocks/shared-module.mocks.js.map +0 -1
  146. package/dist/test/mocks/shared.mocks.d.ts +0 -19
  147. package/dist/test/mocks/shared.mocks.js +0 -29
  148. package/dist/test/mocks/shared.mocks.js.map +0 -1
  149. package/dist/test/modules/rate-limiting.module.d.ts +0 -4
  150. package/dist/test/modules/rate-limiting.module.js +0 -38
  151. package/dist/test/modules/rate-limiting.module.js.map +0 -1
  152. package/dist/test/modules/rule.module.d.ts +0 -5
  153. package/dist/test/modules/rule.module.js +0 -38
  154. package/dist/test/modules/rule.module.js.map +0 -1
  155. package/dist/test/rules/rules.service.spec.d.ts +0 -1
  156. package/dist/test/rules/rules.service.spec.js +0 -115
  157. package/dist/test/rules/rules.service.spec.js.map +0 -1
  158. package/dist/tsconfig.tsbuildinfo +0 -1
package/dist/README.md ADDED
@@ -0,0 +1,583 @@
1
+ # ๐Ÿ“ˆ Rate Limiting Module
2
+
3
+
4
+
5
+ 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**.
6
+
7
+
8
+
9
+ ## โœจ Features
10
+
11
+
12
+
13
+ - Define rate-limiting rules with custom expressions.
14
+
15
+ - Dynamically inject context-aware functions for rule evaluation.
16
+
17
+ - Easily integrate with any resource via a decorator.
18
+
19
+ - Built with extensibility and modularity in mind.
20
+
21
+
22
+
23
+ ## ๐Ÿงฑ How It Works
24
+
25
+
26
+
27
+ 1. You define a rule using a simple expression (e.g., `count_quote( ) > 1`).
28
+
29
+ 2. The rule is evaluated in runtime using data injected from the request context.
30
+
31
+ 3. If the expression is true, the action is blocked.
32
+
33
+
34
+
35
+ ## ๐Ÿงฉ Usage
36
+
37
+
38
+
39
+ ### 1. Install the Module
40
+
41
+
42
+
43
+ ```ts
44
+
45
+ @Module({
46
+
47
+ imports: [
48
+
49
+ RateLimitingModule,
50
+
51
+ RuleEngineModule.forFeatureAsync({
52
+
53
+ useFactory: async (quoteService: QuoteService) => {
54
+
55
+ return await quoteService.countQuote();
56
+
57
+ },
58
+
59
+ inject: [QuoteService],
60
+
61
+ }),
62
+
63
+ ],
64
+
65
+ })
66
+
67
+ export class YourFeatureModule {}
68
+
69
+ ```
70
+
71
+
72
+
73
+ ## 2. Decorate Your Handlers
74
+
75
+
76
+
77
+ Use the `@RateLimitingRuleMetadata` decorator to attach metadata used during rule evaluation.
78
+
79
+
80
+
81
+ ```ts
82
+
83
+ import { RateLimitingRuleMetadata } from 'path-to-rate-limiting-module';
84
+
85
+
86
+
87
+ @RateLimitingRuleMetadata({
88
+
89
+ action: 'create_quote',
90
+
91
+ partnerPath: 'partner.partnerId',
92
+
93
+ providerPath: 'provider',
94
+
95
+ productTypePath: 'productType',
96
+
97
+ marketPath: 'market',
98
+
99
+ })
100
+
101
+ @Post('quotes')
102
+
103
+ createQuote(@Body() body: CreateQuoteDto) {
104
+
105
+ return this.quoteService.create(body);
106
+
107
+ }
108
+
109
+ ```
110
+
111
+
112
+
113
+ * `action`: Name of the action to be controlled.
114
+
115
+ * `partnerPath`: Path to extract the partner ID from the request context.
116
+
117
+ * `providerPath`: Path to extract the provider.
118
+
119
+ * `productTypePath`: Path to extract the product type.
120
+
121
+
122
+
123
+ ## ๐Ÿ›  Rule Engine Integration
124
+
125
+
126
+
127
+ This module depends on an underlying rule engine to evaluate expressions. You must provide the functions used in expressions through:
128
+
129
+
130
+
131
+ ```ts
132
+
133
+ RuleEngineModule.forFeatureAsync({
134
+
135
+ useFactory: async (useCase: RateLimitingInjectFunctionsUseCase) => {
136
+
137
+ return await useCase.getFunctions();
138
+
139
+ // returns an object like { count_quote: async () => number }
140
+
141
+ },
142
+
143
+ inject: [RateLimitingInjectFunctionsUseCase],
144
+
145
+ });
146
+
147
+ ```
148
+
149
+
150
+
151
+ ## ๐Ÿงช Example Rule
152
+
153
+
154
+ A rule stored in your system might look like:
155
+
156
+
157
+ ```json
158
+
159
+ {
160
+ "expression": "count_quote() > 1",
161
+ "action": "create_quote"
162
+ }
163
+
164
+ ```
165
+
166
+
167
+
168
+ If the expression returns `true`, the rate limiter will block further `create_quote` actions for the user based on the context values.
169
+
170
+
171
+ ## ๐Ÿง  Dynamic Parameter Resolution
172
+
173
+ When building queries based on request context, it's possible to reference nested values using paths. This system supports:
174
+
175
+ - Resolving values using variable placeholders like `:applicant.documents[0].value`.
176
+
177
+ - Interpreting date expressions safely, like `date(-5d)` (5 days ago), without using `eval`.
178
+
179
+ ### Supported Syntax
180
+ ```json
181
+
182
+ {
183
+ "value": ":applicant.documents[0].value",
184
+ "label": "applicant.documents.value"
185
+ }
186
+
187
+ ```
188
+
189
+ Will extract the value from the context:
190
+
191
+ ```json
192
+ {
193
+ "applicant": {
194
+ "documents": [
195
+ { "type": "NIF", "value": "ABCDE" }
196
+ ]
197
+ }
198
+ }
199
+ ```
200
+ Result: `"ABCDE"`
201
+
202
+
203
+ You can also use dynamic dates:
204
+
205
+ ```json
206
+ {
207
+ "value": "date(-7d)",
208
+ "label": "createdAt",
209
+ "op": "gte"
210
+ }
211
+ ```
212
+
213
+ Result: `"2025-07-10T00:00:00.000Z"` (7 days ago)
214
+
215
+ ### Supported Date Units
216
+
217
+ - `d`: Days (default)
218
+
219
+ - `M`: Months
220
+
221
+ - `y`: Years
222
+
223
+ Examples:
224
+
225
+ - `date(-5d)` โ†’ 5 days ago
226
+
227
+ - `date(-1M)` โ†’ 1 month ago
228
+
229
+ - `date(0)` โ†’ Today
230
+
231
+ No dynamic code execution (`eval`, `new Function`) is used โ€” expressions are parsed securely.
232
+
233
+ ## โš™๏ธ Supported Operators (`op`)
234
+
235
+ You can optionally define an `op` (operation) field to specify how the value should be interpreted. The system currently supports:
236
+
237
+ ### `gte` โ€” Greater Than or Equal (usually for dates)
238
+
239
+ ```json
240
+ {
241
+ "value": "date(-7d)",
242
+ "label": "createdAt",
243
+ "op": "gte"
244
+ }
245
+ ```
246
+ ๐Ÿ” This means: only include records created after **7 days ago**.
247
+
248
+ ```json
249
+ {
250
+ "value": {
251
+ "from": 1,
252
+ "to": 10
253
+ },
254
+ "label": "createdAt",
255
+ "op": "range"
256
+ }
257
+ ```
258
+ ๐Ÿ” This sets a static date or value range. Can be used for numbers, dates, or custom fields.
259
+
260
+ ### `rangeDate` โ€” Dynamic Date Range
261
+
262
+ ```json
263
+ {
264
+ "value": {
265
+ "from": "date(-30d)",
266
+ "to": "date(0)"
267
+ },
268
+ "label": "createdAt",
269
+ "op": "rangeDate"
270
+ }
271
+ ```
272
+ ๐Ÿ” This automatically resolves both `from` and `to` values as dates. For example, this would generate a date range from **30 days ago** to **now**.
273
+
274
+
275
+ ## ๐Ÿงฌ DSL Support and MongoDB Integration
276
+
277
+ This module uses a lightweight **DSL (Domain-Specific Language)** to define rules and filters in a declarative and expressive way.
278
+
279
+ ### What is a DSL?
280
+
281
+ A **DSL** is a simplified, pseudo-language designed for expressing business logic in a readable and configurable format. In this module, the DSL is used to define:
282
+
283
+ - Conditional expressions (e.g., `count_quote() > 1`)
284
+ - Filter structures using dynamic context variables and operators (`eq`, `range`, `gte`, etc.)
285
+
286
+ These DSL expressions are **not executed as raw code**, but safely parsed and evaluated using resolvers and expression evaluators.
287
+
288
+ ---
289
+
290
+ ### DSL to MongoDB Filter Conversion
291
+
292
+ One powerful feature of this module is the ability to **transform DSL filters into MongoDB-compatible query objects**.
293
+
294
+ For example, the following DSL input:
295
+ ```json
296
+ {
297
+ createdAt: {
298
+ $gte: new Date('2025-06-17T00:00:00.000Z'),
299
+ $lte: new Date('2025-07-17T00:00:00.000Z')
300
+ }
301
+ }
302
+ ```
303
+ This is handled by utility functions like `buildDlsOp`, which map DSL constructs to database query.
304
+
305
+ ### โœ… Benefits of Using DSL
306
+
307
+ - **Separation of logic from code:** Business rules can be stored in DB or configs
308
+
309
+ - **Extensibility:** Easy to support new operators or DB backends (e.g., SQL, Elasticsearch)
310
+
311
+ - **Security:** No raw `eval` or dynamic JS execution
312
+
313
+ - **Readability:** DSL is expressive for non-technical stakeholders
314
+
315
+ By combining DSL definitions with path-based context extraction and safe runtime resolution, this module provides a highly flexible and secure rate-limiting engine ready for dynamic environments.
316
+
317
+ ## ๐Ÿงพ Dynamic Query Context with `variables.queries`
318
+
319
+ The `variables` object is designed to allow structured dynamic query generation through an array of `queries`. Each query defines:
320
+
321
+ - The **function(s)** where this context will be injected (`fnContext`)
322
+ - The **parameters** to be resolved and passed (`query`)
323
+ - **Optional operations** and **default values**
324
+
325
+ ### Structure of `variables.queries`
326
+
327
+ ```json
328
+ {
329
+ "queries": [
330
+ {
331
+ "fnContext": ["countQuoteByParams"],
332
+ "query": [
333
+ {
334
+ "value": ":partner.partnerId",
335
+ "label": "partner.partnerId"
336
+ },
337
+ {
338
+ "value": ":productType",
339
+ "label": "productType"
340
+ },
341
+ {
342
+ "value": ":provider",
343
+ "label": "provider"
344
+ },
345
+ {
346
+ "value": ":applicant.documents[0].value",
347
+ "label": "applicant.documents.value"
348
+ },
349
+ {
350
+ "value": ":applicant.documents[0].type",
351
+ "label": "applicant.documents.type"
352
+ },
353
+ {
354
+ "label": "createdAt",
355
+ "op": "range",
356
+ "value": {
357
+ "to": "date(0)",
358
+ "from": "fn:getValueFromSale"
359
+ },
360
+ "defaultValue": {
361
+ "from": "date(-5d)",
362
+ "to": "date(0)"
363
+ }
364
+ }
365
+ ]
366
+ }
367
+ ]
368
+ }
369
+ ```
370
+
371
+ ### ๐Ÿ” Explanation
372
+
373
+ Field
374
+
375
+ Description
376
+
377
+ `fnContext`
378
+
379
+ The name(s) of the function(s) that will receive this query as parameter input
380
+
381
+ `query`
382
+
383
+ Array of parameter definitions. Each will be resolved based on context or DSL expressions
384
+
385
+ `label`
386
+
387
+ The target field to be passed (e.g. used to generate a MongoDB query or as input to a service)
388
+
389
+ `value`
390
+
391
+ Can be a dynamic reference (e.g., `:context.path`), a static value, or a DSL expression
392
+
393
+ `op`
394
+
395
+ Optional. Defines the operator (`eq`, `range`, `rangeDate`, `gte`, etc.)
396
+
397
+ `defaultValue`
398
+
399
+ Fallback value(s) to be used if dynamic resolution fails (e.g., missing path or function)
400
+
401
+ ----------
402
+
403
+ ### ๐Ÿง  Special Value Formats
404
+
405
+ - `:path.to.value` โ€” Dynamically extracted from the context object
406
+
407
+ - `date(-7d)` โ€” Resolved as a safe date expression
408
+
409
+ - `fn:getValueFromSale` โ€” Calls a predefined function to resolve the value dynamically
410
+
411
+ - Static values like `"moneyhero"` are passed as-is
412
+
413
+ ### ๐Ÿงช Use Case Example
414
+
415
+ Imagine your service defines a method like:
416
+
417
+
418
+ async countQuoteByParams(filters: Record<string, any>): Promise<number>
419
+
420
+ At runtime, the module will:
421
+
422
+ 1. Resolve each `value` in the `query` array using context and supported expressions.
423
+
424
+ 2. Construct a query object:
425
+
426
+ ```json
427
+ {
428
+ partner: { partnerId: "bolttech" },
429
+ productType: "car-insurance",
430
+ provider: "bolttech",
431
+ applicant: {
432
+ documents: [{ type: "NIF", value: "ABCDE" }]
433
+ },
434
+ createdAt: {
435
+ from: new Date("2025-07-25T00:00:00Z"), // resolved from fn:getValueFromSale
436
+ to: new Date("2025-07-30T00:00:00Z") // resolved from date(0)
437
+ }
438
+ }
439
+ ```
440
+
441
+ 3. Inject this into `countQuoteByParams(...)`
442
+ This approach gives you a highly flexible, configurable system for controlling dynamic function parameters via context-based DSLs and runtime evaluation.
443
+
444
+ ----------
445
+ ## ๐Ÿง  Custom Runtime Functions with `variables.functions`
446
+
447
+ In addition to defining query parameters in `variables.queries`, the system also supports **custom runtime functions** through the `variables.functions` array.
448
+
449
+ These functions can be executed **before query construction** to compute values that are **injected** dynamically into query fields.
450
+
451
+ ---
452
+
453
+ ### ๐Ÿ”ง Structure of `functions`
454
+
455
+ Each function definition includes:
456
+
457
+ | Field | Description |
458
+ |------------|-----------------------------------------------------------------------------|
459
+ | `name` | Unique function identifier |
460
+ | `fn` | The function to execute, in the format `functionName(args...)` |
461
+ | `variables`| Parameters passed to the function, which can include subqueries and values |
462
+
463
+ ### ๐Ÿ“Œ Example
464
+
465
+ ```json
466
+ {
467
+ "functions": [
468
+ {
469
+ "name": "getValueFromSale",
470
+ "fn": "getValueFromSale(queries, target)",
471
+ "variables": {
472
+ "target": "createdAt",
473
+ "queries": [
474
+ {
475
+ "value": ":productType",
476
+ "label": "products.productType"
477
+ },
478
+ {
479
+ "value": ":applicant.documents[0].value",
480
+ "label": "applicant.documents.value"
481
+ },
482
+ {
483
+ "value": ":applicant.documents[0].type",
484
+ "label": "applicant.documents.type"
485
+ },
486
+ {
487
+ "value": "date(-5d)",
488
+ "label": "createdAt",
489
+ "op": "gte"
490
+ }
491
+ ]
492
+ }
493
+ }
494
+ ]
495
+ }
496
+ ```
497
+ ### ๐Ÿ”„ Using the Function Output in Queries
498
+
499
+ To use the result of a function, reference it in a `query.value` using the syntax: `fn:functionName`.
500
+
501
+ json
502
+
503
+ CopyEdit
504
+
505
+ ```json
506
+ {
507
+ "label":"createdAt",
508
+ "op":"range",
509
+ "value":{
510
+ "from":"fn:getValueFromSale",
511
+ "to":"date(0)"
512
+ },
513
+ "defaultValue":{
514
+ "from":"date(-5d)",
515
+ "to":"date(0)"
516
+ }
517
+ }
518
+ ```
519
+
520
+ ๐Ÿ” In this case, `getValueFromSale(...)` will run **before** the query is constructed, and the result will be injected into the `from` field.
521
+
522
+ ----------
523
+
524
+ ### ๐Ÿงฉ How It Works
525
+
526
+ 1. The engine scans all `functions` in `variables.functions`.
527
+
528
+ 2. For each `query` entry in `variables.queries`, if `value` (or `from`, `to`) starts with `fn:`, it:
529
+
530
+ - Finds the corresponding `function` by name
531
+
532
+ - Resolves its `variables` (supporting nested `:context.paths`)
533
+
534
+ - Executes the declared function like `getValueFromSale(queries, target)`
535
+
536
+ 3. The return value of the function replaces the `fn:` reference.
537
+
538
+
539
+ ----------
540
+
541
+ ### โœ… Benefits
542
+
543
+ - Enables **pre-processing logic** in a declarative way
544
+
545
+ - Keeps rule/query definitions **clean and context-aware**
546
+
547
+ - Promotes **reuse** of custom logic across different rules or queries
548
+
549
+ - Provides **defaultValue** fallback in case of failure
550
+
551
+
552
+ ----------
553
+
554
+ > ๐Ÿ’ก Tip: Functions must be registered and resolvable at runtime by your system. The evaluation engine must be able to map the function name (`getValueFromSale`) to a real implementation.
555
+
556
+
557
+
558
+ ## ๐Ÿงฐ Requirements
559
+
560
+
561
+ * NestJS
562
+
563
+ * RuleEngineModule (compatible expression parser/executor)
564
+
565
+ * Custom service to inject context functions (like `RateLimitingInjectFunctionsUseCase`)
566
+
567
+
568
+ ## ๐Ÿ“Ž Notes
569
+
570
+
571
+ * Rules should be defined and stored externally (e.g., DB, config).
572
+
573
+ * The context extractor paths must match the structure of your handler inputs (e.g., body, params).
574
+
575
+ * Ensure `getFunctions()` returns **async functions** that provide the required values for the expression.
576
+
577
+
578
+
579
+ ## ๐Ÿงพ License
580
+
581
+
582
+
583
+ MIT
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@edirect/rate-limit-module",
3
+ "version": "11.0.40",
4
+ "main": "./dist/src/index.js",
5
+ "types": "./dist/src/index.d.ts",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./dist/src/index.js",
9
+ "default": "./dist/src/index.js",
10
+ "require": "./dist/src/index.js",
11
+ "types": "./dist/src/index.d.ts"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "dependencies": {
19
+ "@edirect/auth": "^11.0.40",
20
+ "@edirect/logger": "^11.0.40",
21
+ "@nestjs/cache-manager": "^3.1.0",
22
+ "@nestjs/config": "^4.0.2",
23
+ "@nestjs/core": "^11.1.12",
24
+ "@nestjs/mongoose": "^11.0.4",
25
+ "@nestjs/swagger": "^11.2.5",
26
+ "cache-manager-redis-store": "^3.0.1",
27
+ "class-transformer": "^0.5.1",
28
+ "class-validator": "^0.14.3",
29
+ "dayjs": "^1.11.19",
30
+ "ioredis": "^5.9.2",
31
+ "jexl": "^2.3.0",
32
+ "mongoose": "^9.1.4",
33
+ "tslib": "^2.8.1",
34
+ "uuid": "^13.0.0",
35
+ "@nestjs/common": "^11.1.12"
36
+ },
37
+ "devDependencies": {
38
+ "@types/jexl": "^2.3.4"
39
+ },
40
+ "type": "commonjs"
41
+ }
@@ -7,3 +7,4 @@ export interface RateLimitingRuleMetadataOptions {
7
7
  marketPath: string;
8
8
  }
9
9
  export declare const RateLimitingRuleMetadata: (options: RateLimitingRuleMetadataOptions) => import("@nestjs/common").CustomDecorator<string>;
10
+ //# sourceMappingURL=rate-limiting-rule.decorator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limiting-rule.decorator.d.ts","sourceRoot":"","sources":["../../../src/decorators/rate-limiting-rule.decorator.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,gCAAgC,CAAC;AAE3D,MAAM,WAAW,+BAA+B;IAC9C,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,CAAC;IACxB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,eAAO,MAAM,wBAAwB,GACnC,SAAS,+BAA+B,qDACF,CAAC"}
@@ -5,4 +5,3 @@ const common_1 = require("@nestjs/common");
5
5
  exports.RULE_METADATA = 'RATE_LIMITING_RULE_METADATA';
6
6
  const RateLimitingRuleMetadata = (options) => (0, common_1.SetMetadata)(exports.RULE_METADATA, options);
7
7
  exports.RateLimitingRuleMetadata = RateLimitingRuleMetadata;
8
- //# sourceMappingURL=rate-limiting-rule.decorator.js.map
@@ -18,3 +18,4 @@ export declare class RateLimitingRuleGuard implements CanActivate {
18
18
  private _injectFnsResults;
19
19
  private _updateFnsResults;
20
20
  }
21
+ //# sourceMappingURL=rate-limiting.guard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limiting.guard.d.ts","sourceRoot":"","sources":["../../../src/guards/rate-limiting.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,gBAAgB,EAMjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAOzC,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,2CAA2C,CAAC;AAiB9E,qBACa,qBAAsB,YAAW,WAAW;IAIrD,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,WAAW;IAE5B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAN7B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0C;gBAG9C,SAAS,EAAE,SAAS,EACpB,WAAW,EAAE,WAAW,EAExB,UAAU,EAAE,iBAAiB;IAG1C,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IAoH9D,OAAO,CAAC,uBAAuB;IAoB/B,OAAO,CAAC,gBAAgB;IA2BxB,OAAO,CAAC,aAAa;IAsCrB,OAAO,CAAC,iBAAiB;IA4BzB,OAAO,CAAC,cAAc;IAmBtB,OAAO,CAAC,UAAU;YAWJ,iBAAiB;IAiB/B,OAAO,CAAC,iBAAiB;CA4C1B"}