Skip to content

Commit

Permalink
Merge pull request #234 from contentstack/staging
Browse files Browse the repository at this point in the history
Staging-->Master
  • Loading branch information
Contentstack-AnkitaD authored Dec 11, 2024
2 parents d722d4d + 3e9fde0 commit 8db1510
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 62 deletions.
132 changes: 80 additions & 52 deletions fetch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-contentstack",
"version": "5.3.2",
"version": "5.3.3",
"description": "Gatsby source plugin for building websites using Contentstack as a data source",
"scripts": {
"prepublish": "npm run build",
Expand Down
34 changes: 27 additions & 7 deletions src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const getData = async (url, options) => {
});
};

const fetchCsData = async (url, config, query) => {
const fetchCsData = async (url, config, query, SyncRetryCount = 0) => {
query = query || {};
query.include_count = true;
query.environment = config.environment;
Expand Down Expand Up @@ -296,12 +296,32 @@ const getSyncData = async (
* To make final sync call and concatenate the result if found any during on fetch request.
*/
const aggregatedSyncToken = syncToken.filter(item => item !== undefined);
for (const token of aggregatedSyncToken) {
const syncResponse = await fetchCsData(
url,
config,
(query = { sync_token: token })
);
for (const token of aggregatedSyncToken) {

let syncResponse;
try {

syncResponse = await fetchCsData(
url,
config,
(query = { sync_token: token }),
0 // Reset SyncRetryCount for each call
);
} catch (error) {
if (SyncRetryCount < config.httpRetries) {
const timeToWait = 2 ** SyncRetryCount * 100;
//Retry attempt ${retries + 1} after sync token error. Waiting for ${timeToWait} ms...
await waitFor(timeToWait);
return syncResponse = await fetchCsData(
url,
config,
(query = { sync_token: token }),
SyncRetryCount + 1
);
} else {
throw new Error(`Failed to fetch sync data after ${config.httpRetries} retry attempts due to invalid sync token.`);
}
}
aggregatedResponse.data = aggregatedResponse.data?.concat(
...syncResponse.items
);
Expand Down

0 comments on commit 8db1510

Please sign in to comment.