@stackbit/cms-core 1.0.2-develop.1 → 1.0.2-develop.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.
Files changed (64) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/content-store.d.ts +6 -1
  3. package/dist/content-store.d.ts.map +1 -1
  4. package/dist/content-store.js +37 -6
  5. package/dist/content-store.js.map +1 -1
  6. package/dist/types/content-store-api-document-fields.d.ts +3 -33
  7. package/dist/types/content-store-api-document-fields.d.ts.map +1 -1
  8. package/dist/types/content-store-document-fields.d.ts +1 -106
  9. package/dist/types/content-store-document-fields.d.ts.map +1 -1
  10. package/dist/types/content-store-documents.d.ts +4 -2
  11. package/dist/types/content-store-documents.d.ts.map +1 -1
  12. package/dist/types/custom-actions.d.ts +2 -3
  13. package/dist/types/custom-actions.d.ts.map +1 -1
  14. package/dist/types/index.d.ts +1 -0
  15. package/dist/types/index.d.ts.map +1 -1
  16. package/dist/types/index.js +1 -0
  17. package/dist/types/index.js.map +1 -1
  18. package/dist/utils/csi-to-api-docs-converter.d.ts +3 -9
  19. package/dist/utils/csi-to-api-docs-converter.d.ts.map +1 -1
  20. package/dist/utils/csi-to-api-docs-converter.js +23 -27
  21. package/dist/utils/csi-to-api-docs-converter.js.map +1 -1
  22. package/dist/utils/csi-to-store-docs-converter.d.ts.map +1 -1
  23. package/dist/utils/csi-to-store-docs-converter.js +0 -38
  24. package/dist/utils/csi-to-store-docs-converter.js.map +1 -1
  25. package/dist/utils/custom-actions.d.ts +2 -41
  26. package/dist/utils/custom-actions.d.ts.map +1 -1
  27. package/dist/utils/custom-actions.js +3 -198
  28. package/dist/utils/custom-actions.js.map +1 -1
  29. package/dist/utils/custom-search-filters.d.ts +12 -0
  30. package/dist/utils/custom-search-filters.d.ts.map +1 -0
  31. package/dist/utils/custom-search-filters.js +46 -0
  32. package/dist/utils/custom-search-filters.js.map +1 -0
  33. package/dist/utils/field-path-utils.d.ts +1 -1
  34. package/dist/utils/field-path-utils.d.ts.map +1 -1
  35. package/dist/utils/field-path-utils.js +4 -0
  36. package/dist/utils/field-path-utils.js.map +1 -1
  37. package/dist/utils/model-utils.d.ts.map +1 -1
  38. package/dist/utils/model-utils.js +21 -0
  39. package/dist/utils/model-utils.js.map +1 -1
  40. package/dist/utils/store-to-api-docs-converter.d.ts.map +1 -1
  41. package/dist/utils/store-to-api-docs-converter.js +26 -74
  42. package/dist/utils/store-to-api-docs-converter.js.map +1 -1
  43. package/dist/utils/store-to-api-v2-docs-converter.d.ts +2 -3
  44. package/dist/utils/store-to-api-v2-docs-converter.d.ts.map +1 -1
  45. package/dist/utils/store-to-api-v2-docs-converter.js +91 -148
  46. package/dist/utils/store-to-api-v2-docs-converter.js.map +1 -1
  47. package/package.json +5 -5
  48. package/src/content-store.ts +53 -8
  49. package/src/types/content-store-api-document-fields.ts +471 -0
  50. package/src/types/content-store-document-fields.ts +1 -50
  51. package/src/types/content-store-documents.ts +4 -2
  52. package/src/types/custom-actions.ts +2 -3
  53. package/src/types/index.ts +1 -0
  54. package/src/utils/csi-to-api-docs-converter.ts +27 -42
  55. package/src/utils/csi-to-store-docs-converter.ts +1 -42
  56. package/src/utils/custom-actions.ts +4 -277
  57. package/src/utils/field-path-utils.ts +6 -2
  58. package/src/utils/model-utils.ts +20 -2
  59. package/src/utils/store-to-api-docs-converter.ts +26 -79
  60. package/src/utils/store-to-api-v2-docs-converter.ts +441 -0
  61. package/dist/utils/file-cache.d.ts +0 -13
  62. package/dist/utils/file-cache.d.ts.map +0 -1
  63. package/dist/utils/file-cache.js +0 -69
  64. package/dist/utils/file-cache.js.map +0 -1
@@ -1,5 +1,5 @@
1
1
  import _ from 'lodash';
2
- import { Config, PageModel, DataModel, FieldObjectProps, FieldSpecificProps, mapModelFieldsRecursively, ObjectModel } from '@stackbit/sdk';
2
+ import { Config } from '@stackbit/sdk';
3
3
  import * as StackbitTypes from '@stackbit/types';
4
4
  import { mapPromise, omitByNil } from '@stackbit/utils';
5
5
  import * as ContentStoreTypes from '../types';
@@ -7,14 +7,10 @@ import {
7
7
  CustomActionStateChange,
8
8
  APICustomAction,
9
9
  APICustomActionDocumentSpecifier,
10
- APICustomActionDocument,
11
- APICustomActionObject,
12
- APICustomActionField,
13
10
  APIGetCustomActionRequest,
14
11
  APIRunCustomActionRequest,
15
12
  APIRunCustomActionRequestBulk,
16
13
  CustomActionRunStateMap,
17
- ExtendedCustomActionDocument,
18
14
  ExtendedCustomActionObjectModel,
19
15
  ExtendedCustomActionField,
20
16
  APICustomActionGlobal,
@@ -28,30 +24,6 @@ import { mapStoreDocumentToCSIDocumentWithSource, mapStoreFieldToCSIField } from
28
24
  import { getModelAndDocumentFieldForLocalizedFieldPath } from './field-path-utils';
29
25
  import { getContentSourceDataByTypeAndProjectIdOrThrow, getUserContextForSrcTypeThunk } from '../content-store-utils';
30
26
 
31
- /**
32
- * Removes "run" and "state" functions from actions of models, nested objects and fields.
33
- * Sets "needsResolving" flag to let client know if the action needs additional resolving.
34
- * @param {StackbitTypes.ModelMap} modelMap
35
- */
36
- export function stripModelActions({ modelMap }: { modelMap: StackbitTypes.ModelMap }): StackbitTypes.ModelMap {
37
- return _.mapValues(modelMap, (model) => {
38
- if ('actions' in model) {
39
- const { actions, ...restModel } = model;
40
- model = restModel;
41
- }
42
-
43
- model = mapModelFieldsRecursively(model, (field) => {
44
- if ('actions' in field && Array.isArray(field.actions)) {
45
- const { actions, ...restField } = field;
46
- field = restField;
47
- }
48
- return field;
49
- });
50
-
51
- return model;
52
- });
53
- }
54
-
55
27
  export async function getGlobalAndBulkAPIActions({
56
28
  stackbitConfig,
57
29
  customActionRunStateMap,
@@ -113,204 +85,6 @@ export async function getGlobalAndBulkAPIActions({
113
85
  });
114
86
  }
115
87
 
116
- export function getDocumentActionsThunk({
117
- csiDocument,
118
- model,
119
- srcType,
120
- srcProjectId,
121
- customActionRunStateMap
122
- }: {
123
- csiDocument: StackbitTypes.Document;
124
- model: PageModel | DataModel;
125
- srcType: string;
126
- srcProjectId: string;
127
- customActionRunStateMap: ContentStoreTypes.CustomActionRunStateMap;
128
- }): () => APICustomActionDocument[] | undefined {
129
- if (!('actions' in model) || !Array.isArray(model.actions)) {
130
- return () => undefined;
131
- }
132
-
133
- const extendedDocumentActions = model.actions.map((action): ExtendedCustomActionDocument => {
134
- const documentSpec = {
135
- srcType: srcType,
136
- srcProjectId: srcProjectId,
137
- srcDocumentId: csiDocument.id
138
- };
139
- return {
140
- // if configuration is updated, the new action properties will override the stored action properties
141
- ...action,
142
- type: 'document',
143
- actionId: documentActionId({ ...documentSpec, actionName: action.name }),
144
- label: action.label ?? _.startCase(action.name),
145
- documentSpec: documentSpec
146
- };
147
- });
148
-
149
- return () =>
150
- extendedDocumentActions.map((extendedAction) => {
151
- return omitByNil({
152
- type: 'document',
153
- actionId: extendedAction.actionId,
154
- name: extendedAction.name,
155
- label: extendedAction.label,
156
- icon: extendedAction.icon,
157
- inputFields: extendedAction.inputFields,
158
- ...getAPIActionState(extendedAction, customActionRunStateMap)
159
- });
160
- });
161
- }
162
-
163
- export function getObjectModelActionsThunk({
164
- model,
165
- csiParentDocument,
166
- srcType,
167
- srcProjectId,
168
- customActionRunStateMap,
169
- fieldPath
170
- }: {
171
- model: ObjectModel;
172
- csiParentDocument: StackbitTypes.Document;
173
- srcType: string;
174
- srcProjectId: string;
175
- customActionRunStateMap: ContentStoreTypes.CustomActionRunStateMap;
176
- fieldPath: (string | number)[];
177
- }): () => ContentStoreTypes.APICustomActionObject[] | undefined {
178
- if (!('actions' in model) || !Array.isArray(model.actions)) {
179
- return () => undefined;
180
- }
181
-
182
- const extendedObjectModelActions = model.actions.map((action): ExtendedCustomActionObjectModel => {
183
- const documentSpec = {
184
- srcType: srcType,
185
- srcProjectId: srcProjectId,
186
- srcDocumentId: csiParentDocument.id
187
- };
188
- return {
189
- // if configuration is updated, the new action properties will override the stored action properties
190
- ...action,
191
- type: 'objectModel',
192
- actionId: fieldPathActionId({ ...documentSpec, fieldPath, actionName: action.name }),
193
- label: action.label ?? _.startCase(action.name),
194
- documentSpec: documentSpec,
195
- fieldPath
196
- };
197
- });
198
-
199
- return () =>
200
- extendedObjectModelActions.map((extendedAction) => {
201
- return omitByNil({
202
- type: 'object',
203
- actionId: extendedAction.actionId,
204
- name: extendedAction.name,
205
- label: extendedAction.label,
206
- icon: extendedAction.icon,
207
- inputFields: extendedAction.inputFields,
208
- ...getAPIActionState(extendedAction, customActionRunStateMap)
209
- });
210
- });
211
- }
212
-
213
- export function getObjectFieldActionsThunk({
214
- modelField,
215
- csiParentDocument,
216
- srcType,
217
- srcProjectId,
218
- customActionRunStateMap,
219
- fieldPath
220
- }: {
221
- modelField: FieldObjectProps;
222
- csiParentDocument: StackbitTypes.Document;
223
- srcType: string;
224
- srcProjectId: string;
225
- customActionRunStateMap: ContentStoreTypes.CustomActionRunStateMap;
226
- fieldPath: (string | number)[];
227
- }): () => APICustomActionObject[] | undefined {
228
- if (!('actions' in modelField) || !Array.isArray(modelField.actions)) {
229
- return () => undefined;
230
- }
231
- const objectFieldActions = modelField.actions.filter((action): action is StackbitTypes.CustomActionObjectField => action.type === 'object');
232
-
233
- const extendedObjectFieldActions = objectFieldActions.map((action): ExtendedCustomActionObjectField => {
234
- const documentSpec = {
235
- srcType: srcType,
236
- srcProjectId: srcProjectId,
237
- srcDocumentId: csiParentDocument.id
238
- };
239
- return {
240
- // if configuration is updated, the new action properties will override the stored action properties
241
- ...action,
242
- type: 'objectField',
243
- actionId: fieldPathActionId({ ...documentSpec, fieldPath, actionName: action.name }),
244
- label: action.label ?? _.startCase(action.name),
245
- documentSpec: documentSpec,
246
- fieldPath
247
- };
248
- });
249
-
250
- return () =>
251
- extendedObjectFieldActions.map((extendedAction) => {
252
- return omitByNil({
253
- type: 'object',
254
- actionId: extendedAction.actionId,
255
- name: extendedAction.name,
256
- label: extendedAction.label,
257
- icon: extendedAction.icon,
258
- inputFields: extendedAction.inputFields,
259
- ...getAPIActionState(extendedAction, customActionRunStateMap)
260
- });
261
- });
262
- }
263
-
264
- export function getFieldActions({
265
- modelField,
266
- csiParentDocument,
267
- srcType,
268
- srcProjectId,
269
- customActionRunStateMap,
270
- fieldPath
271
- }: {
272
- modelField: FieldSpecificProps;
273
- csiParentDocument: StackbitTypes.Document;
274
- srcType: string;
275
- srcProjectId: string;
276
- customActionRunStateMap: ContentStoreTypes.CustomActionRunStateMap;
277
- fieldPath: (string | number)[];
278
- }): APICustomActionField[] | undefined {
279
- if (!('actions' in modelField) || !Array.isArray(modelField.actions)) {
280
- return undefined;
281
- }
282
- const fieldActions = modelField.actions.filter((action): action is StackbitTypes.CustomActionField => action.type !== 'object');
283
-
284
- const extendedFieldActions = fieldActions.map((action): ExtendedCustomActionField => {
285
- const documentSpec = {
286
- srcType: srcType,
287
- srcProjectId: srcProjectId,
288
- srcDocumentId: csiParentDocument.id
289
- };
290
- return {
291
- // if configuration is updated, the new action properties will override the stored action properties
292
- ...action,
293
- type: 'field',
294
- actionId: fieldPathActionId({ ...documentSpec, fieldPath, actionName: action.name }),
295
- label: action.label ?? _.startCase(action.name),
296
- documentSpec: documentSpec,
297
- fieldPath
298
- };
299
- });
300
-
301
- return extendedFieldActions.map((extendedAction) => {
302
- return omitByNil({
303
- type: 'field',
304
- actionId: extendedAction.actionId,
305
- name: extendedAction.name,
306
- label: extendedAction.label,
307
- icon: extendedAction.icon,
308
- inputFields: extendedAction.inputFields,
309
- ...getAPIActionState(extendedAction, customActionRunStateMap)
310
- });
311
- });
312
- }
313
-
314
88
  export async function resolveCustomActionsById({
315
89
  getActionRequest,
316
90
  customActionRunStateMap,
@@ -620,28 +394,6 @@ function getCSIDocumentWithSourceFromDocumentSpec(
620
394
  : undefined;
621
395
  }
622
396
 
623
- function getAPIActionState(
624
- extendedAction: ExtendedCustomAction,
625
- customActionRunStateMap: CustomActionRunStateMap
626
- ): {
627
- state: StackbitTypes.CustomActionState | 'unknown';
628
- needsResolving: boolean;
629
- } {
630
- const actionId = extendedAction.actionId;
631
- const actionRunState = customActionRunStateMap[actionId];
632
- let state: StackbitTypes.CustomActionState | 'unknown';
633
- let needsResolving = false;
634
- if (actionRunState?.runningHandler) {
635
- state = 'running';
636
- } else if (extendedAction.state) {
637
- state = 'unknown';
638
- needsResolving = true;
639
- } else {
640
- state = actionRunState?.lastResultState ?? 'enabled';
641
- }
642
- return { state, needsResolving };
643
- }
644
-
645
397
  function getHandlerParamsForObjectModelAction({
646
398
  extendedAction,
647
399
  contentSourceDataById
@@ -733,10 +485,10 @@ function getHandlerParamsForFieldAction({
733
485
  }) as {
734
486
  // list items cannot have actions, therefore we can safely reduce the type to non list item fields
735
487
  modelField: StackbitTypes.Field;
736
- documentField: ContentStoreTypes.DocumentField;
488
+ documentField?: ContentStoreTypes.DocumentField;
737
489
  };
738
490
 
739
- const csiDocumentField = mapStoreFieldToCSIField(documentField);
491
+ const csiDocumentField = documentField ? mapStoreFieldToCSIField(documentField) : undefined;
740
492
 
741
493
  return {
742
494
  parentDocument: mappedCSIDocument,
@@ -882,7 +634,7 @@ function findCustomActionById({
882
634
  }
883
635
 
884
636
  if (modelField.type === 'model') {
885
- if (documentField.type !== 'model' || documentField.localized || documentField.isUnset) {
637
+ if (!documentField || documentField.type !== 'model' || documentField.localized || documentField.isUnset) {
886
638
  return undefined;
887
639
  }
888
640
  const modelName = documentField.srcModelName;
@@ -925,31 +677,6 @@ function getGlobalActionNameFromId(actionId: string) {
925
677
  return actionId.substring('config.actions.'.length);
926
678
  }
927
679
 
928
- function documentActionId({ srcType, srcProjectId, srcDocumentId, actionName }: APICustomActionDocumentSpecifier & { actionName: string }): string {
929
- return JSON.stringify({
930
- srcType,
931
- srcProjectId,
932
- srcDocumentId,
933
- actionName
934
- });
935
- }
936
-
937
- function fieldPathActionId({
938
- srcType,
939
- srcProjectId,
940
- srcDocumentId,
941
- fieldPath,
942
- actionName
943
- }: APICustomActionDocumentSpecifier & { fieldPath: (string | number)[]; actionName: string }): string {
944
- return JSON.stringify({
945
- srcType,
946
- srcProjectId,
947
- srcDocumentId,
948
- fieldPath,
949
- actionName
950
- });
951
- }
952
-
953
680
  function parseActionId(actionId: string):
954
681
  | {
955
682
  srcType?: string;
@@ -532,7 +532,7 @@ export function getModelAndDocumentFieldForLocalizedFieldPath({
532
532
  fieldPath: (string | number)[];
533
533
  modelMap: Record<string, Model>;
534
534
  isFullFieldPath?: boolean;
535
- }): { modelField: Field | FieldListItems; documentField: ContentStoreTypes.DocumentField | ContentStoreTypes.DocumentListFieldItems } {
535
+ }): { modelField: Field | FieldListItems; documentField?: ContentStoreTypes.DocumentField | ContentStoreTypes.DocumentListFieldItems } {
536
536
  if (isFullFieldPath) {
537
537
  if (_.head(fieldPath) !== 'fields') {
538
538
  throw new Error('fieldPath must start with "fields" specifier');
@@ -566,7 +566,7 @@ export function getModelAndDocumentFieldForLocalizedFieldPath({
566
566
  docField: ContentStoreTypes.DocumentField | ContentStoreTypes.DocumentListFieldItems,
567
567
  modelField: Field | FieldListItems,
568
568
  fieldPath: (string | number)[]
569
- ): { modelField: Field | FieldListItems; documentField: ContentStoreTypes.DocumentField | ContentStoreTypes.DocumentListFieldItems } {
569
+ ): { modelField: Field | FieldListItems; documentField?: ContentStoreTypes.DocumentField | ContentStoreTypes.DocumentListFieldItems } {
570
570
  // if no more items in fieldPath return the found document and model fields
571
571
  if (fieldPath.length === 0) {
572
572
  return {
@@ -589,6 +589,10 @@ export function getModelAndDocumentFieldForLocalizedFieldPath({
589
589
  fieldPath = _.tail(fieldPath);
590
590
  const localizedDocField = getDocumentFieldForLocale(docField, locale);
591
591
  if (!localizedDocField) {
592
+ if (!fieldPath.length) {
593
+ // field locale is not set
594
+ return { modelField, documentField: undefined };
595
+ }
592
596
  throw new Error(`fieldPath '${origFieldPath.join('.')}' doesn't specify the locale`);
593
597
  }
594
598
  docField = localizedDocField;
@@ -1,6 +1,5 @@
1
1
  import _ from 'lodash';
2
-
3
- import { FieldCrossReferenceModel, Logger, ModelWithSource } from '@stackbit/types';
2
+ import { FieldCrossReferenceModel, Logger, ModelWithSource, CustomActionField, CustomActionObjectField } from '@stackbit/types';
4
3
  import {
5
4
  Model,
6
5
  assignLabelFieldIfNeeded,
@@ -43,6 +42,18 @@ export function normalizeModels({ models, logger }: { models: ModelWithSource[];
43
42
 
44
43
  assignLabelFieldIfNeeded(model);
45
44
 
45
+ if ((model.type === 'data' || model.type === 'page') && model.actions) {
46
+ model.actions = model.actions.map((action) => ({
47
+ ...action,
48
+ type: 'document'
49
+ }));
50
+ } else if (model.type === 'object' && model.actions) {
51
+ model.actions = model.actions.map((action) => ({
52
+ ...action,
53
+ type: 'object'
54
+ }));
55
+ }
56
+
46
57
  model = mapModelFieldsRecursively(model, (field) => {
47
58
  field = { ...field };
48
59
 
@@ -50,6 +61,13 @@ export function normalizeModels({ models, logger }: { models: ModelWithSource[];
50
61
  field.label = _.startCase(field.name);
51
62
  }
52
63
 
64
+ if (field.actions) {
65
+ field.actions = field.actions.map((action) => ({
66
+ ...action,
67
+ type: action.type ?? 'field'
68
+ })) as (CustomActionField | CustomActionObjectField)[];
69
+ }
70
+
53
71
  mapListItemsPropsOrSelfSpecificProps(field, (listItemsPropsOrField) => {
54
72
  if (isObjectField(listItemsPropsOrField)) {
55
73
  assignLabelFieldIfNeeded(listItemsPropsOrField);