@stackbit/cms-core 0.5.2-develop.3 → 0.5.2

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.
@@ -21,29 +21,29 @@ import { getAssetSourceBySourceName, transformAssetSourceDataForAssetSource } fr
21
21
  export type CreateDocumentThunk = ({
22
22
  updateOperationFields,
23
23
  contentSourceData,
24
- modelName
24
+ modelName,
25
+ locale
25
26
  }: {
26
27
  updateOperationFields: Record<string, CSITypes.UpdateOperationField>;
27
28
  contentSourceData: ContentStoreTypes.ContentSourceData;
28
29
  modelName: string;
30
+ locale: string | undefined;
29
31
  }) => Promise<{ documentId: string }>;
30
32
 
31
33
  export function getCreateDocumentThunk({
32
34
  stackbitConfig,
33
35
  getContentSourceDataById,
34
36
  logger,
35
- locale,
36
37
  defaultLocaleDocumentId,
37
38
  user
38
39
  }: {
39
40
  stackbitConfig: Config | null;
40
41
  getContentSourceDataById: () => Record<string, ContentStoreTypes.ContentSourceData>;
41
42
  logger: CSITypes.Logger;
42
- locale?: string;
43
43
  defaultLocaleDocumentId?: string;
44
44
  user?: ContentStoreTypes.User;
45
45
  }): CreateDocumentThunk {
46
- return async ({ updateOperationFields, modelName, contentSourceData }) => {
46
+ return async ({ updateOperationFields, modelName, contentSourceData, locale }) => {
47
47
  // When passing model and modelMap to contentSourceInstance, we have to pass
48
48
  // the original models (i.e., csiModel and csiModelMap) that we've received
49
49
  // from that contentSourceInstance. We can't pass internal models as they
@@ -112,18 +112,22 @@ export function getCreateDocumentThunk({
112
112
  */
113
113
  export async function createDocumentRecursively({
114
114
  object,
115
+ locale,
115
116
  modelName,
116
117
  contentSourceId,
117
118
  contentSourceDataById,
118
119
  assetSources,
119
- createDocument
120
+ createDocument,
121
+ userLogger
120
122
  }: {
121
123
  object?: Record<string, any>;
124
+ locale: string | undefined;
122
125
  modelName: string;
123
126
  contentSourceId: string;
124
127
  contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
125
128
  assetSources: CSITypes.AssetSource[];
126
129
  createDocument: CreateDocumentThunk;
130
+ userLogger: CSITypes.Logger;
127
131
  }): Promise<{ documentId: string; newRefDocumentIds: string[] }> {
128
132
  const contentSourceData = contentSourceDataById[contentSourceId];
129
133
  if (!contentSourceData) {
@@ -143,18 +147,21 @@ export async function createDocumentRecursively({
143
147
  modelFields,
144
148
  csiModelFields,
145
149
  fieldPath: [modelName],
150
+ locale,
146
151
  modelMap,
147
152
  csiModelMap,
148
153
  contentSourceId,
149
154
  contentSourceDataById,
150
155
  assetSources,
151
- createDocument
156
+ createDocument,
157
+ userLogger
152
158
  });
153
159
 
154
160
  const result = await createDocument({
155
161
  updateOperationFields: nestedResult.fields,
156
162
  contentSourceData: contentSourceData,
157
- modelName: modelName
163
+ modelName: modelName,
164
+ locale: locale
158
165
  });
159
166
  return {
160
167
  documentId: result.documentId,
@@ -174,23 +181,27 @@ async function createObjectRecursively({
174
181
  modelFields,
175
182
  csiModelFields,
176
183
  fieldPath,
184
+ locale,
177
185
  modelMap,
178
186
  csiModelMap,
179
187
  contentSourceId,
180
188
  contentSourceDataById,
181
189
  assetSources,
182
- createDocument
190
+ createDocument,
191
+ userLogger
183
192
  }: {
184
193
  object?: Record<string, any>;
185
194
  modelFields: Field[];
186
195
  csiModelFields: Field[];
187
196
  fieldPath: (string | number)[];
197
+ locale: string | undefined;
188
198
  modelMap: Record<string, SDKModel>;
189
199
  csiModelMap: Record<string, CSIModel>;
190
200
  contentSourceId: string;
191
201
  contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
192
202
  assetSources: CSITypes.AssetSource[];
193
203
  createDocument: CreateDocumentThunk;
204
+ userLogger: CSITypes.Logger;
194
205
  }): Promise<{
195
206
  fields: Record<string, CSITypes.UpdateOperationField>;
196
207
  newRefDocumentIds: string[];
@@ -217,16 +228,30 @@ async function createObjectRecursively({
217
228
  throw new Error(`no model field found for field at path ${fieldPathToString(fieldPath.concat(fieldName))}`);
218
229
  }
219
230
  let value;
220
- if (fieldName in object) {
231
+ if (modelField.const) {
232
+ if (fieldName in object && typeof object[fieldName] !== 'undefined') {
233
+ userLogger.warn(
234
+ `got a value for a constant field '${fieldName}' while creating a document, ignoring the provided value, using the 'const' value instead.`
235
+ );
236
+ _.pull(objectFieldNames, fieldName);
237
+ }
238
+ // if the model field has the "const" property, use its value
239
+ if (typeof modelField.const === 'function') {
240
+ value = await modelField.const({ data: object, locale });
241
+ } else {
242
+ value = modelField.const;
243
+ }
244
+ } else if (fieldName in object) {
221
245
  // if the object has a field name matching a model field, use it
222
246
  value = object[fieldName];
223
247
  _.pull(objectFieldNames, fieldName);
224
- } else if (modelField.const) {
225
- // if the model field has the "const" property, use its value
226
- value = modelField.const;
227
248
  } else if (!_.isNil(modelField.default)) {
228
249
  // if the model field has the "default" property, use its value
229
- value = modelField.default;
250
+ if (typeof modelField.default === 'function') {
251
+ value = await modelField.default({ data: object, locale });
252
+ } else {
253
+ value = modelField.default;
254
+ }
230
255
  }
231
256
  if (!_.isNil(value)) {
232
257
  const fieldResult = await createUpdateOperationFieldRecursively({
@@ -234,20 +259,21 @@ async function createObjectRecursively({
234
259
  modelField,
235
260
  csiModelField,
236
261
  fieldPath: fieldPath.concat(fieldName),
262
+ locale,
237
263
  modelMap,
238
264
  csiModelMap,
239
265
  contentSourceId,
240
266
  contentSourceDataById,
241
267
  assetSources,
242
- createDocument
268
+ createDocument,
269
+ userLogger
243
270
  });
244
271
  result.fields[fieldName] = fieldResult.field;
245
272
  result.newRefDocumentIds = result.newRefDocumentIds.concat(fieldResult.newRefDocumentIds);
246
273
  }
247
274
  }
248
275
  if (objectFieldNames.length > 0) {
249
- //TODO use logger
250
- console.error(`no model fields found when creating a document with fields: '${objectFieldNames.join(', ')}'`);
276
+ userLogger.warn(`unknown fields were provided while creating a document, ignoring unknown fields: '${objectFieldNames.join(', ')}'`);
251
277
  }
252
278
 
253
279
  return result;
@@ -258,23 +284,27 @@ async function createUpdateOperationFieldRecursively({
258
284
  modelField,
259
285
  csiModelField,
260
286
  fieldPath,
287
+ locale,
261
288
  modelMap,
262
289
  csiModelMap,
263
290
  contentSourceId,
264
291
  contentSourceDataById,
265
292
  assetSources,
266
- createDocument
293
+ createDocument,
294
+ userLogger
267
295
  }: {
268
296
  value: any;
269
297
  modelField: FieldSpecificProps;
270
298
  csiModelField: FieldSpecificProps;
271
299
  fieldPath: (string | number)[];
300
+ locale: string | undefined;
272
301
  modelMap: Record<string, SDKModel>;
273
302
  csiModelMap: Record<string, CSIModel>;
274
303
  contentSourceId: string;
275
304
  contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
276
305
  assetSources: CSITypes.AssetSource[];
277
306
  createDocument: CreateDocumentThunk;
307
+ userLogger: CSITypes.Logger;
278
308
  }): Promise<{ field: CSITypes.UpdateOperationField; newRefDocumentIds: string[] }> {
279
309
  if (csiModelField.type === 'object') {
280
310
  if (modelField.type !== 'object') {
@@ -285,12 +315,14 @@ async function createUpdateOperationFieldRecursively({
285
315
  modelFields: modelField.fields,
286
316
  csiModelFields: csiModelField.fields,
287
317
  fieldPath,
318
+ locale,
288
319
  modelMap,
289
320
  csiModelMap,
290
321
  contentSourceId,
291
322
  contentSourceDataById,
292
323
  assetSources,
293
- createDocument
324
+ createDocument,
325
+ userLogger
294
326
  });
295
327
  return {
296
328
  field: {
@@ -325,12 +357,14 @@ async function createUpdateOperationFieldRecursively({
325
357
  modelFields: model.fields ?? [],
326
358
  csiModelFields: csiModel.fields ?? [],
327
359
  fieldPath,
360
+ locale,
328
361
  modelMap,
329
362
  csiModelMap,
330
363
  contentSourceId,
331
364
  contentSourceDataById,
332
365
  assetSources,
333
- createDocument
366
+ createDocument,
367
+ userLogger
334
368
  });
335
369
  return {
336
370
  field: {
@@ -416,11 +450,13 @@ async function createUpdateOperationFieldRecursively({
416
450
  }
417
451
  const { documentId, newRefDocumentIds } = await createDocumentRecursively({
418
452
  object: rest,
453
+ locale,
419
454
  modelName,
420
455
  contentSourceId,
421
456
  contentSourceDataById,
422
457
  assetSources,
423
- createDocument
458
+ createDocument,
459
+ userLogger
424
460
  });
425
461
  return {
426
462
  field: {
@@ -457,11 +493,13 @@ async function createUpdateOperationFieldRecursively({
457
493
  }
458
494
  const { documentId, newRefDocumentIds } = await createDocumentRecursively({
459
495
  object: rest,
496
+ locale,
460
497
  modelName,
461
498
  contentSourceId: getContentSourceId(refSrcType, refProjectId),
462
499
  contentSourceDataById,
463
500
  assetSources,
464
- createDocument
501
+ createDocument,
502
+ userLogger
465
503
  });
466
504
  _newRefDocumentIds.push(documentId, ...newRefDocumentIds);
467
505
  refObject = { refId: documentId, refSrcType, refProjectId };
@@ -488,12 +526,14 @@ async function createUpdateOperationFieldRecursively({
488
526
  modelField: itemsField,
489
527
  csiModelField: csiItemsField,
490
528
  fieldPath: fieldPath.concat(index),
529
+ locale,
491
530
  modelMap,
492
531
  csiModelMap,
493
532
  contentSourceId,
494
533
  contentSourceDataById,
495
534
  assetSources,
496
- createDocument
535
+ createDocument,
536
+ userLogger
497
537
  });
498
538
  if (result.field.type === 'list') {
499
539
  throw new Error('list cannot have list as immediate child');
@@ -551,23 +591,27 @@ export async function convertOperationField({
551
591
  fieldPath,
552
592
  modelField,
553
593
  csiModelField,
594
+ locale,
554
595
  modelMap,
555
596
  csiModelMap,
556
597
  contentSourceId,
557
598
  contentSourceDataById,
558
599
  assetSources,
559
- createDocument
600
+ createDocument,
601
+ userLogger
560
602
  }: {
561
603
  operationField: ContentStoreTypes.UpdateOperationField;
562
604
  fieldPath: (string | number)[];
563
605
  modelField: FieldSpecificProps;
564
606
  csiModelField: FieldSpecificProps;
607
+ locale: string | undefined;
565
608
  modelMap: Record<string, SDKModel>;
566
609
  csiModelMap: Record<string, SDKModel>;
567
610
  contentSourceId: string;
568
611
  contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
569
612
  assetSources: CSITypes.AssetSource[];
570
613
  createDocument: CreateDocumentThunk;
614
+ userLogger: CSITypes.Logger;
571
615
  }): Promise<CSITypes.UpdateOperationField> {
572
616
  switch (operationField.type) {
573
617
  case 'object': {
@@ -579,12 +623,14 @@ export async function convertOperationField({
579
623
  modelFields: modelField.fields,
580
624
  csiModelFields: csiModelField.fields,
581
625
  fieldPath,
626
+ locale,
582
627
  modelMap,
583
628
  csiModelMap,
584
629
  contentSourceId,
585
630
  contentSourceDataById,
586
631
  assetSources,
587
- createDocument
632
+ createDocument,
633
+ userLogger
588
634
  });
589
635
  return {
590
636
  type: operationField.type,
@@ -602,12 +648,14 @@ export async function convertOperationField({
602
648
  modelFields: model.fields ?? [],
603
649
  csiModelFields: csiModel.fields ?? [],
604
650
  fieldPath,
651
+ locale,
605
652
  modelMap,
606
653
  csiModelMap,
607
654
  contentSourceId,
608
655
  contentSourceDataById,
609
656
  assetSources,
610
- createDocument
657
+ createDocument,
658
+ userLogger
611
659
  });
612
660
  return {
613
661
  type: operationField.type,
@@ -645,12 +693,14 @@ export async function convertOperationField({
645
693
  modelField: modelField.items,
646
694
  csiModelField: csiModelField.items,
647
695
  fieldPath: fieldPath.concat(index),
696
+ locale,
648
697
  modelMap,
649
698
  csiModelMap,
650
699
  contentSourceId,
651
700
  contentSourceDataById,
652
701
  assetSources,
653
- createDocument
702
+ createDocument,
703
+ userLogger
654
704
  });
655
705
  if (result.field.type === 'list') {
656
706
  throw new Error('list cannot have list as immediate child');
@@ -702,12 +752,14 @@ export async function convertOperationField({
702
752
  modelField,
703
753
  csiModelField,
704
754
  fieldPath,
755
+ locale,
705
756
  modelMap,
706
757
  csiModelMap,
707
758
  contentSourceId,
708
759
  contentSourceDataById,
709
760
  assetSources,
710
- createDocument
761
+ createDocument,
762
+ userLogger
711
763
  });
712
764
  return result.field;
713
765
  }