@@ -38,7 +38,7 @@ class Optimizely {
3838 let promises = [ ] ;
3939
4040 values . map ( async ( value ) => {
41- if ( "contentLink" in value ) {
41+ if ( value && Object . prototype . hasOwnProperty . call ( value , "contentLink" ) && Object . keys ( value ) ?. length > 0 ) {
4242 if ( value . contentLink . id ) {
4343 promises . push ( request . run ( "get" , CONTENT_ENDPOINT + value . contentLink . id + "?expand=*" , body ) ) ;
4444 } else {
@@ -69,51 +69,35 @@ class Optimizely {
6969
7070 return Promise . resolve ( values ) ;
7171 } )
72- . catch ( ( err ) => {
73- this . log . error ( `An error occurred while fetching ${ "`" + label + "`" } data from the Optimizely/Episerver API` , err . message ) ;
74-
75- return Promise . reject ( err ) ;
76- } ) ;
77- } else if ( values && typeof values === "object" && Object . keys ( values ) ?. length > 0 ) {
72+ . catch ( ( err ) => Promise . reject ( err ) ) ;
73+ } else if ( values && Object . prototype . toString . call ( values ) === "[object Object]" && Object . keys ( values ) ?. length > 0 ) {
7874 let promises = [ ] ;
7975
80- Object . keys ( values ) . map ( async ( key ) => {
81- if ( "contentLink" in values [ key ] ) {
82- if ( values [ key ] . contentLink . id ) {
83- promises . push ( request . run ( "get" , CONTENT_ENDPOINT + values [ key ] . contentLink . id + "?expand=*" , body ) ) ;
84- } else {
85- let message = "Expanded `contentLink` in `" + key + "` is missing `id` key" ;
76+ if ( values && Object . prototype . hasOwnProperty . call ( values , "id" ) && Object . keys ( values ) ?. length > 0 ) {
77+ promises . push ( request . run ( "get" , CONTENT_ENDPOINT + values . id + "?expand=*" , body ) ) ;
78+ } else {
79+ let message = "Expanded `" + label + "` is missing `id` key" ;
8680
87- throw message ;
88- }
89- } else {
90- let message = "Expanded `" + key + "` is missing `contentLink` key" ;
91-
92- throw message ;
93- }
94- } ) ;
81+ throw message ;
82+ }
9583
9684 await Promise . allSettled ( promises )
9785 . then ( ( res ) => {
9886 if ( res && Array . isArray ( res ) && res ?. length > 0 ) {
9987 res
10088 . filter ( ( { status, value } ) => status === "fulfilled" && value !== null )
101- . map ( ( { value } , index ) => {
89+ . map ( ( { value } ) => {
10290 const { data } = value ;
10391
104- values [ Object . keys ( values ) [ index ] ] = data ;
92+ values = data ;
10593
106- return values [ Object . keys ( values ) [ index ] ] ;
94+ return values ;
10795 } ) ;
10896 }
10997
11098 return Promise . resolve ( values ) ;
11199 } )
112- . catch ( ( err ) => {
113- this . log . error ( `An error occurred while fetching ${ "`" + label + "`" } data from the Optimizely/Episerver API` , err . message ) ;
114-
115- return Promise . reject ( err ) ;
116- } ) ;
100+ . catch ( ( err ) => Promise . reject ( err ) ) ;
117101 } else {
118102 this . log . warn ( "Current `" + label + "` is not an array, object or null. Skipping..." ) ;
119103 }
@@ -137,26 +121,36 @@ class Optimizely {
137121 temp . contentLink ?. expanded ?. images && Array . isArray ( temp . contentLink ?. expanded ?. images ) && temp . contentLink ?. expanded ?. images ?. length > 0 ? await handleExpandedKeyValues ( temp . contentLink . expanded . images , "images" ) : null ;
138122 const formPromise =
139123 temp . contentLink ?. expanded ?. form && Array . isArray ( temp . contentLink ?. expanded ?. form ) && temp . contentLink ?. expanded ?. form ?. length > 0 ? await handleExpandedKeyValues ( temp . contentLink ?. expanded ?. form , "form" ) : null ;
124+
140125 const expandedKeyValuesPromises = [ dynamicStylesPromise , itemsPromise , imagesPromise , formPromise ] || [ ] ;
126+
141127 await Promise . allSettled ( expandedKeyValuesPromises )
142128 . then ( ( res ) => {
143129 if ( res && Array . isArray ( res ) && res ?. length > 0 ) {
144130 res
145131 . filter ( ( { status, value } ) => status === "fulfilled" && value !== null )
146132 . map ( ( { value } , index ) => {
147133 switch ( index ) {
148- case 0 :
134+ case 0 : {
135+ delete temp . contentLink . expanded . dynamicStyles ;
149136 temp . contentLink . expanded . dynamicStyles = value ;
150137 break ;
151- case 1 :
138+ }
139+ case 1 : {
140+ delete temp . contentLink . expanded . items ;
152141 temp . contentLink . expanded . items = value ;
153142 break ;
154- case 2 :
143+ }
144+ case 2 : {
145+ delete temp . contentLink . expanded . images ;
155146 temp . contentLink . expanded . images = value ;
156147 break ;
157- case 3 :
148+ }
149+ case 3 : {
150+ delete temp . contentLink . expanded . form ;
158151 temp . contentLink . expanded . form = value ;
159152 break ;
153+ }
160154 default :
161155 break ;
162156 }
@@ -223,30 +217,38 @@ class Optimizely {
223217 . catch ( ( err ) => Promise . reject ( err ) ) ;
224218
225219 return expandedData ;
226- } else if ( data && typeof data === "object" && Object . keys ( data ) . length > 0 ) {
227- const temp = Object . assign ( { } , data ) ;
220+ } else if ( data && Object . prototype . toString . call ( data ) === "[object Object]" && Object . keys ( data ) ?. length > 0 ) {
221+ try {
222+ const expandedDataPromise = new Promise ( ( resolve , reject ) => {
223+ const temp = Object . assign ( { } , data ) ;
228224
229- const { contentBlocks, contentBlocksTop, contentBlocksBottom } = temp ;
225+ const { contentBlocks, contentBlocksTop, contentBlocksBottom } = temp ;
230226
231- if ( contentBlocks && Array . isArray ( contentBlocks ) && contentBlocks ?. length > 0 ) {
232- let expandedContentBlocks = await handleContentBlocks ( contentBlocks ) ;
227+ if ( contentBlocks && Array . isArray ( contentBlocks ) && contentBlocks ?. length > 0 ) {
228+ let expandedContentBlocks = handleContentBlocks ( contentBlocks ) ;
233229
234- temp . contentBlocks = expandedContentBlocks ;
235- }
230+ temp . contentBlocks = expandedContentBlocks ;
231+ }
236232
237- if ( contentBlocksTop && Array . isArray ( contentBlocksTop ) && contentBlocksTop ?. length > 0 ) {
238- let expandedContentBlocksTop = await handleContentBlocks ( contentBlocksTop ) ;
233+ if ( contentBlocksTop && Array . isArray ( contentBlocksTop ) && contentBlocksTop ?. length > 0 ) {
234+ let expandedContentBlocksTop = handleContentBlocks ( contentBlocksTop ) ;
239235
240- temp . contentBlocksTop = expandedContentBlocksTop ;
241- }
236+ temp . contentBlocksTop = expandedContentBlocksTop ;
237+ }
242238
243- if ( contentBlocksBottom && Array . isArray ( contentBlocksBottom ) && contentBlocksBottom ?. length > 0 ) {
244- let expandedContentBlocksBottom = await handleContentBlocks ( contentBlocksBottom ) ;
239+ if ( contentBlocksBottom && Array . isArray ( contentBlocksBottom ) && contentBlocksBottom ?. length > 0 ) {
240+ let expandedContentBlocksBottom = handleContentBlocks ( contentBlocksBottom ) ;
245241
246- temp . contentBlocksBottom = expandedContentBlocksBottom ;
247- }
242+ temp . contentBlocksBottom = expandedContentBlocksBottom ;
243+ }
248244
249- return temp ;
245+ return temp ? resolve ( temp ) : reject ( temp ) ;
246+ } ) ;
247+
248+ return expandedDataPromise ;
249+ } catch ( err ) {
250+ return Promise . reject ( err ) ;
251+ }
250252 } else return data ;
251253 }
252254
0 commit comments