1
- const ALLOWED_CONFIGS = [ " prod" , " stage" , " dev" ] ;
1
+ const ALLOWED_CONFIGS = [ ' prod' , ' stage' , ' dev' ] ;
2
2
3
3
/**
4
4
* This function calculates the environment in which the site is running based on the URL.
@@ -9,19 +9,19 @@ const ALLOWED_CONFIGS = ["prod", "stage", "dev"];
9
9
*/
10
10
export const calcEnvironment = ( ) => {
11
11
const { host, href } = window . location ;
12
- let environment = " prod" ;
13
- if ( href . includes ( " .aem.page" ) || host . includes ( " staging" ) ) {
14
- environment = " stage" ;
12
+ let environment = ' prod' ;
13
+ if ( href . includes ( ' .aem.page' ) || host . includes ( ' staging' ) ) {
14
+ environment = ' stage' ;
15
15
}
16
- if ( href . includes ( " localhost" ) ) {
17
- environment = " dev" ;
16
+ if ( href . includes ( ' localhost' ) ) {
17
+ environment = ' dev' ;
18
18
}
19
19
20
- const environmentFromConfig = window . sessionStorage . getItem ( " environment" ) ;
20
+ const environmentFromConfig = window . sessionStorage . getItem ( ' environment' ) ;
21
21
if (
22
- environmentFromConfig &&
23
- ALLOWED_CONFIGS . includes ( environmentFromConfig ) &&
24
- environment !== " prod"
22
+ environmentFromConfig
23
+ && ALLOWED_CONFIGS . includes ( environmentFromConfig )
24
+ && environment !== ' prod'
25
25
) {
26
26
return environmentFromConfig ;
27
27
}
@@ -31,8 +31,8 @@ export const calcEnvironment = () => {
31
31
32
32
function buildConfigURL ( environment ) {
33
33
const env = environment || calcEnvironment ( ) ;
34
- let fileName = " configs.json" ;
35
- if ( env !== " prod" ) {
34
+ let fileName = ' configs.json' ;
35
+ if ( env !== ' prod' ) {
36
36
fileName = `configs-${ env } .json` ;
37
37
}
38
38
const configURL = new URL ( `${ window . location . origin } /${ fileName } ` ) ;
@@ -45,15 +45,15 @@ const getConfigForEnvironment = async (environment) => {
45
45
try {
46
46
const configJSON = window . sessionStorage . getItem ( `config:${ env } ` ) ;
47
47
if ( ! configJSON ) {
48
- throw new Error ( " No config in session storage" ) ;
48
+ throw new Error ( ' No config in session storage' ) ;
49
49
}
50
50
51
51
const parsedConfig = JSON . parse ( configJSON ) ;
52
52
if (
53
- ! parsedConfig [ " :expiry" ] ||
54
- parsedConfig [ " :expiry" ] < Math . round ( Date . now ( ) / 1000 )
53
+ ! parsedConfig [ ' :expiry' ]
54
+ || parsedConfig [ ' :expiry' ] < Math . round ( Date . now ( ) / 1000 )
55
55
) {
56
- throw new Error ( " Config expired" ) ;
56
+ throw new Error ( ' Config expired' ) ;
57
57
}
58
58
59
59
return parsedConfig ;
@@ -63,7 +63,7 @@ const getConfigForEnvironment = async (environment) => {
63
63
throw new Error ( `Failed to fetch config for ${ env } ` ) ;
64
64
}
65
65
configJSON = await configJSON . json ( ) ;
66
- configJSON [ " :expiry" ] = Math . round ( Date . now ( ) / 1000 ) + 7200 ;
66
+ configJSON [ ' :expiry' ] = Math . round ( Date . now ( ) / 1000 ) + 7200 ;
67
67
window . sessionStorage . setItem ( `config:${ env } ` , JSON . stringify ( configJSON ) ) ;
68
68
return configJSON ;
69
69
}
@@ -94,25 +94,23 @@ export const getConfigValue = async (configParam, environment) => {
94
94
export const getHeaders = async ( scope , environment ) => {
95
95
const env = environment || calcEnvironment ( ) ;
96
96
const config = await getConfigForEnvironment ( env ) ;
97
- const configElements = config . data . filter ( ( el ) =>
98
- el ?. key . includes ( `headers.${ scope } ` )
99
- ) ;
97
+ const configElements = config . data . filter ( ( el ) => el ?. key . includes ( `headers.${ scope } ` ) ) ;
100
98
101
99
return configElements . reduce ( ( obj , item ) => {
102
100
let { key } = item ;
103
101
if ( key . includes ( `commerce.headers.${ scope } .` ) ) {
104
- key = key . replace ( `commerce.headers.${ scope } .` , "" ) ;
102
+ key = key . replace ( `commerce.headers.${ scope } .` , '' ) ;
105
103
}
106
104
return { ...obj , [ key ] : item . value } ;
107
105
} , { } ) ;
108
106
} ;
109
107
110
108
export const getCookie = ( cookieName ) => {
111
- const cookies = document . cookie . split ( ";" ) ;
109
+ const cookies = document . cookie . split ( ';' ) ;
112
110
let foundValue ;
113
111
114
112
cookies . forEach ( ( cookie ) => {
115
- const [ name , value ] = cookie . trim ( ) . split ( "=" ) ;
113
+ const [ name , value ] = cookie . trim ( ) . split ( '=' ) ;
116
114
if ( name === cookieName ) {
117
115
foundValue = decodeURIComponent ( value ) ;
118
116
}
@@ -121,5 +119,4 @@ export const getCookie = (cookieName) => {
121
119
return foundValue ;
122
120
} ;
123
121
124
- export const checkIsAuthenticated = ( ) =>
125
- ! ! getCookie ( "auth_dropin_user_token" ) ?? false ;
122
+ export const checkIsAuthenticated = ( ) => ! ! getCookie ( 'auth_dropin_user_token' ) ?? false ;
0 commit comments