From 64ff62f379a6100ad26280051580419b75ac0905 Mon Sep 17 00:00:00 2001 From: Vikram Kalta Date: Sun, 26 Sep 2021 12:36:59 +0530 Subject: [PATCH 1/2] fix: added additional params in sync api to fetch unpublished and deleted data --- fetch.js | 4 +-- gatsby-node.js | 71 +++++++++++++++++++++------------------------- src/fetch.js | 4 +-- src/gatsby-node.js | 61 ++++++--------------------------------- 4 files changed, 45 insertions(+), 95 deletions(-) diff --git a/fetch.js b/fetch.js index 79e2798..31d176a 100644 --- a/fetch.js +++ b/fetch.js @@ -42,8 +42,8 @@ exports.fetchData = /*#__PURE__*/function () { } : { init: true }; - syncEntryParams.type = 'entry_published'; - syncAssetParams.type = 'asset_published'; + syncEntryParams.type = 'entry_published, entry_unpublished, entry_deleted'; + syncAssetParams.type = 'asset_published, asset_unpublished, asset_deleted'; _context.prev = 8; _context.next = 11; return Promise.all([fetchSyncData(syncEntryParams, configOptions), fetchSyncData(syncAssetParams, configOptions)]); diff --git a/gatsby-node.js b/gatsby-node.js index 5c69c8c..e432c2f 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -135,7 +135,7 @@ exports.createSchemaCustomization = /*#__PURE__*/function () { exports.sourceNodes = /*#__PURE__*/function () { var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(_ref5, configOptions) { - var cache, actions, getNode, getNodes, createNodeId, store, reporter, createContentDigest, getNodesByType, getCache, createNode, deleteNode, touchNode, setPluginStatus, syncToken, _store$getState, status, typePrefix, _yield$fetchData, contentstackData, syncData, entriesNodeIds, assetsNodeIds, existingNodes, countOfSupportedFormatFiles, deleteContentstackNodes, nextSyncToken, newState; + var cache, actions, getNode, getNodes, createNodeId, reporter, createContentDigest, getNodesByType, getCache, createNode, deleteNode, touchNode, typePrefix, tokenKey, syncToken, _yield$fetchData, contentstackData, syncData, entriesNodeIds, assetsNodeIds, existingNodes, countOfSupportedFormatFiles, deleteContentstackNodes; return _regenerator["default"].wrap(function _callee2$(_context2) { while (1) { @@ -160,27 +160,27 @@ exports.sourceNodes = /*#__PURE__*/function () { } }; - cache = _ref5.cache, actions = _ref5.actions, getNode = _ref5.getNode, getNodes = _ref5.getNodes, createNodeId = _ref5.createNodeId, store = _ref5.store, reporter = _ref5.reporter, createContentDigest = _ref5.createContentDigest, getNodesByType = _ref5.getNodesByType, getCache = _ref5.getCache; - createNode = actions.createNode, deleteNode = actions.deleteNode, touchNode = actions.touchNode, setPluginStatus = actions.setPluginStatus; - _store$getState = store.getState(), status = _store$getState.status; // use a custom type prefix if specified + cache = _ref5.cache, actions = _ref5.actions, getNode = _ref5.getNode, getNodes = _ref5.getNodes, createNodeId = _ref5.createNodeId, reporter = _ref5.reporter, createContentDigest = _ref5.createContentDigest, getNodesByType = _ref5.getNodesByType, getCache = _ref5.getCache; + createNode = actions.createNode, deleteNode = actions.deleteNode, touchNode = actions.touchNode; // use a custom type prefix if specified typePrefix = configOptions.type_prefix || 'Contentstack'; + tokenKey = "".concat(typePrefix.toLowerCase(), "-sync-token-").concat(configOptions.api_key); + _context2.next = 7; + return cache.get(tokenKey); - if (status && status.plugins && status.plugins['gatsby-source-contentstack']) { - syncToken = status.plugins['gatsby-source-contentstack']["".concat(typePrefix.toLowerCase(), "-sync-token-").concat(configOptions.api_key)]; - } - + case 7: + syncToken = _context2.sent; configOptions.syncToken = syncToken || null; - _context2.next = 9; + _context2.next = 11; return fetchData(configOptions, reporter); - case 9: + case 11: _yield$fetchData = _context2.sent; contentstackData = _yield$fetchData.contentstackData; - _context2.next = 13; + _context2.next = 15; return cache.get(typePrefix); - case 13: + case 15: contentstackData.contentTypes = _context2.sent; syncData = contentstackData.syncData.reduce(function (merged, item) { if (!merged[item.type]) { @@ -249,18 +249,14 @@ exports.sourceNodes = /*#__PURE__*/function () { _context2.t0 = configOptions.downloadImages; if (!_context2.t0) { - _context2.next = 26; + _context2.next = 28; break; } - _context2.next = 26; + _context2.next = 28; return cache.set(SUPPORTED_FILES_COUNT, countOfSupportedFormatFiles); - case 26: - // syncData.asset_published && syncData.asset_published.forEach((item) => { - // const entryNodeId = makeAssetNodeUid(item.data, createNodeId, typePrefix); - // assetsNodeIds.add(entryNodeId); - // }); + case 28: // adding nodes contentstackData.contentTypes.forEach(function (contentType) { contentType.uid = contentType.uid.replace(/-/g, '_'); @@ -283,12 +279,12 @@ exports.sourceNodes = /*#__PURE__*/function () { }); if (!configOptions.downloadImages) { - _context2.next = 38; + _context2.next = 40; break; } - _context2.prev = 30; - _context2.next = 33; + _context2.prev = 32; + _context2.next = 35; return downloadAssets({ cache: cache, getCache: getCache, @@ -298,28 +294,28 @@ exports.sourceNodes = /*#__PURE__*/function () { reporter: reporter }, typePrefix, configOptions); - case 33: - _context2.next = 38; + case 35: + _context2.next = 40; break; - case 35: - _context2.prev = 35; - _context2.t1 = _context2["catch"](30); + case 37: + _context2.prev = 37; + _context2.t1 = _context2["catch"](32); reporter.info('Something went wrong while downloading assets. Details: ' + _context2.t1); - case 38: + case 40: // deleting nodes syncData.entry_unpublished && syncData.entry_unpublished.forEach(function (item) { - deleteContentstackNodes(item.data, 'entry'); + return deleteContentstackNodes(item.data, 'entry'); }); syncData.asset_unpublished && syncData.asset_unpublished.forEach(function (item) { - deleteContentstackNodes(item.data, 'asset'); + return deleteContentstackNodes(item.data, 'asset'); }); syncData.entry_deleted && syncData.entry_deleted.forEach(function (item) { - deleteContentstackNodes(item.data, 'entry'); + return deleteContentstackNodes(item.data, 'entry'); }); syncData.asset_deleted && syncData.asset_deleted.forEach(function (item) { - deleteContentstackNodes(item.data, 'asset'); + return deleteContentstackNodes(item.data, 'asset'); }); syncData.content_type_deleted && syncData.content_type_deleted.forEach(function (item) { item.content_type_uid = item.content_type_uid.replace(/-/g, '_'); @@ -329,20 +325,17 @@ exports.sourceNodes = /*#__PURE__*/function () { sameContentTypeNodes.forEach(function (node) { return deleteNode(node); }); - }); // Updating the syncToken - - nextSyncToken = contentstackData.sync_token; // Storing the sync state for the next sync + }); // Caching token for the next sync - newState = {}; - newState["".concat(typePrefix.toLowerCase(), "-sync-token-").concat(configOptions.api_key)] = nextSyncToken; - setPluginStatus(newState); + _context2.next = 47; + return cache.set(tokenKey, contentstackData.sync_token); case 47: case "end": return _context2.stop(); } } - }, _callee2, null, [[30, 35]]); + }, _callee2, null, [[32, 37]]); })); return function (_x3, _x4) { diff --git a/src/fetch.js b/src/fetch.js index d336340..265a63a 100644 --- a/src/fetch.js +++ b/src/fetch.js @@ -26,8 +26,8 @@ exports.fetchData = async (configOptions, reporter) => { init: true, }; - syncEntryParams.type = 'entry_published'; - syncAssetParams.type = 'asset_published'; + syncEntryParams.type = 'entry_published, entry_unpublished, entry_deleted'; + syncAssetParams.type = 'asset_published, asset_unpublished, asset_deleted'; try { const [syncEntryData, syncAssetData] = await Promise.all([fetchSyncData(syncEntryParams, configOptions), fetchSyncData(syncAssetParams, configOptions)]); diff --git a/src/gatsby-node.js b/src/gatsby-node.js index eda6ae2..c0e511b 100644 --- a/src/gatsby-node.js +++ b/src/gatsby-node.js @@ -108,36 +108,17 @@ exports.sourceNodes = async ({ getNode, getNodes, createNodeId, - store, reporter, createContentDigest, getNodesByType, getCache, }, configOptions) => { - const { - createNode, - deleteNode, - touchNode, - setPluginStatus, - } = actions; - let syncToken; - const { - status, - } = store.getState(); + const { createNode, deleteNode, touchNode } = actions; // use a custom type prefix if specified const typePrefix = configOptions.type_prefix || 'Contentstack'; - - if ( - status && - status.plugins && - status.plugins['gatsby-source-contentstack'] - ) { - syncToken = - status.plugins['gatsby-source-contentstack'][ - `${typePrefix.toLowerCase()}-sync-token-${configOptions.api_key}` - ]; - } + const tokenKey = `${typePrefix.toLowerCase()}-sync-token-${configOptions.api_key}`; + const syncToken = await cache.get(tokenKey); configOptions.syncToken = syncToken || null; @@ -213,10 +194,6 @@ exports.sourceNodes = async ({ }); // Cache the found count configOptions.downloadImages && await cache.set(SUPPORTED_FILES_COUNT, countOfSupportedFormatFiles); - // syncData.asset_published && syncData.asset_published.forEach((item) => { - // const entryNodeId = makeAssetNodeUid(item.data, createNodeId, typePrefix); - // assetsNodeIds.add(entryNodeId); - // }); // adding nodes contentstackData.contentTypes.forEach(contentType => { @@ -294,26 +271,13 @@ exports.sourceNodes = async ({ } // deleting nodes + syncData.entry_unpublished && syncData.entry_unpublished.forEach(item => deleteContentstackNodes(item.data, 'entry')); - syncData.entry_unpublished && - syncData.entry_unpublished.forEach(item => { - deleteContentstackNodes(item.data, 'entry'); - }); + syncData.asset_unpublished && syncData.asset_unpublished.forEach(item => deleteContentstackNodes(item.data, 'asset')); - syncData.asset_unpublished && - syncData.asset_unpublished.forEach(item => { - deleteContentstackNodes(item.data, 'asset'); - }); + syncData.entry_deleted && syncData.entry_deleted.forEach(item => deleteContentstackNodes(item.data, 'entry')); - syncData.entry_deleted && - syncData.entry_deleted.forEach(item => { - deleteContentstackNodes(item.data, 'entry'); - }); - - syncData.asset_deleted && - syncData.asset_deleted.forEach(item => { - deleteContentstackNodes(item.data, 'asset'); - }); + syncData.asset_deleted && syncData.asset_deleted.forEach(item => deleteContentstackNodes(item.data, 'asset')); syncData.content_type_deleted && syncData.content_type_deleted.forEach(item => { @@ -326,15 +290,8 @@ exports.sourceNodes = async ({ ); }); - // Updating the syncToken - const nextSyncToken = contentstackData.sync_token; - - // Storing the sync state for the next sync - const newState = {}; - newState[ - `${typePrefix.toLowerCase()}-sync-token-${configOptions.api_key}` - ] = nextSyncToken; - setPluginStatus(newState); + // Caching token for the next sync + await cache.set(tokenKey, contentstackData.sync_token); }; From 806fb769bc66d5d6b9162a808c06657cf3035bb2 Mon Sep 17 00:00:00 2001 From: Vikram Kalta Date: Sun, 26 Sep 2021 12:44:31 +0530 Subject: [PATCH 2/2] fix: updated version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2d99285..81fec1f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gatsby-source-contentstack", - "version": "3.0.0", + "version": "3.0.1", "description": "Gatsby source plugin for building websites using Contentstack as a data source", "scripts": { "prepublish": "npm run build",