@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.
- package/lib/content/Document.d.ts +5 -5
- package/lib/content/Document.js +15 -3
- package/package.json +1 -1
- package/src/content/Document.ts +39 -12
|
@@ -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,
|
|
4649
|
+
export declare function traverseDocument({ document, customType, }: {
|
|
4650
4650
|
document: Document;
|
|
4651
|
-
|
|
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
|
}): {
|
package/lib/content/Document.js
CHANGED
|
@@ -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,
|
|
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
|
|
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
|
-
|
|
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
package/src/content/Document.ts
CHANGED
|
@@ -9,7 +9,12 @@ import {
|
|
|
9
9
|
TraverseWidgetContentFn,
|
|
10
10
|
} from "../_internal/utils"
|
|
11
11
|
import { WidgetKey } from "../common"
|
|
12
|
-
import {
|
|
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
|
-
|
|
172
|
+
customType,
|
|
168
173
|
}: {
|
|
169
174
|
document: Document
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
|
|
268
|
-
|
|
269
|
-
|
|
288
|
+
customType:
|
|
289
|
+
| StaticCustomType
|
|
290
|
+
| {
|
|
291
|
+
customTypeId: string
|
|
292
|
+
fields: Record<string, StaticWidget>
|
|
293
|
+
},
|
|
270
294
|
) {
|
|
271
|
-
const
|
|
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
|
-
|
|
306
|
+
customType,
|
|
280
307
|
})({
|
|
281
308
|
transformSlice: ({ content, model }) => {
|
|
282
309
|
if (isCompositeSliceItemContent(content) && model?.type === "SharedSlice")
|