@nest-boot/graphql-rate-limit 7.0.3 → 7.0.4

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.
@@ -1,2 +1,12 @@
1
1
  import { type Complexity } from "@nest-boot/graphql";
2
+ /**
3
+ * Complexity estimator for connection fields (Relay-style pagination).
4
+ *
5
+ * @remarks
6
+ * Multiplies `childComplexity` by the page size (`first` or `last` argument),
7
+ * defaulting to 1 if neither is specified.
8
+ *
9
+ * @param options - The complexity estimator arguments
10
+ * @returns The calculated complexity value
11
+ */
2
12
  export declare const connectionComplexity: Complexity;
@@ -1,6 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.connectionComplexity = void 0;
4
+ /**
5
+ * Complexity estimator for connection fields (Relay-style pagination).
6
+ *
7
+ * @remarks
8
+ * Multiplies `childComplexity` by the page size (`first` or `last` argument),
9
+ * defaulting to 1 if neither is specified.
10
+ *
11
+ * @param options - The complexity estimator arguments
12
+ * @returns The calculated complexity value
13
+ */
4
14
  const connectionComplexity = (options) => {
5
15
  return (Number(options.args.first ?? options.args.last ?? 1) *
6
16
  options.childComplexity);
@@ -1 +1 @@
1
- {"version":3,"file":"connection.complexity.js","sourceRoot":"","sources":["../../src/complexities/connection.complexity.ts"],"names":[],"mappings":";;;AAKO,MAAM,oBAAoB,GAAe,CAC9C,OAAgC,EAChC,EAAE;IACF,OAAO,CACL,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC;QACpD,OAAO,CAAC,eAAe,CACxB,CAAC;AACJ,CAAC,CAAC;AAPW,QAAA,oBAAoB,wBAO/B"}
1
+ {"version":3,"file":"connection.complexity.js","sourceRoot":"","sources":["../../src/complexities/connection.complexity.ts"],"names":[],"mappings":";;;AAKA;;;;;;;;;GASG;AACI,MAAM,oBAAoB,GAAe,CAC9C,OAAgC,EAChC,EAAE;IACF,OAAO,CACL,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC;QACpD,OAAO,CAAC,eAAe,CACxB,CAAC;AACJ,CAAC,CAAC;AAPW,QAAA,oBAAoB,wBAO/B"}
@@ -1,5 +1,13 @@
1
+ /** Options for the `@Complexity` decorator. */
1
2
  export interface ComplexityOptions {
3
+ /** Base complexity value for the field or resolver. */
2
4
  value?: number;
5
+ /** Argument names whose values multiply the complexity (e.g. `["first"]`). */
3
6
  multipliers?: string[];
4
7
  }
8
+ /**
9
+ * Decorator that applies a `@complexity` directive to a GraphQL field or type.
10
+ * @param options - Complexity configuration options
11
+ * @returns A combined method, property, and class decorator
12
+ */
5
13
  export declare function Complexity(options: ComplexityOptions): MethodDecorator & PropertyDecorator & ClassDecorator;
@@ -2,6 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Complexity = Complexity;
4
4
  const graphql_1 = require("@nest-boot/graphql");
5
+ /**
6
+ * Decorator that applies a `@complexity` directive to a GraphQL field or type.
7
+ * @param options - Complexity configuration options
8
+ * @returns A combined method, property, and class decorator
9
+ */
5
10
  function Complexity(options) {
6
11
  return (0, graphql_1.Directive)(`@complexity(value: ${String(options.value ?? 1)}, multipliers: ${JSON.stringify(options.multipliers ?? [])})`);
7
12
  }
@@ -1 +1 @@
1
- {"version":3,"file":"complexity.decorator.js","sourceRoot":"","sources":["../../src/decorators/complexity.decorator.ts"],"names":[],"mappings":";;AAOA,gCAQC;AAfD,gDAA+C;AAO/C,SAAgB,UAAU,CACxB,OAA0B;IAE1B,OAAO,IAAA,mBAAS,EACd,sBAAsB,MAAM,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,kBAAkB,IAAI,CAAC,SAAS,CAC9E,OAAO,CAAC,WAAW,IAAI,EAAE,CAC1B,GAAG,CACL,CAAC;AACJ,CAAC"}
1
+ {"version":3,"file":"complexity.decorator.js","sourceRoot":"","sources":["../../src/decorators/complexity.decorator.ts"],"names":[],"mappings":";;AAeA,gCAQC;AAvBD,gDAA+C;AAU/C;;;;GAIG;AACH,SAAgB,UAAU,CACxB,OAA0B;IAE1B,OAAO,IAAA,mBAAS,EACd,sBAAsB,MAAM,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,CAAC,kBAAkB,IAAI,CAAC,SAAS,CAC9E,OAAO,CAAC,WAAW,IAAI,EAAE,CAC1B,GAAG,CACL,CAAC;AACJ,CAAC"}
@@ -1,3 +1,17 @@
1
+ /** Redis command name used by the rate limiter. */
1
2
  export declare const REDIS_COMMAND = "GRAPHQL_RATE_LIMIT";
3
+ /** Injection token for the resolved GraphQL rate limit options. */
2
4
  export declare const OPTIONS_TOKEN: unique symbol;
3
- export declare const ConfigurableModuleClass: import("@nestjs/common").ConfigurableModuleCls<Partial<import("./interfaces").GraphQLRateLimitOptions>, "forRoot", "create", {}>, MODULE_OPTIONS_TOKEN: string | symbol, OPTIONS_TYPE: Partial<import("./interfaces").GraphQLRateLimitOptions> & Partial<{}>, ASYNC_OPTIONS_TYPE: import("@nestjs/common").ConfigurableModuleAsyncOptions<Partial<import("./interfaces").GraphQLRateLimitOptions>, "create"> & Partial<{}>;
5
+ /**
6
+ * Module definition for the GraphQL rate limit module.
7
+ * @internal
8
+ */
9
+ export declare const
10
+ /** @internal Base configurable module class. */
11
+ ConfigurableModuleClass: import("@nestjs/common").ConfigurableModuleCls<Partial<import("./interfaces").GraphQLRateLimitOptions>, "forRoot", "create", {}>,
12
+ /** @internal Module options injection token. */
13
+ MODULE_OPTIONS_TOKEN: string | symbol,
14
+ /** @internal Synchronous options type. */
15
+ OPTIONS_TYPE: Partial<import("./interfaces").GraphQLRateLimitOptions> & Partial<{}>,
16
+ /** @internal Asynchronous options type. */
17
+ ASYNC_OPTIONS_TYPE: import("@nestjs/common").ConfigurableModuleAsyncOptions<Partial<import("./interfaces").GraphQLRateLimitOptions>, "create"> & Partial<{}>;
@@ -3,9 +3,23 @@ var _a;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.ASYNC_OPTIONS_TYPE = exports.OPTIONS_TYPE = exports.MODULE_OPTIONS_TOKEN = exports.ConfigurableModuleClass = exports.OPTIONS_TOKEN = exports.REDIS_COMMAND = void 0;
5
5
  const common_1 = require("@nestjs/common");
6
+ /** Redis command name used by the rate limiter. */
6
7
  exports.REDIS_COMMAND = "GRAPHQL_RATE_LIMIT";
8
+ /** Injection token for the resolved GraphQL rate limit options. */
7
9
  exports.OPTIONS_TOKEN = Symbol("GraphQLRateLimitOptions");
10
+ /**
11
+ * Module definition for the GraphQL rate limit module.
12
+ * @internal
13
+ */
8
14
  _a = new common_1.ConfigurableModuleBuilder()
9
15
  .setClassMethodName("forRoot")
10
- .build(), exports.ConfigurableModuleClass = _a.ConfigurableModuleClass, exports.MODULE_OPTIONS_TOKEN = _a.MODULE_OPTIONS_TOKEN, exports.OPTIONS_TYPE = _a.OPTIONS_TYPE, exports.ASYNC_OPTIONS_TYPE = _a.ASYNC_OPTIONS_TYPE;
16
+ .build(),
17
+ /** @internal Base configurable module class. */
18
+ exports.ConfigurableModuleClass = _a.ConfigurableModuleClass,
19
+ /** @internal Module options injection token. */
20
+ exports.MODULE_OPTIONS_TOKEN = _a.MODULE_OPTIONS_TOKEN,
21
+ /** @internal Synchronous options type. */
22
+ exports.OPTIONS_TYPE = _a.OPTIONS_TYPE,
23
+ /** @internal Asynchronous options type. */
24
+ exports.ASYNC_OPTIONS_TYPE = _a.ASYNC_OPTIONS_TYPE;
11
25
  //# sourceMappingURL=graphql-rate-limit.module-definition.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"graphql-rate-limit.module-definition.js","sourceRoot":"","sources":["../src/graphql-rate-limit.module-definition.ts"],"names":[],"mappings":";;;;AAAA,2CAA2D;AAI9C,QAAA,aAAa,GAAG,oBAAoB,CAAC;AAErC,QAAA,aAAa,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAElD,KAKT,IAAI,kCAAyB,EAAiC;KAC/D,kBAAkB,CAAC,SAAS,CAAC;KAC7B,KAAK,EAAE,EANR,+BAAuB,+BACvB,4BAAoB,4BACpB,oBAAY,oBACZ,0BAAkB,yBAGT"}
1
+ {"version":3,"file":"graphql-rate-limit.module-definition.js","sourceRoot":"","sources":["../src/graphql-rate-limit.module-definition.ts"],"names":[],"mappings":";;;;AAAA,2CAA2D;AAI3D,mDAAmD;AACtC,QAAA,aAAa,GAAG,oBAAoB,CAAC;AAElD,mEAAmE;AACtD,QAAA,aAAa,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAAC;AAE/D;;;GAGG;AACU,KAST,IAAI,kCAAyB,EAAiC;KAC/D,kBAAkB,CAAC,SAAS,CAAC;KAC7B,KAAK,EAAE;AAVR,gDAAgD;AAChD,+BAAuB;AACvB,gDAAgD;AAChD,4BAAoB;AACpB,0CAA0C;AAC1C,oBAAY;AACZ,2CAA2C;AAC3C,0BAAkB,yBAGT"}
@@ -1,3 +1,23 @@
1
- import { ConfigurableModuleClass } from "./graphql-rate-limit.module-definition";
1
+ import { type DynamicModule } from "@nestjs/common";
2
+ import { ASYNC_OPTIONS_TYPE, ConfigurableModuleClass, OPTIONS_TYPE } from "./graphql-rate-limit.module-definition";
3
+ /**
4
+ * GraphQL rate limiting module using Redis-backed leaky bucket algorithm.
5
+ *
6
+ * @remarks
7
+ * Provides query complexity analysis and rate limiting for GraphQL operations.
8
+ * Uses Redis for distributed rate limit state and supports custom ID extraction.
9
+ */
2
10
  export declare class GraphQLRateLimitModule extends ConfigurableModuleClass {
11
+ /**
12
+ * Registers the GraphQLRateLimitModule with the given options.
13
+ * @param options - Configuration options including rate limit thresholds and Redis connection
14
+ * @returns Dynamic module configuration
15
+ */
16
+ static forRoot(options: typeof OPTIONS_TYPE): DynamicModule;
17
+ /**
18
+ * Registers the GraphQLRateLimitModule asynchronously with factory functions.
19
+ * @param options - Async configuration options
20
+ * @returns Dynamic module configuration
21
+ */
22
+ static forRootAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule;
3
23
  }
@@ -12,7 +12,30 @@ const common_1 = require("@nestjs/common");
12
12
  const graphql_rate_limit_module_definition_1 = require("./graphql-rate-limit.module-definition");
13
13
  const graphql_rate_limit_plugin_1 = require("./graphql-rate-limit.plugin");
14
14
  const graphql_rate_limit_storage_1 = require("./graphql-rate-limit.storage");
15
+ /**
16
+ * GraphQL rate limiting module using Redis-backed leaky bucket algorithm.
17
+ *
18
+ * @remarks
19
+ * Provides query complexity analysis and rate limiting for GraphQL operations.
20
+ * Uses Redis for distributed rate limit state and supports custom ID extraction.
21
+ */
15
22
  let GraphQLRateLimitModule = class GraphQLRateLimitModule extends graphql_rate_limit_module_definition_1.ConfigurableModuleClass {
23
+ /**
24
+ * Registers the GraphQLRateLimitModule with the given options.
25
+ * @param options - Configuration options including rate limit thresholds and Redis connection
26
+ * @returns Dynamic module configuration
27
+ */
28
+ static forRoot(options) {
29
+ return super.forRoot(options);
30
+ }
31
+ /**
32
+ * Registers the GraphQLRateLimitModule asynchronously with factory functions.
33
+ * @param options - Async configuration options
34
+ * @returns Dynamic module configuration
35
+ */
36
+ static forRootAsync(options) {
37
+ return super.forRootAsync(options);
38
+ }
16
39
  };
17
40
  exports.GraphQLRateLimitModule = GraphQLRateLimitModule;
18
41
  exports.GraphQLRateLimitModule = GraphQLRateLimitModule = __decorate([
@@ -1 +1 @@
1
- {"version":3,"file":"graphql-rate-limit.module.js","sourceRoot":"","sources":["../src/graphql-rate-limit.module.ts"],"names":[],"mappings":";;;;;;;;;AACA,4CAA+C;AAC/C,2CAAgD;AAGhD,iGAIgD;AAChD,2EAAqE;AACrE,6EAAuE;AAkDhE,IAAM,sBAAsB,GAA5B,MAAM,sBAAuB,SAAQ,8DAAuB;CAAG,CAAA;AAAzD,wDAAsB;iCAAtB,sBAAsB;IA5ClC,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC;QACN,OAAO,EAAE;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O,EAAE,KAAK;4BACd,CAAC,CAAC,IAAwC,EAAE,EAAE;gCAC5C,MAAM,GAAG,GAAI,IAAI,CAAC,YAAiC,CAAC,GAAG,CAAC;gCACxD,MAAM,EAAE,GAAG,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC;gCAEhD,IAAI,OAAO,EAAE,KAAK,WAAW,EAAE,CAAC;oCAC9B,MAAM,IAAI,KAAK,CACb,8LAA8L,CAC/L,CAAC;gCACJ,CAAC;gCAED,OAAO,EAAE,CAAC;4BACZ,CAAC,CAAC;qBACL,CAAC;gBACJ,CAAC;aACF;SACF;QACD,OAAO,EAAE,CAAC,oDAAa,CAAC;KACzB,CAAC;GACW,sBAAsB,CAoBlC"}
@@ -23,19 +23,19 @@ function shopifyEstimator(args, type) {
23
23
  if (type instanceof graphql_2.GraphQLNonNull || type instanceof graphql_2.GraphQLList) {
24
24
  return shopifyEstimator(args, type.ofType);
25
25
  }
26
- // GraphQL Connection 表示的是一对多的关系,Connection 的消耗是两点加上要返回的对象数量。
26
+ // A GraphQL Connection represents a one-to-many relationship. The cost is two points plus the number of objects to return.
27
27
  if (type instanceof graphql_2.GraphQLObjectType && type.name.endsWith("Connection")) {
28
28
  return 2 + args.childComplexity * (args.args.first ?? args.args.last ?? 0);
29
29
  }
30
- // Object 是查询的基本单位,一般代码一个单次的 server 端操作,可以是一次数据库查询,也可以一次内部服务的访问。
31
- // Interface Union Object 类似,只不过是能返回不同类型的 object,所以算一点。
30
+ // An Object is the basic unit of a query, generally representing a single server-side operation such as a database query or an internal service call.
31
+ // Interface and Union are similar to Object but can return different types of objects, so they cost one point.
32
32
  if (type instanceof graphql_2.GraphQLObjectType ||
33
33
  type instanceof graphql_2.GraphQLInterfaceType ||
34
34
  type instanceof graphql_2.GraphQLUnionType) {
35
35
  return 1 + args.childComplexity;
36
36
  }
37
- // Scalar Enum Object 本身的一部分,在 Object 里我们已经算过消耗了。
38
- // Scalar Enum 其实就是 Object 上的某个字段,一个 Object 上多返回几个字段消耗是比较少的。
37
+ // Scalar and Enum are part of the Object itself; we've already counted their cost in the Object.
38
+ // They are just fields on an Object, and returning a few extra fields costs relatively little.
39
39
  if (type instanceof graphql_2.GraphQLScalarType || type instanceof graphql_2.GraphQLEnumType) {
40
40
  return 0;
41
41
  }
@@ -1 +1 @@
1
- {"version":3,"file":"graphql-rate-limit.plugin.js","sourceRoot":"","sources":["../src/graphql-rate-limit.plugin.ts"],"names":[],"mappings":";AAAA,qDAAqD;;;;;;;;;;;;AAQrD,gDAAuD;AACvD,2CAAwC;AACxC,2CAA+C;AAE/C,qCASiB;AACjB,uEAOkC;AAElC,6EAAuE;AAGvE,sFAAsF;AACtF,SAAS,gBAAgB,CACvB,IAA6B,EAC7B,IAAkB;IAElB,IAAI,GAAG,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;IAE/B,IAAI,IAAI,YAAY,wBAAc,IAAI,IAAI,YAAY,qBAAW,EAAE,CAAC;QAClE,OAAO,gBAAgB,CAAC,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IAC7C,CAAC;IAED,+DAA+D;IAC/D,IAAI,IAAI,YAAY,2BAAiB,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;QAC1E,OAAO,CAAC,GAAG,IAAI,CAAC,eAAe,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC;IAC7E,CAAC;IAED,gEAAgE;IAChE,2DAA2D;IAC3D,IACE,IAAI,YAAY,2BAAiB;QACjC,IAAI,YAAY,8BAAoB;QACpC,IAAI,YAAY,0BAAgB,EAChC,CAAC;QACD,OAAO,CAAC,GAAG,IAAI,CAAC,eAAe,CAAC;IAClC,CAAC;IAED,qDA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AS,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC;wCACvD,IAAI,CAAC,eAAe,IAAI,SAAS,CAAC;wCAClC,MAAM;oCACR,CAAC;gCACH,CAAC;4BACH,CAAC;wBACH,CAAC,CAAC;oBACJ,CAAC;iBACF,CAAC;YACJ,CAAC;YACD,gBAAgB,EAAE,KAAK,EAAE,OAAO,EAAiB,EAAE;gBACjD,IACE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,KAAK,QAAQ;oBACvC,MAAM,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,YAAY,EAC5C,CAAC;oBACD,MAAM,EAAE,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,WAAW,EAAE,GAClE,MAAM,IAAI,CAAC,OAAO,CAAC,QAAQ,CACzB,OAAO,EACP,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC,eAAe,CAC/C,CAAC;oBAEJ,IAAI,CAAC,cAAc,GAAG;wBACpB,OAAO;wBACP,gBAAgB;wBAChB,kBAAkB;wBAClB,WAAW;qBACZ,CAAC;oBAEF,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,UAAU,GAAG;wBAC9C,GAAG,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,UAAU;wBAChD,IAAI;qBACL,CAAC;gBACJ,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAA;AA5HY,wDAAsB;iCAAtB,sBAAsB;IADlC,IAAA,eAAM,GAAE;qCAeqB,oDAAuB;QACjB,2BAAiB;GAfxC,sBAAsB,CA4HlC"}
@@ -26,47 +26,47 @@ let GraphQLRateLimitStorage = class GraphQLRateLimitStorage {
26
26
  this.redis.defineCommand(graphql_rate_limit_module_definition_1.REDIS_COMMAND, {
27
27
  numberOfKeys: 5,
28
28
  lua: /* lua */ `
29
- -- 获取当前时间戳
29
+ -- Get current timestamp
30
30
  local currentTimestamp = redis.call("TIME")[1]
31
31
 
32
- -- 获取参数
32
+ -- Get arguments
33
33
  local bucketKeyPrefix = KEYS[1]
34
34
  local maximumAvailable = tonumber(KEYS[2])
35
35
  local restoreRate = tonumber(KEYS[3])
36
36
  local id = KEYS[4]
37
37
  local complexity = tonumber(KEYS[5])
38
38
 
39
- -- 定义存储桶的键
39
+ -- Define the bucket key
40
40
  local bucketKey = bucketKeyPrefix .. ":" .. id
41
41
 
42
42
  local keyExpireSeconds = math.ceil(maximumAvailable / restoreRate)
43
43
 
44
- -- 获取存储桶中当前的令牌数量,如果不存在,则设置为最大可用令牌数
44
+ -- Get the current number of tokens in the bucket; if it doesn't exist, set to max available
45
45
  local currentlyAvailable = redis.call("HGET", bucketKey, "currentlyAvailable")
46
46
  if not currentlyAvailable then
47
47
  currentlyAvailable = maximumAvailable
48
48
  end
49
49
 
50
- -- 如果存储桶为空,设置上次更新时间戳为当前时间戳
50
+ -- If the bucket is empty, set the last updated timestamp to the current timestamp
51
51
  local updatedTimestamp = redis.call("HGET", bucketKey, "updatedTimestamp")
52
52
  if not updatedTimestamp then
53
53
  updatedTimestamp = currentTimestamp
54
54
  end
55
55
 
56
- -- 更新上次更新的时间戳
56
+ -- Update the last updated timestamp
57
57
  redis.call("HSET", bucketKey, "updatedTimestamp", currentTimestamp)
58
58
 
59
- -- 更新存储桶的过期时间
59
+ -- Update the bucket's expiration time
60
60
  redis.call("EXPIRE", bucketKey, keyExpireSeconds)
61
61
 
62
- -- 计算自上次更新以来要恢复的令牌数量,并恢复存储桶中的令牌数量
62
+ -- Calculate the number of tokens to restore since the last update, and restore tokens in the bucket
63
63
  local intervalSeconds = currentTimestamp - updatedTimestamp;
64
64
  if intervalSeconds > 0 then
65
65
  currentlyAvailable = math.min((restoreRate * intervalSeconds) + currentlyAvailable, maximumAvailable);
66
66
  redis.call("HSET", bucketKey, "currentlyAvailable", currentlyAvailable)
67
67
  end
68
68
 
69
- -- 检查是否有足够的令牌供扣减,如果有,则扣减并返回剩余令牌数
69
+ -- Check if there are enough tokens to deduct; if so, deduct and return the remaining tokens
70
70
  local newCurrentlyAvailable = currentlyAvailable - complexity
71
71
  if newCurrentlyAvailable >= 0 then
72
72
  currentlyAvailable = newCurrentlyAvailable
@@ -1,11 +1,20 @@
1
+ /** Throttle status information for rate-limited queries. */
1
2
  export interface CostThrottleStatus {
3
+ /** Whether the request is currently blocked due to rate limiting. */
2
4
  blocked: boolean;
5
+ /** Maximum cost budget available. */
3
6
  maximumAvailable: number;
7
+ /** Remaining cost budget currently available. */
4
8
  currentlyAvailable: number;
9
+ /** Rate at which cost budget restores (points per second). */
5
10
  restoreRate: number;
6
11
  }
12
+ /** Response containing query cost and throttle status information. */
7
13
  export interface CostResponse {
14
+ /** The estimated cost of the query before execution. */
8
15
  requestedQueryCost: number;
16
+ /** The actual cost of the query after execution. */
9
17
  actualQueryCost: number;
18
+ /** Current throttle status, if rate limiting is active. */
10
19
  throttleStatus?: CostThrottleStatus;
11
20
  }
@@ -1,2 +1,3 @@
1
1
  import { GraphQLRateLimitOptions } from "./graphql-rate-limit-options.interface";
2
+ /** Module-level configuration options for GraphQL rate limiting (all fields optional). */
2
3
  export type GraphQLRateLimitModuleOptions = Partial<GraphQLRateLimitOptions>;
@@ -1,11 +1,19 @@
1
1
  import { BaseContext, GraphQLRequestContext } from "@apollo/server";
2
2
  import { RedisOptions } from "ioredis";
3
+ /** Configuration options for GraphQL rate limiting. */
3
4
  export interface GraphQLRateLimitOptions {
5
+ /** Redis connection options for rate limit state storage. */
4
6
  connection?: RedisOptions;
7
+ /** Maximum allowed query complexity per request. */
5
8
  maxComplexity: number;
9
+ /** Default complexity assigned to fields without explicit `@complexity` directives. */
6
10
  defaultComplexity: number;
11
+ /** Redis key prefix for rate limit state. */
7
12
  keyPrefix: string;
13
+ /** Rate at which cost budget restores (points per second). */
8
14
  restoreRate: number;
15
+ /** Maximum cost budget available per client. */
9
16
  maximumAvailable: number;
17
+ /** Function to extract a unique client identifier from the request context. */
10
18
  getId: (args: GraphQLRequestContext<BaseContext>) => string;
11
19
  }