@sanity/schema 4.20.0-next.70 → 4.20.0-next.72

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.
@@ -217,7 +217,7 @@ export declare class DescriptorConverter {
217
217
  *
218
218
  * This is automatically cached in a weak map.
219
219
  */
220
- get(schema: Schema): SetSynchronization<RegistryType>
220
+ get(schema: Schema): Promise<SetSynchronization<RegistryType>>
221
221
  }
222
222
 
223
223
  export declare function extractSchema(
package/lib/_internal.js CHANGED
@@ -23,28 +23,49 @@ class DescriptorConverter {
23
23
  *
24
24
  * This is automatically cached in a weak map.
25
25
  */
26
- get(schema) {
26
+ async get(schema) {
27
27
  let value = this.cache.get(schema);
28
28
  if (value) return value;
29
- const builder = new SetBuilder();
30
- for (const name of schema.getLocalTypeNames()) {
31
- const typeDef = convertTypeDef(schema.get(name));
32
- builder.addObject("sanity.schema.namedType", { name, typeDef });
33
- }
34
- return schema.parent && builder.addSet(this.get(schema.parent)), value = builder.build("sanity.schema.registry"), this.cache.set(schema, value), value;
29
+ const options = {
30
+ fields: /* @__PURE__ */ new Map(),
31
+ duplicateFields: /* @__PURE__ */ new Map(),
32
+ arrayElements: /* @__PURE__ */ new Map(),
33
+ duplicateArrayElements: /* @__PURE__ */ new Map()
34
+ }, namedTypes = schema.getLocalTypeNames().map((name) => {
35
+ const typeDef = convertTypeDef(schema.get(name), name, options);
36
+ return { name, typeDef };
37
+ }), rewriteMap = /* @__PURE__ */ new Map();
38
+ for (const [fieldDef, key] of options.duplicateFields.entries())
39
+ rewriteMap.set(fieldDef, { __type: "hoisted", key });
40
+ for (const [arrayElem, key] of options.duplicateArrayElements.entries())
41
+ rewriteMap.set(arrayElem, { __type: "hoisted", key });
42
+ const builder = new SetBuilder({ rewriteMap });
43
+ for (const [fieldDef, key] of options.duplicateFields.entries())
44
+ builder.addObject("sanity.schema.hoisted", { key, value: { ...fieldDef } });
45
+ for (const [arrayElem, key] of options.duplicateArrayElements.entries())
46
+ builder.addObject("sanity.schema.hoisted", { key, value: { ...arrayElem } });
47
+ for (const namedType of namedTypes)
48
+ builder.addObject("sanity.schema.namedType", namedType);
49
+ return schema.parent && builder.addSet(await this.get(schema.parent)), value = builder.build("sanity.schema.registry"), this.cache.set(schema, value), value;
35
50
  }
36
51
  }
37
- function convertCommonTypeDef(schemaType, opts) {
52
+ function convertCommonTypeDef(schemaType, path, opts) {
38
53
  const ownProps = OWN_PROPS_NAME in schemaType ? schemaType[OWN_PROPS_NAME] : schemaType;
39
54
  let fields;
40
- Array.isArray(ownProps.fields) && (fields = ownProps.fields.map(
41
- ({ name, group, fieldset, type }) => ({
55
+ Array.isArray(ownProps.fields) && (fields = ownProps.fields.map((field) => {
56
+ const fieldPath = `${path}.${field.name}`, value = opts.fields.get(field);
57
+ if (value) {
58
+ const otherPath = opts.duplicateFields.get(value);
59
+ return (!otherPath || isLessCanonicalName(fieldPath, otherPath)) && opts.duplicateFields.set(value, fieldPath), value;
60
+ }
61
+ const { name, group, fieldset, type } = field, converted = {
42
62
  name,
43
- typeDef: convertTypeDef(type),
63
+ typeDef: convertTypeDef(type, fieldPath, opts),
44
64
  groups: arrayifyString(group),
45
65
  fieldset
46
- })
47
- ));
66
+ };
67
+ return opts.fields.set(field, converted), converted;
68
+ }));
48
69
  let fieldsets;
49
70
  Array.isArray(ownProps.fieldsets) && (fieldsets = filterStringKey(
50
71
  "name",
@@ -93,8 +114,8 @@ function convertCommonTypeDef(schemaType, opts) {
93
114
  orderings
94
115
  };
95
116
  }
96
- function convertTypeDef(schemaType, opts) {
97
- const common2 = convertCommonTypeDef(schemaType);
117
+ function convertTypeDef(schemaType, path, opts) {
118
+ const common2 = convertCommonTypeDef(schemaType, path, opts);
98
119
  if (!schemaType.type)
99
120
  return {
100
121
  extends: null,
@@ -105,10 +126,18 @@ function convertTypeDef(schemaType, opts) {
105
126
  case "array":
106
127
  return {
107
128
  extends: "array",
108
- of: schemaType.of.map((ofType) => ({
109
- name: ofType.name,
110
- typeDef: convertTypeDef(ofType)
111
- })),
129
+ of: schemaType.of.map((ofType, idx) => {
130
+ const itemPath = `${path}.${ofType.name}`, value = opts.arrayElements.get(ofType);
131
+ if (value) {
132
+ const otherPath = opts.duplicateArrayElements.get(value);
133
+ return (!otherPath || isLessCanonicalName(itemPath, otherPath)) && opts.duplicateArrayElements.set(value, itemPath), value;
134
+ }
135
+ const converted = {
136
+ name: ofType.name,
137
+ typeDef: convertTypeDef(ofType, `${path}.${ofType.name}`, opts)
138
+ };
139
+ return opts.arrayElements.set(ofType, converted), converted;
140
+ }),
112
141
  ...common2
113
142
  };
114
143
  case "reference":
@@ -409,6 +438,9 @@ function maybeOrderingBy(val) {
409
438
  if (!(!field || !direction))
410
439
  return { field, direction };
411
440
  }
441
+ function isLessCanonicalName(a, b) {
442
+ return a.length < b.length || a.length === b.length && a < b;
443
+ }
412
444
  function processSchemaSynchronization(sync, response) {
413
445
  return processSetSynchronization(sync, response);
414
446
  }