@prismicio/types-internal 3.11.0-alpha.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.
@@ -78,47 +78,36 @@ export function traverseVariation(args: {
78
78
  onField: OnFieldFn<NestableWidget | Group | NestedGroup>
79
79
  }): Variation {
80
80
  const { path: prevPath, variation, onField } = args
81
- return {
82
- ...variation,
83
- ...(variation.primary && {
84
- primary: Object.entries(variation.primary).reduce<
85
- Record<string, NestableWidget | Group>
86
- >((acc, [key, prevField]) => {
87
- const path = prevPath.concat("primary", key)
88
- let field
89
- switch (prevField.type) {
90
- case "Group":
91
- field = traverseGroup({
92
- path,
93
- group: prevField,
94
- onField: onField as OnFieldFn<NestableWidget | NestedGroup>,
95
- })
96
- break
97
- default:
98
- field = prevField
99
- break
100
- }
101
- acc[key] = onField({
102
- path,
103
- key,
104
- field,
105
- })
106
- return acc
107
- }, {}),
108
- }),
109
- ...(variation.items && {
110
- items: Object.entries(variation.items).reduce<
111
- Record<string, NestableWidget>
112
- >((acc, [key, field]) => {
113
- const path = prevPath.concat("items", key)
114
- acc[key] = (onField as OnFieldFn<NestableWidget>)({
81
+
82
+ const primary: Record<string, NestableWidget | Group> = {}
83
+ for (const [key, prevField] of Object.entries(variation.primary ?? {})) {
84
+ const path = [...prevPath, "primary", key]
85
+ let field
86
+ switch (prevField.type) {
87
+ case "Group":
88
+ field = traverseGroup({
115
89
  path,
116
- key,
117
- field,
90
+ group: prevField,
91
+ onField: onField as OnFieldFn<NestableWidget | NestedGroup>,
118
92
  })
119
- return acc
120
- }, {}),
121
- }),
93
+ break
94
+ default:
95
+ field = prevField
96
+ break
97
+ }
98
+ primary[key] = onField({ path, key, field })
99
+ }
100
+
101
+ const items: Record<string, NestableWidget> = {}
102
+ for (const [key, field] of Object.entries(variation.items ?? {})) {
103
+ const path = [...prevPath, "items", key]
104
+ items[key] = (onField as OnFieldFn<NestableWidget>)({ path, key, field })
105
+ }
106
+
107
+ return {
108
+ ...variation,
109
+ ...(variation.primary && { primary }),
110
+ ...(variation.items && { items }),
122
111
  }
123
112
  }
124
113
 
@@ -128,15 +117,21 @@ export function traverseSharedSlice(args: {
128
117
  onField: OnFieldFn<NestableWidget | Group | NestedGroup>
129
118
  }): SharedSlice {
130
119
  const { path: prevPath, slice, onField } = args
131
- return {
132
- ...slice,
133
- variations: slice.variations.map((variation) => {
134
- const path = prevPath.concat(variation.id)
135
- return traverseVariation({
120
+
121
+ const variations: Variation[] = []
122
+ for (const variation of slice.variations) {
123
+ const path = [...prevPath, variation.id]
124
+ variations.push(
125
+ traverseVariation({
136
126
  path,
137
127
  variation,
138
128
  onField,
139
- })
140
- }),
129
+ }),
130
+ )
131
+ }
132
+
133
+ return {
134
+ ...slice,
135
+ variations,
141
136
  }
142
137
  }
@@ -120,60 +120,59 @@ export function traverseSlices<T extends DynamicSlices | StaticSlices>(args: {
120
120
  onField: OnFieldFn<NestableWidget | Group | NestedGroup>
121
121
  }): T {
122
122
  const { path: prevPath, slices, onField } = args
123
- const choices =
124
- slices.config?.choices &&
125
- Object.entries(slices.config.choices).reduce<
126
- Record<string, typeof slices.config.choices[string]>
127
- >((acc, [key, prevModel]) => {
128
- const path = prevPath.concat(key)
129
- let model
130
- switch (prevModel.type) {
131
- case "Slice":
132
- model = traverseCompositeSlice({
133
- path,
134
- slice: prevModel,
135
- onField: onField as OnFieldFn<NestableWidget>,
136
- })
137
- break
138
- case "SharedSlice":
139
- if ("variations" in prevModel)
140
- model = traverseSharedSlice({
141
- path,
142
- slice: prevModel as SharedSlice,
143
- onField,
144
- })
145
- else model = prevModel
146
- break
147
- // Group and other fields are technically possible because of legacy slices.
148
- case "Group":
149
- model = onField({
123
+
124
+ if (!slices.config?.choices) return slices
125
+
126
+ const choices: Record<string, typeof slices.config.choices[string]> = {}
127
+ for (const [key, prevModel] of Object.entries(slices.config.choices)) {
128
+ const path = [...prevPath, key]
129
+ let model
130
+ switch (prevModel.type) {
131
+ case "Slice":
132
+ model = traverseCompositeSlice({
133
+ path,
134
+ slice: prevModel,
135
+ onField: onField as OnFieldFn<NestableWidget>,
136
+ })
137
+ break
138
+ case "SharedSlice":
139
+ if ("variations" in prevModel)
140
+ model = traverseSharedSlice({
150
141
  path,
151
- key,
152
- field: traverseNestedGroup({
153
- path,
154
- group: prevModel,
155
- onField: onField as OnFieldFn<NestableWidget>,
156
- }),
142
+ slice: prevModel as SharedSlice,
143
+ onField,
157
144
  })
158
- break
159
- default:
160
- model = onField({
145
+ else model = prevModel
146
+ break
147
+ // Group and other fields are technically possible because of legacy slices.
148
+ case "Group":
149
+ model = onField({
150
+ path,
151
+ key,
152
+ field: traverseNestedGroup({
161
153
  path,
162
- key,
163
- field: prevModel,
164
- })
165
- break
166
- }
167
- acc[key] = model as typeof slices.config.choices[string]
168
- return acc
169
- }, {})
154
+ group: prevModel,
155
+ onField: onField as OnFieldFn<NestableWidget>,
156
+ }),
157
+ })
158
+ break
159
+ default:
160
+ model = onField({
161
+ path,
162
+ key,
163
+ field: prevModel,
164
+ })
165
+ break
166
+ }
167
+
168
+ choices[key] = model as typeof slices.config.choices[string]
169
+ }
170
+
170
171
  return {
171
172
  ...slices,
172
- ...(slices.config && {
173
- config: {
174
- ...slices.config,
175
- ...(choices && { choices }),
176
- },
177
- }),
173
+ config: {
174
+ ...slices.config,
175
+ choices,
176
+ },
178
177
  }
179
178
  }