@frontegg/react-hooks 6.148.0 → 6.150.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 +14 -10
- package/auth/entitlements.js +74 -17
- package/index.js +1 -1
- package/node/auth/entitlements.js +73 -16
- package/node/index.js +1 -1
- package/package.json +3 -3
package/auth/entitlements.d.ts
CHANGED
|
@@ -1,31 +1,35 @@
|
|
|
1
1
|
import { Entitlement, EntitledToOptions } from '@frontegg/redux-store';
|
|
2
|
+
import { CustomAttributes } from '@frontegg/entitlements-javascript-commons';
|
|
2
3
|
/**
|
|
3
|
-
@param key
|
|
4
|
-
@
|
|
4
|
+
@param key feature key
|
|
5
|
+
@param customAttributes user attributes
|
|
6
|
+
@returns if the user is entitled to the given feature and attributes. Attaching the justification if not
|
|
5
7
|
*/
|
|
6
|
-
export declare const useFeatureEntitlements: (key: string) => Entitlement;
|
|
8
|
+
export declare const useFeatureEntitlements: (key: string, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
|
|
7
9
|
/**
|
|
8
|
-
@param key
|
|
9
|
-
@
|
|
10
|
+
@param key permission key
|
|
11
|
+
@param customAttributes user attributes
|
|
12
|
+
@returns if the user is entitled to the given permission and attributes. Attaching the justification if not
|
|
10
13
|
*/
|
|
11
|
-
export declare const usePermissionEntitlements: (key: string) => Entitlement;
|
|
14
|
+
export declare const usePermissionEntitlements: (key: string, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
|
|
12
15
|
/**
|
|
13
16
|
@param entitledToOptions including permission or feature key
|
|
14
|
-
@
|
|
17
|
+
@param customAttributes user attributes
|
|
18
|
+
@returns if the user is entitled to the given feature or permission and attributes (check only one). Attaching the justification if not
|
|
15
19
|
*/
|
|
16
|
-
export declare const useEntitlements: (options: EntitledToOptions) => Entitlement;
|
|
20
|
+
export declare const useEntitlements: (options: EntitledToOptions, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
|
|
17
21
|
/**
|
|
18
22
|
@param entitledToOptions including permission or feature key
|
|
19
23
|
@returns an action your can use to detect if the user is entitled to the given feature or permission (check only one). Attaching the justification if not
|
|
20
24
|
*/
|
|
21
25
|
export declare const useEntitlementsActions: () => {
|
|
22
|
-
isEntitledTo: (options: EntitledToOptions) => Entitlement;
|
|
26
|
+
isEntitledTo: (options: EntitledToOptions, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
|
|
23
27
|
};
|
|
24
28
|
/**
|
|
25
29
|
@returns if the option to use entitlements is enabled
|
|
26
30
|
*/
|
|
27
31
|
export declare const useEntitlementsOptions: () => {
|
|
28
32
|
isEntitlementsEnabled: boolean;
|
|
29
|
-
isEntitledTo: (options: EntitledToOptions) => Entitlement;
|
|
33
|
+
isEntitledTo: (options: EntitledToOptions, customAttributes?: Record<string, string | number | boolean | Date> | undefined) => Entitlement;
|
|
30
34
|
verifyIsEntitledFF: boolean;
|
|
31
35
|
};
|
package/auth/entitlements.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { getPermissionEntitlements, getFeatureEntitlements, getEntitlements } from '@frontegg/redux-store';
|
|
2
|
+
import { FeatureFlags, USE_ENTITLEMENTS_V2_ENDPOINT_FF } from '@frontegg/rest-api';
|
|
2
3
|
import { useAuth } from './hooks';
|
|
3
4
|
import { useMemo } from 'react';
|
|
4
|
-
import { useShadowDom } from '../common';
|
|
5
|
+
import { useRootState, useShadowDom } from '../common';
|
|
5
6
|
import { useFeatureFlags } from '../flags';
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -12,30 +13,74 @@ const useEntitlementsState = () => useAuth(({
|
|
|
12
13
|
}) => user == null ? void 0 : user.entitlements);
|
|
13
14
|
|
|
14
15
|
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
* @returns user state
|
|
17
|
+
*/
|
|
18
|
+
const useUserState = () => useAuth(({
|
|
19
|
+
user
|
|
20
|
+
}) => user);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param customAttributes user attributes
|
|
24
|
+
* @returns is entitled query data including: entitltments state, final attributes (consumer and frontegg) and API version to use
|
|
25
|
+
*/
|
|
26
|
+
const useEntitlementsQueryData = customAttributes => {
|
|
27
|
+
const user = useUserState();
|
|
19
28
|
const entitlements = useEntitlementsState();
|
|
20
|
-
|
|
29
|
+
const {
|
|
30
|
+
appName
|
|
31
|
+
} = useRootState();
|
|
32
|
+
const [useEntitlementsV2] = FeatureFlags.getFeatureFlags([USE_ENTITLEMENTS_V2_ENDPOINT_FF], appName);
|
|
33
|
+
const attributes = {
|
|
34
|
+
custom: customAttributes,
|
|
35
|
+
jwt: user
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
entitlements,
|
|
39
|
+
attributes,
|
|
40
|
+
isV2: useEntitlementsV2
|
|
41
|
+
};
|
|
21
42
|
};
|
|
22
43
|
|
|
23
44
|
/**
|
|
24
|
-
@param key
|
|
25
|
-
@
|
|
45
|
+
@param key feature key
|
|
46
|
+
@param customAttributes user attributes
|
|
47
|
+
@returns if the user is entitled to the given feature and attributes. Attaching the justification if not
|
|
26
48
|
*/
|
|
27
|
-
export const
|
|
28
|
-
const
|
|
29
|
-
|
|
49
|
+
export const useFeatureEntitlements = (key, customAttributes) => {
|
|
50
|
+
const {
|
|
51
|
+
entitlements,
|
|
52
|
+
attributes,
|
|
53
|
+
isV2
|
|
54
|
+
} = useEntitlementsQueryData(customAttributes);
|
|
55
|
+
return getFeatureEntitlements(entitlements, key, attributes, isV2);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
@param key permission key
|
|
60
|
+
@param customAttributes user attributes
|
|
61
|
+
@returns if the user is entitled to the given permission and attributes. Attaching the justification if not
|
|
62
|
+
*/
|
|
63
|
+
export const usePermissionEntitlements = (key, customAttributes) => {
|
|
64
|
+
const {
|
|
65
|
+
entitlements,
|
|
66
|
+
attributes,
|
|
67
|
+
isV2
|
|
68
|
+
} = useEntitlementsQueryData(customAttributes);
|
|
69
|
+
return getPermissionEntitlements(entitlements, key, attributes, isV2);
|
|
30
70
|
};
|
|
31
71
|
|
|
32
72
|
/**
|
|
33
73
|
@param entitledToOptions including permission or feature key
|
|
34
|
-
@
|
|
74
|
+
@param customAttributes user attributes
|
|
75
|
+
@returns if the user is entitled to the given feature or permission and attributes (check only one). Attaching the justification if not
|
|
35
76
|
*/
|
|
36
|
-
export const useEntitlements = options => {
|
|
37
|
-
const
|
|
38
|
-
|
|
77
|
+
export const useEntitlements = (options, customAttributes) => {
|
|
78
|
+
const {
|
|
79
|
+
entitlements,
|
|
80
|
+
attributes,
|
|
81
|
+
isV2
|
|
82
|
+
} = useEntitlementsQueryData(customAttributes);
|
|
83
|
+
return getEntitlements(entitlements, options, attributes, isV2);
|
|
39
84
|
};
|
|
40
85
|
|
|
41
86
|
/**
|
|
@@ -43,10 +88,22 @@ export const useEntitlements = options => {
|
|
|
43
88
|
@returns an action your can use to detect if the user is entitled to the given feature or permission (check only one). Attaching the justification if not
|
|
44
89
|
*/
|
|
45
90
|
export const useEntitlementsActions = () => {
|
|
91
|
+
// this code is duplicated because React is yelling when using useEntitlementsQueryData inside the isEntitledTo function because it's not a hook
|
|
92
|
+
const user = useUserState();
|
|
46
93
|
const entitlements = useEntitlementsState();
|
|
94
|
+
const {
|
|
95
|
+
appName
|
|
96
|
+
} = useRootState();
|
|
97
|
+
const [useEntitlementsV2] = FeatureFlags.getFeatureFlags([USE_ENTITLEMENTS_V2_ENDPOINT_FF], appName);
|
|
47
98
|
return useMemo(() => ({
|
|
48
|
-
isEntitledTo: options =>
|
|
49
|
-
|
|
99
|
+
isEntitledTo: (options, customAttributes) => {
|
|
100
|
+
const attributes = {
|
|
101
|
+
custom: customAttributes,
|
|
102
|
+
jwt: user
|
|
103
|
+
};
|
|
104
|
+
return getEntitlements(entitlements, options, attributes, useEntitlementsV2);
|
|
105
|
+
}
|
|
106
|
+
}), [user, entitlements, useEntitlementsV2]);
|
|
50
107
|
};
|
|
51
108
|
|
|
52
109
|
/**
|
package/index.js
CHANGED
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.usePermissionEntitlements = exports.useFeatureEntitlements = exports.useEntitlementsOptions = exports.useEntitlementsActions = exports.useEntitlements = void 0;
|
|
7
7
|
var _reduxStore = require("@frontegg/redux-store");
|
|
8
|
+
var _restApi = require("@frontegg/rest-api");
|
|
8
9
|
var _hooks = require("./hooks");
|
|
9
10
|
var _react = require("react");
|
|
10
11
|
var _common = require("../common");
|
|
@@ -17,32 +18,76 @@ const useEntitlementsState = () => (0, _hooks.useAuth)(({
|
|
|
17
18
|
}) => user == null ? void 0 : user.entitlements);
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
* @returns user state
|
|
22
|
+
*/
|
|
23
|
+
const useUserState = () => (0, _hooks.useAuth)(({
|
|
24
|
+
user
|
|
25
|
+
}) => user);
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param customAttributes user attributes
|
|
29
|
+
* @returns is entitled query data including: entitltments state, final attributes (consumer and frontegg) and API version to use
|
|
30
|
+
*/
|
|
31
|
+
const useEntitlementsQueryData = customAttributes => {
|
|
32
|
+
const user = useUserState();
|
|
24
33
|
const entitlements = useEntitlementsState();
|
|
25
|
-
|
|
34
|
+
const {
|
|
35
|
+
appName
|
|
36
|
+
} = (0, _common.useRootState)();
|
|
37
|
+
const [useEntitlementsV2] = _restApi.FeatureFlags.getFeatureFlags([_restApi.USE_ENTITLEMENTS_V2_ENDPOINT_FF], appName);
|
|
38
|
+
const attributes = {
|
|
39
|
+
custom: customAttributes,
|
|
40
|
+
jwt: user
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
entitlements,
|
|
44
|
+
attributes,
|
|
45
|
+
isV2: useEntitlementsV2
|
|
46
|
+
};
|
|
26
47
|
};
|
|
27
48
|
|
|
28
49
|
/**
|
|
29
|
-
@param key
|
|
30
|
-
@
|
|
50
|
+
@param key feature key
|
|
51
|
+
@param customAttributes user attributes
|
|
52
|
+
@returns if the user is entitled to the given feature and attributes. Attaching the justification if not
|
|
53
|
+
*/
|
|
54
|
+
const useFeatureEntitlements = (key, customAttributes) => {
|
|
55
|
+
const {
|
|
56
|
+
entitlements,
|
|
57
|
+
attributes,
|
|
58
|
+
isV2
|
|
59
|
+
} = useEntitlementsQueryData(customAttributes);
|
|
60
|
+
return (0, _reduxStore.getFeatureEntitlements)(entitlements, key, attributes, isV2);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
@param key permission key
|
|
65
|
+
@param customAttributes user attributes
|
|
66
|
+
@returns if the user is entitled to the given permission and attributes. Attaching the justification if not
|
|
31
67
|
*/
|
|
32
68
|
exports.useFeatureEntitlements = useFeatureEntitlements;
|
|
33
|
-
const usePermissionEntitlements = key => {
|
|
34
|
-
const
|
|
35
|
-
|
|
69
|
+
const usePermissionEntitlements = (key, customAttributes) => {
|
|
70
|
+
const {
|
|
71
|
+
entitlements,
|
|
72
|
+
attributes,
|
|
73
|
+
isV2
|
|
74
|
+
} = useEntitlementsQueryData(customAttributes);
|
|
75
|
+
return (0, _reduxStore.getPermissionEntitlements)(entitlements, key, attributes, isV2);
|
|
36
76
|
};
|
|
37
77
|
|
|
38
78
|
/**
|
|
39
79
|
@param entitledToOptions including permission or feature key
|
|
40
|
-
@
|
|
80
|
+
@param customAttributes user attributes
|
|
81
|
+
@returns if the user is entitled to the given feature or permission and attributes (check only one). Attaching the justification if not
|
|
41
82
|
*/
|
|
42
83
|
exports.usePermissionEntitlements = usePermissionEntitlements;
|
|
43
|
-
const useEntitlements = options => {
|
|
44
|
-
const
|
|
45
|
-
|
|
84
|
+
const useEntitlements = (options, customAttributes) => {
|
|
85
|
+
const {
|
|
86
|
+
entitlements,
|
|
87
|
+
attributes,
|
|
88
|
+
isV2
|
|
89
|
+
} = useEntitlementsQueryData(customAttributes);
|
|
90
|
+
return (0, _reduxStore.getEntitlements)(entitlements, options, attributes, isV2);
|
|
46
91
|
};
|
|
47
92
|
|
|
48
93
|
/**
|
|
@@ -51,10 +96,22 @@ const useEntitlements = options => {
|
|
|
51
96
|
*/
|
|
52
97
|
exports.useEntitlements = useEntitlements;
|
|
53
98
|
const useEntitlementsActions = () => {
|
|
99
|
+
// this code is duplicated because React is yelling when using useEntitlementsQueryData inside the isEntitledTo function because it's not a hook
|
|
100
|
+
const user = useUserState();
|
|
54
101
|
const entitlements = useEntitlementsState();
|
|
102
|
+
const {
|
|
103
|
+
appName
|
|
104
|
+
} = (0, _common.useRootState)();
|
|
105
|
+
const [useEntitlementsV2] = _restApi.FeatureFlags.getFeatureFlags([_restApi.USE_ENTITLEMENTS_V2_ENDPOINT_FF], appName);
|
|
55
106
|
return (0, _react.useMemo)(() => ({
|
|
56
|
-
isEntitledTo: options
|
|
57
|
-
|
|
107
|
+
isEntitledTo: (options, customAttributes) => {
|
|
108
|
+
const attributes = {
|
|
109
|
+
custom: customAttributes,
|
|
110
|
+
jwt: user
|
|
111
|
+
};
|
|
112
|
+
return (0, _reduxStore.getEntitlements)(entitlements, options, attributes, useEntitlementsV2);
|
|
113
|
+
}
|
|
114
|
+
}), [user, entitlements, useEntitlementsV2]);
|
|
58
115
|
};
|
|
59
116
|
|
|
60
117
|
/**
|
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.150.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.150.0",
|
|
10
|
+
"@frontegg/types": "6.150.0",
|
|
11
11
|
"@types/react": "*",
|
|
12
12
|
"get-value": "^3.0.1",
|
|
13
13
|
"react-redux": "^7.x"
|