@stackbit/cms-core 0.2.1 → 0.3.0-develop.1

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.
Files changed (54) hide show
  1. package/dist/content-store-utils.d.ts +5 -1
  2. package/dist/content-store-utils.d.ts.map +1 -1
  3. package/dist/content-store-utils.js +28 -3
  4. package/dist/content-store-utils.js.map +1 -1
  5. package/dist/content-store.d.ts +12 -1
  6. package/dist/content-store.d.ts.map +1 -1
  7. package/dist/content-store.js +399 -177
  8. package/dist/content-store.js.map +1 -1
  9. package/dist/types/content-store-document-fields.d.ts +26 -4
  10. package/dist/types/content-store-document-fields.d.ts.map +1 -1
  11. package/dist/types/content-store-documents.d.ts +14 -3
  12. package/dist/types/content-store-documents.d.ts.map +1 -1
  13. package/dist/types/content-store-types.d.ts +7 -1
  14. package/dist/types/content-store-types.d.ts.map +1 -1
  15. package/dist/utils/backward-compatibility.d.ts +184 -0
  16. package/dist/utils/backward-compatibility.d.ts.map +1 -0
  17. package/dist/utils/backward-compatibility.js +151 -0
  18. package/dist/utils/backward-compatibility.js.map +1 -0
  19. package/dist/utils/config-delegate.d.ts +11 -0
  20. package/dist/utils/config-delegate.d.ts.map +1 -0
  21. package/dist/utils/config-delegate.js +226 -0
  22. package/dist/utils/config-delegate.js.map +1 -0
  23. package/dist/utils/create-update-csi-docs.d.ts +7 -5
  24. package/dist/utils/create-update-csi-docs.d.ts.map +1 -1
  25. package/dist/utils/create-update-csi-docs.js +24 -24
  26. package/dist/utils/create-update-csi-docs.js.map +1 -1
  27. package/dist/utils/csi-to-store-docs-converter.d.ts +17 -3
  28. package/dist/utils/csi-to-store-docs-converter.d.ts.map +1 -1
  29. package/dist/utils/csi-to-store-docs-converter.js +187 -47
  30. package/dist/utils/csi-to-store-docs-converter.js.map +1 -1
  31. package/dist/utils/site-map.d.ts.map +1 -1
  32. package/dist/utils/site-map.js +4 -1
  33. package/dist/utils/site-map.js.map +1 -1
  34. package/dist/utils/store-to-api-docs-converter.d.ts +6 -1
  35. package/dist/utils/store-to-api-docs-converter.d.ts.map +1 -1
  36. package/dist/utils/store-to-api-docs-converter.js +140 -51
  37. package/dist/utils/store-to-api-docs-converter.js.map +1 -1
  38. package/dist/utils/store-to-csi-docs-converter.d.ts +1 -0
  39. package/dist/utils/store-to-csi-docs-converter.d.ts.map +1 -1
  40. package/dist/utils/store-to-csi-docs-converter.js +2 -1
  41. package/dist/utils/store-to-csi-docs-converter.js.map +1 -1
  42. package/package.json +5 -5
  43. package/src/content-store-utils.ts +40 -6
  44. package/src/content-store.ts +552 -299
  45. package/src/types/content-store-document-fields.ts +16 -4
  46. package/src/types/content-store-documents.ts +12 -3
  47. package/src/types/content-store-types.ts +4 -1
  48. package/src/utils/backward-compatibility.ts +269 -0
  49. package/src/utils/config-delegate.ts +277 -0
  50. package/src/utils/create-update-csi-docs.ts +47 -50
  51. package/src/utils/csi-to-store-docs-converter.ts +256 -43
  52. package/src/utils/site-map.ts +19 -7
  53. package/src/utils/store-to-api-docs-converter.ts +185 -52
  54. package/src/utils/store-to-csi-docs-converter.ts +1 -1
@@ -17,7 +17,7 @@ export type CreateDocumentCallback = ({
17
17
  updateOperationFields: Record<string, CSITypes.UpdateOperationField>;
18
18
  contentSourceData: ContentStoreTypes.ContentSourceData;
19
19
  modelName: string;
20
- }) => Promise<CSITypes.Document>;
20
+ }) => Promise<{ documentId: string }>;
21
21
 
22
22
  export function getCreateDocumentThunk({
23
23
  locale,
@@ -84,8 +84,8 @@ export function getCreateDocumentThunk({
84
84
  *
85
85
  * Returns an object with two fields:
86
86
  * 1. `document` holding the created CSITypes.Document for the passed object.
87
- * 2. `newRefDocuments` holding list of created CSITypes.Document that were
88
- * created recursively for `reference` fields having the `$$type` property.
87
+ * 2. `newRefDocumentIds` containing the list of the created document IDs that
88
+ * were created recursively for `reference` fields having the `$$type` property.
89
89
  */
90
90
  export async function createDocumentRecursively({
91
91
  object,
@@ -99,7 +99,7 @@ export async function createDocumentRecursively({
99
99
  contentSourceId: string;
100
100
  contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
101
101
  createDocument: CreateDocumentCallback;
102
- }): Promise<{ document: CSITypes.Document; newRefDocuments: CSITypes.Document[] }> {
102
+ }): Promise<{ documentId: string; newRefDocumentIds: string[] }> {
103
103
  const contentSourceData = contentSourceDataById[contentSourceId];
104
104
  if (!contentSourceData) {
105
105
  throw new Error(`no content source data for id '${contentSourceId}'`);
@@ -145,14 +145,14 @@ export async function createDocumentRecursively({
145
145
  createDocument
146
146
  });
147
147
 
148
- const document = await createDocument({
148
+ const result = await createDocument({
149
149
  updateOperationFields: nestedResult.fields,
150
150
  contentSourceData: contentSourceData,
151
151
  modelName: modelName
152
152
  });
153
153
  return {
154
- document: document,
155
- newRefDocuments: nestedResult.newRefDocuments
154
+ documentId: result.documentId,
155
+ newRefDocumentIds: nestedResult.newRefDocumentIds
156
156
  };
157
157
  }
158
158
 
@@ -188,15 +188,15 @@ async function createObjectRecursively({
188
188
  createDocument: CreateDocumentCallback;
189
189
  }): Promise<{
190
190
  fields: Record<string, CSITypes.UpdateOperationField>;
191
- newRefDocuments: CSITypes.Document[];
191
+ newRefDocumentIds: string[];
192
192
  }> {
193
193
  object = object ?? {};
194
194
  const result: {
195
195
  fields: Record<string, CSITypes.UpdateOperationField>;
196
- newRefDocuments: CSITypes.Document[];
196
+ newRefDocumentIds: string[];
197
197
  } = {
198
198
  fields: {},
199
- newRefDocuments: []
199
+ newRefDocumentIds: []
200
200
  };
201
201
  // When creating new documents, we are iterating over the model's fields to
202
202
  // construct the update operations. In case object has a field that does not
@@ -236,7 +236,7 @@ async function createObjectRecursively({
236
236
  createDocument
237
237
  });
238
238
  result.fields[fieldName] = fieldResult.field;
239
- result.newRefDocuments = result.newRefDocuments.concat(fieldResult.newRefDocuments);
239
+ result.newRefDocumentIds = result.newRefDocumentIds.concat(fieldResult.newRefDocumentIds);
240
240
  }
241
241
  }
242
242
  if (objectFieldNames.length > 0) {
@@ -267,7 +267,7 @@ async function createUpdateOperationFieldRecursively({
267
267
  contentSourceId: string;
268
268
  contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
269
269
  createDocument: CreateDocumentCallback;
270
- }): Promise<{ field: CSITypes.UpdateOperationField; newRefDocuments: CSITypes.Document[] }> {
270
+ }): Promise<{ field: CSITypes.UpdateOperationField; newRefDocumentIds: string[] }> {
271
271
  if (csiModelField.type === 'object') {
272
272
  if (modelField.type !== 'object') {
273
273
  throw new Error(`field type mismatch between content-source and mapped models at field path ${fieldPathToString(fieldPath)}`);
@@ -288,7 +288,7 @@ async function createUpdateOperationFieldRecursively({
288
288
  type: 'object',
289
289
  fields: result.fields
290
290
  },
291
- newRefDocuments: result.newRefDocuments
291
+ newRefDocumentIds: result.newRefDocumentIds
292
292
  };
293
293
  } else if (csiModelField.type === 'model') {
294
294
  if (modelField.type !== 'model') {
@@ -328,7 +328,7 @@ async function createUpdateOperationFieldRecursively({
328
328
  modelName: modelName,
329
329
  fields: result.fields
330
330
  },
331
- newRefDocuments: result.newRefDocuments
331
+ newRefDocumentIds: result.newRefDocumentIds
332
332
  };
333
333
  } else if (csiModelField.type === 'image') {
334
334
  let refId: string | undefined;
@@ -338,7 +338,7 @@ async function createUpdateOperationFieldRecursively({
338
338
  type: 'image',
339
339
  value: _.omit(value, ['$$type']) // backwards compatibility with legacy presets
340
340
  },
341
- newRefDocuments: []
341
+ newRefDocumentIds: []
342
342
  };
343
343
  }
344
344
  if (_.isPlainObject(value)) {
@@ -355,7 +355,7 @@ async function createUpdateOperationFieldRecursively({
355
355
  refType: 'asset',
356
356
  refId: refId
357
357
  },
358
- newRefDocuments: []
358
+ newRefDocumentIds: []
359
359
  };
360
360
  } else if (csiModelField.type === 'reference') {
361
361
  if (modelField.type !== 'reference') {
@@ -369,7 +369,7 @@ async function createUpdateOperationFieldRecursively({
369
369
  refType: 'document',
370
370
  refId: refId
371
371
  },
372
- newRefDocuments: []
372
+ newRefDocumentIds: []
373
373
  };
374
374
  } else {
375
375
  const modelNames = modelField.models;
@@ -386,7 +386,7 @@ async function createUpdateOperationFieldRecursively({
386
386
  if (!modelName) {
387
387
  throw new Error('reference field type must have $$type or $$ref properties when creating new documents');
388
388
  }
389
- const { document, newRefDocuments } = await createDocumentRecursively({
389
+ const { documentId, newRefDocumentIds } = await createDocumentRecursively({
390
390
  object: rest,
391
391
  modelName,
392
392
  contentSourceId,
@@ -397,9 +397,9 @@ async function createUpdateOperationFieldRecursively({
397
397
  field: {
398
398
  type: 'reference',
399
399
  refType: 'document',
400
- refId: document.id
400
+ refId: documentId
401
401
  },
402
- newRefDocuments: [document, ...newRefDocuments]
402
+ newRefDocumentIds: [documentId, ...newRefDocumentIds]
403
403
  };
404
404
  }
405
405
  } else if (
@@ -411,7 +411,7 @@ async function createUpdateOperationFieldRecursively({
411
411
  }
412
412
  let { $$ref: refId = null, $$type: modelName = null, $$refSrcType: refSrcType = null, $$refProjectId: refProjectId = null, ...rest } = value;
413
413
  let refObject;
414
- const newRefDocuments: CSITypes.Document[] = [];
414
+ const _newRefDocumentIds: string[] = [];
415
415
  if (refId && refSrcType && refProjectId) {
416
416
  refObject = { refId, refSrcType, refProjectId };
417
417
  } else {
@@ -426,19 +426,19 @@ async function createUpdateOperationFieldRecursively({
426
426
  if (!modelName || !refSrcType || !refProjectId) {
427
427
  throw new Error('reference field type must have $$type or $$ref properties when creating new documents');
428
428
  }
429
- const { document, newRefDocuments } = await createDocumentRecursively({
429
+ const { documentId, newRefDocumentIds } = await createDocumentRecursively({
430
430
  object: rest,
431
431
  modelName,
432
432
  contentSourceId: getContentSourceId(refSrcType, refProjectId),
433
433
  contentSourceDataById,
434
434
  createDocument
435
435
  });
436
- newRefDocuments.push(document, ...newRefDocuments);
437
- refObject = { refId: document.id, refSrcType, refProjectId };
436
+ _newRefDocumentIds.push(documentId, ...newRefDocumentIds);
437
+ refObject = { refId: documentId, refSrcType, refProjectId };
438
438
  }
439
439
  return {
440
440
  field: updateOperationValueFieldWithCrossReference(csiModelField.type, refObject),
441
- newRefDocuments
441
+ newRefDocumentIds: _newRefDocumentIds
442
442
  };
443
443
  } else if (csiModelField.type === 'list') {
444
444
  if (modelField.type !== 'list') {
@@ -452,35 +452,32 @@ async function createUpdateOperationFieldRecursively({
452
452
  if (!itemsField || !csiItemsField) {
453
453
  throw new Error(`list field does not define items`);
454
454
  }
455
- const arrayResult = await mapPromise(
456
- value,
457
- async (item, index): Promise<{ field: UpdateOperationListFieldItem; newRefDocuments: CSITypes.Document[] }> => {
458
- const result = await createUpdateOperationFieldRecursively({
459
- value: item,
460
- modelField: itemsField,
461
- csiModelField: csiItemsField,
462
- fieldPath: fieldPath.concat(index),
463
- modelMap,
464
- csiModelMap,
465
- contentSourceId,
466
- contentSourceDataById,
467
- createDocument
468
- });
469
- if (result.field.type === 'list') {
470
- throw new Error('list cannot have list as immediate child');
471
- }
472
- return {
473
- field: result.field,
474
- newRefDocuments: result.newRefDocuments
475
- };
455
+ const arrayResult = await mapPromise(value, async (item, index): Promise<{ field: UpdateOperationListFieldItem; newRefDocumentIds: string[] }> => {
456
+ const result = await createUpdateOperationFieldRecursively({
457
+ value: item,
458
+ modelField: itemsField,
459
+ csiModelField: csiItemsField,
460
+ fieldPath: fieldPath.concat(index),
461
+ modelMap,
462
+ csiModelMap,
463
+ contentSourceId,
464
+ contentSourceDataById,
465
+ createDocument
466
+ });
467
+ if (result.field.type === 'list') {
468
+ throw new Error('list cannot have list as immediate child');
476
469
  }
477
- );
470
+ return {
471
+ field: result.field,
472
+ newRefDocumentIds: result.newRefDocumentIds
473
+ };
474
+ });
478
475
  return {
479
476
  field: {
480
477
  type: 'list',
481
478
  items: arrayResult.map((result) => result.field)
482
479
  },
483
- newRefDocuments: arrayResult.reduce((result: CSITypes.Document[], { newRefDocuments }) => result.concat(newRefDocuments), [])
480
+ newRefDocumentIds: arrayResult.reduce((result: string[], { newRefDocumentIds }) => result.concat(newRefDocumentIds), [])
484
481
  };
485
482
  } else if (
486
483
  (csiModelField.type === 'string' || csiModelField.type === 'text') &&
@@ -492,7 +489,7 @@ async function createUpdateOperationFieldRecursively({
492
489
  type: csiModelField.type,
493
490
  value: JSON.stringify(value)
494
491
  },
495
- newRefDocuments: []
492
+ newRefDocumentIds: []
496
493
  };
497
494
  }
498
495
  return {
@@ -500,7 +497,7 @@ async function createUpdateOperationFieldRecursively({
500
497
  type: csiModelField.type,
501
498
  value: value
502
499
  },
503
- newRefDocuments: []
500
+ newRefDocumentIds: []
504
501
  };
505
502
  }
506
503