|
@@ -4,64 +4,79 @@ import { parseURLParams } from '../config';
|
4
|
4
|
import { toState } from '../redux';
|
5
|
5
|
|
6
|
6
|
/**
|
7
|
|
- * Returns the effective value of a property by applying a precedence
|
8
|
|
- * between values in URL, config and profile.
|
|
7
|
+ * Returns the effective value of a configuration/preference/setting by applying
|
|
8
|
+ * a precedence among the values specified by JWT, URL, profile, and config.
|
9
|
9
|
*
|
10
|
|
- * @param {Object|Function} stateful - The redux state object or function
|
11
|
|
- * to retreive the state.
|
12
|
|
- * @param {string} propertyName - The name of the property we need.
|
|
10
|
+ * @param {Object|Function} stateful - The redux state object or
|
|
11
|
+ * {@code getState} function.
|
|
12
|
+ * @param {string} propertyName - The name of the
|
|
13
|
+ * configuration/preference/setting (property) to retrieve.
|
13
|
14
|
* @param {{
|
14
|
|
- * ignoreJWT: boolean,
|
15
|
|
- * ignoreUrlParams: boolean,
|
16
|
|
- * ignoreProfile: boolean,
|
17
|
|
- * ignoreConfig: boolean
|
18
|
|
- * }} precedence - A structure of booleans to set which property sources
|
19
|
|
- * should be ignored.
|
|
15
|
+ * config: boolean,
|
|
16
|
+ * jwt: boolean,
|
|
17
|
+ * profile: boolean,
|
|
18
|
+ * urlParams: boolean
|
|
19
|
+ * }} [sources] - A set/structure of {@code boolean} flags indicating the
|
|
20
|
+ * configuration/preference/setting sources to consider/retrieve values from.
|
20
|
21
|
* @returns {any}
|
21
|
22
|
*/
|
22
|
23
|
export function getPropertyValue(
|
23
|
24
|
stateful: Object | Function,
|
24
|
25
|
propertyName: string,
|
25
|
|
- precedence: Object = {
|
26
|
|
- ignoreJWT: false,
|
27
|
|
- ignoreUrlParams: false,
|
28
|
|
- ignoreProfile: false,
|
29
|
|
- ignoreConfig: false
|
30
|
|
- }
|
|
26
|
+ sources?: Object
|
31
|
27
|
) {
|
|
28
|
+ // Default values don't play nicely with partial objects and we want to make
|
|
29
|
+ // the function easy to use without exhaustively defining all flags:
|
|
30
|
+ sources = { // eslint-disable-line no-param-reassign
|
|
31
|
+ // Defaults:
|
|
32
|
+ config: true,
|
|
33
|
+ jwt: true,
|
|
34
|
+ profile: true,
|
|
35
|
+ urlParams: true,
|
|
36
|
+
|
|
37
|
+ ...sources
|
|
38
|
+ };
|
|
39
|
+
|
|
40
|
+ // Precedence: jwt -> urlParams -> profile -> config.
|
|
41
|
+
|
32
|
42
|
const state = toState(stateful);
|
33
|
|
- const jwt = state['features/base/jwt'];
|
34
|
|
- const urlParams
|
35
|
|
- = parseURLParams(state['features/base/connection'].locationURL);
|
36
|
|
- const profile = state['features/base/profile'];
|
37
|
|
- const config = state['features/base/config'];
|
38
|
|
- const urlParamName = `config.${propertyName}`;
|
39
|
43
|
|
40
|
|
- // Precedence: jwt -> urlParams -> profile -> config
|
|
44
|
+ // jwt
|
|
45
|
+ if (sources.jwt) {
|
|
46
|
+ const value = state['features/base/jwt'][propertyName];
|
41
|
47
|
|
42
|
|
- if (
|
43
|
|
- !precedence.ignoreJWT
|
44
|
|
- && typeof jwt[propertyName] !== 'undefined'
|
45
|
|
- ) {
|
46
|
|
- return jwt[propertyName];
|
|
48
|
+ if (typeof value !== 'undefined') {
|
|
49
|
+ return value[propertyName];
|
|
50
|
+ }
|
47
|
51
|
}
|
48
|
52
|
|
49
|
|
- if (
|
50
|
|
- !precedence.ignoreUrlParams
|
51
|
|
- && typeof urlParams[urlParamName] !== 'undefined'
|
52
|
|
- ) {
|
53
|
|
- return urlParams[urlParamName];
|
|
53
|
+ // urlParams
|
|
54
|
+ if (sources.urlParams) {
|
|
55
|
+ const urlParams
|
|
56
|
+ = parseURLParams(state['features/base/connection'].locationURL);
|
|
57
|
+ const value = urlParams[`config.${propertyName}`];
|
|
58
|
+
|
|
59
|
+ if (typeof value !== 'undefined') {
|
|
60
|
+ return value;
|
|
61
|
+ }
|
54
|
62
|
}
|
55
|
63
|
|
56
|
|
- if (
|
57
|
|
- !precedence.ignoreProfile
|
58
|
|
- && typeof profile[propertyName] !== 'undefined'
|
59
|
|
- ) {
|
60
|
|
- return profile[propertyName];
|
|
64
|
+ // profile
|
|
65
|
+ if (sources.profile) {
|
|
66
|
+ const value = state['features/base/profile'][propertyName];
|
|
67
|
+
|
|
68
|
+ if (typeof value !== 'undefined') {
|
|
69
|
+ return value;
|
|
70
|
+ }
|
61
|
71
|
}
|
62
|
72
|
|
63
|
|
- if (!precedence.ignoreConfig) {
|
64
|
|
- return config[propertyName];
|
|
73
|
+ // config
|
|
74
|
+ if (sources.config) {
|
|
75
|
+ const value = state['features/base/config'][propertyName];
|
|
76
|
+
|
|
77
|
+ if (typeof value !== 'undefined') {
|
|
78
|
+ return value;
|
|
79
|
+ }
|
65
|
80
|
}
|
66
|
81
|
|
67
|
82
|
return undefined;
|