@prismicio/types-internal 2.2.0-alpha.6 → 2.2.0-alpha.7
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 +2 -3
- package/lib/common/Asset.js +2 -3
- package/lib/common/Embed.d.ts +16 -0
- package/lib/common/Embed.js +10 -0
- package/lib/common/index.d.ts +1 -0
- package/lib/common/index.js +1 -0
- package/lib/customtypes/CustomType.d.ts +0 -2
- package/lib/customtypes/CustomType.js +1 -8
- package/lib/import/converters/Document.d.ts +3 -3
- package/lib/import/converters/Document.js +4 -6
- package/lib/import/converters/fields/nestable/Embed.d.ts +2 -1
- package/lib/import/converters/fields/nestable/Embed.js +24 -5
- package/lib/import/converters/fields/nestable/Image.d.ts +2 -1340
- package/lib/import/converters/fields/nestable/Image.js +21 -53
- package/lib/import/converters/fields/nestable/Nestable.d.ts +3 -3
- package/lib/import/converters/fields/nestable/Nestable.js +3 -3
- package/lib/import/validators/Document.js +7 -2
- package/lib/import/validators/fields/ImportField.d.ts +42 -87
- package/lib/import/validators/fields/nestable/Embed.d.ts +6 -23
- package/lib/import/validators/fields/nestable/Embed.js +7 -25
- 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 +42 -87
- 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 +3 -7
- package/src/common/Embed.ts +23 -0
- package/src/common/index.ts +1 -0
- package/src/customtypes/CustomType.ts +0 -13
- package/src/import/converters/Document.ts +8 -14
- package/src/import/converters/fields/nestable/Embed.ts +29 -7
- package/src/import/converters/fields/nestable/Image.ts +23 -99
- package/src/import/converters/fields/nestable/Nestable.ts +6 -6
- package/src/import/validators/Document.ts +9 -1
- package/src/import/validators/fields/nestable/Embed.ts +9 -41
- package/src/import/validators/fields/nestable/Image.ts +1 -1
- package/src/utils/Objects.ts +2 -2
|
@@ -25,42 +25,12 @@ export declare const ImportNestable: {
|
|
|
25
25
|
value: Date | null;
|
|
26
26
|
}, Date | undefined, unknown> | import("io-ts").Type<{
|
|
27
27
|
type: "Embed";
|
|
28
|
-
value:
|
|
28
|
+
value: {
|
|
29
29
|
embed_url: string;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
version?: string | number | null;
|
|
33
|
-
title?: string;
|
|
34
|
-
author_name?: string | null;
|
|
35
|
-
author_url?: string | null;
|
|
36
|
-
provider_name?: string | null;
|
|
37
|
-
provider_url?: string | null;
|
|
38
|
-
cache_age?: string | number | null;
|
|
39
|
-
thumbnail_url?: string | null;
|
|
40
|
-
thumbnail_width?: number | null;
|
|
41
|
-
thumbnail_height?: number | null;
|
|
42
|
-
html?: string | null;
|
|
43
|
-
} & {
|
|
44
|
-
all: unknown;
|
|
45
|
-
}) | null;
|
|
46
|
-
}, ({
|
|
30
|
+
} | null;
|
|
31
|
+
}, {
|
|
47
32
|
embed_url: string;
|
|
48
|
-
|
|
49
|
-
} & {
|
|
50
|
-
version?: string | number | null;
|
|
51
|
-
title?: string;
|
|
52
|
-
author_name?: string | null;
|
|
53
|
-
author_url?: string | null;
|
|
54
|
-
provider_name?: string | null;
|
|
55
|
-
provider_url?: string | null;
|
|
56
|
-
cache_age?: string | number | null;
|
|
57
|
-
thumbnail_url?: string | null;
|
|
58
|
-
thumbnail_width?: number | null;
|
|
59
|
-
thumbnail_height?: number | null;
|
|
60
|
-
html?: string | null;
|
|
61
|
-
} & {
|
|
62
|
-
all: unknown;
|
|
63
|
-
}) | undefined, unknown> | import("io-ts").Type<{
|
|
33
|
+
} | undefined, unknown> | import("io-ts").Type<{
|
|
64
34
|
type: "GeoPoint";
|
|
65
35
|
value: {
|
|
66
36
|
latitude: number;
|
|
@@ -75,12 +45,12 @@ export declare const ImportNestable: {
|
|
|
75
45
|
id: string;
|
|
76
46
|
} & {
|
|
77
47
|
edit?: {
|
|
78
|
-
x
|
|
79
|
-
y
|
|
80
|
-
width
|
|
81
|
-
height
|
|
82
|
-
zoom
|
|
83
|
-
background
|
|
48
|
+
x: number;
|
|
49
|
+
y: number;
|
|
50
|
+
width: number;
|
|
51
|
+
height: number;
|
|
52
|
+
zoom: number;
|
|
53
|
+
background: string;
|
|
84
54
|
};
|
|
85
55
|
credit?: string | null;
|
|
86
56
|
alt?: string | null;
|
|
@@ -90,12 +60,12 @@ export declare const ImportNestable: {
|
|
|
90
60
|
id: string;
|
|
91
61
|
} & {
|
|
92
62
|
edit?: {
|
|
93
|
-
x
|
|
94
|
-
y
|
|
95
|
-
width
|
|
96
|
-
height
|
|
97
|
-
zoom
|
|
98
|
-
background
|
|
63
|
+
x: number;
|
|
64
|
+
y: number;
|
|
65
|
+
width: number;
|
|
66
|
+
height: number;
|
|
67
|
+
zoom: number;
|
|
68
|
+
background: string;
|
|
99
69
|
};
|
|
100
70
|
credit?: string | null;
|
|
101
71
|
alt?: string | null;
|
|
@@ -106,12 +76,12 @@ export declare const ImportNestable: {
|
|
|
106
76
|
id: string;
|
|
107
77
|
} & {
|
|
108
78
|
edit?: {
|
|
109
|
-
x
|
|
110
|
-
y
|
|
111
|
-
width
|
|
112
|
-
height
|
|
113
|
-
zoom
|
|
114
|
-
background
|
|
79
|
+
x: number;
|
|
80
|
+
y: number;
|
|
81
|
+
width: number;
|
|
82
|
+
height: number;
|
|
83
|
+
zoom: number;
|
|
84
|
+
background: string;
|
|
115
85
|
};
|
|
116
86
|
credit?: string | null;
|
|
117
87
|
alt?: string | null;
|
|
@@ -121,12 +91,12 @@ export declare const ImportNestable: {
|
|
|
121
91
|
id: string;
|
|
122
92
|
} & {
|
|
123
93
|
edit?: {
|
|
124
|
-
x
|
|
125
|
-
y
|
|
126
|
-
width
|
|
127
|
-
height
|
|
128
|
-
zoom
|
|
129
|
-
background
|
|
94
|
+
x: number;
|
|
95
|
+
y: number;
|
|
96
|
+
width: number;
|
|
97
|
+
height: number;
|
|
98
|
+
zoom: number;
|
|
99
|
+
background: string;
|
|
130
100
|
};
|
|
131
101
|
credit?: string | null;
|
|
132
102
|
alt?: string | null;
|
|
@@ -193,12 +163,12 @@ export declare const ImportNestable: {
|
|
|
193
163
|
id: string;
|
|
194
164
|
} & {
|
|
195
165
|
edit?: {
|
|
196
|
-
x
|
|
197
|
-
y
|
|
198
|
-
width
|
|
199
|
-
height
|
|
200
|
-
zoom
|
|
201
|
-
background
|
|
166
|
+
x: number;
|
|
167
|
+
y: number;
|
|
168
|
+
width: number;
|
|
169
|
+
height: number;
|
|
170
|
+
zoom: number;
|
|
171
|
+
background: string;
|
|
202
172
|
};
|
|
203
173
|
credit?: string | null;
|
|
204
174
|
alt?: string | null;
|
|
@@ -208,12 +178,12 @@ export declare const ImportNestable: {
|
|
|
208
178
|
id: string;
|
|
209
179
|
} & {
|
|
210
180
|
edit?: {
|
|
211
|
-
x
|
|
212
|
-
y
|
|
213
|
-
width
|
|
214
|
-
height
|
|
215
|
-
zoom
|
|
216
|
-
background
|
|
181
|
+
x: number;
|
|
182
|
+
y: number;
|
|
183
|
+
width: number;
|
|
184
|
+
height: number;
|
|
185
|
+
zoom: number;
|
|
186
|
+
background: string;
|
|
217
187
|
};
|
|
218
188
|
credit?: string | null;
|
|
219
189
|
alt?: string | null;
|
|
@@ -228,24 +198,9 @@ export declare const ImportNestable: {
|
|
|
228
198
|
} | null;
|
|
229
199
|
}> | import("fp-ts/lib/Either").Right<{
|
|
230
200
|
type: "Embed";
|
|
231
|
-
value:
|
|
201
|
+
value: {
|
|
232
202
|
embed_url: string;
|
|
233
|
-
|
|
234
|
-
} & {
|
|
235
|
-
version?: string | number | null;
|
|
236
|
-
title?: string;
|
|
237
|
-
author_name?: string | null;
|
|
238
|
-
author_url?: string | null;
|
|
239
|
-
provider_name?: string | null;
|
|
240
|
-
provider_url?: string | null;
|
|
241
|
-
cache_age?: string | number | null;
|
|
242
|
-
thumbnail_url?: string | null;
|
|
243
|
-
thumbnail_width?: number | null;
|
|
244
|
-
thumbnail_height?: number | null;
|
|
245
|
-
html?: string | null;
|
|
246
|
-
} & {
|
|
247
|
-
all: unknown;
|
|
248
|
-
}) | null;
|
|
203
|
+
} | null;
|
|
249
204
|
}> | import("fp-ts/lib/Either").Right<{
|
|
250
205
|
type: "Date";
|
|
251
206
|
value: Date | null;
|
package/lib/utils/Objects.d.ts
CHANGED
|
@@ -5,4 +5,4 @@ export declare function zipObjects<A, B = A>(objLeft?: Partial<Record<string, A>
|
|
|
5
5
|
export declare function isNotEmpty<A extends object>(obj: A): boolean;
|
|
6
6
|
export declare function withOptionals<T extends object>(object: T, optionals: Array<[keyof T, T[keyof T] | null | undefined]>): T;
|
|
7
7
|
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
8
|
-
export declare function mapValues<T, O>(record: Record<string, T>, fn: (value: T
|
|
8
|
+
export declare function mapValues<T, O>(record: Record<string, T>, fn: (value: T) => O): Record<string, O>;
|
package/lib/utils/Objects.js
CHANGED
|
@@ -37,6 +37,6 @@ function isObject(value) {
|
|
|
37
37
|
}
|
|
38
38
|
exports.isObject = isObject;
|
|
39
39
|
function mapValues(record, fn) {
|
|
40
|
-
return Object.entries(record).reduce((acc, [key, value]) => ({ ...acc, [key]: fn(value
|
|
40
|
+
return Object.entries(record).reduce((acc, [key, value]) => ({ ...acc, [key]: fn(value) }), {});
|
|
41
41
|
}
|
|
42
42
|
exports.mapValues = mapValues;
|
package/package.json
CHANGED
package/src/common/Asset.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
export type AssetId = string
|
|
2
|
-
|
|
3
1
|
export type Asset = {
|
|
4
|
-
id:
|
|
2
|
+
id: string
|
|
5
3
|
last_modified: string
|
|
6
4
|
kind: "image" | "all"
|
|
7
5
|
filename?: string
|
|
@@ -17,10 +15,8 @@ export type Asset = {
|
|
|
17
15
|
}
|
|
18
16
|
|
|
19
17
|
export const getAssetOrThrow =
|
|
20
|
-
(assets: Record<
|
|
18
|
+
(assets: Record<string, Asset>) => (assetId: string) => {
|
|
21
19
|
const asset = assets[assetId]
|
|
22
|
-
if (!asset) {
|
|
23
|
-
throw new Error(`Missing asset with id '${assetId}'!`)
|
|
24
|
-
}
|
|
20
|
+
if (!asset) throw new Error(`Missing asset with id '${assetId}'`)
|
|
25
21
|
return asset
|
|
26
22
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type Embed = {
|
|
2
|
+
type: string
|
|
3
|
+
version?: string | null
|
|
4
|
+
author_name?: string | null
|
|
5
|
+
author_url?: string | null
|
|
6
|
+
provider_name?: string | null
|
|
7
|
+
provider_url?: string | null
|
|
8
|
+
cache_age?: string | null
|
|
9
|
+
thumbnail_url?: string | null
|
|
10
|
+
thumbnail_width?: number | null
|
|
11
|
+
thumbnail_height?: number | null
|
|
12
|
+
html?: string | null
|
|
13
|
+
title?: string | null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type EmbedUrl = string
|
|
17
|
+
|
|
18
|
+
export const getEmbedOrThrow =
|
|
19
|
+
(embeds: Record<string, Embed>) => (embedUrl: string) => {
|
|
20
|
+
const embed = embeds[embedUrl]
|
|
21
|
+
if (!embed) throw new Error(`Missing embed with url '${embedUrl}'`)
|
|
22
|
+
return embed
|
|
23
|
+
}
|
package/src/common/index.ts
CHANGED
|
@@ -8,7 +8,6 @@ import type { SharedSlice } from "./widgets/slices/SharedSlice"
|
|
|
8
8
|
import type { DynamicSlice } from "./widgets/slices/Slice"
|
|
9
9
|
import type { DynamicSlices } from "./widgets/slices/Slices"
|
|
10
10
|
import type { DynamicWidget } from "./widgets/Widget"
|
|
11
|
-
import type { StaticWidget } from "./widgets/Widget"
|
|
12
11
|
|
|
13
12
|
export const CustomTypeFormat = {
|
|
14
13
|
page: "page",
|
|
@@ -195,15 +194,3 @@ export function filterMissingSharedSlices(
|
|
|
195
194
|
return widget
|
|
196
195
|
})
|
|
197
196
|
}
|
|
198
|
-
|
|
199
|
-
export function flattenCustomTypeFields(
|
|
200
|
-
customType: StaticCustomType,
|
|
201
|
-
): Partial<Record<string, StaticWidget>> {
|
|
202
|
-
return Object.values(customType.json).reduce(
|
|
203
|
-
(acc, tab) => ({
|
|
204
|
-
...acc,
|
|
205
|
-
...tab,
|
|
206
|
-
}),
|
|
207
|
-
{},
|
|
208
|
-
)
|
|
209
|
-
}
|
|
@@ -1,24 +1,18 @@
|
|
|
1
|
-
import type { Asset
|
|
1
|
+
import type { Asset } from "../../common"
|
|
2
|
+
import type { Embed } from "../../common/Embed"
|
|
2
3
|
import type { Document, WidgetContent } from "../../content"
|
|
3
|
-
import type { StaticCustomType } from "../../customtypes"
|
|
4
|
-
import { flattenCustomTypeFields, StaticWidget } from "../../customtypes"
|
|
5
4
|
import type { ImportDocument } from "../validators"
|
|
6
5
|
import type { ImportField } from "../validators/fields/ImportField"
|
|
7
6
|
import { convertNestableWidget, uidConverter } from "./fields"
|
|
8
7
|
|
|
9
8
|
export function convertImportToContent(
|
|
10
9
|
document: ImportDocument,
|
|
11
|
-
assets: Record<
|
|
12
|
-
|
|
10
|
+
assets: Record<string, Asset>,
|
|
11
|
+
embeds: Record<string, Embed>,
|
|
13
12
|
): Document {
|
|
14
|
-
const fieldModels = flattenCustomTypeFields(customType)
|
|
15
13
|
return Object.entries(document).reduce<Document>(
|
|
16
14
|
(acc, [fieldKey, fieldValue]) => {
|
|
17
|
-
const newFieldValue = convertWidget(
|
|
18
|
-
fieldValue,
|
|
19
|
-
assets,
|
|
20
|
-
fieldModels[fieldKey],
|
|
21
|
-
)
|
|
15
|
+
const newFieldValue = convertWidget(fieldValue, assets, embeds)
|
|
22
16
|
return newFieldValue ? { ...acc, [fieldKey]: newFieldValue } : acc
|
|
23
17
|
},
|
|
24
18
|
{},
|
|
@@ -27,13 +21,13 @@ export function convertImportToContent(
|
|
|
27
21
|
|
|
28
22
|
function convertWidget(
|
|
29
23
|
field: ImportField,
|
|
30
|
-
assets: Record<
|
|
31
|
-
|
|
24
|
+
assets: Record<string, Asset>,
|
|
25
|
+
embeds: Record<string, Embed>,
|
|
32
26
|
): WidgetContent | undefined {
|
|
33
27
|
switch (field.type) {
|
|
34
28
|
case "UID":
|
|
35
29
|
return uidConverter(field.value)
|
|
36
30
|
default:
|
|
37
|
-
return convertNestableWidget(field, assets,
|
|
31
|
+
return convertNestableWidget(field, assets, embeds)
|
|
38
32
|
}
|
|
39
33
|
}
|
|
@@ -1,15 +1,37 @@
|
|
|
1
|
+
import type { Embed } from "../../../../common"
|
|
2
|
+
import { getEmbedOrThrow } from "../../../../common"
|
|
1
3
|
import type { EmbedContent } from "../../../../content"
|
|
4
|
+
import { withOptionals } from "../../../../utils/Objects"
|
|
2
5
|
import type { ImportEmbed } from "../../../validators"
|
|
3
6
|
|
|
4
7
|
export const embedConverter = (
|
|
5
8
|
field: ImportEmbed["value"],
|
|
9
|
+
embeds: Record<string, Embed>,
|
|
6
10
|
): EmbedContent | undefined => {
|
|
7
11
|
if (field === null) return
|
|
8
|
-
|
|
9
|
-
return
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const embed = getEmbedOrThrow(embeds)(field.embed_url)
|
|
13
|
+
return withOptionals<EmbedContent>(
|
|
14
|
+
{
|
|
15
|
+
embed_url: field.embed_url,
|
|
16
|
+
type: embed.type,
|
|
17
|
+
all: {
|
|
18
|
+
embed_url: field.embed_url,
|
|
19
|
+
...embed,
|
|
20
|
+
},
|
|
21
|
+
__TYPE__: "EmbedContent",
|
|
22
|
+
},
|
|
23
|
+
[
|
|
24
|
+
["version", embed.version],
|
|
25
|
+
["title", embed.title],
|
|
26
|
+
["author_name", embed.author_name],
|
|
27
|
+
["author_url", embed.author_url],
|
|
28
|
+
["provider_name", embed.provider_name],
|
|
29
|
+
["provider_url", embed.provider_url],
|
|
30
|
+
["cache_age", embed.cache_age],
|
|
31
|
+
["thumbnail_url", embed.thumbnail_url],
|
|
32
|
+
["thumbnail_width", embed.thumbnail_width],
|
|
33
|
+
["thumbnail_height", embed.thumbnail_height],
|
|
34
|
+
["html", embed.html],
|
|
35
|
+
],
|
|
36
|
+
)
|
|
15
37
|
}
|
|
@@ -1,129 +1,53 @@
|
|
|
1
|
-
import type { Asset
|
|
2
|
-
import { getAssetOrThrow } from "../../../../common"
|
|
1
|
+
import type { Asset } from "../../../../common"
|
|
3
2
|
import type { ImageContent } from "../../../../content"
|
|
4
|
-
import
|
|
5
|
-
ImageConstraint,
|
|
6
|
-
StaticWidget,
|
|
7
|
-
Thumbnail,
|
|
8
|
-
} from "../../../../customtypes"
|
|
9
|
-
import { withOptionals } from "../../../../utils/Objects"
|
|
3
|
+
import { mapValues, withOptionals } from "../../../../utils/Objects"
|
|
10
4
|
import type { ImageField, ImportImage } from "../../../validators"
|
|
11
5
|
|
|
12
|
-
function convertImage(
|
|
13
|
-
imageField: ImageField | undefined,
|
|
14
|
-
edit: ImageField["edit"] | undefined,
|
|
15
|
-
constraints: ImageConstraint | undefined,
|
|
16
|
-
image: Asset,
|
|
17
|
-
) {
|
|
6
|
+
function convertImage(field: ImageField, image: Asset) {
|
|
18
7
|
return withOptionals<Omit<ImageContent, "__TYPE__" | "thumbnails">>(
|
|
19
8
|
{
|
|
20
9
|
origin: {
|
|
21
|
-
id:
|
|
10
|
+
id: field.id,
|
|
22
11
|
url: image.origin_url,
|
|
23
|
-
// All images returned form Asset API should have width and height properties.
|
|
24
12
|
width: image.width ?? 0,
|
|
25
13
|
height: image.height ?? 0,
|
|
26
14
|
},
|
|
27
|
-
|
|
28
|
-
|
|
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).
|
|
15
|
+
width: field.edit?.width ?? image.width ?? 0,
|
|
16
|
+
height: field.edit?.height ?? image.height ?? 0,
|
|
33
17
|
edit: {
|
|
34
|
-
zoom: edit?.zoom ?? 1,
|
|
18
|
+
zoom: field.edit?.zoom ?? 1,
|
|
35
19
|
crop: {
|
|
36
|
-
x: edit?.x ?? 0,
|
|
37
|
-
y: edit?.y ?? 0,
|
|
20
|
+
x: field.edit?.x ?? 0,
|
|
21
|
+
y: field.edit?.y ?? 0,
|
|
38
22
|
},
|
|
39
|
-
background:
|
|
40
|
-
edit?.background ??
|
|
41
|
-
(image.extension === "png" ? "transparent" : "#ffffff"),
|
|
23
|
+
background: field.edit?.background ?? "transparent",
|
|
42
24
|
},
|
|
43
25
|
url: image.url,
|
|
44
26
|
},
|
|
45
27
|
[
|
|
46
|
-
["alt",
|
|
47
|
-
["credits",
|
|
28
|
+
["alt", field.alt || image.alt],
|
|
29
|
+
["credits", field.credit || image.credits],
|
|
48
30
|
],
|
|
49
31
|
)
|
|
50
32
|
}
|
|
51
33
|
|
|
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
|
|
99
34
|
export const imageConverter = (
|
|
100
|
-
|
|
101
|
-
assets: Record<
|
|
102
|
-
model?: StaticWidget,
|
|
35
|
+
field: ImportImage["value"],
|
|
36
|
+
assets: Record<string, Asset>,
|
|
103
37
|
): ImageContent | undefined => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
throw new Error(
|
|
109
|
-
`Wrong model type! Expected 'Image' but got '${model.type}'`,
|
|
110
|
-
)
|
|
38
|
+
const getImageById = (id: string): Asset => {
|
|
39
|
+
const image = assets[id]
|
|
40
|
+
if (!image) throw new Error(`Missing asset with id '${id}'`)
|
|
41
|
+
return image
|
|
111
42
|
}
|
|
112
43
|
|
|
113
|
-
if (!
|
|
44
|
+
if (!field) return
|
|
114
45
|
|
|
115
46
|
return {
|
|
116
|
-
...convertImage(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
model.config?.constraint,
|
|
120
|
-
getAssetOrThrow(assets)(imageField.id),
|
|
121
|
-
),
|
|
122
|
-
thumbnails: convertThumbnails(
|
|
123
|
-
imageField,
|
|
124
|
-
assets,
|
|
125
|
-
model.config?.thumbnails ?? [],
|
|
47
|
+
...convertImage(field, getImageById(field.id)),
|
|
48
|
+
thumbnails: mapValues(field.thumbnails, (thumbnail) =>
|
|
49
|
+
convertImage(thumbnail, getImageById(thumbnail.id)),
|
|
126
50
|
),
|
|
127
|
-
__TYPE__: "ImageContent"
|
|
51
|
+
__TYPE__: "ImageContent",
|
|
128
52
|
}
|
|
129
53
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { Asset
|
|
1
|
+
import type { Asset } from "../../../../common"
|
|
2
|
+
import type { Embed } from "../../../../common/Embed"
|
|
2
3
|
import type { WidgetContent } from "../../../../content"
|
|
3
|
-
import type { StaticWidget } from "../../../../customtypes"
|
|
4
4
|
import type { ImportNestable } from "../../../validators"
|
|
5
5
|
import {
|
|
6
6
|
booleanConverter,
|
|
@@ -18,8 +18,8 @@ import {
|
|
|
18
18
|
|
|
19
19
|
export function convertNestableWidget(
|
|
20
20
|
field: ImportNestable,
|
|
21
|
-
assets: Record<
|
|
22
|
-
|
|
21
|
+
assets: Record<string, Asset>,
|
|
22
|
+
embeds: Record<string, Embed>,
|
|
23
23
|
): WidgetContent | undefined {
|
|
24
24
|
switch (field.type) {
|
|
25
25
|
case "Boolean":
|
|
@@ -37,13 +37,13 @@ export function convertNestableWidget(
|
|
|
37
37
|
case "Timestamp":
|
|
38
38
|
return timestampConverter(field.value)
|
|
39
39
|
case "Embed":
|
|
40
|
-
return embedConverter(field.value)
|
|
40
|
+
return embedConverter(field.value, embeds)
|
|
41
41
|
case "GeoPoint":
|
|
42
42
|
return geopointConverter(field.value)
|
|
43
43
|
case "Link":
|
|
44
44
|
return linkConverter(field.value, assets)
|
|
45
45
|
case "Image":
|
|
46
|
-
return imageConverter(field.value, assets
|
|
46
|
+
return imageConverter(field.value, assets)
|
|
47
47
|
default:
|
|
48
48
|
throw new Error(
|
|
49
49
|
`Unsupported type of nestable converter ${JSON.stringify(field)}`,
|
|
@@ -6,7 +6,6 @@ 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"
|
|
10
9
|
import { isObject } from "../../utils/Objects"
|
|
11
10
|
import { ImportField } from "./fields/ImportField"
|
|
12
11
|
|
|
@@ -15,6 +14,15 @@ const rawImportDocument = withMessage(
|
|
|
15
14
|
() => "document is not an object",
|
|
16
15
|
)
|
|
17
16
|
|
|
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
|
+
|
|
18
26
|
function validateField(
|
|
19
27
|
document: Record<string, unknown>,
|
|
20
28
|
customType: StaticCustomType,
|
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import * as Either from "fp-ts/
|
|
1
|
+
import * as Either from "fp-ts/Either"
|
|
2
2
|
import { pipe } from "fp-ts/lib/function"
|
|
3
3
|
import * as t from "io-ts"
|
|
4
|
-
import { withMessage } from "io-ts-types"
|
|
5
4
|
|
|
6
5
|
import { EmptyObjectOrElse } from "../../../../validators"
|
|
7
|
-
import {
|
|
8
|
-
NumberOrNull,
|
|
9
|
-
String,
|
|
10
|
-
StringOrNull,
|
|
11
|
-
} from "../../../../validators/BasicTypes"
|
|
12
6
|
import { ImportContent } from "../ImportContent"
|
|
13
7
|
|
|
14
8
|
type URL = string
|
|
@@ -38,49 +32,23 @@ const EmbedUrl = new t.Type<URL, URL, unknown>(
|
|
|
38
32
|
t.identity,
|
|
39
33
|
)
|
|
40
34
|
|
|
41
|
-
const EmbedProto = t.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
embed_url: EmbedUrl,
|
|
45
|
-
type: String,
|
|
46
|
-
}),
|
|
47
|
-
t.partial({
|
|
48
|
-
version: withMessage(
|
|
49
|
-
t.union([t.string, t.number, t.null]),
|
|
50
|
-
() => "The value must be either string, number or null",
|
|
51
|
-
),
|
|
52
|
-
title: String,
|
|
53
|
-
author_name: StringOrNull,
|
|
54
|
-
author_url: StringOrNull,
|
|
55
|
-
provider_name: StringOrNull,
|
|
56
|
-
provider_url: StringOrNull,
|
|
57
|
-
cache_age: withMessage(
|
|
58
|
-
t.union([t.string, t.number, t.null]),
|
|
59
|
-
() => "The value must be either string, number or null",
|
|
60
|
-
),
|
|
61
|
-
thumbnail_url: StringOrNull,
|
|
62
|
-
thumbnail_width: NumberOrNull,
|
|
63
|
-
thumbnail_height: NumberOrNull,
|
|
64
|
-
html: StringOrNull,
|
|
65
|
-
}),
|
|
66
|
-
]),
|
|
67
|
-
)
|
|
35
|
+
const EmbedProto = t.type({
|
|
36
|
+
embed_url: EmbedUrl,
|
|
37
|
+
})
|
|
68
38
|
type EmbedProto = t.TypeOf<typeof EmbedProto>
|
|
69
39
|
|
|
70
|
-
type Embed = EmbedProto
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
const embedValue = new t.Type<Embed>(
|
|
40
|
+
type Embed = EmbedProto
|
|
41
|
+
const Embed = new t.Type<Embed>(
|
|
74
42
|
"ImportEmbedValue",
|
|
75
|
-
(u: unknown): u is Embed => EmbedProto.is(u)
|
|
43
|
+
(u: unknown): u is Embed => EmbedProto.is(u),
|
|
76
44
|
(u: unknown) => {
|
|
77
45
|
return pipe(
|
|
78
46
|
EmbedProto.decode(u),
|
|
79
|
-
Either.map((parsed) => ({
|
|
47
|
+
Either.map((parsed) => ({ embed_url: parsed.embed_url })),
|
|
80
48
|
)
|
|
81
49
|
},
|
|
82
50
|
t.identity,
|
|
83
51
|
)
|
|
84
52
|
|
|
85
|
-
export const ImportEmbed = ImportContent("Embed", EmptyObjectOrElse(
|
|
53
|
+
export const ImportEmbed = ImportContent("Embed", EmptyObjectOrElse(Embed))
|
|
86
54
|
export type ImportEmbed = t.TypeOf<typeof ImportEmbed>
|