@prismicio/types-internal 2.2.0-traverse.alpha-0 → 2.2.0-traverse.alpha-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.
@@ -1,6 +1,6 @@
1
1
  import * as t from "io-ts";
2
2
  import type { ContentPath, TraverseContentFn } from "../../_internal/utils";
3
- import type { Group } from "../../customtypes";
3
+ import type { Group, NestableWidget } from "../../customtypes";
4
4
  import { LegacyContentCtx, WithTypes } from "../LegacyContentCtx";
5
5
  export declare const GroupItemContentType: "GroupItemContent";
6
6
  export declare const GroupItemContent: t.ExactC<t.TypeC<{
@@ -1442,5 +1442,10 @@ export declare function traverseGroupContent({ path, model, content, }: {
1442
1442
  path: ContentPath;
1443
1443
  content: GroupContent;
1444
1444
  model?: Group | undefined;
1445
- }): (transform: TraverseContentFn) => GroupContent | undefined;
1445
+ }): (transform: TraverseContentFn) => GroupContent;
1446
+ export declare function traverseGroupItemsContent({ path, model, content, }: {
1447
+ path: ContentPath;
1448
+ content: Array<GroupItemContent>;
1449
+ model?: Record<string, NestableWidget> | undefined;
1450
+ }): (transform: TraverseContentFn) => Array<GroupItemContent>;
1446
1451
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.traverseGroupContent = exports.GroupContent = exports.isGroupContent = exports.GroupContentType = exports.GroupLegacy = exports.GroupItemLegacy = exports.GroupItemContent = exports.GroupItemContentType = void 0;
3
+ exports.traverseGroupItemsContent = exports.traverseGroupContent = exports.GroupContent = exports.isGroupContent = exports.GroupContentType = exports.GroupLegacy = exports.GroupItemLegacy = exports.GroupItemContent = exports.GroupItemContentType = 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");
@@ -83,13 +83,29 @@ exports.GroupContent = t.strict({
83
83
  });
84
84
  function traverseGroupContent({ path, model, content, }) {
85
85
  return (transform) => {
86
- const groupItems = content.value.map((groupItem, index) => {
86
+ var _a;
87
+ const groupItems = traverseGroupItemsContent({
88
+ path,
89
+ model: (_a = model === null || model === void 0 ? void 0 : model.config) === null || _a === void 0 ? void 0 : _a.fields,
90
+ content: content.value,
91
+ })(transform);
92
+ return {
93
+ __TYPE__: content.__TYPE__,
94
+ value: groupItems,
95
+ };
96
+ };
97
+ }
98
+ exports.traverseGroupContent = traverseGroupContent;
99
+ function traverseGroupItemsContent({ path, model, content, }) {
100
+ return (transform) => {
101
+ return content.map((groupItem, index) => {
102
+ const groupItemPath = path.concat([
103
+ { key: index.toString(), type: "GroupItem" },
104
+ ]);
87
105
  const groupItemFields = groupItem.value.reduce((acc, [fieldKey, fieldContent]) => {
88
- var _a, _b;
89
- const fieldDef = (_b = (_a = model === null || model === void 0 ? void 0 : model.config) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b[fieldKey];
106
+ const fieldDef = model === null || model === void 0 ? void 0 : model[fieldKey];
90
107
  const transformedField = transform({
91
- path: path.concat([
92
- { key: index.toString(), type: "GroupItem" },
108
+ path: groupItemPath.concat([
93
109
  { key: fieldKey, type: fieldContent.__TYPE__ },
94
110
  ]),
95
111
  key: fieldKey,
@@ -107,10 +123,6 @@ function traverseGroupContent({ path, model, content, }) {
107
123
  value: groupItemFields,
108
124
  };
109
125
  });
110
- return {
111
- __TYPE__: content.__TYPE__,
112
- value: groupItems,
113
- };
114
126
  };
115
127
  }
116
- exports.traverseGroupContent = traverseGroupContent;
128
+ exports.traverseGroupItemsContent = traverseGroupItemsContent;
@@ -1706,4 +1706,4 @@ export declare function traverseCompositeSliceContent({ path, sliceName, model,
1706
1706
  sliceName: string;
1707
1707
  content: CompositeSliceContent;
1708
1708
  model?: CompositeSlice | undefined;
1709
- }): (transform: TraverseContentFn) => CompositeSliceContent | undefined;
1709
+ }): (transform: TraverseContentFn) => CompositeSliceContent;
@@ -8,6 +8,7 @@ const function_1 = require("fp-ts/lib/function");
8
8
  const t = (0, tslib_1.__importStar)(require("io-ts"));
9
9
  const LegacyContentCtx_1 = require("../../../LegacyContentCtx");
10
10
  const utils_1 = require("../../../utils");
11
+ const GroupContent_1 = require("../../GroupContent");
11
12
  const nestable_1 = require("../../nestable");
12
13
  const RepeatableContent_1 = require("./RepeatableContent");
13
14
  exports.CompositeSliceContentType = "CompositeSliceContent";
@@ -102,30 +103,11 @@ function traverseCompositeSliceContent({ path, sliceName, model, content, }) {
102
103
  [fieldKey]: transformedField,
103
104
  };
104
105
  }, {});
105
- const items = content.repeat.map((itemBlock) => {
106
- const itemBlockFields = itemBlock.value.reduce((acc, [fieldKey, fieldContent]) => {
107
- var _a;
108
- const fieldDef = (_a = model === null || model === void 0 ? void 0 : model.repeat) === null || _a === void 0 ? void 0 : _a[fieldKey];
109
- const transformedField = transform({
110
- path: path.concat([
111
- { key: "repeat", type: "items" },
112
- { key: fieldKey, type: fieldContent.__TYPE__ },
113
- ]),
114
- key: fieldKey,
115
- apiId: sliceName,
116
- model: fieldDef,
117
- content: fieldContent,
118
- });
119
- // Can happen if the transform function returns undefined to filter out a field
120
- if (!transformedField)
121
- return acc;
122
- return acc.concat([[fieldKey, transformedField]]);
123
- }, []);
124
- return {
125
- __TYPE__: itemBlock.__TYPE__,
126
- value: itemBlockFields,
127
- };
128
- });
106
+ const items = (0, GroupContent_1.traverseGroupItemsContent)({
107
+ path: path.concat([{ key: "repeat", type: "items" }]),
108
+ model: model === null || model === void 0 ? void 0 : model.repeat,
109
+ content: content.repeat,
110
+ })(transform);
129
111
  return {
130
112
  __TYPE__: content.__TYPE__,
131
113
  repeat: items,
@@ -1710,4 +1710,4 @@ export declare function traverseSharedSliceContent({ path, sliceName, model, con
1710
1710
  sliceName: string;
1711
1711
  content: SharedSliceContent;
1712
1712
  model?: SharedSlice | undefined;
1713
- }): (transform: TraverseContentFn) => SharedSliceContent | undefined;
1713
+ }): (transform: TraverseContentFn) => SharedSliceContent;
@@ -9,6 +9,7 @@ const t = (0, tslib_1.__importStar)(require("io-ts"));
9
9
  const io_ts_types_1 = require("io-ts-types");
10
10
  const LegacyContentCtx_1 = require("../../../LegacyContentCtx");
11
11
  const utils_1 = require("../../../utils");
12
+ const GroupContent_1 = require("../../GroupContent");
12
13
  const nestable_1 = require("../../nestable");
13
14
  const RepeatableContent_1 = require("./RepeatableContent");
14
15
  exports.SharedSliceContentType = "SharedSliceContent";
@@ -120,30 +121,11 @@ function traverseSharedSliceContent({ path, sliceName, model, content, }) {
120
121
  [fieldKey]: transformedField,
121
122
  };
122
123
  }, {});
123
- const items = content.items.map((itemBlock) => {
124
- const itemBlockFields = itemBlock.value.reduce((acc, [fieldKey, fieldContent]) => {
125
- var _a;
126
- const fieldDef = variationDef && ((_a = variationDef.items) === null || _a === void 0 ? void 0 : _a[fieldKey]);
127
- const transformedField = transform({
128
- path: path.concat([
129
- { key: "items", type: "items" },
130
- { key: fieldKey, type: fieldContent.__TYPE__ },
131
- ]),
132
- key: fieldKey,
133
- apiId: sliceName,
134
- model: fieldDef,
135
- content: fieldContent,
136
- });
137
- // Can happen if the transform function returns undefined to filter out a field
138
- if (!transformedField)
139
- return acc;
140
- return acc.concat([[fieldKey, transformedField]]);
141
- }, []);
142
- return {
143
- __TYPE__: itemBlock.__TYPE__,
144
- value: itemBlockFields,
145
- };
146
- });
124
+ const items = (0, GroupContent_1.traverseGroupItemsContent)({
125
+ path: path.concat([{ key: "items", type: "items" }]),
126
+ model: variationDef === null || variationDef === void 0 ? void 0 : variationDef.items,
127
+ content: content.items,
128
+ })(transform);
147
129
  return {
148
130
  __TYPE__: content.__TYPE__,
149
131
  variation: content.variation,
@@ -5099,5 +5099,5 @@ export declare function traverseSlices({ path, model, content, }: {
5099
5099
  path: ContentPath;
5100
5100
  content: SlicesContent;
5101
5101
  model?: StaticSlices | undefined;
5102
- }): (transform: TraverseContentFn) => SlicesContent | undefined;
5102
+ }): (transform: TraverseContentFn) => SlicesContent;
5103
5103
  export {};
@@ -79,8 +79,6 @@ function traverseSlices({ path, model, content, }) {
79
79
  return sliceContent.widget;
80
80
  }
81
81
  })();
82
- if (!convertedSliceWidget)
83
- return acc;
84
82
  const convertedSlice = transform({
85
83
  key: sliceContent.key,
86
84
  apiId: sliceContent.name,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-traverse.alpha-0",
3
+ "version": "2.2.0-traverse.alpha-1",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -4,7 +4,7 @@ import { pipe } from "fp-ts/lib/function"
4
4
  import * as t from "io-ts"
5
5
 
6
6
  import type { ContentPath, TraverseContentFn } from "../../_internal/utils"
7
- import type { Group } from "../../customtypes"
7
+ import type { Group, NestableWidget } from "../../customtypes"
8
8
  import {
9
9
  FieldOrSliceType,
10
10
  getFieldCtx,
@@ -129,15 +129,41 @@ export function traverseGroupContent({
129
129
  content: GroupContent
130
130
  model?: Group | undefined
131
131
  }) {
132
- return (transform: TraverseContentFn): GroupContent | undefined => {
133
- const groupItems = content.value.map((groupItem, index) => {
132
+ return (transform: TraverseContentFn): GroupContent => {
133
+ const groupItems = traverseGroupItemsContent({
134
+ path,
135
+ model: model?.config?.fields,
136
+ content: content.value,
137
+ })(transform)
138
+
139
+ return {
140
+ __TYPE__: content.__TYPE__,
141
+ value: groupItems,
142
+ }
143
+ }
144
+ }
145
+
146
+ export function traverseGroupItemsContent({
147
+ path,
148
+ model,
149
+ content,
150
+ }: {
151
+ path: ContentPath
152
+ content: Array<GroupItemContent>
153
+ model?: Record<string, NestableWidget> | undefined
154
+ }) {
155
+ return (transform: TraverseContentFn): Array<GroupItemContent> => {
156
+ return content.map((groupItem, index) => {
157
+ const groupItemPath = path.concat([
158
+ { key: index.toString(), type: "GroupItem" },
159
+ ])
160
+
134
161
  const groupItemFields = groupItem.value.reduce<GroupItemContent["value"]>(
135
162
  (acc, [fieldKey, fieldContent]) => {
136
- const fieldDef = model?.config?.fields?.[fieldKey]
163
+ const fieldDef = model?.[fieldKey]
137
164
 
138
165
  const transformedField = transform({
139
- path: path.concat([
140
- { key: index.toString(), type: "GroupItem" },
166
+ path: groupItemPath.concat([
141
167
  { key: fieldKey, type: fieldContent.__TYPE__ },
142
168
  ]),
143
169
  key: fieldKey,
@@ -157,10 +183,5 @@ export function traverseGroupContent({
157
183
  value: groupItemFields,
158
184
  }
159
185
  })
160
-
161
- return {
162
- __TYPE__: content.__TYPE__,
163
- value: groupItems,
164
- }
165
186
  }
166
187
  }
@@ -14,7 +14,7 @@ import {
14
14
  WithTypes,
15
15
  } from "../../../LegacyContentCtx"
16
16
  import { hasContentType } from "../../../utils"
17
- import type { GroupItemContent } from "../../GroupContent"
17
+ import { traverseGroupItemsContent } from "../../GroupContent"
18
18
  import { NestableContent, NestableLegacy } from "../../nestable"
19
19
  import { RepeatableWidgets, RepeatableWidgetsLegacy } from "./RepeatableContent"
20
20
 
@@ -135,7 +135,7 @@ export function traverseCompositeSliceContent({
135
135
  content: CompositeSliceContent
136
136
  model?: CompositeSlice | undefined
137
137
  }) {
138
- return (transform: TraverseContentFn): CompositeSliceContent | undefined => {
138
+ return (transform: TraverseContentFn): CompositeSliceContent => {
139
139
  const primary = Object.entries(content.nonRepeat).reduce<
140
140
  Record<string, NestableContent>
141
141
  >((acc, [fieldKey, fieldContent]) => {
@@ -159,33 +159,11 @@ export function traverseCompositeSliceContent({
159
159
  }
160
160
  }, {})
161
161
 
162
- const items = content.repeat.map((itemBlock) => {
163
- const itemBlockFields = itemBlock.value.reduce<GroupItemContent["value"]>(
164
- (acc, [fieldKey, fieldContent]) => {
165
- const fieldDef = model?.repeat?.[fieldKey]
166
-
167
- const transformedField = transform({
168
- path: path.concat([
169
- { key: "repeat", type: "items" },
170
- { key: fieldKey, type: fieldContent.__TYPE__ },
171
- ]),
172
- key: fieldKey,
173
- apiId: sliceName,
174
- model: fieldDef,
175
- content: fieldContent,
176
- })
177
- // Can happen if the transform function returns undefined to filter out a field
178
- if (!transformedField) return acc
179
- return acc.concat([[fieldKey, transformedField]])
180
- },
181
- [],
182
- )
183
-
184
- return {
185
- __TYPE__: itemBlock.__TYPE__,
186
- value: itemBlockFields,
187
- }
188
- })
162
+ const items = traverseGroupItemsContent({
163
+ path: path.concat([{ key: "repeat", type: "items" }]),
164
+ model: model?.repeat,
165
+ content: content.repeat,
166
+ })(transform)
189
167
 
190
168
  return {
191
169
  __TYPE__: content.__TYPE__,
@@ -15,7 +15,7 @@ import {
15
15
  WithTypes,
16
16
  } from "../../../LegacyContentCtx"
17
17
  import { hasContentType } from "../../../utils"
18
- import type { GroupItemContent } from "../../GroupContent"
18
+ import { traverseGroupItemsContent } from "../../GroupContent"
19
19
  import { NestableContent, NestableLegacy } from "../../nestable"
20
20
  import { RepeatableWidgets, RepeatableWidgetsLegacy } from "./RepeatableContent"
21
21
 
@@ -146,7 +146,7 @@ export function traverseSharedSliceContent({
146
146
  content: SharedSliceContent
147
147
  model?: SharedSlice | undefined
148
148
  }) {
149
- return (transform: TraverseContentFn): SharedSliceContent | undefined => {
149
+ return (transform: TraverseContentFn): SharedSliceContent => {
150
150
  const variationDef = model?.variations.find(
151
151
  (v) => v.id === content.variation,
152
152
  )
@@ -174,33 +174,11 @@ export function traverseSharedSliceContent({
174
174
  }
175
175
  }, {})
176
176
 
177
- const items = content.items.map((itemBlock) => {
178
- const itemBlockFields = itemBlock.value.reduce<GroupItemContent["value"]>(
179
- (acc, [fieldKey, fieldContent]) => {
180
- const fieldDef = variationDef && variationDef.items?.[fieldKey]
181
-
182
- const transformedField = transform({
183
- path: path.concat([
184
- { key: "items", type: "items" },
185
- { key: fieldKey, type: fieldContent.__TYPE__ },
186
- ]),
187
- key: fieldKey,
188
- apiId: sliceName,
189
- model: fieldDef,
190
- content: fieldContent,
191
- })
192
- // Can happen if the transform function returns undefined to filter out a field
193
- if (!transformedField) return acc
194
- return acc.concat([[fieldKey, transformedField]])
195
- },
196
- [],
197
- )
198
-
199
- return {
200
- __TYPE__: itemBlock.__TYPE__,
201
- value: itemBlockFields,
202
- }
203
- })
177
+ const items = traverseGroupItemsContent({
178
+ path: path.concat([{ key: "items", type: "items" }]),
179
+ model: variationDef?.items,
180
+ content: content.items,
181
+ })(transform)
204
182
 
205
183
  return {
206
184
  __TYPE__: content.__TYPE__,
@@ -75,7 +75,7 @@ export function traverseSlices({
75
75
  content: SlicesContent
76
76
  model?: StaticSlices | undefined
77
77
  }) {
78
- return (transform: TraverseContentFn): SlicesContent | undefined => {
78
+ return (transform: TraverseContentFn): SlicesContent => {
79
79
  const value = content.value.reduce<SlicesContent["value"]>(
80
80
  (acc, sliceContent) => {
81
81
  const sliceModel: StaticSlice | undefined =
@@ -124,8 +124,6 @@ export function traverseSlices({
124
124
  }
125
125
  })()
126
126
 
127
- if (!convertedSliceWidget) return acc
128
-
129
127
  const convertedSlice = transform({
130
128
  key: sliceContent.key,
131
129
  apiId: sliceContent.name,
@@ -1,2 +0,0 @@
1
- import * as t from "io-ts";
2
- export declare const NullOrElse: <A, O = A>(codec: t.Type<A, O, unknown>) => t.Type<A | null, O | undefined, unknown>;
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NullOrElse = void 0;
4
- const tslib_1 = require("tslib");
5
- const Either_1 = require("fp-ts/lib/Either");
6
- const t = (0, tslib_1.__importStar)(require("io-ts"));
7
- const NullOrElse = (codec) => new t.Type("NullOrT", (u) => null === u || codec.is(u), (u) => {
8
- if ((0, Either_1.isRight)(t.null.decode(u)))
9
- return t.success(null);
10
- return codec.decode(u);
11
- }, (chunk) => (chunk ? codec.encode(chunk) : undefined));
12
- exports.NullOrElse = NullOrElse;