@prismicio/types-internal 3.11.0 → 3.11.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.
@@ -165,16 +165,17 @@ function collectSharedSlices(customType) {
165
165
  exports.collectSharedSlices = collectSharedSlices;
166
166
  function traverseCustomType(args) {
167
167
  const { customType, onField } = args;
168
+ const json = {};
169
+ for (const [key, section] of Object.entries(customType.json)) {
170
+ json[key] = (0, Section_1.traverseSection)({
171
+ path: [key],
172
+ section,
173
+ onField,
174
+ });
175
+ }
168
176
  return {
169
177
  ...customType,
170
- json: Object.entries(customType.json).reduce((acc, [key, section]) => {
171
- acc[key] = (0, Section_1.traverseSection)({
172
- path: [key],
173
- section,
174
- onField,
175
- });
176
- return acc;
177
- }, {}),
178
+ json,
178
179
  };
179
180
  }
180
181
  exports.traverseCustomType = traverseCustomType;
@@ -21,9 +21,10 @@ exports.Sections = {
21
21
  },
22
22
  };
23
23
  function traverseSection(args) {
24
- const { path: prevPath, section, onField } = args;
25
- return Object.entries(section).reduce((acc, [key, prevModel]) => {
26
- const path = prevPath.concat(key);
24
+ const { path: prevPath, section: prevSection, onField } = args;
25
+ const section = { ...prevSection };
26
+ for (const [key, prevModel] of Object.entries(prevSection)) {
27
+ const path = [...prevPath, key];
27
28
  let model;
28
29
  switch (prevModel.type) {
29
30
  case "Choice":
@@ -53,8 +54,8 @@ function traverseSection(args) {
53
54
  });
54
55
  break;
55
56
  }
56
- acc[key] = model;
57
- return acc;
58
- }, {});
57
+ section[key] = model;
58
+ }
59
+ return section;
59
60
  }
60
61
  exports.traverseSection = traverseSection;
@@ -34,61 +34,59 @@ exports.Group = createGroup(t.union([NestableWidget_1.NestableWidget, exports.Ne
34
34
  function traverseNestedGroup(args) {
35
35
  var _a;
36
36
  const { path: prevPath, group, onField } = args;
37
- const fields = ((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields) &&
38
- Object.entries(group.config.fields).reduce((acc, [key, model]) => {
39
- const path = prevPath.concat(key);
40
- acc[key] = onField({
41
- path,
42
- key,
43
- field: model,
44
- });
45
- return acc;
46
- }, {});
37
+ if (!((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields))
38
+ return group;
39
+ const fields = {};
40
+ for (const [key, prevField] of Object.entries(group.config.fields)) {
41
+ const path = [...prevPath, key];
42
+ fields[key] = onField({
43
+ path,
44
+ key,
45
+ field: prevField,
46
+ });
47
+ }
47
48
  return {
48
49
  ...group,
49
- ...(group.config && {
50
- config: {
51
- ...group.config,
52
- ...(fields && { fields }),
53
- },
54
- }),
50
+ config: {
51
+ ...group.config,
52
+ fields,
53
+ },
55
54
  };
56
55
  }
57
56
  exports.traverseNestedGroup = traverseNestedGroup;
58
57
  function traverseGroup(args) {
59
58
  var _a;
60
59
  const { path: prevPath, group, onField } = args;
61
- const fields = ((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields) &&
62
- Object.entries(group.config.fields).reduce((acc, [key, prevField]) => {
63
- const path = prevPath.concat(key);
64
- let field;
65
- switch (prevField.type) {
66
- case "Group":
67
- field = traverseNestedGroup({
68
- path,
69
- group: prevField,
70
- onField: onField,
71
- });
72
- break;
73
- default:
74
- field = prevField;
75
- break;
76
- }
77
- acc[key] = onField({
78
- path,
79
- key,
80
- field,
81
- });
82
- return acc;
83
- }, {});
60
+ if (!((_a = group.config) === null || _a === void 0 ? void 0 : _a.fields))
61
+ return group;
62
+ const fields = {};
63
+ for (const [key, prevField] of Object.entries(group.config.fields)) {
64
+ const path = [...prevPath, key];
65
+ let field;
66
+ switch (prevField.type) {
67
+ case "Group":
68
+ field = traverseNestedGroup({
69
+ path,
70
+ group: prevField,
71
+ onField: onField,
72
+ });
73
+ break;
74
+ default:
75
+ field = prevField;
76
+ break;
77
+ }
78
+ fields[key] = onField({
79
+ path,
80
+ key,
81
+ field,
82
+ });
83
+ }
84
84
  return {
85
85
  ...group,
86
- ...(group.config && {
87
- config: {
88
- ...group.config,
89
- ...(fields && { fields }),
90
- },
91
- }),
86
+ config: {
87
+ ...group.config,
88
+ fields,
89
+ },
92
90
  };
93
91
  }
94
92
  exports.traverseGroup = traverseGroup;
@@ -29,27 +29,22 @@ function isCompositeSlice(slice) {
29
29
  }
30
30
  exports.isCompositeSlice = isCompositeSlice;
31
31
  function traverseCompositeSlice(args) {
32
+ var _a, _b;
32
33
  const { path: prevPath, slice, onField } = args;
33
- const nonRepeat = slice["non-repeat"] &&
34
- Object.entries(slice["non-repeat"]).reduce((acc, [key, field]) => {
35
- const path = prevPath.concat("non-repeat", key);
36
- acc[key] = onField({ path, key, field });
37
- return acc;
38
- }, {});
39
- const repeat = slice.repeat &&
40
- Object.entries(slice.repeat).reduce((acc, [key, field]) => {
41
- const path = prevPath.concat("repeat", key);
42
- acc[key] = onField({ path, key, field });
43
- return acc;
44
- }, {});
34
+ const nonRepeat = {};
35
+ for (const [key, field] of Object.entries((_a = slice["non-repeat"]) !== null && _a !== void 0 ? _a : {})) {
36
+ const path = [...prevPath, "non-repeat", key];
37
+ nonRepeat[key] = onField({ path, key, field });
38
+ }
39
+ const repeat = {};
40
+ for (const [key, field] of Object.entries((_b = slice.repeat) !== null && _b !== void 0 ? _b : {})) {
41
+ const path = [...prevPath, "repeat", key];
42
+ repeat[key] = onField({ path, key, field });
43
+ }
45
44
  return {
46
45
  ...slice,
47
- ...(nonRepeat && {
48
- "non-repeat": nonRepeat,
49
- }),
50
- ...(repeat && {
51
- repeat,
52
- }),
46
+ ...(slice["non-repeat"] && { "non-repeat": nonRepeat }),
47
+ ...(slice.repeat && { repeat }),
53
48
  };
54
49
  }
55
50
  exports.traverseCompositeSlice = traverseCompositeSlice;
@@ -48,59 +48,52 @@ function isDynamicSharedSlice(slice) {
48
48
  }
49
49
  exports.isDynamicSharedSlice = isDynamicSharedSlice;
50
50
  function traverseVariation(args) {
51
+ var _a, _b;
51
52
  const { path: prevPath, variation, onField } = args;
52
- return {
53
- ...variation,
54
- ...(variation.primary && {
55
- primary: Object.entries(variation.primary).reduce((acc, [key, prevField]) => {
56
- const path = prevPath.concat("primary", key);
57
- let field;
58
- switch (prevField.type) {
59
- case "Group":
60
- field = (0, Group_1.traverseGroup)({
61
- path,
62
- group: prevField,
63
- onField: onField,
64
- });
65
- break;
66
- default:
67
- field = prevField;
68
- break;
69
- }
70
- acc[key] = onField({
71
- path,
72
- key,
73
- field,
74
- });
75
- return acc;
76
- }, {}),
77
- }),
78
- ...(variation.items && {
79
- items: Object.entries(variation.items).reduce((acc, [key, field]) => {
80
- const path = prevPath.concat("items", key);
81
- acc[key] = onField({
53
+ const primary = {};
54
+ for (const [key, prevField] of Object.entries((_a = variation.primary) !== null && _a !== void 0 ? _a : {})) {
55
+ const path = [...prevPath, "primary", key];
56
+ let field;
57
+ switch (prevField.type) {
58
+ case "Group":
59
+ field = (0, Group_1.traverseGroup)({
82
60
  path,
83
- key,
84
- field,
61
+ group: prevField,
62
+ onField: onField,
85
63
  });
86
- return acc;
87
- }, {}),
88
- }),
64
+ break;
65
+ default:
66
+ field = prevField;
67
+ break;
68
+ }
69
+ primary[key] = onField({ path, key, field });
70
+ }
71
+ const items = {};
72
+ for (const [key, field] of Object.entries((_b = variation.items) !== null && _b !== void 0 ? _b : {})) {
73
+ const path = [...prevPath, "items", key];
74
+ items[key] = onField({ path, key, field });
75
+ }
76
+ return {
77
+ ...variation,
78
+ ...(variation.primary && { primary }),
79
+ ...(variation.items && { items }),
89
80
  };
90
81
  }
91
82
  exports.traverseVariation = traverseVariation;
92
83
  function traverseSharedSlice(args) {
93
84
  const { path: prevPath, slice, onField } = args;
85
+ const variations = [];
86
+ for (const variation of slice.variations) {
87
+ const path = [...prevPath, variation.id];
88
+ variations.push(traverseVariation({
89
+ path,
90
+ variation,
91
+ onField,
92
+ }));
93
+ }
94
94
  return {
95
95
  ...slice,
96
- variations: slice.variations.map((variation) => {
97
- const path = prevPath.concat(variation.id);
98
- return traverseVariation({
99
- path,
100
- variation,
101
- onField,
102
- });
103
- }),
96
+ variations,
104
97
  };
105
98
  }
106
99
  exports.traverseSharedSlice = traverseSharedSlice;
@@ -82,59 +82,58 @@ exports.Slices = {
82
82
  function traverseSlices(args) {
83
83
  var _a;
84
84
  const { path: prevPath, slices, onField } = args;
85
- const choices = ((_a = slices.config) === null || _a === void 0 ? void 0 : _a.choices) &&
86
- Object.entries(slices.config.choices).reduce((acc, [key, prevModel]) => {
87
- const path = prevPath.concat(key);
88
- let model;
89
- switch (prevModel.type) {
90
- case "Slice":
91
- model = (0, CompositeSlice_1.traverseCompositeSlice)({
85
+ if (!((_a = slices.config) === null || _a === void 0 ? void 0 : _a.choices))
86
+ return slices;
87
+ const choices = {};
88
+ for (const [key, prevModel] of Object.entries(slices.config.choices)) {
89
+ const path = [...prevPath, key];
90
+ let model;
91
+ switch (prevModel.type) {
92
+ case "Slice":
93
+ model = (0, CompositeSlice_1.traverseCompositeSlice)({
94
+ path,
95
+ slice: prevModel,
96
+ onField: onField,
97
+ });
98
+ break;
99
+ case "SharedSlice":
100
+ if ("variations" in prevModel)
101
+ model = (0, SharedSlice_1.traverseSharedSlice)({
92
102
  path,
93
103
  slice: prevModel,
94
- onField: onField,
95
- });
96
- break;
97
- case "SharedSlice":
98
- if ("variations" in prevModel)
99
- model = (0, SharedSlice_1.traverseSharedSlice)({
100
- path,
101
- slice: prevModel,
102
- onField,
103
- });
104
- else
105
- model = prevModel;
106
- break;
107
- // Group and other fields are technically possible because of legacy slices.
108
- case "Group":
109
- model = onField({
110
- path,
111
- key,
112
- field: (0, Group_1.traverseNestedGroup)({
113
- path,
114
- group: prevModel,
115
- onField: onField,
116
- }),
104
+ onField,
117
105
  });
118
- break;
119
- default:
120
- model = onField({
106
+ else
107
+ model = prevModel;
108
+ break;
109
+ // Group and other fields are technically possible because of legacy slices.
110
+ case "Group":
111
+ model = onField({
112
+ path,
113
+ key,
114
+ field: (0, Group_1.traverseNestedGroup)({
121
115
  path,
122
- key,
123
- field: prevModel,
124
- });
125
- break;
126
- }
127
- acc[key] = model;
128
- return acc;
129
- }, {});
116
+ group: prevModel,
117
+ onField: onField,
118
+ }),
119
+ });
120
+ break;
121
+ default:
122
+ model = onField({
123
+ path,
124
+ key,
125
+ field: prevModel,
126
+ });
127
+ break;
128
+ }
129
+ choices[key] = model;
130
+ }
130
131
  return {
131
132
  ...slices,
132
- ...(slices.config && {
133
- config: {
134
- ...slices.config,
135
- ...(choices && { choices }),
136
- },
137
- }),
133
+ config: {
134
+ ...slices.config,
135
+ choices,
136
+ },
138
137
  };
139
138
  }
140
139
  exports.traverseSlices = traverseSlices;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "3.11.0",
3
+ "version": "3.11.1",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -260,17 +260,18 @@ export function traverseCustomType<
260
260
  onField: OnFieldFn<UID | NestableWidget | Group | NestedGroup>
261
261
  }): T {
262
262
  const { customType, onField } = args
263
+
264
+ const json: Record<string, typeof customType.json[string]> = {}
265
+ for (const [key, section] of Object.entries(customType.json)) {
266
+ json[key] = traverseSection({
267
+ path: [key],
268
+ section,
269
+ onField,
270
+ })
271
+ }
272
+
263
273
  return {
264
274
  ...customType,
265
- json: Object.entries(customType.json).reduce<
266
- Record<string, typeof customType.json[string]>
267
- >((acc, [key, section]) => {
268
- acc[key] = traverseSection({
269
- path: [key],
270
- section,
271
- onField,
272
- })
273
- return acc
274
- }, {}),
275
+ json,
275
276
  }
276
277
  }
@@ -50,9 +50,11 @@ export function traverseSection<
50
50
  section: T
51
51
  onField: OnFieldFn<UID | NestableWidget | Group | NestedGroup>
52
52
  }): T {
53
- const { path: prevPath, section, onField } = args
54
- return Object.entries(section).reduce((acc, [key, prevModel]) => {
55
- const path = prevPath.concat(key)
53
+ const { path: prevPath, section: prevSection, onField } = args
54
+
55
+ const section = { ...prevSection }
56
+ for (const [key, prevModel] of Object.entries(prevSection)) {
57
+ const path = [...prevPath, key]
56
58
  let model
57
59
  switch (prevModel.type) {
58
60
  case "Choice":
@@ -82,7 +84,7 @@ export function traverseSection<
82
84
  })
83
85
  break
84
86
  }
85
- acc[key] = model
86
- return acc
87
- }, {} as T)
87
+ section[key] = model
88
+ }
89
+ return section
88
90
  }
@@ -55,28 +55,25 @@ export function traverseNestedGroup(args: {
55
55
  onField: OnFieldFn<NestableWidget>
56
56
  }): NestedGroup {
57
57
  const { path: prevPath, group, onField } = args
58
- const fields =
59
- group.config?.fields &&
60
- Object.entries(group.config.fields).reduce<Record<string, NestableWidget>>(
61
- (acc, [key, model]) => {
62
- const path = prevPath.concat(key)
63
- acc[key] = onField({
64
- path,
65
- key,
66
- field: model,
67
- })
68
- return acc
69
- },
70
- {},
71
- )
58
+
59
+ if (!group.config?.fields) return group
60
+
61
+ const fields: Record<string, NestableWidget> = {}
62
+ for (const [key, prevField] of Object.entries(group.config.fields)) {
63
+ const path = [...prevPath, key]
64
+ fields[key] = onField({
65
+ path,
66
+ key,
67
+ field: prevField,
68
+ })
69
+ }
70
+
72
71
  return {
73
72
  ...group,
74
- ...(group.config && {
75
- config: {
76
- ...group.config,
77
- ...(fields && { fields }),
78
- },
79
- }),
73
+ config: {
74
+ ...group.config,
75
+ fields,
76
+ },
80
77
  }
81
78
  }
82
79
 
@@ -86,39 +83,37 @@ export function traverseGroup(args: {
86
83
  onField: OnFieldFn<NestableWidget | NestedGroup>
87
84
  }): Group {
88
85
  const { path: prevPath, group, onField } = args
89
- const fields =
90
- group.config?.fields &&
91
- Object.entries(group.config.fields).reduce<
92
- Record<string, NestableWidget | NestedGroup>
93
- >((acc, [key, prevField]) => {
94
- const path = prevPath.concat(key)
95
- let field
96
- switch (prevField.type) {
97
- case "Group":
98
- field = traverseNestedGroup({
99
- path,
100
- group: prevField,
101
- onField: onField as OnFieldFn<NestableWidget>,
102
- })
103
- break
104
- default:
105
- field = prevField
106
- break
107
- }
108
- acc[key] = onField({
109
- path,
110
- key,
111
- field,
112
- })
113
- return acc
114
- }, {})
86
+
87
+ if (!group.config?.fields) return group
88
+
89
+ const fields: Record<string, NestableWidget | NestedGroup> = {}
90
+ for (const [key, prevField] of Object.entries(group.config.fields)) {
91
+ const path = [...prevPath, key]
92
+ let field
93
+ switch (prevField.type) {
94
+ case "Group":
95
+ field = traverseNestedGroup({
96
+ path,
97
+ group: prevField,
98
+ onField: onField as OnFieldFn<NestableWidget>,
99
+ })
100
+ break
101
+ default:
102
+ field = prevField
103
+ break
104
+ }
105
+ fields[key] = onField({
106
+ path,
107
+ key,
108
+ field,
109
+ })
110
+ }
111
+
115
112
  return {
116
113
  ...group,
117
- ...(group.config && {
118
- config: {
119
- ...group.config,
120
- ...(fields && { fields }),
121
- },
122
- }),
114
+ config: {
115
+ ...group.config,
116
+ fields,
117
+ },
123
118
  }
124
119
  }
@@ -45,33 +45,22 @@ export function traverseCompositeSlice(args: {
45
45
  onField: OnFieldFn<NestableWidget>
46
46
  }): CompositeSlice {
47
47
  const { path: prevPath, slice, onField } = args
48
- const nonRepeat =
49
- slice["non-repeat"] &&
50
- Object.entries(slice["non-repeat"]).reduce<Record<string, NestableWidget>>(
51
- (acc, [key, field]) => {
52
- const path = prevPath.concat("non-repeat", key)
53
- acc[key] = onField({ path, key, field })
54
- return acc
55
- },
56
- {},
57
- )
58
- const repeat =
59
- slice.repeat &&
60
- Object.entries(slice.repeat).reduce<Record<string, NestableWidget>>(
61
- (acc, [key, field]) => {
62
- const path = prevPath.concat("repeat", key)
63
- acc[key] = onField({ path, key, field })
64
- return acc
65
- },
66
- {},
67
- )
48
+
49
+ const nonRepeat: Record<string, NestableWidget> = {}
50
+ for (const [key, field] of Object.entries(slice["non-repeat"] ?? {})) {
51
+ const path = [...prevPath, "non-repeat", key]
52
+ nonRepeat[key] = onField({ path, key, field })
53
+ }
54
+
55
+ const repeat: Record<string, NestableWidget> = {}
56
+ for (const [key, field] of Object.entries(slice.repeat ?? {})) {
57
+ const path = [...prevPath, "repeat", key]
58
+ repeat[key] = onField({ path, key, field })
59
+ }
60
+
68
61
  return {
69
62
  ...slice,
70
- ...(nonRepeat && {
71
- "non-repeat": nonRepeat,
72
- }),
73
- ...(repeat && {
74
- repeat,
75
- }),
63
+ ...(slice["non-repeat"] && { "non-repeat": nonRepeat }),
64
+ ...(slice.repeat && { repeat }),
76
65
  }
77
66
  }