11/* eslint-disable import/no-cycle */
22import { deepmerge } from '@dropins/tools/lib.js' ;
33
4- let AEM_ROOT_PATH = '/' ;
4+ // load config
5+ const CONFIG = await getConfig ( ) ;
6+ const ROOT_PATH = getRootPath ( ) ;
7+ const ROOT_CONFIG = await applyConfigOverrides ( CONFIG , ROOT_PATH ) ;
58
69/**
710 * Builds the URL for the config file.
@@ -29,13 +32,98 @@ function getValue(obj, key) {
2932 } , obj ) ;
3033}
3134
35+ /**
36+ * Get root path
37+ * @param {Object } [config] - The config object.
38+ * @returns {string } - The root path.
39+ */
40+ export function getRootPath ( ) {
41+ const value = Object . keys ( CONFIG ?. public )
42+ // Sort by number of non-empty segments to find the deepest path
43+ . sort ( ( a , b ) => {
44+ const aSegments = a . split ( '/' ) . filter ( Boolean ) . length ;
45+ const bSegments = b . split ( '/' ) . filter ( Boolean ) . length ;
46+ return bSegments - aSegments ;
47+ } )
48+ . find ( ( key ) => window . location . pathname === key || window . location . pathname . startsWith ( key ) ) ;
49+
50+ const rootPath = value ?? '/' ;
51+
52+ if ( ! rootPath . startsWith ( '/' ) || ! rootPath . endsWith ( '/' ) ) {
53+ throw new Error ( 'Invalid root path' ) ;
54+ }
55+
56+ return rootPath ;
57+ }
58+
59+ /**
60+ * Get list of root paths from public config
61+ * @returns {Array } - The list of root paths.
62+ */
63+ export function getListOfRootPaths ( ) {
64+ return Object . keys ( CONFIG ?. public ) . filter ( ( root ) => root !== 'default' ) ;
65+ }
66+
67+ /**
68+ * Checks if the public config contains more than "default"
69+ * @returns true if public config contains more than "default"
70+ */
71+ export function isMultistore ( ) {
72+ return getListOfRootPaths ( ) . length > 1 ;
73+ }
74+
75+ /**
76+ * Retrieves a configuration value.
77+ *
78+ * @param {string } configParam - The configuration parameter to retrieve.
79+ * @returns {Promise<string|undefined> } - The value of the configuration parameter, or undefined.
80+ */
81+ export async function getConfigValue ( configParam ) {
82+ return getValue ( ROOT_CONFIG , configParam ) ;
83+ }
84+
85+ /**
86+ * Retrieves headers from config entries like commerce.headers.pdp.my-header, etc and
87+ * returns as object of all headers like { my-header: value, ... }
88+ */
89+ export async function getHeaders ( scope ) {
90+ const headers = ROOT_CONFIG . headers ?? { } ;
91+ return {
92+ ...headers . all ?? { } ,
93+ ...headers [ scope ] ?? { } ,
94+ } ;
95+ }
96+
97+ /**
98+ * Get cookie
99+ * @param {string } cookieName - The name of the cookie to get
100+ * @returns {string } - The value of the cookie
101+ */
102+ export function getCookie ( cookieName ) {
103+ const cookies = document . cookie . split ( ';' ) ;
104+ let foundValue ;
105+
106+ cookies . forEach ( ( cookie ) => {
107+ const [ name , value ] = cookie . trim ( ) . split ( '=' ) ;
108+ if ( name === cookieName ) {
109+ foundValue = decodeURIComponent ( value ) ;
110+ }
111+ } ) ;
112+
113+ return foundValue ;
114+ }
115+
116+ export function checkIsAuthenticated ( ) {
117+ return ! ! getCookie ( 'auth_dropin_user_token' ) ?? false ;
118+ }
119+
32120/**
33121 * Fetches config from remote and saves in session, then returns it, otherwise
34122 * returns if it already exists.
35123 *
36- * @returns the config JSON from session storage
124+ * @returns { Promise<Object> } - The config JSON from session storage
37125 */
38- const getConfigFromSession = async ( ) => {
126+ async function getConfigFromSession ( ) {
39127 try {
40128 const configJSON = window . sessionStorage . getItem ( 'config' ) ;
41129 if ( ! configJSON ) {
@@ -57,37 +145,6 @@ const getConfigFromSession = async () => {
57145 window . sessionStorage . setItem ( 'config' , JSON . stringify ( configJSON ) ) ;
58146 return configJSON ;
59147 }
60- } ;
61-
62- /**
63- * Retrieves the commerce config.
64- *
65- * @returns {Promise<Object> } - The commerce config.
66- */
67- let configCache ;
68- const getConfig = async ( ) => {
69- if ( ! configCache ) {
70- // Only fetch if not already cached
71- const result = await applyConfigOverrides ( await getConfigFromSession ( ) ) ;
72- configCache = result ;
73- }
74- return configCache ;
75- } ;
76-
77- /**
78- * Get root path
79- */
80- export function getRootPath ( ) {
81- return AEM_ROOT_PATH ?? '/' ;
82- }
83-
84- /**
85- *
86- * @returns true if public config contains more than "default"
87- */
88- export async function isMultistore ( ) {
89- const config = await getConfigFromSession ( ) ;
90- return Object . keys ( config . public ) . filter ( ( root ) => root !== 'default' ) . length > 0 ;
91148}
92149
93150/**
@@ -96,24 +153,7 @@ export async function isMultistore() {
96153 * @param {Object } config - The base config.
97154 * @returns {Object } - The config with overrides applied.
98155 */
99- async function applyConfigOverrides ( config ) {
100- const root = Object . keys ( config . public )
101- // Sort by number of non-empty segments to find the deepest path
102- . sort ( ( a , b ) => {
103- const aSegments = a . split ( '/' ) . filter ( Boolean ) . length ;
104- const bSegments = b . split ( '/' ) . filter ( Boolean ) . length ;
105- return bSegments - aSegments ;
106- } )
107- . find ( ( key ) => window . location . pathname === key || window . location . pathname . startsWith ( key ) ) ;
108-
109- const rootPath = root ?? '/' ;
110-
111- if ( ! rootPath . startsWith ( '/' ) || ! rootPath . endsWith ( '/' ) ) {
112- throw new Error ( 'Invalid root path' ) ;
113- }
114-
115- AEM_ROOT_PATH = rootPath ;
116-
156+ async function applyConfigOverrides ( config , root ) {
117157 const defaultConfig = config . public ?. default ;
118158
119159 if ( ! defaultConfig ) {
@@ -129,41 +169,10 @@ async function applyConfigOverrides(config) {
129169}
130170
131171/**
132- * This function retrieves a configuration value .
172+ * Retrieves the commerce config .
133173 *
134- * @param {string } configParam - The configuration parameter to retrieve.
135- * @returns {Promise<string|undefined> } - The value of the configuration parameter, or undefined.
136- */
137- export const getConfigValue = async ( configParam ) => {
138- const config = await getConfig ( ) ;
139- return getValue ( config , configParam ) ;
140- } ;
141-
142- /**
143- * Retrieves headers from config entries like commerce.headers.pdp.my-header, etc and
144- * returns as object of all headers like { my-header: value, ... }
174+ * @returns {Promise<Object> } - The commerce config.
145175 */
146- export const getHeaders = async ( scope ) => {
147- const config = await getConfig ( ) ;
148- const headers = config . headers ?? { } ;
149- return {
150- ...headers . all ?? { } ,
151- ...headers [ scope ] ?? { } ,
152- } ;
153- } ;
154-
155- export const getCookie = ( cookieName ) => {
156- const cookies = document . cookie . split ( ';' ) ;
157- let foundValue ;
158-
159- cookies . forEach ( ( cookie ) => {
160- const [ name , value ] = cookie . trim ( ) . split ( '=' ) ;
161- if ( name === cookieName ) {
162- foundValue = decodeURIComponent ( value ) ;
163- }
164- } ) ;
165-
166- return foundValue ;
167- } ;
168-
169- export const checkIsAuthenticated = ( ) => ! ! getCookie ( 'auth_dropin_user_token' ) ?? false ;
176+ async function getConfig ( ) {
177+ return getConfigFromSession ( ) ;
178+ }
0 commit comments