@prismicio/types-internal 2.2.0-alpha.5 → 2.2.0-alpha.6
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/common/Asset.d.ts +3 -2
- package/lib/common/Asset.js +3 -2
- package/lib/customtypes/CustomType.d.ts +2 -0
- package/lib/customtypes/CustomType.js +8 -1
- package/lib/import/converters/Document.d.ts +3 -2
- package/lib/import/converters/Document.js +6 -4
- package/lib/import/converters/fields/nestable/Image.d.ts +1340 -2
- package/lib/import/converters/fields/nestable/Image.js +53 -21
- package/lib/import/converters/fields/nestable/Nestable.d.ts +3 -2
- package/lib/import/converters/fields/nestable/Nestable.js +2 -2
- package/lib/import/validators/Document.js +2 -7
- package/lib/import/validators/fields/ImportField.d.ts +36 -36
- package/lib/import/validators/fields/nestable/Image.d.ts +12 -12
- package/lib/import/validators/fields/nestable/Image.js +1 -1
- package/lib/import/validators/fields/nestable/Nestable.d.ts +36 -36
- package/lib/utils/Objects.d.ts +1 -1
- package/lib/utils/Objects.js +1 -1
- package/package.json +1 -1
- package/src/common/Asset.ts +7 -3
- package/src/customtypes/CustomType.ts +13 -0
- package/src/import/converters/Document.ts +14 -5
- package/src/import/converters/fields/nestable/Image.ts +99 -23
- package/src/import/converters/fields/nestable/Nestable.ts +5 -3
- package/src/import/validators/Document.ts +1 -9
- package/src/import/validators/fields/nestable/Image.ts +1 -1
- package/src/utils/Objects.ts +2 -2
|
@@ -1,53 +1,129 @@
|
|
|
1
|
-
import type { Asset } from "../../../../common"
|
|
1
|
+
import type { Asset, AssetId } from "../../../../common"
|
|
2
|
+
import { getAssetOrThrow } from "../../../../common"
|
|
2
3
|
import type { ImageContent } from "../../../../content"
|
|
3
|
-
import {
|
|
4
|
+
import type {
|
|
5
|
+
ImageConstraint,
|
|
6
|
+
StaticWidget,
|
|
7
|
+
Thumbnail,
|
|
8
|
+
} from "../../../../customtypes"
|
|
9
|
+
import { withOptionals } from "../../../../utils/Objects"
|
|
4
10
|
import type { ImageField, ImportImage } from "../../../validators"
|
|
5
11
|
|
|
6
|
-
function convertImage(
|
|
12
|
+
function convertImage(
|
|
13
|
+
imageField: ImageField | undefined,
|
|
14
|
+
edit: ImageField["edit"] | undefined,
|
|
15
|
+
constraints: ImageConstraint | undefined,
|
|
16
|
+
image: Asset,
|
|
17
|
+
) {
|
|
7
18
|
return withOptionals<Omit<ImageContent, "__TYPE__" | "thumbnails">>(
|
|
8
19
|
{
|
|
9
20
|
origin: {
|
|
10
|
-
id:
|
|
21
|
+
id: image.id,
|
|
11
22
|
url: image.origin_url,
|
|
23
|
+
// All images returned form Asset API should have width and height properties.
|
|
12
24
|
width: image.width ?? 0,
|
|
13
25
|
height: image.height ?? 0,
|
|
14
26
|
},
|
|
15
|
-
|
|
16
|
-
|
|
27
|
+
// All images returned form Asset API should have width and height properties.
|
|
28
|
+
// Edit can only overwrite width and height if there are no constraints set
|
|
29
|
+
width: constraints?.width ?? edit?.width ?? image.width ?? 0,
|
|
30
|
+
height: constraints?.height ?? edit?.height ?? image.height ?? 0,
|
|
31
|
+
// If edit is not provided, we crop constraint width and height from the left upper corner.
|
|
32
|
+
// WARN: If constraints are greater than image dimensions cut outside the image (background will fill extra space).
|
|
17
33
|
edit: {
|
|
18
|
-
zoom:
|
|
34
|
+
zoom: edit?.zoom ?? 1,
|
|
19
35
|
crop: {
|
|
20
|
-
x:
|
|
21
|
-
y:
|
|
36
|
+
x: edit?.x ?? 0,
|
|
37
|
+
y: edit?.y ?? 0,
|
|
22
38
|
},
|
|
23
|
-
background:
|
|
39
|
+
background:
|
|
40
|
+
edit?.background ??
|
|
41
|
+
(image.extension === "png" ? "transparent" : "#ffffff"),
|
|
24
42
|
},
|
|
25
43
|
url: image.url,
|
|
26
44
|
},
|
|
27
45
|
[
|
|
28
|
-
["alt",
|
|
29
|
-
["credits",
|
|
46
|
+
["alt", imageField?.alt || image.alt],
|
|
47
|
+
["credits", imageField?.credit || image.credits],
|
|
30
48
|
],
|
|
31
49
|
)
|
|
32
50
|
}
|
|
33
51
|
|
|
52
|
+
// If image field contains thumbnailName definition use it to generate thumbnail
|
|
53
|
+
// If image field is missing thumbnailName derive thumbnail from main image
|
|
54
|
+
// WARN: edit is not inherited from main image as each thumbnail have different constraints
|
|
55
|
+
function convertThumbnail(
|
|
56
|
+
thumbnailName: string,
|
|
57
|
+
thumbnailConstraints: ImageConstraint | undefined,
|
|
58
|
+
imageField: Exclude<ImportImage["value"], null>,
|
|
59
|
+
assets: Record<AssetId, Asset>,
|
|
60
|
+
) {
|
|
61
|
+
const maybeThumbnail = imageField.thumbnails[thumbnailName]
|
|
62
|
+
if (maybeThumbnail) {
|
|
63
|
+
return convertImage(
|
|
64
|
+
maybeThumbnail,
|
|
65
|
+
maybeThumbnail.edit,
|
|
66
|
+
thumbnailConstraints,
|
|
67
|
+
getAssetOrThrow(assets)(maybeThumbnail.id),
|
|
68
|
+
)
|
|
69
|
+
} else {
|
|
70
|
+
return convertImage(
|
|
71
|
+
imageField,
|
|
72
|
+
undefined,
|
|
73
|
+
thumbnailConstraints,
|
|
74
|
+
getAssetOrThrow(assets)(imageField.id),
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function convertThumbnails(
|
|
80
|
+
imageField: Exclude<ImportImage["value"], null>,
|
|
81
|
+
assets: Record<AssetId, Asset>,
|
|
82
|
+
thumbnailsModel: readonly Thumbnail[],
|
|
83
|
+
) {
|
|
84
|
+
return thumbnailsModel.reduce<
|
|
85
|
+
Record<string, ReturnType<typeof convertImage>>
|
|
86
|
+
>((acc, { name, ...constraints }) => {
|
|
87
|
+
return {
|
|
88
|
+
...acc,
|
|
89
|
+
[name]: convertThumbnail(name, constraints, imageField, assets),
|
|
90
|
+
}
|
|
91
|
+
}, {})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Function constraints:
|
|
95
|
+
// * `model` is required
|
|
96
|
+
// * `mode` is of type Image
|
|
97
|
+
// * All assets from `imageField` must be present in `assets`
|
|
98
|
+
// If any of these constraints is not met, function will throw an error
|
|
34
99
|
export const imageConverter = (
|
|
35
|
-
|
|
36
|
-
assets: Record<
|
|
100
|
+
imageField: ImportImage["value"],
|
|
101
|
+
assets: Record<AssetId, Asset>,
|
|
102
|
+
model?: StaticWidget,
|
|
37
103
|
): ImageContent | undefined => {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
104
|
+
if (!model) {
|
|
105
|
+
throw new Error(`'model' parameter is required!`)
|
|
106
|
+
}
|
|
107
|
+
if (model.type !== "Image") {
|
|
108
|
+
throw new Error(
|
|
109
|
+
`Wrong model type! Expected 'Image' but got '${model.type}'`,
|
|
110
|
+
)
|
|
42
111
|
}
|
|
43
112
|
|
|
44
|
-
if (!
|
|
113
|
+
if (!imageField) return
|
|
45
114
|
|
|
46
115
|
return {
|
|
47
|
-
...convertImage(
|
|
48
|
-
|
|
49
|
-
|
|
116
|
+
...convertImage(
|
|
117
|
+
imageField,
|
|
118
|
+
imageField.edit,
|
|
119
|
+
model.config?.constraint,
|
|
120
|
+
getAssetOrThrow(assets)(imageField.id),
|
|
121
|
+
),
|
|
122
|
+
thumbnails: convertThumbnails(
|
|
123
|
+
imageField,
|
|
124
|
+
assets,
|
|
125
|
+
model.config?.thumbnails ?? [],
|
|
50
126
|
),
|
|
51
|
-
__TYPE__: "ImageContent",
|
|
127
|
+
__TYPE__: "ImageContent" as const,
|
|
52
128
|
}
|
|
53
129
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { Asset } from "../../../../common"
|
|
1
|
+
import type { Asset, AssetId } from "../../../../common"
|
|
2
2
|
import type { WidgetContent } from "../../../../content"
|
|
3
|
+
import type { StaticWidget } from "../../../../customtypes"
|
|
3
4
|
import type { ImportNestable } from "../../../validators"
|
|
4
5
|
import {
|
|
5
6
|
booleanConverter,
|
|
@@ -17,7 +18,8 @@ import {
|
|
|
17
18
|
|
|
18
19
|
export function convertNestableWidget(
|
|
19
20
|
field: ImportNestable,
|
|
20
|
-
assets: Record<
|
|
21
|
+
assets: Record<AssetId, Asset>,
|
|
22
|
+
model?: StaticWidget,
|
|
21
23
|
): WidgetContent | undefined {
|
|
22
24
|
switch (field.type) {
|
|
23
25
|
case "Boolean":
|
|
@@ -41,7 +43,7 @@ export function convertNestableWidget(
|
|
|
41
43
|
case "Link":
|
|
42
44
|
return linkConverter(field.value, assets)
|
|
43
45
|
case "Image":
|
|
44
|
-
return imageConverter(field.value, assets)
|
|
46
|
+
return imageConverter(field.value, assets, model)
|
|
45
47
|
default:
|
|
46
48
|
throw new Error(
|
|
47
49
|
`Unsupported type of nestable converter ${JSON.stringify(field)}`,
|
|
@@ -6,6 +6,7 @@ import { withMessage } from "io-ts-types"
|
|
|
6
6
|
|
|
7
7
|
import type { WidgetKey } from "../../common"
|
|
8
8
|
import type { StaticCustomType, StaticWidget } from "../../customtypes"
|
|
9
|
+
import { flattenCustomTypeFields } from "../../customtypes"
|
|
9
10
|
import { isObject } from "../../utils/Objects"
|
|
10
11
|
import { ImportField } from "./fields/ImportField"
|
|
11
12
|
|
|
@@ -14,15 +15,6 @@ const rawImportDocument = withMessage(
|
|
|
14
15
|
() => "document is not an object",
|
|
15
16
|
)
|
|
16
17
|
|
|
17
|
-
function flattenCustomTypeFields(
|
|
18
|
-
customType: StaticCustomType,
|
|
19
|
-
): Partial<Record<string, StaticWidget>> {
|
|
20
|
-
return Object.values(customType.json).reduce((acc, tab) => ({
|
|
21
|
-
...acc,
|
|
22
|
-
...tab,
|
|
23
|
-
}))
|
|
24
|
-
}
|
|
25
|
-
|
|
26
18
|
function validateField(
|
|
27
19
|
document: Record<string, unknown>,
|
|
28
20
|
customType: StaticCustomType,
|
package/src/utils/Objects.ts
CHANGED
|
@@ -44,10 +44,10 @@ export function isObject(value: unknown): value is Record<string, unknown> {
|
|
|
44
44
|
|
|
45
45
|
export function mapValues<T, O>(
|
|
46
46
|
record: Record<string, T>,
|
|
47
|
-
fn: (value: T) => O,
|
|
47
|
+
fn: (value: T, key: string) => O,
|
|
48
48
|
): Record<string, O> {
|
|
49
49
|
return Object.entries<T>(record).reduce<Record<string, O>>(
|
|
50
|
-
(acc, [key, value]) => ({ ...acc, [key]: fn(value) }),
|
|
50
|
+
(acc, [key, value]) => ({ ...acc, [key]: fn(value, key) }),
|
|
51
51
|
{},
|
|
52
52
|
)
|
|
53
53
|
}
|