@stackbit/cms-core 0.7.6-develop.2 → 0.8.0-develop.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.
Files changed (63) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/content-store-utils.d.ts +44 -0
  3. package/dist/content-store-utils.d.ts.map +1 -1
  4. package/dist/content-store-utils.js +213 -1
  5. package/dist/content-store-utils.js.map +1 -1
  6. package/dist/content-store.d.ts +12 -1
  7. package/dist/content-store.d.ts.map +1 -1
  8. package/dist/content-store.js +45 -6
  9. package/dist/content-store.js.map +1 -1
  10. package/dist/index.d.ts +0 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +0 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/types/content-store-document-fields.d.ts +105 -0
  15. package/dist/types/content-store-document-fields.d.ts.map +1 -1
  16. package/dist/types/content-store-documents.d.ts +4 -1
  17. package/dist/types/content-store-documents.d.ts.map +1 -1
  18. package/dist/types/content-store-types.d.ts +10 -10
  19. package/dist/types/content-store-types.d.ts.map +1 -1
  20. package/dist/types/custom-actions.d.ts +108 -0
  21. package/dist/types/custom-actions.d.ts.map +1 -0
  22. package/dist/types/custom-actions.js +3 -0
  23. package/dist/types/custom-actions.js.map +1 -0
  24. package/dist/types/index.d.ts +1 -0
  25. package/dist/types/index.d.ts.map +1 -1
  26. package/dist/types/index.js +1 -0
  27. package/dist/types/index.js.map +1 -1
  28. package/dist/utils/csi-to-store-docs-converter.d.ts +2 -1
  29. package/dist/utils/csi-to-store-docs-converter.d.ts.map +1 -1
  30. package/dist/utils/csi-to-store-docs-converter.js +144 -11
  31. package/dist/utils/csi-to-store-docs-converter.js.map +1 -1
  32. package/dist/utils/custom-actions.d.ts +53 -0
  33. package/dist/utils/custom-actions.d.ts.map +1 -0
  34. package/dist/utils/custom-actions.js +564 -0
  35. package/dist/utils/custom-actions.js.map +1 -0
  36. package/dist/utils/document-hooks.d.ts +9 -0
  37. package/dist/utils/document-hooks.d.ts.map +1 -1
  38. package/dist/utils/document-hooks.js +3 -2
  39. package/dist/utils/document-hooks.js.map +1 -1
  40. package/dist/utils/search-utils.d.ts +1 -1
  41. package/dist/utils/search-utils.d.ts.map +1 -1
  42. package/dist/utils/store-to-api-docs-converter.d.ts.map +1 -1
  43. package/dist/utils/store-to-api-docs-converter.js +77 -29
  44. package/dist/utils/store-to-api-docs-converter.js.map +1 -1
  45. package/dist/utils/store-to-csi-docs-converter.d.ts +3 -0
  46. package/dist/utils/store-to-csi-docs-converter.d.ts.map +1 -1
  47. package/dist/utils/store-to-csi-docs-converter.js +2 -1
  48. package/dist/utils/store-to-csi-docs-converter.js.map +1 -1
  49. package/package.json +5 -5
  50. package/src/content-store-utils.ts +247 -0
  51. package/src/content-store.ts +62 -7
  52. package/src/index.ts +0 -1
  53. package/src/types/content-store-document-fields.ts +55 -2
  54. package/src/types/content-store-documents.ts +4 -1
  55. package/src/types/content-store-types.ts +10 -10
  56. package/src/types/custom-actions.ts +154 -0
  57. package/src/types/index.ts +1 -0
  58. package/src/utils/csi-to-store-docs-converter.ts +220 -12
  59. package/src/utils/custom-actions.ts +707 -0
  60. package/src/utils/document-hooks.ts +3 -3
  61. package/src/utils/search-utils.ts +1 -1
  62. package/src/utils/store-to-api-docs-converter.ts +88 -33
  63. package/src/utils/store-to-csi-docs-converter.ts +4 -4
@@ -4,7 +4,7 @@ import * as StackbitTypes from '@stackbit/types';
4
4
  import * as ContentStoreTypes from '../types';
5
5
  import type { ContentSourceData } from '../types';
6
6
  import type { BackCompatContentSourceInterface } from './backward-compatibility';
7
- import { getUserContextForSrcType, findContentSourcesDataForTypeOrId } from '../content-store-utils';
7
+ import { getUserContextForSrcType, findContentSourcesDataForTypeOrId, getUserContextForSrcTypeThunk } from '../content-store-utils';
8
8
  import { createConfigDelegate } from './config-delegate';
9
9
 
10
10
  export interface DocumentHookBaseOptions {
@@ -146,12 +146,12 @@ function getDocumentHookOptions<Action extends 'createDocument' | 'updateDocumen
146
146
  user: options.user,
147
147
  stackbitConfig: options.stackbitConfig
148
148
  }),
149
- getUserContextForContentSourceType: (srcType) => getUserContextForSrcType(srcType, options.user),
149
+ getUserContextForContentSourceType: getUserContextForSrcTypeThunk(options.user),
150
150
  ...configDelegate
151
151
  };
152
152
  }
153
153
 
154
- function getContentSourceActionsForSourceThunk({
154
+ export function getContentSourceActionsForSourceThunk({
155
155
  getContentSourceDataById,
156
156
  logger,
157
157
  user,
@@ -2,7 +2,7 @@ import _ from 'lodash';
2
2
  import { ImageModel, Model } from '@stackbit/sdk';
3
3
 
4
4
  import * as ContentStoreTypes from '../types';
5
- import { ReferenceValueType, SearchFilter, SearchFilterItem } from '../types/search-filter';
5
+ import { ReferenceValueType, SearchFilter, SearchFilterItem } from '../types';
6
6
  import { getContentSourceId, getDocumentFieldForLocale } from '../content-store-utils';
7
7
 
8
8
  const META_FIELD = {
@@ -1,8 +1,9 @@
1
1
  import _ from 'lodash';
2
2
  import path from 'path';
3
- import { omitByNil } from '@stackbit/utils';
3
+ import { omitByNil, omitByUndefined } from '@stackbit/utils';
4
4
  import * as CSITypes from '@stackbit/types';
5
5
  import * as ContentStoreTypes from '../types';
6
+ import { APICustomActionField, APICustomActionObject } from '../types';
6
7
 
7
8
  export function mapDocumentsToLocalizedApiObjects({
8
9
  documents,
@@ -31,13 +32,14 @@ function documentToLocalizedApiObject({
31
32
  locale?: string;
32
33
  delegate: CSITypes.ConfigDelegate;
33
34
  }): ContentStoreTypes.APIDocumentObject {
34
- const { type, fields, getPreview, ...rest } = document;
35
- return {
35
+ const { type, fields, getPreview, getDocumentActions, ...rest } = document;
36
+ return omitByUndefined({
36
37
  type: 'object',
37
38
  ...rest,
38
39
  srcObjectLabel: getPreview({ delegate, locale }).previewTitle,
40
+ actions: getDocumentActions?.(),
39
41
  fields: toLocalizedAPIFields({ docFields: fields, delegate, locale })
40
- };
42
+ });
41
43
  }
42
44
 
43
45
  function toLocalizedAPIFields({
@@ -50,11 +52,13 @@ function toLocalizedAPIFields({
50
52
  locale?: string;
51
53
  }): Record<string, ContentStoreTypes.DocumentFieldAPI> {
52
54
  return _.mapValues(docFields, (docField) => {
53
- return toLocalizedAPIField({
54
- docField,
55
- delegate,
56
- locale
57
- });
55
+ return omitByUndefined(
56
+ toLocalizedAPIField({
57
+ docField,
58
+ delegate,
59
+ locale
60
+ })
61
+ );
58
62
  });
59
63
  }
60
64
 
@@ -88,6 +92,9 @@ function toLocalizedAPIField({
88
92
  locale: locale
89
93
  };
90
94
  }
95
+ const { getFieldActions, ...restDocField } = docField;
96
+ const fieldActions = getFieldActions?.({ locale: docField.localized ? locale : undefined });
97
+ docField = restDocField;
91
98
  switch (docField.type) {
92
99
  case 'string':
93
100
  case 'text':
@@ -107,12 +114,14 @@ function toLocalizedAPIField({
107
114
  return {
108
115
  ...base,
109
116
  ...localeProps,
110
- ...localeFields(localized)
117
+ ...localeFields(localized),
118
+ actions: fieldActions
111
119
  } as ContentStoreTypes.DocumentStringLikeFieldAPI;
112
120
  }
113
121
  return {
114
122
  ...docField,
115
- ...localeFields(docField.localized)
123
+ ...localeFields(docField.localized),
124
+ actions: fieldActions
116
125
  };
117
126
  }
118
127
  case 'file':
@@ -126,18 +135,21 @@ function toLocalizedAPIField({
126
135
  return {
127
136
  ...base,
128
137
  ...localeProps,
129
- ...localeFields(localized)
138
+ ...localeFields(localized),
139
+ actions: fieldActions
130
140
  };
131
141
  }
132
142
  return {
133
143
  ...base,
134
144
  isUnset: true,
135
- ...localeFields(localized)
145
+ ...localeFields(localized),
146
+ actions: fieldActions
136
147
  };
137
148
  }
138
149
  return {
139
150
  ...docField,
140
- ...localeFields(docField.localized)
151
+ ...localeFields(docField.localized),
152
+ actions: fieldActions
141
153
  };
142
154
  }
143
155
  case 'image': {
@@ -155,13 +167,15 @@ function toLocalizedAPIField({
155
167
  ...base,
156
168
  ...localeProps,
157
169
  fields,
158
- ...localeFields(localized)
170
+ ...localeFields(localized),
171
+ actions: fieldActions
159
172
  };
160
173
  } else {
161
174
  result = {
162
175
  ...base,
163
176
  isUnset: true,
164
- ...localeFields(localized)
177
+ ...localeFields(localized),
178
+ actions: fieldActions
165
179
  };
166
180
  }
167
181
  } else {
@@ -169,7 +183,8 @@ function toLocalizedAPIField({
169
183
  result = {
170
184
  ...docField,
171
185
  type: 'image',
172
- ...localeFields(docField.localized)
186
+ ...localeFields(docField.localized),
187
+ actions: fieldActions
173
188
  };
174
189
  } else {
175
190
  const fields = toLocalizedAPIFields({
@@ -181,7 +196,8 @@ function toLocalizedAPIField({
181
196
  ...docField,
182
197
  type: 'image',
183
198
  fields,
184
- ...localeFields(docField.localized)
199
+ ...localeFields(docField.localized),
200
+ actions: fieldActions
185
201
  };
186
202
  }
187
203
  }
@@ -192,7 +208,8 @@ function toLocalizedAPIField({
192
208
  const { localized, locales, ...base } = docField;
193
209
  const localeProps = locales && locale ? locales[locale] : undefined;
194
210
  if (localeProps) {
195
- const { fields, getPreview, locale } = localeProps;
211
+ const { fields, getPreview, getObjectActions, locale } = localeProps;
212
+ const objectActions = getObjectActions?.();
196
213
  return {
197
214
  ...base,
198
215
  ...localeFields(localized),
@@ -201,6 +218,7 @@ function toLocalizedAPIField({
201
218
  delegate,
202
219
  locale
203
220
  }).previewTitle ?? (isListItem ? 'Item' : 'Object'),
221
+ actions: concatObjectAndFieldActions(objectActions, fieldActions),
204
222
  fields: toLocalizedAPIFields({
205
223
  docFields: fields,
206
224
  delegate,
@@ -211,6 +229,7 @@ function toLocalizedAPIField({
211
229
  return {
212
230
  ...base,
213
231
  ...localeFields(localized),
232
+ actions: fieldActions,
214
233
  isUnset: true
215
234
  };
216
235
  }
@@ -218,10 +237,12 @@ function toLocalizedAPIField({
218
237
  return {
219
238
  ...docField,
220
239
  type: 'object',
221
- ...localeFields(docField.localized)
240
+ ...localeFields(docField.localized),
241
+ actions: fieldActions
222
242
  };
223
243
  }
224
- const { fields, getPreview, ...rest } = docField;
244
+ const { fields, getPreview, getObjectActions, ...rest } = docField;
245
+ const objectActions = getObjectActions?.();
225
246
  return {
226
247
  ...rest,
227
248
  type: 'object',
@@ -230,6 +251,7 @@ function toLocalizedAPIField({
230
251
  delegate,
231
252
  locale
232
253
  }).previewTitle ?? (isListItem ? 'Item' : 'Object'),
254
+ actions: concatObjectAndFieldActions(objectActions, fieldActions),
233
255
  fields: toLocalizedAPIFields({
234
256
  docFields: fields,
235
257
  delegate,
@@ -243,7 +265,8 @@ function toLocalizedAPIField({
243
265
  const { localized, locales, ...base } = docField;
244
266
  const localeProps = locales && locale ? locales[locale] : undefined;
245
267
  if (localeProps) {
246
- const { fields, getPreview, ...rest } = localeProps;
268
+ const { fields, getPreview, getObjectActions, ...rest } = localeProps;
269
+ const objectActions = getObjectActions?.();
247
270
  return {
248
271
  ...base,
249
272
  ...rest,
@@ -253,6 +276,7 @@ function toLocalizedAPIField({
253
276
  delegate,
254
277
  locale
255
278
  }).previewTitle,
279
+ actions: concatObjectAndFieldActions(objectActions, fieldActions),
256
280
  fields: toLocalizedAPIFields({
257
281
  docFields: fields,
258
282
  delegate,
@@ -264,17 +288,20 @@ function toLocalizedAPIField({
264
288
  ...base,
265
289
  type: 'object', // API uses 'object' not 'model'
266
290
  isUnset: true,
267
- ...localeFields(localized)
291
+ ...localeFields(localized),
292
+ actions: fieldActions
268
293
  };
269
294
  }
270
295
  if (docField.isUnset) {
271
296
  return {
272
297
  ...docField,
273
298
  type: 'object', // API uses 'object' not 'model'
274
- ...localeFields(docField.localized)
299
+ ...localeFields(docField.localized),
300
+ actions: fieldActions
275
301
  };
276
302
  } else {
277
- const { fields, getPreview, ...rest } = docField;
303
+ const { fields, getPreview, getObjectActions, ...rest } = docField;
304
+ const objectActions = getObjectActions?.();
278
305
  return {
279
306
  ...rest,
280
307
  type: 'object', // API uses 'object' not 'model'
@@ -283,6 +310,7 @@ function toLocalizedAPIField({
283
310
  delegate,
284
311
  locale
285
312
  }).previewTitle,
313
+ actions: concatObjectAndFieldActions(objectActions, fieldActions),
286
314
  fields: toLocalizedAPIFields({
287
315
  docFields: fields,
288
316
  delegate,
@@ -301,7 +329,8 @@ function toLocalizedAPIField({
301
329
  type: refType === 'asset' ? 'image' : 'object',
302
330
  isUnset: true,
303
331
  ...base,
304
- ...localeFields(localized)
332
+ ...localeFields(localized),
333
+ actions: fieldActions
305
334
  } as ContentStoreTypes.DocumentObjectFieldAPI | ContentStoreTypes.DocumentImageFieldAPI;
306
335
  }
307
336
  return {
@@ -309,7 +338,8 @@ function toLocalizedAPIField({
309
338
  refType: refType === 'asset' ? 'image' : 'object',
310
339
  ...base,
311
340
  ...localeProps,
312
- ...localeFields(localized)
341
+ ...localeFields(localized),
342
+ actions: fieldActions
313
343
  };
314
344
  }
315
345
  const { type, refType, ...base } = docField;
@@ -317,14 +347,16 @@ function toLocalizedAPIField({
317
347
  return {
318
348
  type: refType === 'asset' ? 'image' : 'object',
319
349
  ...base,
320
- ...localeFields(docField.localized)
350
+ ...localeFields(docField.localized),
351
+ actions: fieldActions
321
352
  };
322
353
  }
323
354
  return {
324
355
  type: 'unresolved_reference',
325
356
  refType: refType === 'asset' ? 'image' : 'object',
326
357
  ...base,
327
- ...localeFields(docField.localized)
358
+ ...localeFields(docField.localized),
359
+ actions: fieldActions
328
360
  };
329
361
  }
330
362
  case 'cross-reference': {
@@ -337,7 +369,8 @@ function toLocalizedAPIField({
337
369
  type: 'object',
338
370
  isUnset: true,
339
371
  ...base,
340
- ...localeFields(localized)
372
+ ...localeFields(localized),
373
+ actions: fieldActions
341
374
  };
342
375
  }
343
376
  return {
@@ -345,7 +378,8 @@ function toLocalizedAPIField({
345
378
  refType: refType,
346
379
  ...base,
347
380
  ...localeProps,
348
- ...localeFields(localized)
381
+ ...localeFields(localized),
382
+ actions: fieldActions
349
383
  };
350
384
  }
351
385
  const { type, refType, ...base } = docField;
@@ -353,14 +387,16 @@ function toLocalizedAPIField({
353
387
  return {
354
388
  type: 'object',
355
389
  ...base,
356
- ...localeFields(docField.localized)
390
+ ...localeFields(docField.localized),
391
+ actions: fieldActions
357
392
  };
358
393
  }
359
394
  return {
360
395
  type: 'cross-reference',
361
396
  refType: refType,
362
397
  ...base,
363
- ...localeFields(docField.localized)
398
+ ...localeFields(docField.localized),
399
+ actions: fieldActions
364
400
  };
365
401
  }
366
402
  case 'list': {
@@ -370,6 +406,7 @@ function toLocalizedAPIField({
370
406
  return {
371
407
  ...base,
372
408
  ...localeProps,
409
+ actions: fieldActions,
373
410
  items: (localeProps?.items ?? []).map(
374
411
  (field) =>
375
412
  toLocalizedAPIField({
@@ -386,6 +423,7 @@ function toLocalizedAPIField({
386
423
  return {
387
424
  ...rest,
388
425
  ...localeFields(docField.localized),
426
+ actions: fieldActions,
389
427
  items: items.map(
390
428
  (field) =>
391
429
  toLocalizedAPIField({
@@ -498,3 +536,20 @@ function replaceAssetUrlIfNeeded(staticAssetsPublicPath: string, value: string |
498
536
  }
499
537
  return url;
500
538
  }
539
+
540
+ function concatObjectAndFieldActions(
541
+ objectActions?: APICustomActionObject[],
542
+ fieldActions?: APICustomActionField[]
543
+ ): (APICustomActionObject | APICustomActionField)[] | undefined {
544
+ let result: (APICustomActionObject | APICustomActionField)[] = [];
545
+ if (objectActions) {
546
+ result = result.concat(objectActions);
547
+ }
548
+ if (fieldActions) {
549
+ result = result.concat(fieldActions);
550
+ }
551
+ if (result.length) {
552
+ return result;
553
+ }
554
+ return undefined;
555
+ }
@@ -70,16 +70,16 @@ function mapStoreFieldsToCSIFields(documentFields: Record<string, ContentStoreTy
70
70
  );
71
71
  }
72
72
 
73
- function mapStoreFieldToCSIField<Type extends CSITypes.FieldType>(
73
+ export function mapStoreFieldToCSIField<Type extends CSITypes.FieldType>(
74
74
  documentField: ContentStoreTypes.DocumentFieldLocalizedForType<Type>
75
75
  ): CSITypes.DocumentFieldLocalizedForType<Type> | undefined;
76
- function mapStoreFieldToCSIField<Type extends CSITypes.FieldType>(
76
+ export function mapStoreFieldToCSIField<Type extends CSITypes.FieldType>(
77
77
  documentField: ContentStoreTypes.DocumentFieldNonLocalizedForType<Type>
78
78
  ): CSITypes.DocumentFieldNonLocalizedForType<Type> | undefined;
79
- function mapStoreFieldToCSIField<Type extends CSITypes.FieldType>(
79
+ export function mapStoreFieldToCSIField<Type extends CSITypes.FieldType>(
80
80
  documentField: ContentStoreTypes.DocumentFieldForType<Type>
81
81
  ): CSITypes.DocumentFieldForType<Type> | undefined;
82
- function mapStoreFieldToCSIField(documentField: ContentStoreTypes.DocumentField): CSITypes.DocumentField | undefined {
82
+ export function mapStoreFieldToCSIField(documentField: ContentStoreTypes.DocumentField): CSITypes.DocumentField | undefined {
83
83
  switch (documentField.type) {
84
84
  case 'string':
85
85
  case 'text':