@envelop/rate-limiter 4.0.0-alpha-d0d0776.0 → 4.0.0-alpha-ccec0fc.0

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
@@ -11,21 +11,21 @@ yarn add @envelop/rate-limiter
11
11
  ## Usage Example
12
12
 
13
13
  ```ts
14
- import { envelop } from '@envelop/core';
15
- import { useRateLimiter, IdentifyFn } from '@envelop/rate-limiter';
14
+ import { envelop } from '@envelop/core'
15
+ import { useRateLimiter, IdentifyFn } from '@envelop/rate-limiter'
16
16
 
17
17
  const identifyFn: IdentifyFn = async context => {
18
- return context.request.ip;
19
- };
18
+ return context.request.ip
19
+ }
20
20
 
21
21
  const getEnveloped = envelop({
22
22
  plugins: [
23
23
  // ... other plugins ...
24
24
  useRateLimiter({
25
- identifyFn,
26
- }),
27
- ],
28
- });
25
+ identifyFn
26
+ })
27
+ ]
28
+ })
29
29
  ```
30
30
 
31
31
  > By default, we assume that you have the GraphQL directive definition as part of your GraphQL schema (`directive @rateLimit(max: Int, window: String, message: String) on FIELD_DEFINITION`).
@@ -1,25 +1,18 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- const graphqlRateLimit = require('graphql-rate-limit');
6
-
7
- function getDirective(info, name) {
8
- const { parentType, fieldName, schema } = info;
9
- const schemaType = schema.getType(parentType.name);
10
- const field = schemaType.getFields()[fieldName];
11
- const astNode = field.astNode;
12
- const rateLimitDirective = astNode?.directives?.find(d => d.name.value === name);
13
- return rateLimitDirective || null;
14
- }
15
-
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useRateLimiter = exports.DIRECTIVE_SDL = exports.UnauthenticatedError = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const utils_js_1 = require("./utils.js");
6
+ const graphql_rate_limit_1 = require("graphql-rate-limit");
7
+ tslib_1.__exportStar(require("./utils.js"), exports);
16
8
  class UnauthenticatedError extends Error {
17
9
  }
18
- const DIRECTIVE_SDL = /* GraphQL */ `
10
+ exports.UnauthenticatedError = UnauthenticatedError;
11
+ exports.DIRECTIVE_SDL = `
19
12
  directive @rateLimit(max: Int, window: String, message: String) on FIELD_DEFINITION
20
13
  `;
21
14
  const useRateLimiter = (options) => {
22
- const rateLimiterFn = graphqlRateLimit.getGraphQLRateLimiter({ identifyContext: options.identifyFn });
15
+ const rateLimiterFn = (0, graphql_rate_limit_1.getGraphQLRateLimiter)({ identifyContext: options.identifyFn });
23
16
  return {
24
17
  async onContextBuilding({ extendContext }) {
25
18
  extendContext({
@@ -27,11 +20,12 @@ const useRateLimiter = (options) => {
27
20
  });
28
21
  },
29
22
  async onResolverCalled({ args, root, context, info }) {
30
- const rateLimitDirectiveNode = getDirective(info, options.rateLimitDirectiveName || 'rateLimit');
23
+ var _a, _b, _c;
24
+ const rateLimitDirectiveNode = (0, utils_js_1.getDirective)(info, options.rateLimitDirectiveName || 'rateLimit');
31
25
  if (rateLimitDirectiveNode && rateLimitDirectiveNode.arguments) {
32
- const maxNode = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'max')?.value;
33
- const windowNode = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'window')?.value;
34
- const messageNode = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'message')?.value;
26
+ const maxNode = (_a = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'max')) === null || _a === void 0 ? void 0 : _a.value;
27
+ const windowNode = (_b = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'window')) === null || _b === void 0 ? void 0 : _b.value;
28
+ const messageNode = (_c = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'message')) === null || _c === void 0 ? void 0 : _c.value;
35
29
  const message = messageNode.value;
36
30
  const max = parseInt(maxNode.value);
37
31
  const window = windowNode.value;
@@ -61,11 +55,7 @@ const useRateLimiter = (options) => {
61
55
  },
62
56
  };
63
57
  };
58
+ exports.useRateLimiter = useRateLimiter;
64
59
  function interpolate(message, args) {
65
60
  return message.replace(/\{{([^)]*)\}}/g, (_, key) => args[key.trim()]);
66
61
  }
67
-
68
- exports.DIRECTIVE_SDL = DIRECTIVE_SDL;
69
- exports.UnauthenticatedError = UnauthenticatedError;
70
- exports.getDirective = getDirective;
71
- exports.useRateLimiter = useRateLimiter;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
package/cjs/utils.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDirective = void 0;
4
+ function getDirective(info, name) {
5
+ var _a;
6
+ const { parentType, fieldName, schema } = info;
7
+ const schemaType = schema.getType(parentType.name);
8
+ const field = schemaType.getFields()[fieldName];
9
+ const astNode = field.astNode;
10
+ const rateLimitDirective = (_a = astNode === null || astNode === void 0 ? void 0 : astNode.directives) === null || _a === void 0 ? void 0 : _a.find(d => d.name.value === name);
11
+ return rateLimitDirective || null;
12
+ }
13
+ exports.getDirective = getDirective;
@@ -1,20 +1,12 @@
1
+ import { getDirective } from './utils.js';
1
2
  import { getGraphQLRateLimiter } from 'graphql-rate-limit';
2
-
3
- function getDirective(info, name) {
4
- const { parentType, fieldName, schema } = info;
5
- const schemaType = schema.getType(parentType.name);
6
- const field = schemaType.getFields()[fieldName];
7
- const astNode = field.astNode;
8
- const rateLimitDirective = astNode?.directives?.find(d => d.name.value === name);
9
- return rateLimitDirective || null;
3
+ export * from './utils.js';
4
+ export class UnauthenticatedError extends Error {
10
5
  }
11
-
12
- class UnauthenticatedError extends Error {
13
- }
14
- const DIRECTIVE_SDL = /* GraphQL */ `
6
+ export const DIRECTIVE_SDL = /* GraphQL */ `
15
7
  directive @rateLimit(max: Int, window: String, message: String) on FIELD_DEFINITION
16
8
  `;
17
- const useRateLimiter = (options) => {
9
+ export const useRateLimiter = (options) => {
18
10
  const rateLimiterFn = getGraphQLRateLimiter({ identifyContext: options.identifyFn });
19
11
  return {
20
12
  async onContextBuilding({ extendContext }) {
@@ -23,11 +15,12 @@ const useRateLimiter = (options) => {
23
15
  });
24
16
  },
25
17
  async onResolverCalled({ args, root, context, info }) {
18
+ var _a, _b, _c;
26
19
  const rateLimitDirectiveNode = getDirective(info, options.rateLimitDirectiveName || 'rateLimit');
27
20
  if (rateLimitDirectiveNode && rateLimitDirectiveNode.arguments) {
28
- const maxNode = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'max')?.value;
29
- const windowNode = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'window')?.value;
30
- const messageNode = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'message')?.value;
21
+ const maxNode = (_a = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'max')) === null || _a === void 0 ? void 0 : _a.value;
22
+ const windowNode = (_b = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'window')) === null || _b === void 0 ? void 0 : _b.value;
23
+ const messageNode = (_c = rateLimitDirectiveNode.arguments.find(arg => arg.name.value === 'message')) === null || _c === void 0 ? void 0 : _c.value;
31
24
  const message = messageNode.value;
32
25
  const max = parseInt(maxNode.value);
33
26
  const window = windowNode.value;
@@ -60,5 +53,3 @@ const useRateLimiter = (options) => {
60
53
  function interpolate(message, args) {
61
54
  return message.replace(/\{{([^)]*)\}}/g, (_, key) => args[key.trim()]);
62
55
  }
63
-
64
- export { DIRECTIVE_SDL, UnauthenticatedError, getDirective, useRateLimiter };
package/esm/utils.js ADDED
@@ -0,0 +1,9 @@
1
+ export function getDirective(info, name) {
2
+ var _a;
3
+ const { parentType, fieldName, schema } = info;
4
+ const schemaType = schema.getType(parentType.name);
5
+ const field = schemaType.getFields()[fieldName];
6
+ const astNode = field.astNode;
7
+ const rateLimitDirective = (_a = astNode === null || astNode === void 0 ? void 0 : astNode.directives) === null || _a === void 0 ? void 0 : _a.find(d => d.name.value === name);
8
+ return rateLimitDirective || null;
9
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@envelop/rate-limiter",
3
- "version": "4.0.0-alpha-d0d0776.0",
3
+ "version": "4.0.0-alpha-ccec0fc.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "3.0.0-alpha-d0d0776.0",
7
- "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
6
+ "@envelop/core": "3.0.0-alpha-ccec0fc.0",
7
+ "@graphql-tools/graphql": "0.1.0-alpha-e7752ba5.0"
8
8
  },
9
9
  "dependencies": {
10
10
  "graphql-rate-limit": "3.3.0"
@@ -16,33 +16,42 @@
16
16
  },
17
17
  "author": "Dotan Simha <dotansimha@gmail.com>",
18
18
  "license": "MIT",
19
- "main": "index.js",
20
- "module": "index.mjs",
21
- "typings": "index.d.ts",
19
+ "main": "cjs/index.js",
20
+ "module": "esm/index.js",
21
+ "typings": "typings/index.d.ts",
22
22
  "typescript": {
23
- "definition": "index.d.ts"
23
+ "definition": "typings/index.d.ts"
24
24
  },
25
+ "type": "module",
25
26
  "exports": {
26
27
  ".": {
27
28
  "require": {
28
- "default": "./index.js",
29
- "types": "./index.d.ts"
29
+ "types": "./typings/index.d.ts",
30
+ "default": "./cjs/index.js"
30
31
  },
31
32
  "import": {
32
- "default": "./index.mjs",
33
- "types": "./index.d.ts"
33
+ "types": "./typings/index.d.ts",
34
+ "default": "./esm/index.js"
35
+ },
36
+ "default": {
37
+ "types": "./typings/index.d.ts",
38
+ "default": "./esm/index.js"
34
39
  }
35
40
  },
36
41
  "./*": {
37
42
  "require": {
38
- "default": "./*.js",
39
- "types": "./*.d.ts"
43
+ "types": "./typings/*.d.ts",
44
+ "default": "./cjs/*.js"
40
45
  },
41
46
  "import": {
42
- "default": "./*.mjs",
43
- "types": "./*.d.ts"
47
+ "types": "./typings/*.d.ts",
48
+ "default": "./esm/*.js"
49
+ },
50
+ "default": {
51
+ "types": "./typings/*.d.ts",
52
+ "default": "./esm/*.js"
44
53
  }
45
54
  },
46
55
  "./package.json": "./package.json"
47
56
  }
48
- }
57
+ }
@@ -1,7 +1,7 @@
1
1
  import { Plugin } from '@envelop/core';
2
- import { GraphQLResolveInfo } from 'graphql';
2
+ import { GraphQLResolveInfo } from '@graphql-tools/graphql';
3
3
  import { getGraphQLRateLimiter } from 'graphql-rate-limit';
4
- export * from './utils';
4
+ export * from './utils.js';
5
5
  export declare class UnauthenticatedError extends Error {
6
6
  }
7
7
  export declare type IdentifyFn<ContextType = unknown> = (context: ContextType) => string;
@@ -1,2 +1,2 @@
1
- import { DirectiveNode, GraphQLResolveInfo } from 'graphql';
1
+ import { DirectiveNode, GraphQLResolveInfo } from '@graphql-tools/graphql';
2
2
  export declare function getDirective(info: GraphQLResolveInfo, name: string): null | DirectiveNode;