@@ -168,7 +168,7 @@ const getData = async (url, options) => {
168
168
} ) ;
169
169
} ;
170
170
171
- const fetchCsData = async ( url , config , query , SyncRetryCount = 0 ) => {
171
+ const fetchCsData = async ( url , config , query ) => {
172
172
query = query || { } ;
173
173
query . include_count = true ;
174
174
query . environment = config . environment ;
@@ -235,33 +235,33 @@ const getSyncData = async (
235
235
retries = 0
236
236
) => {
237
237
try {
238
- const response = await fetchCsData ( url , config , query ) ;
238
+ const response = await fetchCsData ( url , config , query ) ;
239
239
240
- /*
240
+ /*
241
241
Below syncToken array would contain type --> 'asset_published', 'entry_published' sync tokens
242
242
*/
243
- if (
244
- response . items . some ( item =>
245
- [ 'entry_published' , 'asset_published' ] . includes ( item . type )
246
- )
247
- ) {
248
- syncToken . push ( response . sync_token ) ;
249
- }
243
+ if (
244
+ response . items . some ( item =>
245
+ [ 'entry_published' , 'asset_published' ] . includes ( item . type )
246
+ )
247
+ ) {
248
+ syncToken . push ( response . sync_token ) ;
249
+ }
250
250
251
- if ( ! aggregatedResponse ) {
252
- aggregatedResponse = { } ;
253
- aggregatedResponse . data = [ ] ;
254
- aggregatedResponse . data = response [ responseKey ] ;
255
- aggregatedResponse . sync_token = response . sync_token ;
256
- } else {
257
- aggregatedResponse . data = aggregatedResponse . data || [ ] ;
258
- aggregatedResponse . data = aggregatedResponse . data . concat (
259
- response [ responseKey ]
260
- ) ;
261
- aggregatedResponse . sync_token = response . sync_token
262
- ? response . sync_token
263
- : aggregatedResponse . sync_token ;
264
- }
251
+ if ( ! aggregatedResponse ) {
252
+ aggregatedResponse = { } ;
253
+ aggregatedResponse . data = [ ] ;
254
+ aggregatedResponse . data = response [ responseKey ] ;
255
+ aggregatedResponse . sync_token = response . sync_token ;
256
+ } else {
257
+ aggregatedResponse . data = aggregatedResponse . data || [ ] ;
258
+ aggregatedResponse . data = aggregatedResponse . data . concat (
259
+ response [ responseKey ]
260
+ ) ;
261
+ aggregatedResponse . sync_token = response . sync_token
262
+ ? response . sync_token
263
+ : aggregatedResponse . sync_token ;
264
+ }
265
265
if ( response . pagination_token ) {
266
266
try {
267
267
return await getSyncData (
@@ -292,47 +292,45 @@ const getSyncData = async (
292
292
}
293
293
294
294
if ( response . sync_token ) {
295
- /**
296
- * To make final sync call and concatenate the result if found any during on fetch request.
297
- */
298
- const aggregatedSyncToken = syncToken . filter ( item => item !== undefined ) ;
295
+ /**
296
+ * To make final sync call and concatenate the result if found any during on fetch request.
297
+ */
298
+ const aggregatedSyncToken = syncToken . filter ( item => item !== undefined ) ;
299
+ let SyncRetryCount = 0 ;
299
300
for ( const token of aggregatedSyncToken ) {
300
-
301
301
let syncResponse ;
302
302
try {
303
-
304
303
syncResponse = await fetchCsData (
305
- url ,
306
- config ,
307
- ( query = { sync_token : token } ) ,
308
- 0 // Reset SyncRetryCount for each call
309
- ) ;
304
+ url ,
305
+ config ,
306
+ ( query = { sync_token : token } )
307
+ ) ;
310
308
} catch ( error ) {
311
- if ( SyncRetryCount < config . httpRetries ) {
309
+ if ( SyncRetryCount < config . httpRetries ) {
312
310
const timeToWait = 2 ** SyncRetryCount * 100 ;
311
+ SyncRetryCount ++ ;
313
312
//Retry attempt ${retries + 1} after sync token error. Waiting for ${timeToWait} ms...
314
313
await waitFor ( timeToWait ) ;
315
- return syncResponse = await fetchCsData (
314
+ return ( syncResponse = await fetchCsData (
316
315
url ,
317
316
config ,
318
- ( query = { sync_token : token } ) ,
319
- SyncRetryCount + 1
320
- ) ;
317
+ ( query = { sync_token : token } )
318
+ ) ) ;
321
319
} else {
322
320
throw new Error ( `Failed to fetch sync data after ${ config . httpRetries } retry attempts due to invalid sync token.` ) ;
323
321
}
324
322
}
325
- aggregatedResponse . data = aggregatedResponse . data ?. concat (
326
- ...syncResponse . items
327
- ) ;
328
- aggregatedResponse . sync_token = syncResponse . sync_token ;
323
+ aggregatedResponse . data = aggregatedResponse . data ?. concat (
324
+ ...syncResponse . items
325
+ ) ;
326
+ aggregatedResponse . sync_token = syncResponse . sync_token ;
327
+ }
329
328
}
330
- }
331
329
332
- syncToken = [ ] ;
333
- return aggregatedResponse ;
334
- } catch ( error ) {
335
- throw new Error ( `Failed to fetch sync data: ${ error . message } ` ) ;
336
- }
330
+ syncToken = [ ] ;
331
+ return aggregatedResponse ;
332
+ } catch ( error ) {
333
+ throw new Error ( `Failed to fetch sync data: ${ error . message } ` ) ;
334
+ }
337
335
} ;
338
336
0 commit comments