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

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,20 +181,17 @@ 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
- transformSlice: ({ content, model }) => {
183
- if ((0, fields_1.isCompositeSliceItemContent)(content) && (model === null || model === void 0 ? void 0 : model.type) === "SharedSlice")
184
- return (0, fields_1.migrateCompositeSlice)(model, content);
185
- if ((0, fields_1.isSimpleSliceItemContent)(content) && (model === null || model === void 0 ? void 0 : model.type) === "SharedSlice")
186
- return (0, fields_1.migrateSimpleSlice)(model, content);
187
- return content;
188
- },
194
+ transformSlice: fields_1.migrateSliceItem,
189
195
  });
190
196
  }
191
197
  exports.migrateDocument = migrateDocument;
@@ -1,4 +1,5 @@
1
1
  import * as t from "io-ts";
2
+ import type { TraverseSliceContentFn } from "../../../_internal/utils";
2
3
  import { LegacyContentCtx, WithTypes } from "../../LegacyContentCtx";
3
4
  import { CompositeSliceContent } from "./Slice/CompositeSliceContent";
4
5
  import { SharedSliceContent } from "./Slice/SharedSliceContent";
@@ -3475,3 +3476,4 @@ export declare const SlicesItemLegacy: (ctx: LegacyContentCtx) => t.Type<{
3475
3476
  } & {
3476
3477
  label?: string | null | undefined;
3477
3478
  }>, unknown>;
3479
+ export declare const migrateSliceItem: TraverseSliceContentFn;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SlicesItemLegacy = exports.isSimpleSliceItemContent = exports.isCompositeSliceItemContent = exports.isSharedSliceItemContent = exports.SliceItemContent = void 0;
3
+ exports.migrateSliceItem = exports.SlicesItemLegacy = exports.isSimpleSliceItemContent = exports.isCompositeSliceItemContent = exports.isSharedSliceItemContent = exports.SliceItemContent = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const fp_ts_1 = require("fp-ts");
6
6
  const Either_1 = require("fp-ts/lib/Either");
@@ -74,3 +74,11 @@ const SlicesItemLegacy = (ctx) => {
74
74
  });
75
75
  };
76
76
  exports.SlicesItemLegacy = SlicesItemLegacy;
77
+ const migrateSliceItem = ({ model, content, }) => {
78
+ if (isCompositeSliceItemContent(content) && (model === null || model === void 0 ? void 0 : model.type) === "SharedSlice")
79
+ return (0, CompositeSliceContent_1.migrateCompositeSlice)(model, content);
80
+ if (isSimpleSliceItemContent(content) && (model === null || model === void 0 ? void 0 : model.type) === "SharedSlice")
81
+ return (0, SimpleSliceContent_1.migrateSimpleSlice)(model, content);
82
+ return content;
83
+ };
84
+ exports.migrateSliceItem = migrateSliceItem;
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-5",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -9,12 +9,14 @@ import {
9
9
  TraverseWidgetContentFn,
10
10
  } from "../_internal/utils"
11
11
  import { WidgetKey } from "../common"
12
- import { type StaticWidget, collectSharedSlices } from "../customtypes"
13
12
  import {
14
- isCompositeSliceItemContent,
15
- isSimpleSliceItemContent,
16
- migrateCompositeSlice,
17
- migrateSimpleSlice,
13
+ type StaticWidget,
14
+ collectSharedSlices,
15
+ flattenStaticWidgets,
16
+ StaticCustomType,
17
+ } from "../customtypes"
18
+ import {
19
+ migrateSliceItem,
18
20
  traverseGroupContent,
19
21
  traverseSlices,
20
22
  WidgetContent,
@@ -164,14 +166,21 @@ export const DocumentLegacy = {
164
166
  */
165
167
  export function traverseDocument({
166
168
  document,
167
- model,
169
+ customType,
168
170
  }: {
169
171
  document: Document
170
- model?: {
171
- customTypeId: string
172
- fields: Record<string, StaticWidget>
173
- }
172
+ customType?:
173
+ | StaticCustomType
174
+ | {
175
+ customTypeId: string
176
+ fields: Record<string, StaticWidget>
177
+ }
178
+ | undefined
174
179
  }) {
180
+ const model =
181
+ customType && StaticCustomType.is(customType)
182
+ ? simplifyCustomType(customType)
183
+ : customType
175
184
  return ({
176
185
  transformWidget = ({ content }) => content,
177
186
  transformSlice = ({ content }) => content,
@@ -238,6 +247,16 @@ export function traverseDocument({
238
247
  }
239
248
  }
240
249
 
250
+ function simplifyCustomType(customType: StaticCustomType): {
251
+ customTypeId: string
252
+ fields: Record<string, StaticWidget>
253
+ } {
254
+ return {
255
+ customTypeId: customType?.id,
256
+ fields: Object.fromEntries(flattenStaticWidgets(customType)),
257
+ }
258
+ }
259
+
241
260
  // /**
242
261
  // * 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
262
  // *
@@ -263,12 +282,17 @@ export function collectWidgets<W extends WidgetContent>(
263
282
 
264
283
  export function migrateDocument(
265
284
  document: Document,
266
- customType: {
267
- customTypeId: string
268
- fields: Record<string, StaticWidget>
269
- },
285
+ customType:
286
+ | StaticCustomType
287
+ | {
288
+ customTypeId: string
289
+ fields: Record<string, StaticWidget>
290
+ },
270
291
  ) {
271
- const needsMigration = Object.values(collectSharedSlices(customType)).some(
292
+ const model = StaticCustomType.is(customType)
293
+ ? simplifyCustomType(customType)
294
+ : customType
295
+ const needsMigration = Object.values(collectSharedSlices(model)).some(
272
296
  (slice) => Boolean(slice.legacyPaths),
273
297
  )
274
298
 
@@ -276,14 +300,8 @@ export function migrateDocument(
276
300
 
277
301
  return traverseDocument({
278
302
  document,
279
- model: customType,
303
+ customType,
280
304
  })({
281
- transformSlice: ({ content, model }) => {
282
- if (isCompositeSliceItemContent(content) && model?.type === "SharedSlice")
283
- return migrateCompositeSlice(model, content)
284
- if (isSimpleSliceItemContent(content) && model?.type === "SharedSlice")
285
- return migrateSimpleSlice(model, content)
286
- return content
287
- },
305
+ transformSlice: migrateSliceItem,
288
306
  })
289
307
  }
@@ -3,6 +3,7 @@ import { isLeft } from "fp-ts/lib/Either"
3
3
  import { pipe } from "fp-ts/lib/function"
4
4
  import * as t from "io-ts"
5
5
 
6
+ import type { TraverseSliceContentFn } from "../../../_internal/utils"
6
7
  import { nullable } from "../../../validators/function"
7
8
  import {
8
9
  getFieldCtx,
@@ -15,6 +16,7 @@ import { SliceContent, SliceLegacy } from "./Slice"
15
16
  import {
16
17
  CompositeSliceContent,
17
18
  isCompositeSliceContent,
19
+ migrateCompositeSlice,
18
20
  } from "./Slice/CompositeSliceContent"
19
21
  import {
20
22
  isSharedSliceContent,
@@ -22,6 +24,7 @@ import {
22
24
  } from "./Slice/SharedSliceContent"
23
25
  import {
24
26
  isSimpleSliceContent,
27
+ migrateSimpleSlice,
25
28
  SimpleSliceContent,
26
29
  } from "./Slice/SimpleSliceContent"
27
30
 
@@ -117,3 +120,14 @@ export const SlicesItemLegacy = (ctx: LegacyContentCtx) => {
117
120
  },
118
121
  )
119
122
  }
123
+
124
+ export const migrateSliceItem: TraverseSliceContentFn = ({
125
+ model,
126
+ content,
127
+ }) => {
128
+ if (isCompositeSliceItemContent(content) && model?.type === "SharedSlice")
129
+ return migrateCompositeSlice(model, content)
130
+ if (isSimpleSliceItemContent(content) && model?.type === "SharedSlice")
131
+ return migrateSimpleSlice(model, content)
132
+ return content
133
+ }