@prismicio/types-internal 2.2.0-traverse.alpha-7 → 2.2.0-traverse.alpha-8

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,8 +1,5 @@
1
1
  import type { SharedSliceItemContent, SliceItemContent, WidgetContent } from "../content";
2
2
  import type { CompositeSlice, Group, NestableWidget, StaticSlices, UID, VariationFields } from "../customtypes";
3
- export declare type PickOnly<T, K extends keyof T> = Pick<T, K> & {
4
- [P in Exclude<keyof T, K>]?: never;
5
- };
6
3
  export declare type TraverseSliceContentFn = <S extends SliceItemContent | SharedSliceItemContent, D extends VariationFields | CompositeSlice | Group | NestableWidget>({ path, key, apiId, model, content, }: {
7
4
  path: ContentPath;
8
5
  key: string;
@@ -157,7 +157,7 @@ exports.traverseDocument = traverseDocument;
157
157
  function simplifyCustomType(customType) {
158
158
  return {
159
159
  customTypeId: customType === null || customType === void 0 ? void 0 : customType.id,
160
- fields: Object.fromEntries((0, customtypes_1.flattenStaticWidgets)(customType)),
160
+ fields: Object.fromEntries((0, customtypes_1.flattenSections)(customType)),
161
161
  };
162
162
  }
163
163
  // /**
@@ -1,5 +1,4 @@
1
1
  import * as t from "io-ts";
2
- import type { NestableWidget } from "../../../customtypes";
3
2
  import type { LegacyContentCtx } from "../../LegacyContentCtx";
4
3
  export declare const NestableContent: t.UnionC<[t.ExactC<t.TypeC<{
5
4
  type: t.StringC;
@@ -993,4 +992,3 @@ export declare const NestableLegacy: (ctx: LegacyContentCtx) => {
993
992
  }> | undefined;
994
993
  encode(value: NestableContent): import("../../LegacyContentCtx").WithTypes<unknown> | undefined;
995
994
  };
996
- export declare function isValidStaticNestableContent(content: NestableContent, model?: NestableWidget): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isValidStaticNestableContent = exports.NestableLegacy = exports.isNestableContent = exports.NestableContent = void 0;
3
+ exports.NestableLegacy = exports.isNestableContent = exports.NestableContent = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const Either_1 = require("fp-ts/lib/Either");
6
6
  const t = (0, tslib_1.__importStar)(require("io-ts"));
@@ -140,31 +140,3 @@ const NestableLegacy = (ctx) => {
140
140
  };
141
141
  };
142
142
  exports.NestableLegacy = NestableLegacy;
143
- function isValidStaticNestableContent(content, model) {
144
- if (!model)
145
- return false;
146
- switch (content.__TYPE__) {
147
- // special case for no content so we can consider it valid no matter its model
148
- case "EmptyContent":
149
- return true;
150
- case "BooleanContent":
151
- return model.type === "Boolean";
152
- case "EmbedContent":
153
- return model.type === "Embed";
154
- case "FieldContent":
155
- return content.type === model.type;
156
- case "GeoPointContent":
157
- return model.type === "GeoPoint";
158
- case "ImageContent":
159
- return model.type === "Image";
160
- case "IntegrationFieldsContent":
161
- return model.type === "IntegrationFields";
162
- case "LinkContent":
163
- return model.type === "Link";
164
- case "StructuredTextContent":
165
- return model.type === "StructuredText";
166
- case "SeparatorContent":
167
- return model.type === "Separator";
168
- }
169
- }
170
- exports.isValidStaticNestableContent = isValidStaticNestableContent;
@@ -100,8 +100,12 @@ function traverseCompositeSliceContent({ path, sliceKey, sliceName, model, conte
100
100
  content: fieldContent,
101
101
  });
102
102
  // Can happen if the transform function returns undefined to filter out a field
103
- if (!transformedField || !(0, nestable_1.isNestableContent)(transformedField))
103
+ if (!transformedField)
104
104
  return acc;
105
+ if (!(0, nestable_1.isNestableContent)(transformedField)) {
106
+ console.warn(`Warn: Non nestable content with key [${fieldKey}] detected in a composite slice [${sliceName}] with key [${sliceKey}]`);
107
+ return acc;
108
+ }
105
109
  return {
106
110
  ...acc,
107
111
  [fieldKey]: transformedField,
@@ -139,18 +143,22 @@ function migrateCompositeSlice(model, content) {
139
143
  variation: model.variationId,
140
144
  primary: Object.entries(content.widget.nonRepeat).reduce((acc, [fieldKey, fieldContent]) => {
141
145
  var _a;
142
- return ((_a = model.fields.primary) === null || _a === void 0 ? void 0 : _a[fieldKey])
143
- ? { ...acc, [fieldKey]: fieldContent }
144
- : acc;
146
+ if (!((_a = model.fields.primary) === null || _a === void 0 ? void 0 : _a[fieldKey])) {
147
+ console.warn(`The widget ${fieldKey} in the non-repeat zone of the composite slice [${model.sliceName}] with key [${content.key}] doesn't exist in the model.`);
148
+ return acc;
149
+ }
150
+ return { ...acc, [fieldKey]: fieldContent };
145
151
  }, {}),
146
152
  items: content.widget.repeat.map((groupItem) => {
147
153
  return {
148
154
  __TYPE__: "GroupItemContent",
149
155
  value: groupItem.value.reduce((acc, [fieldKey, fieldContent]) => {
150
156
  var _a;
151
- return ((_a = model.fields.items) === null || _a === void 0 ? void 0 : _a[fieldKey])
152
- ? acc.concat([[fieldKey, fieldContent]])
153
- : acc;
157
+ if (!((_a = model.fields.items) === null || _a === void 0 ? void 0 : _a[fieldKey])) {
158
+ console.warn(`The widget ${fieldKey} in the repeat zone of the composite slice [${model.sliceName}] with key [${content.key}] doesn't exist in the model.`);
159
+ return acc;
160
+ }
161
+ return acc.concat([[fieldKey, fieldContent]]);
154
162
  }, []),
155
163
  };
156
164
  }, []),
@@ -113,8 +113,12 @@ function traverseSharedSliceContent({ path, sliceKey, sliceName, model, content,
113
113
  content: fieldContent,
114
114
  });
115
115
  // Can happen if the transform function returns undefined to filter out a field
116
- if (!transformedField || !(0, nestable_1.isNestableContent)(transformedField))
116
+ if (!transformedField)
117
117
  return acc;
118
+ if (!(0, nestable_1.isNestableContent)(transformedField)) {
119
+ console.warn(`Warn: Non nestable content with key [${fieldKey}] detected in a shared slice [${sliceName}] with key [${sliceKey}]`);
120
+ return acc;
121
+ }
118
122
  return {
119
123
  ...acc,
120
124
  [fieldKey]: transformedField,
@@ -118,7 +118,7 @@ function migrateSimpleSlice(model, content) {
118
118
  __TYPE__: "SharedSliceContent",
119
119
  variation: model.variationId,
120
120
  primary: ((_a = model.fields.primary) === null || _a === void 0 ? void 0 : _a[content.name])
121
- ? { [content.key]: content.widget }
121
+ ? { [content.name]: content.widget }
122
122
  : {},
123
123
  items: [],
124
124
  },
@@ -2393,7 +2393,7 @@ export declare const CustomType: t.ExactC<t.IntersectionC<[t.TypeC<{
2393
2393
  }>]>>;
2394
2394
  export declare type CustomType = t.TypeOf<typeof CustomType>;
2395
2395
  export declare function flattenWidgets(customType: CustomType): Array<[string, DynamicWidget]>;
2396
- export declare function flattenStaticWidgets(customType: StaticCustomType): Array<[string, StaticWidget]>;
2396
+ export declare function flattenSections(customType: StaticCustomType): Array<[string, StaticWidget]>;
2397
2397
  export declare function toStatic(customType: CustomType, sharedSlices: Map<string, SharedSlice>): StaticCustomType;
2398
2398
  export declare function validateSlices(customType: CustomType, sharedSlices: Map<string, SharedSlice>): Either<CustomTypeSlicesError, CustomType>;
2399
2399
  export declare function collectWidgets(customType: CustomType, f: (ref: string, widget: DynamicWidget) => DynamicWidget | undefined): CustomType;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.collectSharedSlices = exports.flattenCustomTypeFields = exports.filterMissingSharedSlices = exports.collectWidgets = exports.validateSlices = exports.toStatic = exports.flattenStaticWidgets = exports.flattenWidgets = exports.CustomType = exports.StaticCustomType = exports.CustomTypeFormat = void 0;
3
+ exports.collectSharedSlices = exports.flattenCustomTypeFields = exports.filterMissingSharedSlices = exports.collectWidgets = exports.validateSlices = exports.toStatic = exports.flattenSections = exports.flattenWidgets = exports.CustomType = exports.StaticCustomType = exports.CustomTypeFormat = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const Either_1 = require("fp-ts/lib/Either");
6
6
  const t = (0, tslib_1.__importStar)(require("io-ts"));
@@ -60,13 +60,13 @@ function flattenWidgets(customType) {
60
60
  }, []);
61
61
  }
62
62
  exports.flattenWidgets = flattenWidgets;
63
- function flattenStaticWidgets(customType) {
63
+ function flattenSections(customType) {
64
64
  return Object.entries(customType.json).reduce((acc, [, section]) => {
65
65
  const sectionWidgets = Object.entries(section);
66
66
  return acc.concat(sectionWidgets);
67
67
  }, []);
68
68
  }
69
- exports.flattenStaticWidgets = flattenStaticWidgets;
69
+ exports.flattenSections = flattenSections;
70
70
  function _retrieveSharedSlicesRef(customType) {
71
71
  const slicezones = flattenWidgets(customType).filter(([, widget]) => widget.type === "Slices");
72
72
  const allSharedRefs = slicezones.reduce((acc, [, slicezone]) => {
@@ -1,5 +1,4 @@
1
1
  import * as t from "io-ts";
2
- import type { PickOnly } from "../../../_internal/utils";
3
2
  import type { SharedSliceRef } from "./SharedSliceRef";
4
3
  import type { DynamicSlice, StaticSlice } from "./Slice";
5
4
  export declare const Variation: t.ExactC<t.IntersectionC<[t.TypeC<{
@@ -331,7 +330,7 @@ export declare type VariationFields = {
331
330
  type: "SharedSlice";
332
331
  sliceName: string;
333
332
  variationId: string;
334
- fields: PickOnly<Variation, "primary" | "items">;
333
+ fields: Pick<Variation, "primary" | "items">;
335
334
  };
336
335
  export declare const SharedSliceType = "SharedSlice";
337
336
  export declare const SharedSlice: t.ExactC<t.IntersectionC<[t.TypeC<{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-traverse.alpha-7",
3
+ "version": "2.2.0-traverse.alpha-8",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -12,10 +12,6 @@ import type {
12
12
  VariationFields,
13
13
  } from "../customtypes"
14
14
 
15
- export type PickOnly<T, K extends keyof T> = Pick<T, K> & {
16
- [P in Exclude<keyof T, K>]?: never
17
- }
18
-
19
15
  export type TraverseSliceContentFn = <
20
16
  S extends SliceItemContent | SharedSliceItemContent,
21
17
  D extends VariationFields | CompositeSlice | Group | NestableWidget,
@@ -12,7 +12,7 @@ import { WidgetKey } from "../common"
12
12
  import {
13
13
  type StaticWidget,
14
14
  collectSharedSlices,
15
- flattenStaticWidgets,
15
+ flattenSections,
16
16
  StaticCustomType,
17
17
  } from "../customtypes"
18
18
  import {
@@ -253,7 +253,7 @@ function simplifyCustomType(customType: StaticCustomType): {
253
253
  } {
254
254
  return {
255
255
  customTypeId: customType?.id,
256
- fields: Object.fromEntries(flattenStaticWidgets(customType)),
256
+ fields: Object.fromEntries(flattenSections(customType)),
257
257
  }
258
258
  }
259
259
 
@@ -1,7 +1,6 @@
1
1
  import { isRight } from "fp-ts/lib/Either"
2
2
  import * as t from "io-ts"
3
3
 
4
- import type { NestableWidget } from "../../../customtypes"
5
4
  import type { LegacyContentCtx } from "../../LegacyContentCtx"
6
5
  import { EmptyContent, EmptyLegacy, isEmptyContent } from "../EmptyContent"
7
6
  import {
@@ -201,34 +200,3 @@ export const NestableLegacy = (ctx: LegacyContentCtx) => {
201
200
  },
202
201
  }
203
202
  }
204
-
205
- export function isValidStaticNestableContent(
206
- content: NestableContent,
207
- model?: NestableWidget,
208
- ) {
209
- if (!model) return false
210
-
211
- switch (content.__TYPE__) {
212
- // special case for no content so we can consider it valid no matter its model
213
- case "EmptyContent":
214
- return true
215
- case "BooleanContent":
216
- return model.type === "Boolean"
217
- case "EmbedContent":
218
- return model.type === "Embed"
219
- case "FieldContent":
220
- return content.type === model.type
221
- case "GeoPointContent":
222
- return model.type === "GeoPoint"
223
- case "ImageContent":
224
- return model.type === "Image"
225
- case "IntegrationFieldsContent":
226
- return model.type === "IntegrationFields"
227
- case "LinkContent":
228
- return model.type === "Link"
229
- case "StructuredTextContent":
230
- return model.type === "StructuredText"
231
- case "SeparatorContent":
232
- return model.type === "Separator"
233
- }
234
- }
@@ -170,7 +170,13 @@ export function traverseCompositeSliceContent({
170
170
  content: fieldContent,
171
171
  })
172
172
  // Can happen if the transform function returns undefined to filter out a field
173
- if (!transformedField || !isNestableContent(transformedField)) return acc
173
+ if (!transformedField) return acc
174
+ if (!isNestableContent(transformedField)) {
175
+ console.warn(
176
+ `Warn: Non nestable content with key [${fieldKey}] detected in a composite slice [${sliceName}] with key [${sliceKey}]`,
177
+ )
178
+ return acc
179
+ }
174
180
  return {
175
181
  ...acc,
176
182
  [fieldKey]: transformedField,
@@ -214,9 +220,14 @@ export function migrateCompositeSlice(
214
220
  variation: model.variationId,
215
221
  primary: Object.entries(content.widget.nonRepeat).reduce(
216
222
  (acc, [fieldKey, fieldContent]) => {
217
- return model.fields.primary?.[fieldKey]
218
- ? { ...acc, [fieldKey]: fieldContent }
219
- : acc
223
+ if (!model.fields.primary?.[fieldKey]) {
224
+ console.warn(
225
+ `The widget ${fieldKey} in the non-repeat zone of the composite slice [${model.sliceName}] with key [${content.key}] doesn't exist in the model.`,
226
+ )
227
+ return acc
228
+ }
229
+
230
+ return { ...acc, [fieldKey]: fieldContent }
220
231
  },
221
232
  {},
222
233
  ),
@@ -225,9 +236,13 @@ export function migrateCompositeSlice(
225
236
  __TYPE__: "GroupItemContent",
226
237
  value: groupItem.value.reduce<GroupItemContent["value"]>(
227
238
  (acc, [fieldKey, fieldContent]) => {
228
- return model.fields.items?.[fieldKey]
229
- ? acc.concat([[fieldKey, fieldContent]])
230
- : acc
239
+ if (!model.fields.items?.[fieldKey]) {
240
+ console.warn(
241
+ `The widget ${fieldKey} in the repeat zone of the composite slice [${model.sliceName}] with key [${content.key}] doesn't exist in the model.`,
242
+ )
243
+ return acc
244
+ }
245
+ return acc.concat([[fieldKey, fieldContent]])
231
246
  },
232
247
  [],
233
248
  ),
@@ -174,7 +174,13 @@ export function traverseSharedSliceContent({
174
174
  content: fieldContent,
175
175
  })
176
176
  // Can happen if the transform function returns undefined to filter out a field
177
- if (!transformedField || !isNestableContent(transformedField)) return acc
177
+ if (!transformedField) return acc
178
+ if (!isNestableContent(transformedField)) {
179
+ console.warn(
180
+ `Warn: Non nestable content with key [${fieldKey}] detected in a shared slice [${sliceName}] with key [${sliceKey}]`,
181
+ )
182
+ return acc
183
+ }
178
184
  return {
179
185
  ...acc,
180
186
  [fieldKey]: transformedField,
@@ -168,7 +168,7 @@ export function migrateSimpleSlice(
168
168
  __TYPE__: "SharedSliceContent",
169
169
  variation: model.variationId,
170
170
  primary: model.fields.primary?.[content.name]
171
- ? { [content.key]: content.widget }
171
+ ? { [content.name]: content.widget }
172
172
  : {},
173
173
  items: [],
174
174
  },
@@ -75,7 +75,7 @@ export function flattenWidgets(
75
75
  )
76
76
  }
77
77
 
78
- export function flattenStaticWidgets(
78
+ export function flattenSections(
79
79
  customType: StaticCustomType,
80
80
  ): Array<[string, StaticWidget]> {
81
81
  return Object.entries(customType.json).reduce(
@@ -1,7 +1,6 @@
1
1
  import * as t from "io-ts"
2
2
  import { withFallback } from "io-ts-types/lib/withFallback"
3
3
 
4
- import type { PickOnly } from "../../../_internal/utils"
5
4
  import { WidgetKey } from "../../../common"
6
5
  import { NestableWidget } from "../nestable/NestableWidget"
7
6
  import type { SharedSliceRef } from "./SharedSliceRef"
@@ -30,11 +29,12 @@ export const Variation = t.exact(
30
29
 
31
30
  export type Variation = t.TypeOf<typeof Variation>
32
31
 
32
+ // VariationFields is used exclusively for the slices migration, LegacySlice -> SharedSlice
33
33
  export type VariationFields = {
34
34
  type: "SharedSlice"
35
35
  sliceName: string
36
36
  variationId: string
37
- fields: PickOnly<Variation, "primary" | "items">
37
+ fields: Pick<Variation, "primary" | "items">
38
38
  }
39
39
 
40
40
  export const SharedSliceType = "SharedSlice"