@envelop/generic-auth 4.3.0-alpha-aca44e1.0 → 5.0.0-alpha-d0d0776.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
@@ -56,7 +56,7 @@ This method is optional; the default method will just verify the value returned
56
56
  ```ts
57
57
  import { ValidateUserFn } from '@envelop/generic-auth';
58
58
 
59
- const validateUser: ValidateUserFn<UserType> = async (user, context) => {
59
+ const validateUser: ValidateUserFn<UserType> = (params) => {
60
60
  // Here you can implement any custom to check if the user is valid and have access to the server.
61
61
  // This method is being triggered in different flows, based on the mode you chose to implement.
62
62
 
@@ -86,7 +86,7 @@ type UserType = {
86
86
  const resolveUserFn: ResolveUserFn<UserType> = async context => {
87
87
  /* ... */
88
88
  };
89
- const validateUser: ValidateUserFn<UserType> = async (user, context) => {
89
+ const validateUser: ValidateUserFn<UserType> = (params) => {
90
90
  /* ... */
91
91
  };
92
92
 
@@ -155,7 +155,7 @@ type UserType = {
155
155
  const resolveUserFn: ResolveUserFn<UserType> = async context => {
156
156
  /* ... */
157
157
  };
158
- const validateUser: ValidateUserFn<UserType> = async (user, context) => {
158
+ const validateUser: ValidateUserFn<UserType> = async (params) => {
159
159
  /* ... */
160
160
  };
161
161
 
@@ -200,7 +200,7 @@ type UserType = {
200
200
  const resolveUserFn: ResolveUserFn<UserType> = async context => {
201
201
  /* ... */
202
202
  };
203
- const validateUser: ValidateUserFn<UserType> = async (user, context) => {
203
+ const validateUser: ValidateUserFn<UserType> = (params) => {
204
204
  /* ... */
205
205
  };
206
206
 
package/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { DefaultContext, Plugin } from '@envelop/core';
1
+ import { DefaultContext, Maybe, Plugin, PromiseOrValue } from '@envelop/core';
2
2
  import { DirectiveNode, FieldNode, GraphQLError, GraphQLObjectType } from 'graphql';
3
3
  export declare class UnauthenticatedError extends GraphQLError {
4
4
  }
5
- export declare type ResolveUserFn<UserType, ContextType = DefaultContext> = (context: ContextType) => null | UserType | Promise<UserType | null>;
5
+ export declare type ResolveUserFn<UserType, ContextType = DefaultContext> = (context: ContextType) => PromiseOrValue<Maybe<UserType>>;
6
6
  export declare type ValidateUserFnParams<UserType> = {
7
7
  /** The user object. */
8
8
  user: UserType;
@@ -18,7 +18,7 @@ export declare type ValidateUserFnParams<UserType> = {
18
18
  export declare type ValidateUserFn<UserType> = (params: ValidateUserFnParams<UserType>) => void | UnauthenticatedError;
19
19
  export declare const DIRECTIVE_SDL = "\n directive @auth on FIELD_DEFINITION\n";
20
20
  export declare const SKIP_AUTH_DIRECTIVE_SDL = "\n directive @skipAuth on FIELD_DEFINITION\n";
21
- export declare type GenericAuthPluginOptions<UserType extends {} = {}, ContextType extends DefaultContext = DefaultContext> = {
21
+ export declare type GenericAuthPluginOptions<UserType extends {} = {}, ContextType = DefaultContext, CurrentUserKey extends string = 'currentUser'> = {
22
22
  /**
23
23
  * Here you can implement any custom sync/async code, and use the context built so far in Envelop and the HTTP request
24
24
  * to find the current user.
@@ -30,7 +30,7 @@ export declare type GenericAuthPluginOptions<UserType extends {} = {}, ContextTy
30
30
  * Overrides the default field name for injecting the user into the execution `context`.
31
31
  * @default currentUser
32
32
  */
33
- contextFieldName?: 'currentUser' | string;
33
+ contextFieldName?: CurrentUserKey;
34
34
  } & ({
35
35
  /**
36
36
  * This mode offers complete protection for the entire API.
@@ -75,6 +75,6 @@ export declare type GenericAuthPluginOptions<UserType extends {} = {}, ContextTy
75
75
  });
76
76
  export declare function defaultProtectAllValidateFn<UserType>(params: ValidateUserFnParams<UserType>): void | UnauthenticatedError;
77
77
  export declare function defaultProtectSingleValidateFn<UserType>(params: ValidateUserFnParams<UserType>): void | UnauthenticatedError;
78
- export declare const useGenericAuth: <UserType extends {} = {}, ContextType extends DefaultContext = DefaultContext>(options: GenericAuthPluginOptions<UserType, ContextType>) => Plugin<{
78
+ export declare const useGenericAuth: <UserType extends {} = {}, ContextType = DefaultContext, CurrentUserKey extends string = "currentUser">(options: GenericAuthPluginOptions<UserType, ContextType, CurrentUserKey>) => Plugin<{
79
79
  validateUser: ValidateUserFn<UserType>;
80
- }>;
80
+ } & Record<CurrentUserKey, UserType>>;
package/index.js CHANGED
@@ -26,16 +26,14 @@ function defaultProtectSingleValidateFn(params) {
26
26
  }
27
27
  }
28
28
  const useGenericAuth = (options) => {
29
- var _a, _b;
30
29
  const contextFieldName = options.contextFieldName || 'currentUser';
31
30
  if (options.mode === 'protect-all' || options.mode === 'protect-granular') {
32
- const directiveOrExtensionFieldName = (_a = options.directiveOrExtensionFieldName) !== null && _a !== void 0 ? _a : (options.mode === 'protect-all' ? 'skipAuth' : 'auth');
33
- const validateUser = (_b = options.validateUser) !== null && _b !== void 0 ? _b : (options.mode === 'protect-all' ? defaultProtectAllValidateFn : defaultProtectSingleValidateFn);
31
+ const directiveOrExtensionFieldName = options.directiveOrExtensionFieldName ?? (options.mode === 'protect-all' ? 'skipAuth' : 'auth');
32
+ const validateUser = options.validateUser ?? (options.mode === 'protect-all' ? defaultProtectAllValidateFn : defaultProtectSingleValidateFn);
34
33
  const extractAuthMeta = (input) => {
35
- var _a, _b, _c;
36
34
  return {
37
- fieldAuthExtension: (_a = input.extensions) === null || _a === void 0 ? void 0 : _a[directiveOrExtensionFieldName],
38
- fieldAuthDirectiveNode: (_c = (_b = input.astNode) === null || _b === void 0 ? void 0 : _b.directives) === null || _c === void 0 ? void 0 : _c.find(directive => directive.name.value === directiveOrExtensionFieldName),
35
+ fieldAuthExtension: input.extensions?.[directiveOrExtensionFieldName],
36
+ fieldAuthDirectiveNode: input.astNode?.directives?.find(directive => directive.name.value === directiveOrExtensionFieldName),
39
37
  };
40
38
  };
41
39
  return {
@@ -96,7 +94,7 @@ const useGenericAuth = (options) => {
96
94
  },
97
95
  };
98
96
  }
99
- else if (options.mode === 'resolve-only') {
97
+ if (options.mode === 'resolve-only') {
100
98
  return {
101
99
  async onContextBuilding({ context, extendContext }) {
102
100
  const user = await options.resolveUserFn(context);
package/index.mjs CHANGED
@@ -22,16 +22,14 @@ function defaultProtectSingleValidateFn(params) {
22
22
  }
23
23
  }
24
24
  const useGenericAuth = (options) => {
25
- var _a, _b;
26
25
  const contextFieldName = options.contextFieldName || 'currentUser';
27
26
  if (options.mode === 'protect-all' || options.mode === 'protect-granular') {
28
- const directiveOrExtensionFieldName = (_a = options.directiveOrExtensionFieldName) !== null && _a !== void 0 ? _a : (options.mode === 'protect-all' ? 'skipAuth' : 'auth');
29
- const validateUser = (_b = options.validateUser) !== null && _b !== void 0 ? _b : (options.mode === 'protect-all' ? defaultProtectAllValidateFn : defaultProtectSingleValidateFn);
27
+ const directiveOrExtensionFieldName = options.directiveOrExtensionFieldName ?? (options.mode === 'protect-all' ? 'skipAuth' : 'auth');
28
+ const validateUser = options.validateUser ?? (options.mode === 'protect-all' ? defaultProtectAllValidateFn : defaultProtectSingleValidateFn);
30
29
  const extractAuthMeta = (input) => {
31
- var _a, _b, _c;
32
30
  return {
33
- fieldAuthExtension: (_a = input.extensions) === null || _a === void 0 ? void 0 : _a[directiveOrExtensionFieldName],
34
- fieldAuthDirectiveNode: (_c = (_b = input.astNode) === null || _b === void 0 ? void 0 : _b.directives) === null || _c === void 0 ? void 0 : _c.find(directive => directive.name.value === directiveOrExtensionFieldName),
31
+ fieldAuthExtension: input.extensions?.[directiveOrExtensionFieldName],
32
+ fieldAuthDirectiveNode: input.astNode?.directives?.find(directive => directive.name.value === directiveOrExtensionFieldName),
35
33
  };
36
34
  };
37
35
  return {
@@ -92,7 +90,7 @@ const useGenericAuth = (options) => {
92
90
  },
93
91
  };
94
92
  }
95
- else if (options.mode === 'resolve-only') {
93
+ if (options.mode === 'resolve-only') {
96
94
  return {
97
95
  async onContextBuilding({ context, extendContext }) {
98
96
  const user = await options.resolveUserFn(context);
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@envelop/generic-auth",
3
- "version": "4.3.0-alpha-aca44e1.0",
3
+ "version": "5.0.0-alpha-d0d0776.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "2.4.0-alpha-aca44e1.0",
6
+ "@envelop/core": "3.0.0-alpha-d0d0776.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {
10
- "@envelop/extended-validation": "1.7.0-alpha-aca44e1.0"
10
+ "@envelop/extended-validation": "2.0.0-alpha-d0d0776.0"
11
11
  },
12
12
  "repository": {
13
13
  "type": "git",
14
- "url": "https://github.com/dotansimha/envelop.git",
14
+ "url": "https://github.com/n1ru4l/envelop.git",
15
15
  "directory": "packages/plugins/generic-auth"
16
16
  },
17
17
  "author": "Dotan Simha <dotansimha@gmail.com>",
@@ -24,12 +24,24 @@
24
24
  },
25
25
  "exports": {
26
26
  ".": {
27
- "require": "./index.js",
28
- "import": "./index.mjs"
27
+ "require": {
28
+ "default": "./index.js",
29
+ "types": "./index.d.ts"
30
+ },
31
+ "import": {
32
+ "default": "./index.mjs",
33
+ "types": "./index.d.ts"
34
+ }
29
35
  },
30
36
  "./*": {
31
- "require": "./*.js",
32
- "import": "./*.mjs"
37
+ "require": {
38
+ "default": "./*.js",
39
+ "types": "./*.d.ts"
40
+ },
41
+ "import": {
42
+ "default": "./*.mjs",
43
+ "types": "./*.d.ts"
44
+ }
33
45
  },
34
46
  "./package.json": "./package.json"
35
47
  }