@stackbit/cms-sanity 0.2.44-staging.2 → 0.2.45-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.
package/dist/utils.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.resolveLabelFieldForModel = void 0;
6
+ exports.typeIsPrimitive = exports.getItemTypeForListItem = exports.isLocalizedModelField = exports.resolvedFieldType = exports.getSanityAliasFieldType = exports.resolveLabelFieldForModel = void 0;
7
7
  const lodash_1 = __importDefault(require("lodash"));
8
8
  function resolveLabelFieldForModel(model, modelLabelFieldPath, fields) {
9
9
  let labelField = lodash_1.default.get(model, modelLabelFieldPath, null);
@@ -26,4 +26,96 @@ function resolveLabelFieldForModel(model, modelLabelFieldPath, fields) {
26
26
  return labelField || null;
27
27
  }
28
28
  exports.resolveLabelFieldForModel = resolveLabelFieldForModel;
29
+ function getSanityAliasFieldType({ resolvedType, model, modelFieldPath }) {
30
+ const fieldAlias = model.context?.fieldAliasMap?.[modelFieldPath.join('.')] ?? [];
31
+ return fieldAlias?.find((alias) => alias.resolvedTypeName === resolvedType)?.origTypeName ?? resolvedType;
32
+ }
33
+ exports.getSanityAliasFieldType = getSanityAliasFieldType;
34
+ function resolvedFieldType({ sanityFieldType, model, modelFieldPath }) {
35
+ const fieldAlias = model.context?.fieldAliasMap?.[modelFieldPath.join('.')] ?? [];
36
+ return fieldAlias?.find((alias) => alias.origTypeName === sanityFieldType)?.resolvedTypeName ?? sanityFieldType;
37
+ }
38
+ exports.resolvedFieldType = resolvedFieldType;
39
+ function isLocalizedModelField(modelField) {
40
+ return 'localized' in modelField && modelField.localized;
41
+ }
42
+ exports.isLocalizedModelField = isLocalizedModelField;
43
+ /**
44
+ * Sanity 'Array' field type can hold multiple field types.
45
+ *
46
+ * For example, Sanity Arrays can simultaneously include items of `model`
47
+ * and `reference` types. https://www.sanity.io/docs/array-type#wT47gyCx
48
+ *
49
+ * With that, Sanity Arrays cannot include both primitive and complex types:
50
+ * https://www.sanity.io/docs/array-type#fNBIr84P
51
+ *
52
+ * TODO:
53
+ * This is not yet supported by Stackbit's TypeScript types, so the `any`
54
+ * must be used. Additionally, if a Sanity array has multiple types of items one
55
+ * of which is the 'object' type, then it will also have the 'name' property to
56
+ * allow matching 'object' items to their types. The 'name' property is not
57
+ * supported in Stackbit list items, so '@ts-ignore' is used.
58
+ *
59
+ * However, Stackbit client app should be able to render this types of lists correctly.
60
+ *
61
+ * @example A list that can include items of type 'model', 'reference' and 'object'.
62
+ * {
63
+ * type: 'list',
64
+ * items: [{
65
+ * type: 'model',
66
+ * models: [...]
67
+ * }, {
68
+ * type: 'reference',
69
+ * models: [...]
70
+ * }, {
71
+ * type: 'object',
72
+ * name: 'nested_object_name',
73
+ * fields: {...}
74
+ * }]
75
+ * }
76
+ */
77
+ function getItemTypeForListItem(listItem, fieldModel) {
78
+ const itemModels = fieldModel.items ?? { type: 'string' };
79
+ // in Sanity, list items may have multiple types, in this case, 'items' will be an array
80
+ if (!lodash_1.default.isArray(itemModels)) {
81
+ return itemModels;
82
+ }
83
+ // Handle primitive list item types
84
+ // For primitive list items, the list will hold the primitive values as is,
85
+ // therefore, use JavaScript's `typeof` to infer the type of the values
86
+ const type = lodash_1.default.get(listItem, '_type');
87
+ if (!type) {
88
+ const type = typeof listItem;
89
+ if (typeIsPrimitive(type)) {
90
+ return { type: type };
91
+ }
92
+ return null;
93
+ }
94
+ if (type === 'reference') {
95
+ return lodash_1.default.find(itemModels, { type: 'reference' }) ?? null;
96
+ }
97
+ else if (type === 'block') {
98
+ return lodash_1.default.find(itemModels, { type: 'richText' }) ?? null;
99
+ }
100
+ else {
101
+ return (lodash_1.default.find(itemModels, (itemModel) => {
102
+ if (itemModel.type === 'model') {
103
+ return lodash_1.default.includes(itemModel.models, type);
104
+ }
105
+ else {
106
+ // if field was one of base types (object, image, slug, etc.)
107
+ // and it had a "name" property, then the "_type" will be equal to that name,
108
+ // otherwise the "_type" will be equal to the base type
109
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
110
+ // @ts-ignore
111
+ return itemModel.name === type || itemModel.type === type;
112
+ }
113
+ }) ?? null);
114
+ }
115
+ }
116
+ exports.getItemTypeForListItem = getItemTypeForListItem;
117
+ function typeIsPrimitive(type) {
118
+ return ['string', 'number', 'boolean'].includes(type);
119
+ }
120
+ exports.typeIsPrimitive = typeIsPrimitive;
29
121
  //# sourceMappingURL=utils.js.map
package/dist/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAuB;AAGvB,SAAgB,yBAAyB,CAAC,KAAU,EAAE,mBAA2B,EAAE,MAAe;IAC9F,IAAI,UAAU,GAAG,gBAAC,CAAC,GAAG,CAAC,KAAK,EAAE,mBAAmB,EAAE,IAAI,CAAC,CAAC;IACzD,IAAI,UAAU,EAAE;QACZ,OAAO,UAAU,CAAC;KACrB;IACD,wCAAwC;IACxC,IAAI,UAAU,GAAG,gBAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,OAAO,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9G,IAAI,CAAC,UAAU,EAAE;QACb,wCAAwC;QACxC,UAAU,GAAG,gBAAC,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,OAAO,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;KAC7G;IACD,IAAI,CAAC,UAAU,EAAE;QACb,+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AAI,CAAC,CAAC;AAC1D,CAAC;AAFD,0CAEC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stackbit/cms-sanity",
3
- "version": "0.2.44-staging.2",
3
+ "version": "0.2.45-develop.2",
4
4
  "description": "Stackbit Sanity CMS Interface",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -32,9 +32,10 @@
32
32
  "@sanity/block-tools": "^2.29.3",
33
33
  "@sanity/client": "^3.3.0",
34
34
  "@sanity/schema": "^2.29.3",
35
- "@stackbit/cms-core": "1.0.19-staging.2",
36
- "@stackbit/types": "0.11.8-staging.1",
37
- "@stackbit/utils": "0.4.8-staging.1",
35
+ "@sanity/types": "^3.31.0",
36
+ "@stackbit/cms-core": "1.0.20-develop.2",
37
+ "@stackbit/types": "0.11.9-develop.2",
38
+ "@stackbit/utils": "0.4.9-develop.2",
38
39
  "fs-extra": "^9.0.13",
39
40
  "glob": "10.2.6",
40
41
  "jsdom": "^20.0.0",
@@ -50,5 +51,5 @@
50
51
  "@types/tinycolor2": "^1.4.3",
51
52
  "@types/uuid": "^8.3.4"
52
53
  },
53
- "gitHead": "c0d4161f28b2fd0acffb82fa86d84a610ecf6cdf"
54
+ "gitHead": "706448ac139536d5a06d36fd529b7544f3da77a4"
54
55
  }
package/src/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as fetcher from './sanity-schema-fetcher';
2
2
  import * as converter from './sanity-schema-legacy-converter';
3
3
  import { UserCommandSpawner } from '@stackbit/types';
4
-
5
4
  export * from './sanity-content-source';
5
+ export { SchemaContext, ModelContext, ModelWithContext } from './sanity-schema-converter';
6
6
  export { DocumentContext, AssetContext, ContextualDocument, ContextualAsset } from './sanity-document-converter';
7
7
  export { default as SanityEncoderDelegate } from './sanity-encoder-delegate';
8
8