22
33import { ACCESS_CONTROL_ALLOW_CREDENTIALS , ACCESS_CONTROL_ALLOW_HEADERS , CORS_ORIGIN , REQUEST_TIMEOUT , REQUEST_URL_SLUG } from "./constants" ;
44import Auth from "./utils/auth" ;
5- import { convertObjectToString } from "./utils/convertValues" ;
5+ import { convertObjectToString , convertStringToLowercase } from "./utils/convertValues" ;
66import { logger } from "./utils/logger" ;
77import { Optimizely } from "./utils/optimizely" ;
88
@@ -20,7 +20,7 @@ const handleCreateNodeFromData = async (item, nodeType, helpers, endpoint, log)
2020
2121 const nodeMetadata = {
2222 ...item ,
23- id : createNodeId ( `${ nodeType } -${ item ?. id || item ?. contentLink ?. id || item ?. name || null } ` ) ,
23+ id : createNodeId ( `${ nodeType } -${ endpoint } ` ) ,
2424 parent : null ,
2525 children : [ ] ,
2626 internal : {
@@ -34,12 +34,12 @@ const handleCreateNodeFromData = async (item, nodeType, helpers, endpoint, log)
3434
3535 await createNode ( node )
3636 . then ( ( ) => {
37- log . http ( `[CREATE NODE] ${ endpoint } - ${ createNodeId ( `${ nodeType } -${ item . id || item . name } ` ) } (OK)` ) ;
37+ log . http ( `[NODE] ${ endpoint } - ${ createNodeId ( `${ nodeType } -${ endpoint } ` ) } (OK)` ) ;
3838
3939 return Promise . resolve ( node ) ;
4040 } )
4141 . catch ( ( err ) => {
42- log . error ( `[CREATE NODE] ${ endpoint } - ${ createNodeId ( `${ nodeType } -${ item . id || item . name } ` ) } (FAIL)` ) ;
42+ log . error ( `[NODE] ${ endpoint } - ${ createNodeId ( `${ nodeType } -${ endpoint } ` ) } (FAIL)` ) ;
4343
4444 return Promise . reject ( err ) ;
4545 } ) ;
@@ -108,7 +108,7 @@ exports.onPreInit = () => console.info("@epicdesignlabs/gatsby-source-optimizely
108108 * @param {Object } pluginOptions
109109 * @returns {Promise<void> } Node creation promise
110110 */
111- exports . sourceNodes = async ( { actions, createNodeId, createContentDigest } , pluginOptions ) => {
111+ exports . sourceNodes = async ( { actions, cache , createNodeId, createContentDigest } , pluginOptions ) => {
112112 // Prepare plugin options
113113 const {
114114 auth : { site_url = null , username = null , password = null , grant_type = "password" , client_id = "Default" , headers = { } } ,
@@ -127,46 +127,84 @@ exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }, plu
127127 createNodeId
128128 } ) ;
129129
130- // Authenticate with the Optimizely/Episerver
131- const auth = new Auth ( { site_url, username, password, grant_type, client_id, log, response_type, request_timeout } ) ;
132-
133- const authData = await auth . post ( ) ;
134-
135- // Display the auth data
136- log . info ( `(OK) [AUTH] ${ convertObjectToString ( authData ) } ` ) ;
137-
138- // Create a new Optimizely instance
139- const optimizely = new Optimizely ( {
140- site_url,
141- response_type,
142- headers : Object . assign ( headers , {
143- "Authorization" : `Bearer ${ authData . access_token } ` ,
144- "Access-Control-Allow-Headers" : ACCESS_CONTROL_ALLOW_HEADERS ,
145- "Access-Control-Allow-Credentials" : ACCESS_CONTROL_ALLOW_CREDENTIALS ,
146- "Access-Control-Allow-Origin" : CORS_ORIGIN
147- } ) ,
148- log,
149- request_timeout
150- } ) ;
130+ // Store the response from the Optimizely/Episerver API in the cache
131+ const cacheKey = "gatsby-source-optimizely-api-data-key" ;
132+ const nodes = [ ] ;
133+
134+ let sourceData = await cache . get ( cacheKey ) ;
135+
136+ // If the data is not cached, fetch it from the Optimizely/Episerver API
137+ if ( ! sourceData ) {
138+ // Authenticate with the Optimizely/Episerver
139+ const auth = new Auth ( { site_url, username, password, grant_type, client_id, log, response_type, request_timeout } ) ;
140+
141+ const authData = await auth . post ( ) ;
142+
143+ // Display the auth data
144+ authData ?. access_token ? log . info ( `[AUTH] ${ authData . access_token } - OK` ) : log . error ( `[AUTH] ${ authData . access_token } - FAIL` ) ;
145+
146+ // Create a new Optimizely instance
147+ const optimizely = new Optimizely ( {
148+ site_url,
149+ response_type,
150+ headers : Object . assign ( headers , {
151+ "Authorization" : `Bearer ${ authData . access_token } ` ,
152+ "Access-Control-Allow-Headers" : ACCESS_CONTROL_ALLOW_HEADERS ,
153+ "Access-Control-Allow-Credentials" : ACCESS_CONTROL_ALLOW_CREDENTIALS ,
154+ "Access-Control-Allow-Origin" : CORS_ORIGIN
155+ } ) ,
156+ log,
157+ request_timeout
158+ } ) ;
159+
160+ for ( const [ nodeName , endpoint ] of Object . entries ( endpoints ) ) {
161+ await optimizely
162+ . get ( endpoint )
163+ . then ( async ( res ) => {
164+ nodes . push ( {
165+ nodeName,
166+ data : res
167+ } ) ;
168+
169+ // Create nodes for each item in the response
170+ return res && Array . isArray ( res ) && res . length > 0
171+ ? await Promise . allSettled ( res . map ( async ( datum ) => Promise . resolve ( await handleCreateNodeFromData ( datum , nodeName , helpers , convertStringToLowercase ( datum . contentLink . url ) , log ) ) ) )
172+ : Promise . resolve ( await handleCreateNodeFromData ( res , nodeName , helpers , endpoint , log ) ) ;
173+ } )
174+ . catch ( ( err ) => {
175+ log . error ( `[GET] ${ site_url + REQUEST_URL_SLUG + endpoint } (${ err . status + " " + err . statusText } )` ) ;
151176
152- for ( const [ nodeName , endpoint ] of Object . entries ( endpoints ) ) {
153- try {
154- const res = await optimizely . get ( endpoint ) ;
155-
156- if ( res && Array . isArray ( res ) && res ?. length > 0 ) {
157- await Promise . allSettled (
158- res . map ( async ( datum ) => {
159- await handleCreateNodeFromData ( datum , nodeName , helpers , site_url + REQUEST_URL_SLUG + endpoint , log ) ;
160- } )
161- ) ;
162- } else {
163- await handleCreateNodeFromData ( res , nodeName , helpers , site_url + REQUEST_URL_SLUG + endpoint , log ) ;
164- }
165- } catch ( error ) {
166- console . log ( error ) ;
167-
168- log . error ( `An error occurred while fetching ${ endpoint } endpoint data` , error . message ) ;
177+ return Promise . reject ( err ) ;
178+ } ) ;
169179 }
180+
181+ // Store the response from the Optimizely/Episerver API in the cache
182+ sourceData = nodes . length > 0 ? nodes : sourceData ;
183+
184+ // Cache the data
185+ await cache
186+ . set ( cacheKey , sourceData )
187+ . then ( ( ) => {
188+ // If the data is cached, display a success message
189+ log . info ( `[CACHE] ${ cacheKey } (OK) - ${ sourceData ?. length || 0 } items` ) ;
190+
191+ return Promise . resolve ( ) ;
192+ } )
193+ . catch ( ( err ) => {
194+ // If the data is not cached, display an error message
195+ log . error ( `[CACHE] ${ cacheKey } (FAIL)` ) ;
196+
197+ return Promise . reject ( err ) ;
198+ } ) ;
199+ } else {
200+ // Create nodes for each item in the cached response
201+ sourceData ?. map ( ( { nodeName = "" , data = [ ] } ) =>
202+ data ?. map ( async ( item ) =>
203+ item && Array . isArray ( item ) && item . length > 0
204+ ? await Promise . allSettled ( item . map ( async ( datum ) => Promise . resolve ( await handleCreateNodeFromData ( datum , nodeName , helpers , convertStringToLowercase ( datum . contentLink . url ) , log ) ) ) )
205+ : Promise . resolve ( await handleCreateNodeFromData ( item , nodeName , helpers , convertStringToLowercase ( item . contentLink . url ) , log ) )
206+ )
207+ ) || null ;
170208 }
171209
172210 log . info ( "@epicdesignlabs/gatsby-source-optimizely task processing complete!" ) ;
0 commit comments