Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added support for subsequent sync calls #201

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .github/workflows/sast-scan.yml

This file was deleted.

11 changes: 0 additions & 11 deletions .github/workflows/secrets-scan.yml

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Here’s an example site built using this source plugin: https://xenodochial-hod

>- Use Node v18+ and React v18+ while using v5.x.x of gatsby-source-contentstack.
>- Please refer migration guide: [Migrating from v4 to v5](https://v5.gatsbyjs.com/docs/reference/release-notes/migrating-from-v4-to-v5/)
>- Added support for subsequent fetch calls when data is being published during ongoing init calls or build process.


## Install
Expand Down
65 changes: 60 additions & 5 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.0.4",
"version": "5.0.5",
"description": "Gatsby source plugin for building websites using Contentstack as a data source",
"scripts": {
"prepublish": "npm run build",
Expand Down
34 changes: 34 additions & 0 deletions src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const OPTIONS_ENTRIES_CLASS_MAPPING = {
let activity;
let globalConfig;

const syncToken = [];


exports.fetchData = async (
configOptions,
reporter,
Expand Down Expand Up @@ -224,6 +227,18 @@ const getSyncData = async (
aggregatedResponse = null
) => {
const response = await fetchCsData(url, config, query);

/*
Below syncToken array would contain type --> 'asset_published', 'entry_published' sync tokens
*/
if (
response.items.some(item =>
['entry_published', 'asset_published'].includes(item.type)
)
) {
syncToken.push(response.sync_token);
}

if (!aggregatedResponse) {
aggregatedResponse = {};
aggregatedResponse.data = [];
Expand All @@ -247,5 +262,24 @@ const getSyncData = async (
aggregatedResponse
);
}

if (response.sync_token) {
/**
* 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 })
);
aggregatedResponse.data = aggregatedResponse.data?.concat(
...syncResponse.items
);
aggregatedResponse.sync_token = syncResponse.sync_token;
}
}

return aggregatedResponse;
};