@prismicio/types-internal 2.2.0-traverse.alpha-3 → 2.2.0-traverse.alpha-4

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.
@@ -1,7 +1,7 @@
1
1
  import * as t from "io-ts";
2
2
  import { ContentPath, TraverseSliceContentFn, TraverseWidgetContentFn } from "../_internal/utils";
3
3
  import { WidgetKey } from "../common";
4
- import { type StaticWidget } from "../customtypes";
4
+ import { type StaticWidget, StaticCustomType } from "../customtypes";
5
5
  import { WidgetContent } from "./fields";
6
6
  import { FieldOrSliceType, WithTypes } from "./LegacyContentCtx";
7
7
  export declare const Document: t.RecordC<t.Type<string, string, unknown>, t.UnionC<[t.ExactC<t.TypeC<{
@@ -4646,18 +4646,18 @@ export declare const DocumentLegacy: {
4646
4646
  * @param transform: A user function that provides a way to transform any kind of content wherever it is in a structured Prismic object content.
4647
4647
  * @returns a transformed document with the user's transformation applied with the transform function
4648
4648
  */
4649
- export declare function traverseDocument({ document, model, }: {
4649
+ export declare function traverseDocument({ document, customType, }: {
4650
4650
  document: Document;
4651
- model?: {
4651
+ customType?: StaticCustomType | {
4652
4652
  customTypeId: string;
4653
4653
  fields: Record<string, StaticWidget>;
4654
- };
4654
+ } | undefined;
4655
4655
  }): ({ transformWidget, transformSlice, }: {
4656
4656
  transformWidget?: TraverseWidgetContentFn;
4657
4657
  transformSlice?: TraverseSliceContentFn;
4658
4658
  }) => Document;
4659
4659
  export declare function collectWidgets<W extends WidgetContent>(document: Document, is: (content: WidgetContent, path: ContentPath) => content is W): Record<string, W>;
4660
- export declare function migrateDocument(document: Document, customType: {
4660
+ export declare function migrateDocument(document: Document, customType: StaticCustomType | {
4661
4661
  customTypeId: string;
4662
4662
  fields: Record<string, StaticWidget>;
4663
4663
  }): {
@@ -100,7 +100,10 @@ exports.DocumentLegacy = {
100
100
  * @param transform: A user function that provides a way to transform any kind of content wherever it is in a structured Prismic object content.
101
101
  * @returns a transformed document with the user's transformation applied with the transform function
102
102
  */
103
- function traverseDocument({ document, model, }) {
103
+ function traverseDocument({ document, customType, }) {
104
+ const model = customType && customtypes_1.StaticCustomType.is(customType)
105
+ ? simplifyCustomType(customType)
106
+ : customType;
104
107
  return ({ transformWidget = ({ content }) => content, transformSlice = ({ content }) => content, }) => {
105
108
  const fieldModels = model &&
106
109
  Object.entries(model.fields).reduce((acc, [key, def]) => ({ ...acc, [key]: def }), {});
@@ -151,6 +154,12 @@ function traverseDocument({ document, model, }) {
151
154
  };
152
155
  }
153
156
  exports.traverseDocument = traverseDocument;
157
+ function simplifyCustomType(customType) {
158
+ return {
159
+ customTypeId: customType === null || customType === void 0 ? void 0 : customType.id,
160
+ fields: Object.fromEntries((0, customtypes_1.flattenStaticWidgets)(customType)),
161
+ };
162
+ }
154
163
  // /**
155
164
  // * The goal is to be able to collect all widgets or slices of a given type at any level of nesting inside a prismic content
156
165
  // *
@@ -172,12 +181,15 @@ function collectWidgets(document, is) {
172
181
  }
173
182
  exports.collectWidgets = collectWidgets;
174
183
  function migrateDocument(document, customType) {
175
- const needsMigration = Object.values((0, customtypes_1.collectSharedSlices)(customType)).some((slice) => Boolean(slice.legacyPaths));
184
+ const model = customtypes_1.StaticCustomType.is(customType)
185
+ ? simplifyCustomType(customType)
186
+ : customType;
187
+ const needsMigration = Object.values((0, customtypes_1.collectSharedSlices)(model)).some((slice) => Boolean(slice.legacyPaths));
176
188
  if (!needsMigration)
177
189
  return document;
178
190
  return traverseDocument({
179
191
  document,
180
- model: customType,
192
+ customType,
181
193
  })({
182
194
  transformSlice: ({ content, model }) => {
183
195
  if ((0, fields_1.isCompositeSliceItemContent)(content) && (model === null || model === void 0 ? void 0 : model.type) === "SharedSlice")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-traverse.alpha-3",
3
+ "version": "2.2.0-traverse.alpha-4",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -9,7 +9,12 @@ import {
9
9
  TraverseWidgetContentFn,
10
10
  } from "../_internal/utils"
11
11
  import { WidgetKey } from "../common"
12
- import { type StaticWidget, collectSharedSlices } from "../customtypes"
12
+ import {
13
+ type StaticWidget,
14
+ collectSharedSlices,
15
+ flattenStaticWidgets,
16
+ StaticCustomType,
17
+ } from "../customtypes"
13
18
  import {
14
19
  isCompositeSliceItemContent,
15
20
  isSimpleSliceItemContent,
@@ -164,14 +169,21 @@ export const DocumentLegacy = {
164
169
  */
165
170
  export function traverseDocument({
166
171
  document,
167
- model,
172
+ customType,
168
173
  }: {
169
174
  document: Document
170
- model?: {
171
- customTypeId: string
172
- fields: Record<string, StaticWidget>
173
- }
175
+ customType?:
176
+ | StaticCustomType
177
+ | {
178
+ customTypeId: string
179
+ fields: Record<string, StaticWidget>
180
+ }
181
+ | undefined
174
182
  }) {
183
+ const model =
184
+ customType && StaticCustomType.is(customType)
185
+ ? simplifyCustomType(customType)
186
+ : customType
175
187
  return ({
176
188
  transformWidget = ({ content }) => content,
177
189
  transformSlice = ({ content }) => content,
@@ -238,6 +250,16 @@ export function traverseDocument({
238
250
  }
239
251
  }
240
252
 
253
+ function simplifyCustomType(customType: StaticCustomType): {
254
+ customTypeId: string
255
+ fields: Record<string, StaticWidget>
256
+ } {
257
+ return {
258
+ customTypeId: customType?.id,
259
+ fields: Object.fromEntries(flattenStaticWidgets(customType)),
260
+ }
261
+ }
262
+
241
263
  // /**
242
264
  // * The goal is to be able to collect all widgets or slices of a given type at any level of nesting inside a prismic content
243
265
  // *
@@ -263,12 +285,17 @@ export function collectWidgets<W extends WidgetContent>(
263
285
 
264
286
  export function migrateDocument(
265
287
  document: Document,
266
- customType: {
267
- customTypeId: string
268
- fields: Record<string, StaticWidget>
269
- },
288
+ customType:
289
+ | StaticCustomType
290
+ | {
291
+ customTypeId: string
292
+ fields: Record<string, StaticWidget>
293
+ },
270
294
  ) {
271
- const needsMigration = Object.values(collectSharedSlices(customType)).some(
295
+ const model = StaticCustomType.is(customType)
296
+ ? simplifyCustomType(customType)
297
+ : customType
298
+ const needsMigration = Object.values(collectSharedSlices(model)).some(
272
299
  (slice) => Boolean(slice.legacyPaths),
273
300
  )
274
301
 
@@ -276,7 +303,7 @@ export function migrateDocument(
276
303
 
277
304
  return traverseDocument({
278
305
  document,
279
- model: customType,
306
+ customType,
280
307
  })({
281
308
  transformSlice: ({ content, model }) => {
282
309
  if (isCompositeSliceItemContent(content) && model?.type === "SharedSlice")