@@ -17,30 +17,37 @@ import {
1717} from "./constants" ;
1818import { Optimizely } from "./libs/optimizely" ;
1919import { convertObjectToString , convertStringToLowercase } from "./utils/convertValues" ;
20+ import { isEqual } from "./utils/isEqual" ;
21+ import { isEmpty } from "./utils/typeCheck" ;
2022
2123/**
2224 * @description Source nodes from cache
2325 * @param {Object } cache
2426 * @returns {Promise<void> } Cache promise
2527 */
26- const handleSourceNodesFromCache = async ( cache = null , helpers = { } ) =>
27- cache && Array . isArray ( cache ) && cache ?. length > 0
28- ? await Promise . allSettled (
29- cache . map ( ( result ) => {
30- const { nodeName = "" , data = null } = result ;
31-
32- return data && Array . isArray ( data ) && data . length > 0 && typeof nodeName === "string" && nodeName ?. length > 0
33- ? data . map ( async ( item ) =>
34- item && Array . isArray ( item ) && item . length > 0
35- ? item . map ( async ( datum ) => await handleCreateNodeFromData ( datum , nodeName , helpers , convertStringToLowercase ( datum ?. contentLink ?. url ) ) )
36- : await handleCreateNodeFromData ( item , nodeName , helpers , convertStringToLowercase ( item ?. contentLink ?. url ) )
37- )
38- : null ;
39- } )
40- )
41- . then ( ( res ) => res )
42- . catch ( ( err ) => err )
43- : null ;
28+ const handleSourceNodesFromCache = async ( cache = null , helpers = { } ) => {
29+ if ( ! isEmpty ( cache ) ) {
30+ await Promise . allSettled (
31+ cache . map ( async ( result ) => {
32+ const { nodeName = "" , data = null } = result ;
33+
34+ await handleCreateNodeFromData ( data , nodeName , helpers , convertStringToLowercase ( data ?. contentLink ?. url ) ) ;
35+ } )
36+ )
37+ . then ( ( res ) => {
38+ // Send log message to console if node creation was successful
39+ console . info ( `@epicdesignlabs/gatsby-source-optimizely: Successfully created nodes from ${ cache ?. length } endpoint(s)` ) ;
40+
41+ return Promise . resolve ( res ) ;
42+ } )
43+ . catch ( ( err ) => {
44+ // Send log message to console if node creation failed
45+ console . error ( `@epicdesignlabs/gatsby-source-optimizely: Failed to create nodes from ${ cache ?. length } endpoint(s) - ${ err . message } ` ) ;
46+
47+ return Promise . reject ( err ) ;
48+ } ) ;
49+ }
50+ } ;
4451
4552/**
4653 * @description Create a node from the data
@@ -197,62 +204,74 @@ exports.sourceNodes = async ({ actions, cache, createNodeId, createContentDigest
197204 // Authenticate with the Optimizely/Episerver
198205 const auth = await optimizely . authenticate ( ) ;
199206
200- console . log ( auth ) ;
201-
202- // Get the endpoints from the Optimizely/Episerver site and store as promises
203- // for (const [nodeName, endpoint] of Object.entries(endpoints)) {
204- // try {
205- // const url = site_url + endpoint;
206- // const { data: endpointData } = await optimizely.get({
207- // url,
208- // headers: {
209- // ...headers,
210- // "Authorization": `Bearer ${auth?.access_token}`,
211- // "Access-Control-Allow-Credentials": ACCESS_CONTROL_ALLOW_CREDENTIALS
212- // }
213- // });
214-
215- // dataPromises.push({
216- // nodeName,
217- // data: endpointData || null
218- // });
219- // } catch (err) {
220- // console.error(`[GET] ${site_url + endpoint} (${err.status + " " + err.statusText})`);
221- // return Promise.reject(err);
222- // }
223- // }
224-
225- // Store the data in the cache
226- // sourceData = dataPromises?.length > 0 ? dataPromises : sourceData;
227-
228- // Cache the data
229- // await cache
230- // .set(cacheKey, sourceData)
231- // .then(async () => {
232- // // If the data is cached, display a success message
233- // console.info(`[CACHE] ${cacheKey} (OK) - ${sourceData?.length || 0} items`);
234-
235- // // If the data is cached, resolve the promise
236- // return Promise.resolve(sourceData);
237- // })
238- // .catch((err) => {
239- // // If the data is not cached, display an error message
240- // console.error(`[CACHE] ${cacheKey} - (FAIL) - ${err.message}`);
241- // return Promise.reject(err);
242- // });
243-
244- // Create nodes from the cached data
245- // sourceData?.map(
246- // ({ nodeName = "", data = null }) =>
247- // data?.map(async (item) =>
248- // item && Array.isArray(item) && item.length > 0
249- // ? await Promise.allSettled(item.map(async (datum) => await handleCreateNodeFromData(datum, nodeName, helpers, datum.contentLink.url)))
250- // : await handleCreateNodeFromData(item, nodeName, helpers, item.contentLink.url)
251- // ) || null
252- // ) || null;
253-
254- // Resolve the promise
255- return Promise . resolve ( sourceData ) ;
207+ if ( ! isEmpty ( auth ?. access_token ) ) {
208+ // Send log message to console if authentication was successful
209+ console . info ( `[AUTH] ${ convertObjectToString ( auth ) } ` ) ;
210+
211+ // Get the endpoints from the Optimizely/Episerver site and store as promises
212+ for ( const [ nodeName , endpoint ] of Object . entries ( endpoints ) ) {
213+ try {
214+ const url = site_url + endpoint ;
215+
216+ const results = await optimizely . get ( {
217+ url,
218+ headers : {
219+ ...headers ,
220+ "Authorization" : `Bearer ${ auth ?. access_token } ` ,
221+ "Access-Control-Allow-Credentials" : ACCESS_CONTROL_ALLOW_CREDENTIALS
222+ } ,
223+ endpoint : nodeName
224+ } ) ;
225+
226+ dataPromises . push ( {
227+ nodeName,
228+ data : results || null
229+ } ) ;
230+ } catch ( err ) {
231+ // Send log message to console if an error occurred
232+ console . error ( `[GET] ${ site_url + endpoint } (${ err . status + " " + err . statusText } )` ) ;
233+
234+ // Reject the promise
235+ return Promise . reject ( err ) ;
236+ }
237+ }
238+
239+ if ( ! isEmpty ( dataPromises ) ) {
240+ // Compare the data in the cache with the data from the Optimizely/Episerver site
241+ if ( ! isEqual ( sourceData , dataPromises ) ) {
242+ // Store the data in the cache
243+ sourceData = dataPromises || sourceData ;
244+
245+ // If the source data is different from the cached data, display a success message
246+ console . info ( `[CACHE] ${ cacheKey } (OK) - ${ sourceData ?. length || 0 } items` ) ;
247+
248+ // Cache the data
249+ await cache . set ( cacheKey , sourceData ) ;
250+ } else {
251+ // If the source data is the same as the cached data, display a warning message
252+ console . warn ( `[CACHE] Cache with key ${ cacheKey } was detected but no changes were made. Proceeding with node creation using cached data.` ) ;
253+ }
254+
255+ console . log ( sourceData ) ;
256+
257+ // Create nodes from the data
258+ sourceData ?. map ( ( { nodeName = "" , data = null } ) =>
259+ data ?. map ( async ( item ) =>
260+ item && Array . isArray ( item ) && item . length > 0
261+ ? await Promise . allSettled ( item . map ( async ( datum ) => await handleCreateNodeFromData ( datum , nodeName , helpers , datum . contentLink . url ) ) )
262+ : await handleCreateNodeFromData ( item , nodeName , helpers , item . contentLink . url )
263+ )
264+ ) ;
265+ } else {
266+ console . error ( `@epicdesignlabs/gatsby-source-optimizely: No data was sourced from ${ site_url } ` ) ;
267+
268+ return Promise . reject ( ) ;
269+ }
270+ } else {
271+ console . error ( `[AUTH] ${ site_url } (${ auth ?. error_description } )` ) ;
272+
273+ return Promise . reject ( auth ) ;
274+ }
256275} ;
257276
258277exports . onPostBuild = async ( { reporter, basePath, pathPrefix } ) => reporter . info ( `@epicdesignlabs/gatsby-source-optimizely task processing complete with current site built with "basePath": ${ basePath } and "pathPrefix": ${ pathPrefix } ` ) ;
0 commit comments