@limitkit/core 1.0.1 → 1.0.2
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 +19 -20
- package/dist/index.d.mts +13 -7
- package/dist/index.d.ts +13 -7
- package/dist/index.js +10 -4
- package/dist/index.mjs +10 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -200,35 +200,34 @@ In the snippet below, assuming the `/report` endpoint performs expensive computa
|
|
|
200
200
|
|
|
201
201
|
```ts
|
|
202
202
|
interface RateLimitResult {
|
|
203
|
-
allowed: boolean
|
|
204
|
-
limit: number,
|
|
205
|
-
remaining: number,
|
|
206
|
-
reset: number,
|
|
207
|
-
retryAt?: number
|
|
203
|
+
allowed: boolean;
|
|
208
204
|
failedAt: string | null;
|
|
209
|
-
|
|
205
|
+
rules: IdentifiedRateLimitRuleResult[]
|
|
210
206
|
}
|
|
211
207
|
```
|
|
212
208
|
|
|
213
209
|
| Field | Meaning |
|
|
214
210
|
| ------------ | ---------------------------------- |
|
|
215
211
|
| `allowed` | request permitted or blocked |
|
|
216
|
-
| `limit` | maximum requests allowed |
|
|
217
|
-
| `remaining` | remaining quota |
|
|
218
|
-
| `reset` | timestamp (ms) when quota fully resets |
|
|
219
|
-
| `retryAt` | seconds until next allowed request |
|
|
220
212
|
| `failedAt` | the name of the rule failed, `null` if every rule passes |
|
|
221
|
-
| `
|
|
222
|
-
|
|
223
|
-
When the request is allowed:
|
|
224
|
-
* The `limit` is the **minimum** of all the rules.
|
|
225
|
-
* The `remaining` is the **minimum** of all the rules.
|
|
226
|
-
* The `reset` is the **maximum** of all the rules.
|
|
213
|
+
| `rules` | an array containing the results of all evaluated rules |
|
|
227
214
|
|
|
215
|
+
The result of each evaluated rule is represented as `IdentifiedRateLimitRuleResult` interface:
|
|
228
216
|
|
|
229
217
|
```ts
|
|
230
|
-
interface
|
|
231
|
-
|
|
232
|
-
|
|
218
|
+
interface IdentifiedRateLimitRuleResult {
|
|
219
|
+
allowed: boolean;
|
|
220
|
+
limit: number;
|
|
221
|
+
remaining: number;
|
|
222
|
+
resetAt: number;
|
|
223
|
+
retryAt?: number;
|
|
233
224
|
}
|
|
234
|
-
```
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
| Field | Meaning |
|
|
228
|
+
| ------------ | ---------------------------------- |
|
|
229
|
+
| `allowed` | request permitted or blocked by the rule |
|
|
230
|
+
| `limit` | the maximum number of requests allowed by the rule |
|
|
231
|
+
| `remaining` | the remaining number of requests allowed by the rule |
|
|
232
|
+
| `resetAt` | the Unix timestamp (ms) after which the limit for the rule fully resets |
|
|
233
|
+
| `retryAt` | the Unix timestamp (ms) after which the request is allowed by the rule (`undefined` when allowed) |
|
package/dist/index.d.mts
CHANGED
|
@@ -278,8 +278,8 @@ interface RateLimitConfig<C = unknown> {
|
|
|
278
278
|
/**
|
|
279
279
|
* Core rate limiter implementation that enforces rate limiting rules.
|
|
280
280
|
*
|
|
281
|
-
* The RateLimiter evaluates rules in order and
|
|
282
|
-
*
|
|
281
|
+
* The RateLimiter evaluates rules in order and stops if a rule fails.
|
|
282
|
+
* The request is allowed if every rule passes.
|
|
283
283
|
*
|
|
284
284
|
* Use cases:
|
|
285
285
|
* - API rate limiting (requests per second/minute)
|
|
@@ -305,7 +305,7 @@ interface RateLimitConfig<C = unknown> {
|
|
|
305
305
|
*
|
|
306
306
|
* const result = await limiter.consume({ userId: 'user-123' });
|
|
307
307
|
* if (!result.allowed) {
|
|
308
|
-
* return 429
|
|
308
|
+
* return 429
|
|
309
309
|
* }
|
|
310
310
|
* ```
|
|
311
311
|
* @see Limiter
|
|
@@ -330,8 +330,9 @@ declare class RateLimiter<C = unknown> implements Limiter<C> {
|
|
|
330
330
|
/**
|
|
331
331
|
* Check if a request should be allowed under the configured rate limits.
|
|
332
332
|
*
|
|
333
|
-
* Evaluates each rule in order from left to right.
|
|
334
|
-
* If
|
|
333
|
+
* Evaluates each rule in order from left to right.
|
|
334
|
+
* If a rule fails, remaining rules won't be evaluated and the request is rejected.
|
|
335
|
+
*
|
|
335
336
|
*
|
|
336
337
|
* Each rule resolution (key, cost, policy) can be static or dynamic:
|
|
337
338
|
* - Static: evaluated once and reused
|
|
@@ -340,8 +341,6 @@ declare class RateLimiter<C = unknown> implements Limiter<C> {
|
|
|
340
341
|
*
|
|
341
342
|
*
|
|
342
343
|
* @param ctx - Request context passed to rule resolvers to determine dynamic values.
|
|
343
|
-
* @returns Promise resolving to the rate limit result. If debug mode is enabled,
|
|
344
|
-
* includes details about each evaluated rule and which rule failed (if any).
|
|
345
344
|
*
|
|
346
345
|
* @example
|
|
347
346
|
* ```typescript
|
|
@@ -356,7 +355,14 @@ declare class RateLimiter<C = unknown> implements Limiter<C> {
|
|
|
356
355
|
* }
|
|
357
356
|
* ```
|
|
358
357
|
*
|
|
358
|
+
* @returns {RateLimitResult} an object containing:
|
|
359
|
+
* - `allowed` (boolean): whether the request is allowed
|
|
360
|
+
* - `failedRule` (string): the name of the failed rule, `null` if every rule passes
|
|
361
|
+
* - `rules` ({@link IdentifiedRateLimitRuleResult}): details of all the rules evaluated
|
|
362
|
+
*
|
|
359
363
|
* @throws UndefinedKeyException if the key is empty or undefined
|
|
364
|
+
*
|
|
365
|
+
* @see RateLimitResult
|
|
360
366
|
*/
|
|
361
367
|
consume(ctx: C): Promise<RateLimitResult>;
|
|
362
368
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -278,8 +278,8 @@ interface RateLimitConfig<C = unknown> {
|
|
|
278
278
|
/**
|
|
279
279
|
* Core rate limiter implementation that enforces rate limiting rules.
|
|
280
280
|
*
|
|
281
|
-
* The RateLimiter evaluates rules in order and
|
|
282
|
-
*
|
|
281
|
+
* The RateLimiter evaluates rules in order and stops if a rule fails.
|
|
282
|
+
* The request is allowed if every rule passes.
|
|
283
283
|
*
|
|
284
284
|
* Use cases:
|
|
285
285
|
* - API rate limiting (requests per second/minute)
|
|
@@ -305,7 +305,7 @@ interface RateLimitConfig<C = unknown> {
|
|
|
305
305
|
*
|
|
306
306
|
* const result = await limiter.consume({ userId: 'user-123' });
|
|
307
307
|
* if (!result.allowed) {
|
|
308
|
-
* return 429
|
|
308
|
+
* return 429
|
|
309
309
|
* }
|
|
310
310
|
* ```
|
|
311
311
|
* @see Limiter
|
|
@@ -330,8 +330,9 @@ declare class RateLimiter<C = unknown> implements Limiter<C> {
|
|
|
330
330
|
/**
|
|
331
331
|
* Check if a request should be allowed under the configured rate limits.
|
|
332
332
|
*
|
|
333
|
-
* Evaluates each rule in order from left to right.
|
|
334
|
-
* If
|
|
333
|
+
* Evaluates each rule in order from left to right.
|
|
334
|
+
* If a rule fails, remaining rules won't be evaluated and the request is rejected.
|
|
335
|
+
*
|
|
335
336
|
*
|
|
336
337
|
* Each rule resolution (key, cost, policy) can be static or dynamic:
|
|
337
338
|
* - Static: evaluated once and reused
|
|
@@ -340,8 +341,6 @@ declare class RateLimiter<C = unknown> implements Limiter<C> {
|
|
|
340
341
|
*
|
|
341
342
|
*
|
|
342
343
|
* @param ctx - Request context passed to rule resolvers to determine dynamic values.
|
|
343
|
-
* @returns Promise resolving to the rate limit result. If debug mode is enabled,
|
|
344
|
-
* includes details about each evaluated rule and which rule failed (if any).
|
|
345
344
|
*
|
|
346
345
|
* @example
|
|
347
346
|
* ```typescript
|
|
@@ -356,7 +355,14 @@ declare class RateLimiter<C = unknown> implements Limiter<C> {
|
|
|
356
355
|
* }
|
|
357
356
|
* ```
|
|
358
357
|
*
|
|
358
|
+
* @returns {RateLimitResult} an object containing:
|
|
359
|
+
* - `allowed` (boolean): whether the request is allowed
|
|
360
|
+
* - `failedRule` (string): the name of the failed rule, `null` if every rule passes
|
|
361
|
+
* - `rules` ({@link IdentifiedRateLimitRuleResult}): details of all the rules evaluated
|
|
362
|
+
*
|
|
359
363
|
* @throws UndefinedKeyException if the key is empty or undefined
|
|
364
|
+
*
|
|
365
|
+
* @see RateLimitResult
|
|
360
366
|
*/
|
|
361
367
|
consume(ctx: C): Promise<RateLimitResult>;
|
|
362
368
|
}
|
package/dist/index.js
CHANGED
|
@@ -107,8 +107,9 @@ var RateLimiter = class {
|
|
|
107
107
|
/**
|
|
108
108
|
* Check if a request should be allowed under the configured rate limits.
|
|
109
109
|
*
|
|
110
|
-
* Evaluates each rule in order from left to right.
|
|
111
|
-
* If
|
|
110
|
+
* Evaluates each rule in order from left to right.
|
|
111
|
+
* If a rule fails, remaining rules won't be evaluated and the request is rejected.
|
|
112
|
+
*
|
|
112
113
|
*
|
|
113
114
|
* Each rule resolution (key, cost, policy) can be static or dynamic:
|
|
114
115
|
* - Static: evaluated once and reused
|
|
@@ -117,8 +118,6 @@ var RateLimiter = class {
|
|
|
117
118
|
*
|
|
118
119
|
*
|
|
119
120
|
* @param ctx - Request context passed to rule resolvers to determine dynamic values.
|
|
120
|
-
* @returns Promise resolving to the rate limit result. If debug mode is enabled,
|
|
121
|
-
* includes details about each evaluated rule and which rule failed (if any).
|
|
122
121
|
*
|
|
123
122
|
* @example
|
|
124
123
|
* ```typescript
|
|
@@ -133,7 +132,14 @@ var RateLimiter = class {
|
|
|
133
132
|
* }
|
|
134
133
|
* ```
|
|
135
134
|
*
|
|
135
|
+
* @returns {RateLimitResult} an object containing:
|
|
136
|
+
* - `allowed` (boolean): whether the request is allowed
|
|
137
|
+
* - `failedRule` (string): the name of the failed rule, `null` if every rule passes
|
|
138
|
+
* - `rules` ({@link IdentifiedRateLimitRuleResult}): details of all the rules evaluated
|
|
139
|
+
*
|
|
136
140
|
* @throws UndefinedKeyException if the key is empty or undefined
|
|
141
|
+
*
|
|
142
|
+
* @see RateLimitResult
|
|
137
143
|
*/
|
|
138
144
|
async consume(ctx) {
|
|
139
145
|
const evaluatedRules = [];
|
package/dist/index.mjs
CHANGED
|
@@ -70,8 +70,9 @@ var RateLimiter = class {
|
|
|
70
70
|
/**
|
|
71
71
|
* Check if a request should be allowed under the configured rate limits.
|
|
72
72
|
*
|
|
73
|
-
* Evaluates each rule in order from left to right.
|
|
74
|
-
* If
|
|
73
|
+
* Evaluates each rule in order from left to right.
|
|
74
|
+
* If a rule fails, remaining rules won't be evaluated and the request is rejected.
|
|
75
|
+
*
|
|
75
76
|
*
|
|
76
77
|
* Each rule resolution (key, cost, policy) can be static or dynamic:
|
|
77
78
|
* - Static: evaluated once and reused
|
|
@@ -80,8 +81,6 @@ var RateLimiter = class {
|
|
|
80
81
|
*
|
|
81
82
|
*
|
|
82
83
|
* @param ctx - Request context passed to rule resolvers to determine dynamic values.
|
|
83
|
-
* @returns Promise resolving to the rate limit result. If debug mode is enabled,
|
|
84
|
-
* includes details about each evaluated rule and which rule failed (if any).
|
|
85
84
|
*
|
|
86
85
|
* @example
|
|
87
86
|
* ```typescript
|
|
@@ -96,7 +95,14 @@ var RateLimiter = class {
|
|
|
96
95
|
* }
|
|
97
96
|
* ```
|
|
98
97
|
*
|
|
98
|
+
* @returns {RateLimitResult} an object containing:
|
|
99
|
+
* - `allowed` (boolean): whether the request is allowed
|
|
100
|
+
* - `failedRule` (string): the name of the failed rule, `null` if every rule passes
|
|
101
|
+
* - `rules` ({@link IdentifiedRateLimitRuleResult}): details of all the rules evaluated
|
|
102
|
+
*
|
|
99
103
|
* @throws UndefinedKeyException if the key is empty or undefined
|
|
104
|
+
*
|
|
105
|
+
* @see RateLimitResult
|
|
100
106
|
*/
|
|
101
107
|
async consume(ctx) {
|
|
102
108
|
const evaluatedRules = [];
|