@agility/content-sync 1.0.4 → 1.1.0

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.4",
3
+ "version": "1.1.0",
4
4
  "description": "JavaScript SDK for synchronizing content from Agility CMS",
5
5
  "main": "dist/agility-sync-sdk.node.js",
6
6
  "scripts": {
@@ -12,21 +12,23 @@ export default async function (languageCode, token) {
12
12
  let itemCount = 0
13
13
  let busy = false
14
14
  let waitMS = 0
15
- const waitMaxMS = 30000
16
- const waitIntervalMS = 500
15
+ const waitMaxMS = 60000
16
+ const waitIntervalMS = 1000
17
17
 
18
18
  do {
19
+ let syncRet = null
20
+
19
21
 
20
22
  //sync content items...
21
- const syncRet = await this.agilityClient.getSyncContent({
23
+ syncRet = await this.agilityClient.getSyncContent({
22
24
  syncToken: token,
23
25
  pageSize: 100,
24
26
  languageCode: languageCode,
25
27
 
26
28
  });
27
29
 
28
- if (syncRet.busy !== undefined
29
- && syncRet.busy === true) {
30
+
31
+ if (syncRet === undefined || (syncRet.busy !== undefined && syncRet.busy === true)) {
30
32
  //if the api is being updated, wait a few ms and try again...
31
33
  waitMS += waitIntervalMS
32
34
  if (waitMS > waitMaxMS) {
@@ -220,7 +220,14 @@ const getSyncState = async (languageCode) => {
220
220
  * @param {boolean} [requestParams.expandAllContentLinks] - Whether or not to expand entire linked content references, includings lists and items that are rendered in the CMS as Grid or Link. Default is **false**
221
221
  * @returns {Promise<Object>} - Returns a content item object.
222
222
  */
223
- const getContentItem = async ({ contentID, languageCode, depth = 2, expandAllContentLinks = false }) => {
223
+ const getContentItem = async ({ contentID, languageCode, depth, contentLinkDepth, expandAllContentLinks = false }) => {
224
+
225
+ if (depth === undefined && contentLinkDepth !== undefined) {
226
+ depth = contentLinkDepth
227
+ } else if (depth === undefined && contentLinkDepth === undefined) {
228
+ depth = 2
229
+ }
230
+
224
231
  const contentItem = await store.getItem({
225
232
  options,
226
233
  itemType: "item",
@@ -250,33 +257,92 @@ const expandContentItem = async ({ contentItem, languageCode, depth, expandAllCo
250
257
  depth: depth - 1,
251
258
  });
252
259
  if (childItem != null) fields[fieldName] = childItem;
253
- } else if (fieldValue.sortids && fieldValue.sortids.split) {
254
- //multi linked item
255
- const sortIDAry = fieldValue.sortids.split(",");
260
+ } else if (fieldValue.fulllist === true
261
+ && fieldValue.referencename
262
+ && expandAllContentLinks === true) {
263
+
264
+ //LINK TO THE FULL LIST
265
+ const referenceName = fieldValue.referencename
266
+
267
+ const skip = 0
268
+ const take = 50
269
+
270
+ const list = await getContentList({
271
+ referenceName,
272
+ languageCode,
273
+ depth: depth - 1,
274
+ expandAllContentLinks,
275
+ skip,
276
+ take
277
+ })
278
+
279
+ let sortIDAry = []
280
+
281
+ if (fieldValue.sortids && fieldValue.sortids.split) {
282
+ sortIDAry = fieldValue.sortids.split(",");
283
+ }
284
+
285
+ let itemCount = 0
256
286
  const childItems = [];
257
287
  for (const childItemID of sortIDAry) {
288
+ itemCount++
258
289
  const childItem = await getContentItem({
259
290
  contentID: childItemID,
260
291
  languageCode,
261
292
  depth: depth - 1,
293
+ expandAllContentLinks,
294
+ skip,
295
+ take
296
+ });
297
+
298
+ if (childItem != null) {
299
+ childItems.push(childItem);
300
+ }
301
+ }
302
+
303
+ for (const listItem of list) {
304
+ itemCount++
305
+ if (itemCount > 50) break;
306
+
307
+ const listItemContentID = listItem.contentID
308
+ if (sortIDAry.includes(`${listItemContentID}`)) {
309
+ continue;
310
+ }
311
+
312
+ const childItem = await getContentItem({
313
+ contentID: listItemContentID,
314
+ languageCode,
315
+ depth: depth - 1,
262
316
  expandAllContentLinks
263
317
  });
264
318
  if (childItem != null) childItems.push(childItem);
265
319
  }
320
+
266
321
  fields[fieldName] = childItems;
267
- } else if (fieldValue.referencename
268
- && expandAllContentLinks === true) {
269
- //if we are expanding ALL content links...
270
322
 
271
- const childList = await getContentList({
272
- referenceName: fieldValue.referencename,
323
+ } else if (fieldValue.sortids && fieldValue.sortids.split && fieldValue.fulllist !== true) {
324
+ //MULTI LINKED ITEM
325
+ let sortIDAry = []
326
+ if (fieldValue.sortids && fieldValue.sortids.split) {
327
+ sortIDAry = fieldValue.sortids.split(",");
328
+ }
329
+
330
+ const skip = expandAllContentLinks ? 0 : undefined
331
+ const take = expandAllContentLinks ? 50 : undefined
332
+
333
+ const childItems = [];
334
+ for (const childItemID of sortIDAry) {
335
+ const childItem = await getContentItem({
336
+ contentID: childItemID,
273
337
  languageCode,
274
338
  depth: depth - 1,
275
339
  expandAllContentLinks,
276
- take: 50
277
- })
278
-
279
- fields[fieldName] = childList
340
+ skip,
341
+ take
342
+ });
343
+ if (childItem != null) childItems.push(childItem);
344
+ }
345
+ fields[fieldName] = childItems;
280
346
 
281
347
  }
282
348
  }
@@ -357,7 +423,14 @@ const getContentList = async ({ referenceName, languageCode, depth = 0, expandAl
357
423
  * @param {*} { pageID, languageCode, depth = 3 }
358
424
  * @returns
359
425
  */
360
- const getPage = async ({ pageID, languageCode, depth = 3 }) => {
426
+ const getPage = async ({ pageID, languageCode, depth, contentLinkDepth, expandAllContentLinks = false }) => {
427
+
428
+ if (depth === undefined && contentLinkDepth !== undefined) {
429
+ depth = contentLinkDepth
430
+ } else if (depth === undefined && contentLinkDepth === undefined) {
431
+ depth = 2
432
+ }
433
+
361
434
  let pageItem = await store.getItem({
362
435
  options,
363
436
  itemType: "page",
@@ -376,6 +449,7 @@ const getPage = async ({ pageID, languageCode, depth = 3 }) => {
376
449
  contentID: mod.item.contentid,
377
450
  languageCode,
378
451
  depth: depth - 1,
452
+ expandAllContentLinks
379
453
  });
380
454
  mod.item = moduleItem;
381
455
  }
@@ -9,7 +9,7 @@ import storeInterfaceFileSystem from './store-interface-filesystem'
9
9
 
10
10
  function getSyncClient (userConfig) {
11
11
  validateConfigParams(userConfig);
12
- return createSyncCient(userConfig);
12
+ return createSyncClient(userConfig);
13
13
  }
14
14
 
15
15
  function validateConfigParams(configParams) {
@@ -39,11 +39,11 @@ const defaultConfig = {
39
39
  }
40
40
  };
41
41
 
42
- function createSyncCient(userConfig) {
42
+ function createSyncClient(userConfig) {
43
43
  let config = {
44
44
  ...defaultConfig,
45
45
  ...userConfig
46
-
46
+
47
47
  }
48
48
 
49
49
  const agilityClient = agility.getApi({
@@ -60,7 +60,7 @@ function createSyncCient(userConfig) {
60
60
 
61
61
  //set the sync storage interface provider, it will also validate it
62
62
  storeInterface.setStore(store, config.store.options);
63
-
63
+
64
64
  return {
65
65
  config,
66
66
  agilityClient,