@agility/content-sync 1.0.0 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agility/content-sync",
3
- "version": "1.0.0",
3
+ "version": "1.0.4",
4
4
  "description": "JavaScript SDK for synchronizing content from Agility CMS",
5
5
  "main": "dist/agility-sync-sdk.node.js",
6
6
  "scripts": {
@@ -15,9 +15,9 @@
15
15
  "author": "Agility CMS",
16
16
  "license": "MIT",
17
17
  "contributors": [
18
- "Joel Varty",
19
- "James Vidler",
20
- "Joshua Isaac"
18
+ "Joel Varty",
19
+ "James Vidler",
20
+ "Joshua Isaac"
21
21
  ],
22
22
  "bugs": {
23
23
  "url": "https://github.com/agility/agility-sync-sdk/issues"
@@ -26,7 +26,7 @@
26
26
  "dependencies": {
27
27
  "@agility/content-fetch": "^1.0.0",
28
28
  "dotenv": "^8.2.0",
29
- "proper-lockfile": "^4.1.1"
29
+ "proper-lockfile": "^4.1.2"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@babel/cli": "^7.2.3",
@@ -53,15 +53,19 @@ export default async function (languageCode, token) {
53
53
  const syncItems = syncRet.items;
54
54
 
55
55
  //if we don't get anything back, kick out
56
- if (syncItems.length === 0) {
57
- break;
56
+ if (syncItems.length > 0 ) {
57
+
58
+ for (let index = 0; index < syncItems.length; index++) {
59
+ await storeInterface.saveContentItem({ contentItem: syncItems[index], languageCode });
60
+ }
58
61
  }
59
62
 
60
- for (let index = 0; index < syncItems.length; index++) {
61
- await storeInterface.saveContentItem({ contentItem: syncItems[index], languageCode });
63
+ if (syncRet.syncToken > token) {
64
+ token = syncRet.syncToken;
65
+ } else {
66
+ break;
62
67
  }
63
68
 
64
- token = syncRet.syncToken;
65
69
  itemCount += syncItems.length;
66
70
 
67
71
  } while (token > 0 || busy === true)
@@ -1,7 +1,7 @@
1
1
  import { logInfo, logWarning, sleep } from '../util'
2
2
 
3
3
  export default async function (languageCode, token) {
4
- const storeInterface = this.store;
4
+ const storeInterface = this.store;
5
5
  if (!token) token = 0;
6
6
 
7
7
  let itemCount = 0;
@@ -29,7 +29,7 @@ export default async function (languageCode, token) {
29
29
  break
30
30
  }
31
31
 
32
- if (! busy) {
32
+ if (!busy) {
33
33
  busy = true
34
34
  logInfo("Sync API is busy. Waiting...")
35
35
  }
@@ -46,16 +46,21 @@ export default async function (languageCode, token) {
46
46
 
47
47
  const syncItems = syncRet.items;
48
48
 
49
- //if we don't get anything back, kick out
50
- if (syncItems.length === 0) {
51
- break;
49
+ //if we have something...
50
+ if (syncItems.length > 0) {
51
+
52
+
53
+ for (let index = 0; index < syncItems.length; index++) {
54
+ await storeInterface.savePageItem({ pageItem: syncItems[index], languageCode });
55
+ }
52
56
  }
53
57
 
54
- for (let index = 0; index < syncItems.length; index++) {
55
- await storeInterface.savePageItem({ pageItem: syncItems[index], languageCode });
58
+ if (syncRet.syncToken > token) {
59
+ token = syncRet.syncToken;
60
+ } else {
61
+ break;
56
62
  }
57
63
 
58
- token = syncRet.syncToken;
59
64
  itemCount += syncItems.length;
60
65
 
61
66
 
@@ -1,23 +1,64 @@
1
+ /**
2
+ * The function to handle saving/updating an item to your storage. This could be a Content Item, Page, Url Redirections, Sync State (state), or Sitemap.
3
+ * @param {Object} params - The parameters object
4
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
5
+ * @param {Object} params.item - The object representing the Content Item, Page, Url Redirections, Sync State (state), or Sitemap that needs to be saved/updated
6
+ * @param {String} params.itemType - The type of item being saved/updated, expected values are `item`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
7
+ * @param {String} params.languageCode - The locale code associated to the item being saved/updated
8
+ * @param {(String|Number)} params.itemID - The ID of the item being saved/updated - this could be a string or number depending on the itemType
9
+ * @returns {Void}
10
+ */
1
11
  const saveItem = async ({ options, item, itemType, languageCode, itemID }) => {
2
12
  console.log(`Console Interface: saveItem has been called`);
3
13
  return null;
4
14
  }
5
-
15
+ /**
16
+ * The function to handle deleting an item to your storage. This could be a Content Item, Page, Url Redirections, Sync State (state), or Sitemap.
17
+ * @param {Object} params - The parameters object
18
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
19
+ * @param {String} params.itemType - The type of item being deleted, expected values are `item`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
20
+ * @param {String} params.languageCode - The locale code associated to the item being saved/updated
21
+ * @param {(String|Number)} params.itemID - The ID of the item being deleted - this could be a string or number depending on the itemType
22
+ * @returns {Void}
23
+ */
6
24
  const deleteItem = async ({ options, itemType, languageCode, itemID }) => {
7
25
  console.log(`Console Interface: deleteItem has been called`);
8
26
  return null;
9
27
  }
10
-
28
+ /**
29
+ * The function to handle updating and placing a Content Item into a "list" so that you can handle querying a collection of items.
30
+ * @param {Object} params - The parameters object
31
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
32
+ * @param {Object} params.item - The object representing the Content Item
33
+ * @param {String} params.languageCode - The locale code associated to the item being saved/updated
34
+ * @param {(String|Number)} params.itemID - The ID of the item being updated - this could be a string or number depending on the itemType
35
+ * @param {String} params.referenceName - The reference name of the Content List that this Content Item should be added to
36
+ * @param {String} params.definitionName - The Model name that the Content Item is based on
37
+ * @returns {Void}
38
+ */
11
39
  const mergeItemToList = async ({ options, item, languageCode, itemID, referenceName, definitionName }) => {
12
40
  console.log(`Console Interface: mergeItemToList has been called`);
13
41
  return null;
14
42
  }
15
-
43
+ /**
44
+ * The function to handle retrieving a Content Item, Page, Url Redirections, Sync State (state), or Sitemap
45
+ * @param {Object} params - The parameters object
46
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
47
+ * @param {String} params.itemType - The type of item being accessed, expected values are `item`, `list`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
48
+ * @param {String} params.languageCode - The locale code associated to the item being accessed
49
+ * @param {(String|Number)} params.itemID - The ID of the item being accessed - this could be a string or number depending on the itemType
50
+ * @returns {Object}
51
+ */
16
52
  const getItem = async ({ options, itemType, languageCode, itemID }) => {
17
53
  console.log(`Console Interface: getItem has been called`)
18
54
  return null;
19
55
  }
20
-
56
+ /**
57
+ * The function to handle clearing the cache of synchronized data from the CMS
58
+ * @param {Object} params - The parameters object
59
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
60
+ * @returns {Void}
61
+ */
21
62
  const clearItems = async ({ options }) => {
22
63
  console.log(`Console Interface: clearItem has been called`)
23
64
  return null;
@@ -9,13 +9,17 @@ require("dotenv").config({
9
9
  path: `.env.${process.env.NODE_ENV}`,
10
10
  })
11
11
 
12
-
13
- const getFilePath = ({ options, itemType, languageCode, itemID }) => {
14
- const fileName = `${itemID}.json`;
15
-
16
- return path.join(options.rootPath, languageCode, itemType, fileName);
17
- }
18
-
12
+ /**
13
+ * The function to handle saving/updating an item to your storage. This could be a Content Item, Page, Url Redirections, Sync State (state), or Sitemap.
14
+ * @param {Object} params - The parameters object
15
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
16
+ * @param {String} params.options.rootPath - The path to store/access the content as JSON
17
+ * @param {Object} params.item - The object representing the Content Item, Page, Url Redirections, Sync State (state), or Sitemap that needs to be saved/updated
18
+ * @param {String} params.itemType - The type of item being saved/updated, expected values are `item`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
19
+ * @param {String} params.languageCode - The locale code associated to the item being saved/updated
20
+ * @param {(String|Number)} params.itemID - The ID of the item being saved/updated - this could be a string or number depending on the itemType
21
+ * @returns {Void}
22
+ */
19
23
  const saveItem = async ({ options, item, itemType, languageCode, itemID }) => {
20
24
 
21
25
  let filePath = getFilePath({ options, itemType, languageCode, itemID });
@@ -30,7 +34,16 @@ const saveItem = async ({ options, item, itemType, languageCode, itemID }) => {
30
34
  let json = JSON.stringify(item);
31
35
  fs.writeFileSync(filePath, json);
32
36
  }
33
-
37
+ /**
38
+ * The function to handle deleting an item to your storage. This could be a Content Item, Page, Url Redirections, Sync State (state), or Sitemap.
39
+ * @param {Object} params - The parameters object
40
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
41
+ * @param {String} params.options.rootPath - The path to store/access the content as JSON
42
+ * @param {String} params.itemType - The type of item being deleted, expected values are `item`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
43
+ * @param {String} params.languageCode - The locale code associated to the item being saved/updated
44
+ * @param {(String|Number)} params.itemID - The ID of the item being deleted - this could be a string or number depending on the itemType
45
+ * @returns {Void}
46
+ */
34
47
  const deleteItem = async ({ options, itemType, languageCode, itemID }) => {
35
48
 
36
49
  let filePath = getFilePath({ options, itemType, languageCode, itemID });
@@ -40,7 +53,18 @@ const deleteItem = async ({ options, itemType, languageCode, itemID }) => {
40
53
  }
41
54
 
42
55
  }
43
-
56
+ /**
57
+ * The function to handle updating and placing a Content Item into a "list" so that you can handle querying a collection of items.
58
+ * @param {Object} params - The parameters object
59
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
60
+ * @param {String} params.options.rootPath - The path to store/access the content as JSON
61
+ * @param {Object} params.item - The object representing the Content Item
62
+ * @param {String} params.languageCode - The locale code associated to the item being saved/updated
63
+ * @param {(String|Number)} params.itemID - The ID of the item being updated - this could be a string or number depending on the itemType
64
+ * @param {String} params.referenceName - The reference name of the Content List that this Content Item should be added to
65
+ * @param {String} params.definitionName - The Model name that the Content Item is based on
66
+ * @returns {Void}
67
+ */
44
68
  const mergeItemToList = async ({ options, item, languageCode, itemID, referenceName, definitionName }) => {
45
69
 
46
70
  let contentList = await getItem({ options, itemType: "list", languageCode, itemID: referenceName });
@@ -75,7 +99,16 @@ const mergeItemToList = async ({ options, item, languageCode, itemID, referenceN
75
99
 
76
100
  await saveItem({ options, item: contentList, itemType: "list", languageCode, itemID: referenceName });
77
101
  }
78
-
102
+ /**
103
+ * The function to handle retrieving a Content Item, Page, Url Redirections, Sync State (state), or Sitemap
104
+ * @param {Object} params - The parameters object
105
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
106
+ * @param {String} params.options.rootPath - The path to store/access the content as JSON
107
+ * @param {String} params.itemType - The type of item being accessed, expected values are `item`, `list`, `page`, `sitemap`, `nestedsitemap`, `state`, `urlredirections`
108
+ * @param {String} params.languageCode - The locale code associated to the item being accessed
109
+ * @param {(String|Number)} params.itemID - The ID of the item being accessed - this could be a string or number depending on the itemType
110
+ * @returns {Object}
111
+ */
79
112
  const getItem = async ({ options, itemType, languageCode, itemID }) => {
80
113
  let filePath = getFilePath({ options, itemType, languageCode, itemID });
81
114
 
@@ -85,18 +118,23 @@ const getItem = async ({ options, itemType, languageCode, itemID }) => {
85
118
  return JSON.parse(json);
86
119
  }
87
120
 
121
+ /**
122
+ * The function to handle clearing the cache of synchronized data from the CMS
123
+ * @param {Object} params - The parameters object
124
+ * @param {Object} params.options - A flexible object that can contain any properties specifically related to this interface
125
+ * @param {String} params.options.rootPath - The path to store/access the content as JSON
126
+ * @returns {Void}
127
+ */
88
128
  const clearItems = async ({ options }) => {
89
129
  fs.rmdirSync(options.rootPath, { recursive: true })
90
130
  }
91
131
 
92
- const waitOnLock = async (lockFile) => {
93
132
 
94
- while (await check(lockFile)) {
95
- await sleep(100)
96
- }
97
-
98
- }
99
133
 
134
+ /**
135
+ * The function to handle multi-threaded Syncs that may be happening at the same time. If you need to prevent a sync from happening and let it wait until another sync has finished use this.
136
+ * @returns {Promise}
137
+ */
100
138
  const mutexLock = async () => {
101
139
 
102
140
 
@@ -137,7 +175,18 @@ const mutexLock = async () => {
137
175
  }
138
176
 
139
177
 
178
+ //private function to get a wait on a lock file
179
+ const waitOnLock = async (lockFile) => {
180
+ while (await check(lockFile)) {
181
+ await sleep(100)
182
+ }
183
+ }
140
184
 
185
+ //private function to get path of an item
186
+ const getFilePath = ({ options, itemType, languageCode, itemID }) => {
187
+ const fileName = `${itemID}.json`;
188
+ return path.join(options.rootPath, languageCode, itemType, fileName);
189
+ }
141
190
 
142
191
 
143
192
  module.exports = {