@stackbit/cms-sanity 0.2.45-develop.1 → 0.2.45-staging.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 (34) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js.map +1 -1
  5. package/dist/sanity-content-source.d.ts +21 -10
  6. package/dist/sanity-content-source.d.ts.map +1 -1
  7. package/dist/sanity-content-source.js +74 -242
  8. package/dist/sanity-content-source.js.map +1 -1
  9. package/dist/sanity-document-converter.d.ts +11 -9
  10. package/dist/sanity-document-converter.d.ts.map +1 -1
  11. package/dist/sanity-document-converter.js +262 -205
  12. package/dist/sanity-document-converter.js.map +1 -1
  13. package/dist/sanity-operation-converter.d.ts +60 -0
  14. package/dist/sanity-operation-converter.d.ts.map +1 -0
  15. package/dist/sanity-operation-converter.js +664 -0
  16. package/dist/sanity-operation-converter.js.map +1 -0
  17. package/dist/sanity-schema-converter.d.ts +35 -3
  18. package/dist/sanity-schema-converter.d.ts.map +1 -1
  19. package/dist/sanity-schema-converter.js +290 -43
  20. package/dist/sanity-schema-converter.js.map +1 -1
  21. package/dist/sanity-schema-fetcher.d.ts +3 -3
  22. package/dist/sanity-schema-fetcher.d.ts.map +1 -1
  23. package/dist/utils.d.ts +53 -0
  24. package/dist/utils.d.ts.map +1 -1
  25. package/dist/utils.js +93 -1
  26. package/dist/utils.js.map +1 -1
  27. package/package.json +6 -5
  28. package/src/index.ts +1 -1
  29. package/src/sanity-content-source.ts +109 -317
  30. package/src/sanity-document-converter.ts +332 -231
  31. package/src/sanity-operation-converter.ts +785 -0
  32. package/src/sanity-schema-converter.ts +424 -70
  33. package/src/sanity-schema-fetcher.ts +3 -3
  34. package/src/utils.ts +98 -0
package/src/utils.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import _ from 'lodash';
2
2
  import { Field } from '@stackbit/types';
3
+ import * as StackbitTypes from '@stackbit/types';
4
+ import { ModelWithContext } from './sanity-schema-converter';
3
5
 
4
6
  export function resolveLabelFieldForModel(model: any, modelLabelFieldPath: string, fields: Field[]): string {
5
7
  let labelField = _.get(model, modelLabelFieldPath, null);
@@ -21,3 +23,99 @@ export function resolveLabelFieldForModel(model: any, modelLabelFieldPath: strin
21
23
  }
22
24
  return labelField || null;
23
25
  }
26
+
27
+ export function getSanityAliasFieldType({ resolvedType, model, modelFieldPath }: { resolvedType: string; model: ModelWithContext; modelFieldPath: string[] }) {
28
+ const fieldAlias = model.context?.fieldAliasMap?.[modelFieldPath.join('.')] ?? [];
29
+ return fieldAlias?.find((alias) => alias.resolvedTypeName === resolvedType)?.origTypeName ?? resolvedType;
30
+ }
31
+
32
+ export function resolvedFieldType({ sanityFieldType, model, modelFieldPath }: { sanityFieldType: string; model: ModelWithContext; modelFieldPath: string[] }) {
33
+ const fieldAlias = model.context?.fieldAliasMap?.[modelFieldPath.join('.')] ?? [];
34
+ return fieldAlias?.find((alias) => alias.origTypeName === sanityFieldType)?.resolvedTypeName ?? sanityFieldType;
35
+ }
36
+
37
+ export function isLocalizedModelField(modelField: StackbitTypes.Field | StackbitTypes.FieldListItems) {
38
+ return 'localized' in modelField && modelField.localized;
39
+ }
40
+
41
+ export interface FieldListMultiItem {
42
+ type: 'list';
43
+ items: StackbitTypes.FieldListItems | StackbitTypes.FieldListItems[];
44
+ }
45
+
46
+ /**
47
+ * Sanity 'Array' field type can hold multiple field types.
48
+ *
49
+ * For example, Sanity Arrays can simultaneously include items of `model`
50
+ * and `reference` types. https://www.sanity.io/docs/array-type#wT47gyCx
51
+ *
52
+ * With that, Sanity Arrays cannot include both primitive and complex types:
53
+ * https://www.sanity.io/docs/array-type#fNBIr84P
54
+ *
55
+ * TODO:
56
+ * This is not yet supported by Stackbit's TypeScript types, so the `any`
57
+ * must be used. Additionally, if a Sanity array has multiple types of items one
58
+ * of which is the 'object' type, then it will also have the 'name' property to
59
+ * allow matching 'object' items to their types. The 'name' property is not
60
+ * supported in Stackbit list items, so '@ts-ignore' is used.
61
+ *
62
+ * However, Stackbit client app should be able to render this types of lists correctly.
63
+ *
64
+ * @example A list that can include items of type 'model', 'reference' and 'object'.
65
+ * {
66
+ * type: 'list',
67
+ * items: [{
68
+ * type: 'model',
69
+ * models: [...]
70
+ * }, {
71
+ * type: 'reference',
72
+ * models: [...]
73
+ * }, {
74
+ * type: 'object',
75
+ * name: 'nested_object_name',
76
+ * fields: {...}
77
+ * }]
78
+ * }
79
+ */
80
+ export function getItemTypeForListItem(listItem: any, fieldModel: FieldListMultiItem): StackbitTypes.FieldListItems | null {
81
+ const itemModels = fieldModel.items ?? { type: 'string' };
82
+ // in Sanity, list items may have multiple types, in this case, 'items' will be an array
83
+ if (!_.isArray(itemModels)) {
84
+ return itemModels;
85
+ }
86
+ // Handle primitive list item types
87
+ // For primitive list items, the list will hold the primitive values as is,
88
+ // therefore, use JavaScript's `typeof` to infer the type of the values
89
+ const type = _.get(listItem, '_type');
90
+ if (!type) {
91
+ const type = typeof listItem;
92
+ if (typeIsPrimitive(type)) {
93
+ return { type: type };
94
+ }
95
+ return null;
96
+ }
97
+ if (type === 'reference') {
98
+ return _.find(itemModels, { type: 'reference' }) ?? null;
99
+ } else if (type === 'block') {
100
+ return _.find(itemModels, { type: 'richText' }) ?? null;
101
+ } else {
102
+ return (
103
+ _.find(itemModels, (itemModel) => {
104
+ if (itemModel.type === 'model') {
105
+ return _.includes(itemModel.models, type);
106
+ } else {
107
+ // if field was one of base types (object, image, slug, etc.)
108
+ // and it had a "name" property, then the "_type" will be equal to that name,
109
+ // otherwise the "_type" will be equal to the base type
110
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
111
+ // @ts-ignore
112
+ return itemModel.name === type || itemModel.type === type;
113
+ }
114
+ }) ?? null
115
+ );
116
+ }
117
+ }
118
+
119
+ export function typeIsPrimitive(type: string): type is 'string' | 'number' | 'boolean' {
120
+ return ['string', 'number', 'boolean'].includes(type);
121
+ }