@@ -15,37 +15,30 @@ import Optimizely from "./utils/optimizely";
1515 * @param {Object } log
1616 * @returns {Promise<void> } Node creation promise
1717 */
18- const handleCreateNodeFromData = ( item , nodeType , helpers , endpoint , log ) => {
19- if ( item && Object . prototype . toString . call ( item ) === "[object Object]" && Object . keys ( item ) ?. length > 0 ) {
20- const nodeMetadata = {
21- ...item ,
22- id : helpers . createNodeId ( `${ nodeType } -${ item ?. id || item ?. name } ` ) ,
23- parent : null ,
24- children : [ ] ,
25- internal : {
26- type : nodeType ,
27- content : convertObjectToString ( item ) ,
28- contentDigest : helpers . createContentDigest ( item )
29- }
30- } ;
31-
32- const node = Object . assign ( { } , item , nodeMetadata ) ;
33-
34- helpers
35- . createNode ( node )
36- . then ( ( ) => {
37- log . warn ( `(OK) [CREATE NODE] ${ endpoint } - ${ helpers . createNodeId ( `${ nodeType } -${ item . id || item . name } ` ) } ` ) ;
38-
39- return node ;
40- } )
41- . catch ( ( err ) => {
42- log . error ( `(FAIL) [CREATE NODE] ${ endpoint } - ${ helpers . createNodeId ( `${ nodeType } -${ item . id || item . name } ` ) } ` , err . message ) ;
43-
44- throw err ;
45- } ) ;
18+ const handleCreateNodeFromData = async ( item , nodeType , helpers , endpoint , log ) => {
19+ const nodeMetadata = {
20+ ...item ,
21+ id : helpers . createNodeId ( `${ nodeType } -${ item ?. id || item ?. name } ` ) ,
22+ parent : null ,
23+ children : [ ] ,
24+ internal : {
25+ type : nodeType ,
26+ content : convertObjectToString ( item ) ,
27+ contentDigest : helpers . createContentDigest ( item )
28+ }
29+ } ;
30+
31+ const node = Object . assign ( { } , item , nodeMetadata ) ;
32+
33+ try {
34+ await helpers . createNode ( node ) ;
35+
36+ log . warn ( `(OK) [CREATE NODE] ${ endpoint } - ${ helpers . createNodeId ( `${ nodeType } -${ item . id || item . name } ` ) } ` ) ;
37+
38+ return node ;
39+ } catch ( error ) {
40+ log . error ( `(FAIL) [CREATE NODE] ${ endpoint } - ${ helpers . createNodeId ( `${ nodeType } -${ item . id || item . name } ` ) } ` , error . message ) ;
4641 }
47-
48- return Promise . resolve ( ) ;
4942} ;
5043
5144/**
@@ -112,15 +105,14 @@ exports.onPreInit = () => console.info("@epicdesignlabs/gatsby-source-optimizely
112105 * @param {Object } pluginOptions
113106 * @returns {Promise<void> } Node creation promise
114107 */
115- exports . sourceNodes = async ( { actions, cache , createNodeId, createContentDigest } , pluginOptions ) => {
108+ exports . sourceNodes = async ( { actions, createNodeId, createContentDigest } , pluginOptions ) => {
116109 // Prepare plugin options
117110 const {
118111 auth : { site_url = null , username = null , password = null , grant_type = "password" , client_id = "Default" , headers = { } } ,
119112 endpoints = null ,
120113 log_level = "info" ,
121114 response_type = "json" ,
122- request_timeout = REQUEST_TIMEOUT ,
123- enable_cache = false
115+ request_timeout = REQUEST_TIMEOUT
124116 } = pluginOptions ;
125117
126118 // Custom logger based on the `log_level` plugin option
@@ -132,72 +124,47 @@ exports.sourceNodes = async ({ actions, cache, createNodeId, createContentDigest
132124 createNodeId
133125 } ) ;
134126
135- // Store the response from the Optimizely/Episerver API in the cache
136- const cacheKey = "gatsby-source-optimizely-api-data-key" ;
137- let sourceData = await cache . get ( cacheKey ) ;
138-
139- // Fetch fresh data if nothing is found in the cache or a plugin option has changed
140- if ( ! sourceData || ! enable_cache ) {
141- // Authenticate with the Optimizely/Episerver
142- const auth = new Auth ( { site_url, username, password, grant_type, client_id, log, response_type, request_timeout } ) ;
143-
144- const authData = await auth . post ( ) ;
145-
146- log . warn ( "Cache is disabled or empty. Fetching fresh data from the Optimizely/Episerver API..." ) ;
147-
148- // Display the auth data
149- log . info ( `(OK) [AUTH] ${ convertObjectToString ( authData ) } ` ) ;
150-
151- // Create a new Optimizely instance
152- const optimizely = new Optimizely ( {
153- site_url,
154- response_type,
155- headers : Object . assign ( headers , {
156- "Authorization" : `Bearer ${ authData . access_token } ` ,
157- "Access-Control-Allow-Headers" : ACCESS_CONTROL_ALLOW_HEADERS ,
158- "Access-Control-Allow-Credentials" : ACCESS_CONTROL_ALLOW_CREDENTIALS ,
159- "Access-Control-Allow-Origin" : CORS_ORIGIN
160- } ) ,
161- log,
162- request_timeout
163- } ) ;
164-
165- // Get the endpoints from the Optimizely/Episerver site and create nodes
166- await Promise . allSettled (
167- Object . entries ( endpoints ) . map (
168- async ( [ nodeName , endpoint ] ) =>
169- await optimizely
170- . get ( endpoint )
171- . then ( ( res ) => [ res , nodeName , endpoint ] )
172- . catch ( ( err ) => {
173- log . error ( `An error occurred while fetching ${ endpoint } endpoint data` , err . message ) ;
174-
175- throw err ;
176- } )
177- )
178- )
179- . then ( async ( res ) => {
180- const data = await cache . set ( cacheKey , res ) ;
181-
182- sourceData = data ;
183-
184- return sourceData ;
185- } )
186- . catch ( ( err ) => {
187- log . error ( "An error occurred while fetching data from the Optimizely/Episerver API" , err . message ) ;
127+ // Authenticate with the Optimizely/Episerver
128+ const auth = new Auth ( { site_url, username, password, grant_type, client_id, log, response_type, request_timeout } ) ;
188129
189- throw err ;
190- } ) ;
191- }
130+ const authData = await auth . post ( ) ;
131+
132+ log . warn ( "Cache is disabled or empty. Fetching fresh data from the Optimizely/Episerver API..." ) ;
192133
193- // Create nodes from the response data
194- sourceData . forEach ( ( { status, value } ) =>
195- status === "fulfilled"
196- ? value [ 0 ] && Array . isArray ( value [ 0 ] ) && value [ 0 ] ?. length > 0
197- ? value [ 0 ] . map ( ( datum ) => handleCreateNodeFromData ( datum , value [ 1 ] , helpers , site_url + REQUEST_URL_SLUG + value [ 2 ] , log ) )
198- : handleCreateNodeFromData ( value [ 0 ] , value [ 1 ] , helpers , site_url + REQUEST_URL_SLUG + value [ 2 ] , log )
199- : null
200- ) ;
134+ // Display the auth data
135+ log . info ( `(OK) [AUTH] ${ convertObjectToString ( authData ) } ` ) ;
136+
137+ // Create a new Optimizely instance
138+ const optimizely = new Optimizely ( {
139+ site_url,
140+ response_type,
141+ headers : Object . assign ( headers , {
142+ "Authorization" : `Bearer ${ authData . access_token } ` ,
143+ "Access-Control-Allow-Headers" : ACCESS_CONTROL_ALLOW_HEADERS ,
144+ "Access-Control-Allow-Credentials" : ACCESS_CONTROL_ALLOW_CREDENTIALS ,
145+ "Access-Control-Allow-Origin" : CORS_ORIGIN
146+ } ) ,
147+ log,
148+ request_timeout
149+ } ) ;
150+
151+ for ( const [ nodeName , endpoint ] of Object . entries ( endpoints ) ) {
152+ try {
153+ const res = await optimizely . get ( endpoint ) ;
154+ if ( res && Array . isArray ( res ) && res ?. length > 0 ) {
155+ await Promise . allSettled (
156+ res . map ( async ( datum ) => {
157+ await handleCreateNodeFromData ( datum , nodeName , helpers , site_url + REQUEST_URL_SLUG + endpoint , log ) ;
158+ } )
159+ ) ;
160+ } else {
161+ await handleCreateNodeFromData ( res , nodeName , helpers , site_url + REQUEST_URL_SLUG + endpoint , log ) ;
162+ }
163+ } catch ( error ) {
164+ console . log ( error ) ;
165+ log . error ( `An error occurred while fetching ${ endpoint } endpoint data` , error . message ) ;
166+ }
167+ }
201168
202169 log . info ( "@epicdesignlabs/gatsby-source-optimizely task processing complete!" ) ;
203170
0 commit comments