@frontegg/react-hooks 6.117.0-alpha.1 → 6.119.0-alpha.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/auth/entitlements.d.ts +15 -6
- package/auth/entitlements.js +52 -10
- package/auth/hooks.d.ts +1 -1
- package/auth/hooks.js +13 -3
- package/index.js +1 -1
- package/node/auth/entitlements.js +54 -9
- package/node/auth/hooks.js +13 -3
- package/node/index.js +1 -1
- package/package.json +3 -3
package/auth/entitlements.d.ts
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
|
-
import { EntitlementsState, EntitlementsActions,
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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;
|
package/auth/entitlements.js
CHANGED
|
@@ -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
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
18
|
-
|
|
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
|
-
|
|
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
|
-
|
|
119
|
-
|
|
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
|
@@ -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
|
|
18
|
-
|
|
19
|
-
|
|
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
|
|
22
|
+
const useFeatureEntitlements = keys => {
|
|
23
23
|
const {
|
|
24
|
-
entitlements
|
|
25
|
-
|
|
26
|
-
|
|
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;
|
package/node/auth/hooks.js
CHANGED
|
@@ -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
|
-
|
|
135
|
-
|
|
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
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontegg/react-hooks",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.119.0-alpha.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.
|
|
10
|
-
"@frontegg/types": "6.
|
|
9
|
+
"@frontegg/redux-store": "6.119.0-alpha.0",
|
|
10
|
+
"@frontegg/types": "6.119.0-alpha.0",
|
|
11
11
|
"@types/react": "*",
|
|
12
12
|
"get-value": "^3.0.1",
|
|
13
13
|
"react-redux": "^7.x"
|