@adobe-commerce/elsie 1.2.2-alpha1 → 1.3.0-alpha02

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/config/vite.mjs CHANGED
@@ -25,18 +25,11 @@ import banner from 'vite-plugin-banner';
25
25
  const env = loadEnv('', process.cwd());
26
26
 
27
27
  // Load Elsie Config
28
- const elsieConfig = await import(
29
- path.resolve(process.cwd(), './.elsie.js')
30
- ).then((m) => m.default);
31
-
32
- const packageJSON = await import(
33
- path.resolve(process.cwd(), './package.json'),
34
- {
35
- assert: {
36
- type: 'json',
37
- },
38
- }
39
- ).then((m) => m.default);
28
+ const elsieConfig = await import(path.resolve(process.cwd(), './.elsie.js')).then((m) => m.default);
29
+
30
+ // Read package.json using createRequire (compatible with Node 20 and 22)
31
+ const require = createRequire(import.meta.url);
32
+ const packageJSON = require(path.resolve(process.cwd(), './package.json'));
40
33
 
41
34
  // Paths
42
35
  const paths = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe-commerce/elsie",
3
- "version": "1.2.2-alpha1",
3
+ "version": "1.3.0-alpha02",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "description": "Domain Package SDK",
6
6
  "engines": {
@@ -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>
@@ -0,0 +1,161 @@
1
+ import { deepmerge } from '../deepmerge';
2
+ import { getPathValue } from '../get-path-value';
3
+
4
+ interface ConfigHeaders {
5
+ all?: Record<string, string>;
6
+ [key: string]: Record<string, string> | undefined;
7
+ }
8
+
9
+ interface ConfigPublic {
10
+ default: ConfigRoot;
11
+ [key: string]: ConfigRoot;
12
+ }
13
+
14
+ interface ConfigRoot {
15
+ headers?: ConfigHeaders;
16
+ [key: string]: any;
17
+ }
18
+
19
+ interface Config {
20
+ public: ConfigPublic;
21
+ [key: string]: any;
22
+ }
23
+
24
+ // Private state
25
+ let config: Config | null = null;
26
+ let rootPath: string | null = null;
27
+ let rootConfig: ConfigRoot | null = null;
28
+
29
+ /**
30
+ * Reset the config state
31
+ */
32
+ function resetConfig() {
33
+ config = null;
34
+ rootPath = null;
35
+ rootConfig = null;
36
+ }
37
+
38
+ /**
39
+ * Get root path
40
+ * @param {Object} [configObj=config] - The config object.
41
+ * @returns {string} - The root path.
42
+ */
43
+ function getRootPath(configObj: Config | null = config): string {
44
+ if (!configObj) {
45
+ console.warn('No config found. Please call initializeConfig() first.');
46
+ return '/';
47
+ }
48
+
49
+ const value = Object.keys(configObj?.public)
50
+ // Sort by number of non-empty segments to find the deepest path
51
+ .sort((a, b) => {
52
+ const aSegments = a.split('/').filter(Boolean).length;
53
+ const bSegments = b.split('/').filter(Boolean).length;
54
+ return bSegments - aSegments;
55
+ })
56
+ .find(
57
+ (key) =>
58
+ window.location.pathname === key ||
59
+ window.location.pathname.startsWith(key)
60
+ );
61
+
62
+ return value ?? '/';
63
+ }
64
+
65
+ /**
66
+ * Get list of root paths from public config
67
+ * @returns {Array} - The list of root paths.
68
+ */
69
+ function getListOfRootPaths(): string[] {
70
+ if (!config) {
71
+ console.warn('No config found. Please call initializeConfig() first.');
72
+ return [];
73
+ }
74
+
75
+ return Object.keys(config.public).filter((root) => root !== 'default');
76
+ }
77
+
78
+ /**
79
+ * Checks if the public config contains more than "default"
80
+ * @returns {boolean} - true if public config contains more than "default"
81
+ */
82
+ function isMultistore(): boolean {
83
+ return getListOfRootPaths().length >= 1;
84
+ }
85
+
86
+ /**
87
+ * Retrieves headers from config entries like commerce.headers.pdp.my-header, etc and
88
+ * returns as object of all headers like { my-header: value, ... }
89
+ * @param {string} scope - The scope of the headers to retrieve.
90
+ * @returns {Object} - The headers.
91
+ */
92
+ function getHeaders(scope: string): Record<string, string> {
93
+ if (!rootConfig) {
94
+ throw new Error(
95
+ 'Configuration not initialized. Call initializeConfig() first.'
96
+ );
97
+ }
98
+ const headers = rootConfig.headers ?? {};
99
+ return {
100
+ ...(headers.all ?? {}),
101
+ ...(headers[scope] ?? {}),
102
+ };
103
+ }
104
+
105
+ /**
106
+ * Applies config overrides from metadata.
107
+ *
108
+ * @param {Object} [configObj] - The base config.
109
+ * @param {string} [root] - The root path.
110
+ * @returns {Object} - The config with overrides applied.
111
+ */
112
+ function applyConfigOverrides(
113
+ configObj: Config | null,
114
+ root: string | null
115
+ ): ConfigRoot {
116
+ const defaultConfig = configObj!.public?.default;
117
+
118
+ if (root === '/' || !configObj!.public[root as keyof ConfigPublic])
119
+ return defaultConfig;
120
+
121
+ return deepmerge(
122
+ defaultConfig,
123
+ configObj!.public[root as keyof ConfigPublic]
124
+ );
125
+ }
126
+
127
+ /**
128
+ * Initializes the configuration system.
129
+ * @returns {Object} The initialized root configuration
130
+ */
131
+ function initializeConfig(configObj: Config): ConfigRoot {
132
+ config = configObj;
133
+ rootPath = getRootPath(config);
134
+ rootConfig = applyConfigOverrides(config, rootPath);
135
+ return rootConfig;
136
+ }
137
+
138
+ /**
139
+ * Retrieves a configuration value.
140
+ *
141
+ * @param {string} configParam - The configuration parameter to retrieve.
142
+ * @returns {string|undefined} - The value of the configuration parameter, or undefined.
143
+ */
144
+ function getConfigValue(configParam: string): any {
145
+ if (!rootConfig) {
146
+ throw new Error(
147
+ 'Configuration not initialized. Call initializeConfig() first.'
148
+ );
149
+ }
150
+ return getPathValue(rootConfig, configParam);
151
+ }
152
+
153
+ export {
154
+ initializeConfig,
155
+ getRootPath,
156
+ getListOfRootPaths,
157
+ isMultistore,
158
+ getConfigValue,
159
+ getHeaders,
160
+ resetConfig,
161
+ };
@@ -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
+ }