@adobe-commerce/elsie 1.3.0-alpha01 → 1.3.0-alpha03
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/package.json
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Meta, Unstyled } from '@storybook/blocks';
|
|
2
|
+
|
|
3
|
+
<Meta title="Utilities/getCookie" />
|
|
4
|
+
<Unstyled>
|
|
5
|
+
|
|
6
|
+
# getCookie(cookieName)
|
|
7
|
+
|
|
8
|
+
This function takes in a cookie name and returns the value of the cookie.
|
|
9
|
+
|
|
10
|
+
## Params
|
|
11
|
+
|
|
12
|
+
`cookieName`
|
|
13
|
+
: The name of the cookie to get
|
|
14
|
+
|
|
15
|
+
## Returns
|
|
16
|
+
|
|
17
|
+
Returns the value of the cookie.
|
|
18
|
+
|
|
19
|
+
## Examples
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { getCookie } from '@adobe-commerce/elsie/lib';
|
|
23
|
+
|
|
24
|
+
const result = getCookie('foo');
|
|
25
|
+
|
|
26
|
+
console.log(result); // "bar"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
</Unstyled>
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Meta, Unstyled } from '@storybook/blocks';
|
|
2
|
+
|
|
3
|
+
<Meta title="Utilities/getPathValue" />
|
|
4
|
+
<Unstyled>
|
|
5
|
+
|
|
6
|
+
# getPathValue(obj, key)
|
|
7
|
+
|
|
8
|
+
This function takes in an object and a key and returns the value of the key.
|
|
9
|
+
|
|
10
|
+
## Params
|
|
11
|
+
|
|
12
|
+
`obj`
|
|
13
|
+
: The object to get the value from
|
|
14
|
+
|
|
15
|
+
`key`
|
|
16
|
+
: The key to get the value from (supports dot notation)
|
|
17
|
+
|
|
18
|
+
## Returns
|
|
19
|
+
|
|
20
|
+
Returns the value of the key.
|
|
21
|
+
|
|
22
|
+
## Examples
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { getPathValue } from '@adobe-commerce/elsie/lib';
|
|
26
|
+
|
|
27
|
+
const obj = {
|
|
28
|
+
foo: {
|
|
29
|
+
bar: 'baz',
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const result = getPathValue(obj, 'foo.bar');
|
|
34
|
+
|
|
35
|
+
console.log(result); // "baz"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
</Unstyled>
|
package/src/lib/aem/configs.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { deepmerge } from '../deepmerge';
|
|
2
|
+
import { getPathValue } from '../get-path-value';
|
|
2
3
|
|
|
3
4
|
interface ConfigHeaders {
|
|
4
5
|
all?: Record<string, string>;
|
|
@@ -34,51 +35,6 @@ function resetConfig() {
|
|
|
34
35
|
rootConfig = null;
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
/**
|
|
38
|
-
* Builds the URL for the config file.
|
|
39
|
-
*
|
|
40
|
-
* @returns {URL} - The URL for the config file.
|
|
41
|
-
*/
|
|
42
|
-
function buildConfigURL() {
|
|
43
|
-
return new URL(`${window.location.origin}/config.json`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Retrieves a value from a config object using dot notation.
|
|
48
|
-
*
|
|
49
|
-
* @param {Object} obj - The config object.
|
|
50
|
-
* @param {string} key - The key to retrieve (supports dot notation).
|
|
51
|
-
* @returns {any} - The value of the key.
|
|
52
|
-
*/
|
|
53
|
-
function getValue(obj: Record<string, any>, key: string): any {
|
|
54
|
-
return key.split('.').reduce((current: Record<string, any>, part: string) => {
|
|
55
|
-
if (!Object.prototype.hasOwnProperty.call(current, part)) {
|
|
56
|
-
console.warn(`Property ${key} does not exist in the object`);
|
|
57
|
-
return undefined;
|
|
58
|
-
}
|
|
59
|
-
return current[part];
|
|
60
|
-
}, obj);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Get cookie
|
|
65
|
-
* @param {string} cookieName - The name of the cookie to get
|
|
66
|
-
* @returns {string} - The value of the cookie
|
|
67
|
-
*/
|
|
68
|
-
function getCookie(cookieName: string): string | undefined {
|
|
69
|
-
const cookies = document.cookie.split(';');
|
|
70
|
-
let foundValue;
|
|
71
|
-
|
|
72
|
-
cookies.forEach((cookie) => {
|
|
73
|
-
const [name, value] = cookie.trim().split('=');
|
|
74
|
-
if (name === cookieName) {
|
|
75
|
-
foundValue = decodeURIComponent(value);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
return foundValue;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
38
|
/**
|
|
83
39
|
* Get root path
|
|
84
40
|
* @param {Object} [configObj=config] - The config object.
|
|
@@ -149,8 +105,8 @@ function getHeaders(scope: string): Record<string, string> {
|
|
|
149
105
|
/**
|
|
150
106
|
* Applies config overrides from metadata.
|
|
151
107
|
*
|
|
152
|
-
* @param {Object} [configObj
|
|
153
|
-
* @param {string} [root
|
|
108
|
+
* @param {Object} [configObj] - The base config.
|
|
109
|
+
* @param {string} [root] - The root path.
|
|
154
110
|
* @returns {Object} - The config with overrides applied.
|
|
155
111
|
*/
|
|
156
112
|
function applyConfigOverrides(
|
|
@@ -168,43 +124,12 @@ function applyConfigOverrides(
|
|
|
168
124
|
);
|
|
169
125
|
}
|
|
170
126
|
|
|
171
|
-
/**
|
|
172
|
-
* Fetches config from remote and saves in session, then returns it, otherwise
|
|
173
|
-
* returns if it already exists.
|
|
174
|
-
*
|
|
175
|
-
* @returns {Promise<Object>} - The config JSON from session storage
|
|
176
|
-
*/
|
|
177
|
-
async function getConfigFromSession(): Promise<Config> {
|
|
178
|
-
try {
|
|
179
|
-
const configJSON = window.sessionStorage.getItem('config');
|
|
180
|
-
if (!configJSON) {
|
|
181
|
-
throw new Error('No config in session storage');
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const parsedConfig = JSON.parse(configJSON);
|
|
185
|
-
if (
|
|
186
|
-
!parsedConfig[':expiry'] ||
|
|
187
|
-
parsedConfig[':expiry'] < Math.round(Date.now() / 1000)
|
|
188
|
-
) {
|
|
189
|
-
throw new Error('Config expired');
|
|
190
|
-
}
|
|
191
|
-
return parsedConfig;
|
|
192
|
-
} catch (e) {
|
|
193
|
-
const config = await fetch(buildConfigURL());
|
|
194
|
-
if (!config.ok) throw new Error('Failed to fetch config');
|
|
195
|
-
const configJSON = await config.json();
|
|
196
|
-
configJSON[':expiry'] = Math.round(Date.now() / 1000) + 7200;
|
|
197
|
-
window.sessionStorage.setItem('config', JSON.stringify(configJSON));
|
|
198
|
-
return configJSON;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
127
|
/**
|
|
203
128
|
* Initializes the configuration system.
|
|
204
|
-
* @returns {
|
|
129
|
+
* @returns {Object} The initialized root configuration
|
|
205
130
|
*/
|
|
206
|
-
|
|
207
|
-
config =
|
|
131
|
+
function initializeConfig(configObj: Config): ConfigRoot {
|
|
132
|
+
config = configObj;
|
|
208
133
|
rootPath = getRootPath(config);
|
|
209
134
|
rootConfig = applyConfigOverrides(config, rootPath);
|
|
210
135
|
return rootConfig;
|
|
@@ -222,12 +147,11 @@ function getConfigValue(configParam: string): any {
|
|
|
222
147
|
'Configuration not initialized. Call initializeConfig() first.'
|
|
223
148
|
);
|
|
224
149
|
}
|
|
225
|
-
return
|
|
150
|
+
return getPathValue(rootConfig, configParam);
|
|
226
151
|
}
|
|
227
152
|
|
|
228
153
|
export {
|
|
229
154
|
initializeConfig,
|
|
230
|
-
getCookie,
|
|
231
155
|
getRootPath,
|
|
232
156
|
getListOfRootPaths,
|
|
233
157
|
isMultistore,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get cookie
|
|
3
|
+
* @param {string} cookieName - The name of the cookie to get
|
|
4
|
+
* @returns {string} - The value of the cookie
|
|
5
|
+
*/
|
|
6
|
+
export function getCookie(cookieName: string): string | undefined {
|
|
7
|
+
const cookies = document.cookie.split(';');
|
|
8
|
+
let foundValue;
|
|
9
|
+
|
|
10
|
+
cookies.forEach((cookie) => {
|
|
11
|
+
const [name, value] = cookie.trim().split('=');
|
|
12
|
+
if (name === cookieName) {
|
|
13
|
+
foundValue = decodeURIComponent(value);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
return foundValue;
|
|
18
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieves a value from a config object using dot notation.
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} obj - The config object.
|
|
5
|
+
* @param {string} key - The key to retrieve (supports dot notation).
|
|
6
|
+
* @returns {any} - The value of the key.
|
|
7
|
+
*/
|
|
8
|
+
export function getPathValue(obj: Record<string, any>, key: string): any {
|
|
9
|
+
try {
|
|
10
|
+
return key
|
|
11
|
+
.split('.')
|
|
12
|
+
.reduce((current: Record<string, any>, part: string) => {
|
|
13
|
+
if (!Object.prototype.hasOwnProperty.call(current, part)) {
|
|
14
|
+
throw Error();
|
|
15
|
+
}
|
|
16
|
+
return current[part];
|
|
17
|
+
}, obj);
|
|
18
|
+
} catch {
|
|
19
|
+
console.warn(`Property ${key} does not exist in the object`);
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
package/src/lib/index.ts
CHANGED
|
@@ -22,3 +22,5 @@ export * from '@adobe-commerce/elsie/lib/vcomponent';
|
|
|
22
22
|
export * from '@adobe-commerce/elsie/lib/image-params-keymap';
|
|
23
23
|
export * from '@adobe-commerce/elsie/lib/is-number';
|
|
24
24
|
export * from '@adobe-commerce/elsie/lib/deviceUtils';
|
|
25
|
+
export * from '@adobe-commerce/elsie/lib/get-path-value';
|
|
26
|
+
export * from '@adobe-commerce/elsie/lib/get-cookie';
|