@frontegg/react-hooks 6.117.0-alpha.1 → 6.118.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.
@@ -1,11 +1,20 @@
1
- import { EntitlementsState, EntitlementsActions, Entitlements } from '@frontegg/redux-store';
1
+ import { EntitlementsState, EntitlementsActions, Entitlement, EntitledToOptions } from '@frontegg/redux-store';
2
2
  export declare type EntitlementsStateMapper<S> = (state: EntitlementsState) => S;
3
3
  export declare function useEntitlementsState(): EntitlementsState;
4
4
  export declare function useEntitlementsState<S>(stateMapper: EntitlementsStateMapper<S>): S;
5
5
  export declare const useEntitlementsActions: () => EntitlementsActions;
6
6
  /**
7
- @param keys The requested features entitlements
8
-
9
- Returns Entitlements contains true/false for every key (state of is key entitled)
10
- */
11
- export declare const useEntitlements: (keys: string[]) => Entitlements;
7
+ @param keys
8
+ @returns if the user is entitled to the given feature. Attaching the justification if not
9
+ */
10
+ export declare const useFeatureEntitlements: (keys: string) => Entitlement;
11
+ /**
12
+ @param key
13
+ @returns if the user is entitled to the given permission. Attaching the justification if not
14
+ */
15
+ export declare const usePermissionEntitlements: (key: string) => Entitlement;
16
+ /**
17
+ @param entitledToOptions including permission or feature key
18
+ @returns if the user is entitled to the given feature or permission (check only one). Attaching the justification if not
19
+ */
20
+ export declare const useEntitlements: (options: EntitledToOptions) => Entitlement;
@@ -1,19 +1,61 @@
1
- import { entitlementsActions, entitlementsReducers, getEntitlements } from '@frontegg/redux-store';
1
+ import { entitlementsActions, entitlementsReducers, getPermissionEntitlements, getFeatureEntitlements, getEntitlements } from '@frontegg/redux-store';
2
2
  import { reducerActionsGenerator, stateHookGenerator } from './hooks';
3
3
  const defaultMapper = state => state;
4
4
  export function useEntitlementsState(stateMapper = defaultMapper) {
5
- return stateHookGenerator(stateMapper, 'entitlementsState');
5
+ return stateHookGenerator(stateMapper, 'entitlementsState', true);
6
6
  }
7
7
  export const useEntitlementsActions = () => reducerActionsGenerator(entitlementsActions, entitlementsReducers);
8
8
 
9
9
  /**
10
- @param keys The requested features entitlements
11
-
12
- Returns Entitlements contains true/false for every key (state of is key entitled)
13
- */
14
- export const useEntitlements = keys => {
10
+ @param keys
11
+ @returns if the user is entitled to the given feature. Attaching the justification if not
12
+ */
13
+ export const useFeatureEntitlements = keys => {
15
14
  const {
16
- entitlements
17
- } = useEntitlementsState();
18
- return getEntitlements(entitlements, keys);
15
+ entitlements,
16
+ options
17
+ } = useEntitlementsState(({
18
+ entitlements,
19
+ options
20
+ }) => ({
21
+ entitlements,
22
+ options
23
+ }));
24
+ return getFeatureEntitlements(entitlements, keys, options);
25
+ };
26
+
27
+ /**
28
+ @param key
29
+ @returns if the user is entitled to the given permission. Attaching the justification if not
30
+ */
31
+ export const usePermissionEntitlements = key => {
32
+ const {
33
+ entitlements,
34
+ options
35
+ } = useEntitlementsState(({
36
+ entitlements,
37
+ options
38
+ }) => ({
39
+ entitlements,
40
+ options
41
+ }));
42
+ return getPermissionEntitlements(entitlements, key, options);
43
+ };
44
+
45
+ /**
46
+ @param entitledToOptions including permission or feature key
47
+ @returns if the user is entitled to the given feature or permission (check only one). Attaching the justification if not
48
+ */
49
+ export const useEntitlements = options => {
50
+ const {
51
+ entitlements,
52
+ options: entitlementsVendorConfig
53
+ } = useEntitlementsState(({
54
+ entitlements,
55
+ options
56
+ }) => ({
57
+ entitlements,
58
+ options
59
+ }));
60
+ return getEntitlements(entitlements, options, entitlementsVendorConfig);
19
61
  };
package/auth/hooks.d.ts CHANGED
@@ -58,5 +58,5 @@ export declare const useAuthUserOrNull: () => User | null;
58
58
  * hooks helpers
59
59
  */
60
60
  export declare const sliceReducerActionsBy: <T extends SliceCaseReducers<any>>(reducer: T) => CaseReducerActions<T>;
61
- export declare const stateHookGenerator: (stateMapper: any, subState: keyof AuthState) => any;
61
+ export declare const stateHookGenerator: (stateMapper: any, subState: keyof AuthState, enableMapperFalsyReturnValue?: boolean) => any;
62
62
  export declare const reducerActionsGenerator: (actions: any, reducers: SliceCaseReducers<any>) => any;
package/auth/hooks.js CHANGED
@@ -113,10 +113,20 @@ export const sliceReducerActionsBy = reducer => {
113
113
  }));
114
114
  return reducerActions.reduce((p, n) => _extends({}, p, n), {});
115
115
  };
116
- export const stateHookGenerator = (stateMapper, subState) => {
116
+
117
+ /*
118
+ * enableMapperFalsyReturnValue was added to be backward compatible with all usages
119
+ * the scenario that require enableMapperFalsyReturnValue of true is when the
120
+ * mapper function returns a falsy value then we want to return it as is - it's a valid case like in entitlements (when it's undefined)
121
+ * IMO all usages should be like that
122
+ */
123
+ export const stateHookGenerator = (stateMapper, subState, enableMapperFalsyReturnValue = false) => {
117
124
  return useSelector(state => {
118
- var _stateMapper;
119
- return (_stateMapper = stateMapper == null ? void 0 : stateMapper(state[authStoreName][subState])) != null ? _stateMapper : state[authStoreName][subState];
125
+ const mapperValue = stateMapper == null ? void 0 : stateMapper(state[authStoreName][subState]);
126
+ if (enableMapperFalsyReturnValue && stateMapper) {
127
+ return mapperValue;
128
+ }
129
+ return mapperValue != null ? mapperValue : state[authStoreName][subState];
120
130
  }, shallowEqual);
121
131
  };
122
132
  export const reducerActionsGenerator = (actions, reducers) => {
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license Frontegg v6.117.0-alpha.1
1
+ /** @license Frontegg v6.118.0
2
2
  *
3
3
  * This source code is licensed under the MIT license found in the
4
4
  * LICENSE file in the root directory of this source tree.
@@ -5,24 +5,69 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.useEntitlementsActions = exports.useEntitlements = void 0;
7
7
  exports.useEntitlementsState = useEntitlementsState;
8
+ exports.usePermissionEntitlements = exports.useFeatureEntitlements = void 0;
8
9
  var _reduxStore = require("@frontegg/redux-store");
9
10
  var _hooks = require("./hooks");
10
11
  const defaultMapper = state => state;
11
12
  function useEntitlementsState(stateMapper = defaultMapper) {
12
- return (0, _hooks.stateHookGenerator)(stateMapper, 'entitlementsState');
13
+ return (0, _hooks.stateHookGenerator)(stateMapper, 'entitlementsState', true);
13
14
  }
14
15
  const useEntitlementsActions = () => (0, _hooks.reducerActionsGenerator)(_reduxStore.entitlementsActions, _reduxStore.entitlementsReducers);
15
16
 
16
17
  /**
17
- @param keys The requested features entitlements
18
-
19
- Returns Entitlements contains true/false for every key (state of is key entitled)
20
- */
18
+ @param keys
19
+ @returns if the user is entitled to the given feature. Attaching the justification if not
20
+ */
21
21
  exports.useEntitlementsActions = useEntitlementsActions;
22
- const useEntitlements = keys => {
22
+ const useFeatureEntitlements = keys => {
23
23
  const {
24
- entitlements
25
- } = useEntitlementsState();
26
- return (0, _reduxStore.getEntitlements)(entitlements, keys);
24
+ entitlements,
25
+ options
26
+ } = useEntitlementsState(({
27
+ entitlements,
28
+ options
29
+ }) => ({
30
+ entitlements,
31
+ options
32
+ }));
33
+ return (0, _reduxStore.getFeatureEntitlements)(entitlements, keys, options);
34
+ };
35
+
36
+ /**
37
+ @param key
38
+ @returns if the user is entitled to the given permission. Attaching the justification if not
39
+ */
40
+ exports.useFeatureEntitlements = useFeatureEntitlements;
41
+ const usePermissionEntitlements = key => {
42
+ const {
43
+ entitlements,
44
+ options
45
+ } = useEntitlementsState(({
46
+ entitlements,
47
+ options
48
+ }) => ({
49
+ entitlements,
50
+ options
51
+ }));
52
+ return (0, _reduxStore.getPermissionEntitlements)(entitlements, key, options);
53
+ };
54
+
55
+ /**
56
+ @param entitledToOptions including permission or feature key
57
+ @returns if the user is entitled to the given feature or permission (check only one). Attaching the justification if not
58
+ */
59
+ exports.usePermissionEntitlements = usePermissionEntitlements;
60
+ const useEntitlements = options => {
61
+ const {
62
+ entitlements,
63
+ options: entitlementsVendorConfig
64
+ } = useEntitlementsState(({
65
+ entitlements,
66
+ options
67
+ }) => ({
68
+ entitlements,
69
+ options
70
+ }));
71
+ return (0, _reduxStore.getEntitlements)(entitlements, options, entitlementsVendorConfig);
27
72
  };
28
73
  exports.useEntitlements = useEntitlements;
@@ -128,11 +128,21 @@ const sliceReducerActionsBy = reducer => {
128
128
  }));
129
129
  return reducerActions.reduce((p, n) => (0, _extends2.default)({}, p, n), {});
130
130
  };
131
+
132
+ /*
133
+ * enableMapperFalsyReturnValue was added to be backward compatible with all usages
134
+ * the scenario that require enableMapperFalsyReturnValue of true is when the
135
+ * mapper function returns a falsy value then we want to return it as is - it's a valid case like in entitlements (when it's undefined)
136
+ * IMO all usages should be like that
137
+ */
131
138
  exports.sliceReducerActionsBy = sliceReducerActionsBy;
132
- const stateHookGenerator = (stateMapper, subState) => {
139
+ const stateHookGenerator = (stateMapper, subState, enableMapperFalsyReturnValue = false) => {
133
140
  return (0, _FronteggStoreContext.useSelector)(state => {
134
- var _stateMapper;
135
- return (_stateMapper = stateMapper == null ? void 0 : stateMapper(state[_reduxStore.authStoreName][subState])) != null ? _stateMapper : state[_reduxStore.authStoreName][subState];
141
+ const mapperValue = stateMapper == null ? void 0 : stateMapper(state[_reduxStore.authStoreName][subState]);
142
+ if (enableMapperFalsyReturnValue && stateMapper) {
143
+ return mapperValue;
144
+ }
145
+ return mapperValue != null ? mapperValue : state[_reduxStore.authStoreName][subState];
136
146
  }, _reactRedux.shallowEqual);
137
147
  };
138
148
  exports.stateHookGenerator = stateHookGenerator;
package/node/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license Frontegg v6.117.0-alpha.1
1
+ /** @license Frontegg v6.118.0
2
2
  *
3
3
  * This source code is licensed under the MIT license found in the
4
4
  * LICENSE file in the root directory of this source tree.
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@frontegg/react-hooks",
3
- "version": "6.117.0-alpha.1",
3
+ "version": "6.118.0",
4
4
  "main": "./node/index.js",
5
5
  "license": "MIT",
6
6
  "author": "Frontegg LTD",
7
7
  "dependencies": {
8
8
  "@babel/runtime": "^7.18.6",
9
- "@frontegg/redux-store": "6.117.0-alpha.1",
10
- "@frontegg/types": "6.117.0-alpha.1",
9
+ "@frontegg/redux-store": "6.118.0",
10
+ "@frontegg/types": "6.118.0",
11
11
  "@types/react": "*",
12
12
  "get-value": "^3.0.1",
13
13
  "react-redux": "^7.x"