@prismicio/types-internal 2.2.0-alpha.7 → 2.2.0-alpha.9

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.
Files changed (38) hide show
  1. package/lib/common/Asset.d.ts +3 -2
  2. package/lib/common/Asset.js +3 -2
  3. package/lib/common/index.d.ts +0 -1
  4. package/lib/common/index.js +0 -1
  5. package/lib/customtypes/CustomType.d.ts +2 -0
  6. package/lib/customtypes/CustomType.js +8 -1
  7. package/lib/import/converters/Document.d.ts +2 -3
  8. package/lib/import/converters/Document.js +4 -4
  9. package/lib/import/converters/fields/nestable/Embed.d.ts +1 -2
  10. package/lib/import/converters/fields/nestable/Embed.js +5 -24
  11. package/lib/import/converters/fields/nestable/Image.d.ts +2 -2
  12. package/lib/import/converters/fields/nestable/Image.js +30 -20
  13. package/lib/import/converters/fields/nestable/Nestable.d.ts +2 -3
  14. package/lib/import/converters/fields/nestable/Nestable.js +2 -2
  15. package/lib/import/validators/Document.js +2 -7
  16. package/lib/import/validators/fields/ImportField.d.ts +57 -96
  17. package/lib/import/validators/fields/nestable/Embed.d.ts +23 -6
  18. package/lib/import/validators/fields/nestable/Embed.js +25 -7
  19. package/lib/import/validators/fields/nestable/Image.d.ts +47 -30
  20. package/lib/import/validators/fields/nestable/Image.js +53 -22
  21. package/lib/import/validators/fields/nestable/Nestable.d.ts +57 -96
  22. package/lib/import/validators/fields/nestable/Nestable.js +2 -2
  23. package/lib/utils/Objects.d.ts +1 -1
  24. package/lib/utils/Objects.js +1 -1
  25. package/package.json +1 -1
  26. package/src/common/Asset.ts +7 -3
  27. package/src/common/index.ts +0 -1
  28. package/src/customtypes/CustomType.ts +13 -0
  29. package/src/import/converters/Document.ts +5 -8
  30. package/src/import/converters/fields/nestable/Embed.ts +7 -29
  31. package/src/import/converters/fields/nestable/Image.ts +46 -26
  32. package/src/import/converters/fields/nestable/Nestable.ts +3 -5
  33. package/src/import/validators/Document.ts +1 -9
  34. package/src/import/validators/fields/nestable/Embed.ts +41 -9
  35. package/src/import/validators/fields/nestable/Image.ts +118 -48
  36. package/src/import/validators/fields/nestable/Nestable.ts +2 -2
  37. package/src/utils/Objects.ts +2 -2
  38. package/src/common/Embed.ts +0 -23
@@ -5,41 +5,72 @@ const tslib_1 = require("tslib");
5
5
  const Either_1 = require("fp-ts/Either");
6
6
  const function_1 = require("fp-ts/lib/function");
7
7
  const t = (0, tslib_1.__importStar)(require("io-ts"));
8
- const Objects_1 = require("../../../../utils/Objects");
9
8
  const validators_1 = require("../../../../validators");
10
9
  const BasicTypes_1 = require("../../../../validators/BasicTypes");
11
10
  const ImportContent_1 = require("../ImportContent");
12
- const ImageFieldCodec = validators_1.AnyObject.pipe(t.intersection([
11
+ const ImageFieldValidator = BasicTypes_1.AnyObject.pipe(t.intersection([
13
12
  t.type({
14
13
  id: BasicTypes_1.String,
15
14
  }),
16
15
  t.partial({
17
- edit: validators_1.AnyObject.pipe(t.strict({
18
- x: validators_1.Number,
19
- y: validators_1.Number,
20
- width: validators_1.Number,
21
- height: validators_1.Number,
22
- zoom: validators_1.Number,
16
+ dimensions: BasicTypes_1.AnyObject.pipe(t.partial({
17
+ width: BasicTypes_1.Number,
18
+ height: BasicTypes_1.Number,
19
+ })),
20
+ edit: BasicTypes_1.AnyObject.pipe(t.partial({
21
+ x: BasicTypes_1.Number,
22
+ y: BasicTypes_1.Number,
23
+ zoom: BasicTypes_1.Number,
23
24
  background: BasicTypes_1.String,
24
25
  })),
25
26
  credit: BasicTypes_1.StringOrNull,
26
27
  alt: BasicTypes_1.StringOrNull,
27
28
  }),
28
29
  ]));
29
- const ThumbnailsCodec = t.record(t.string, ImageFieldCodec);
30
- const ImageFieldWithThumbnails = new t.Type("ImageFieldWithThumbnails", (u) => ImageFieldCodec.is(u) &&
30
+ const ThumbnailsValidator = t.record(t.string, ImageFieldValidator);
31
+ const encodeImageField = (image, mainImage, constraints) => {
32
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
33
+ const background = (_a = image === null || image === void 0 ? void 0 : image.edit) === null || _a === void 0 ? void 0 : _a.background;
34
+ const width = (_d = (_b = constraints === null || constraints === void 0 ? void 0 : constraints.width) !== null && _b !== void 0 ? _b : (_c = image === null || image === void 0 ? void 0 : image.dimensions) === null || _c === void 0 ? void 0 : _c.width) !== null && _d !== void 0 ? _d : (_e = mainImage.dimensions) === null || _e === void 0 ? void 0 : _e.width;
35
+ const height = (_h = (_f = constraints === null || constraints === void 0 ? void 0 : constraints.height) !== null && _f !== void 0 ? _f : (_g = image === null || image === void 0 ? void 0 : image.dimensions) === null || _g === void 0 ? void 0 : _g.height) !== null && _h !== void 0 ? _h : (_j = mainImage.dimensions) === null || _j === void 0 ? void 0 : _j.height;
36
+ const alt = image === null || image === void 0 ? void 0 : image.alt;
37
+ const credit = image === null || image === void 0 ? void 0 : image.credit;
38
+ return {
39
+ id: (_k = image === null || image === void 0 ? void 0 : image.id) !== null && _k !== void 0 ? _k : mainImage === null || mainImage === void 0 ? void 0 : mainImage.id,
40
+ edit: {
41
+ x: (_m = (_l = image === null || image === void 0 ? void 0 : image.edit) === null || _l === void 0 ? void 0 : _l.x) !== null && _m !== void 0 ? _m : 0,
42
+ y: (_p = (_o = image === null || image === void 0 ? void 0 : image.edit) === null || _o === void 0 ? void 0 : _o.y) !== null && _p !== void 0 ? _p : 0,
43
+ zoom: (_r = (_q = image === null || image === void 0 ? void 0 : image.edit) === null || _q === void 0 ? void 0 : _q.zoom) !== null && _r !== void 0 ? _r : 1,
44
+ ...(background !== undefined ? { background } : {}),
45
+ },
46
+ dimensions: {
47
+ ...(width !== undefined ? { width } : {}),
48
+ ...(height !== undefined ? { height } : {}),
49
+ },
50
+ ...(alt !== undefined ? { alt } : {}),
51
+ ...(credit !== undefined ? { credit } : {}),
52
+ };
53
+ };
54
+ const encodeThumbnails = (mainImage, thumbnails, field) => {
55
+ var _a, _b, _c;
56
+ return (_c = (_b = (_a = field === null || field === void 0 ? void 0 : field.config) === null || _a === void 0 ? void 0 : _a.thumbnails) === null || _b === void 0 ? void 0 : _b.reduce((acc, thumbnail) => ({
57
+ ...acc,
58
+ [thumbnail.name]: encodeImageField(thumbnails[thumbnail.name], mainImage, thumbnail),
59
+ }), {})) !== null && _c !== void 0 ? _c : {};
60
+ };
61
+ const ImageFieldWithThumbnails = (field) => new t.Type("ImageFieldWithThumbnails", (u) => ImageFieldValidator.is(u) &&
31
62
  "thumbnails" in u &&
32
- ThumbnailsCodec.is(u["thumbnails"]), (u, ctx) => {
33
- return (0, function_1.pipe)(ImageFieldCodec.validate(u, ctx), (0, Either_1.chain)((proto) => {
34
- const { id, edit, credit, alt, ...maybeThumbnails } = proto;
35
- return (0, function_1.pipe)(ThumbnailsCodec.validate(maybeThumbnails, ctx), (0, Either_1.map)((thumbnails) => (0, Objects_1.withOptionals)({
36
- id,
37
- thumbnails,
38
- }, [
39
- ["edit", edit],
40
- ["credit", credit],
41
- ["alt", alt],
42
- ])));
63
+ ThumbnailsValidator.is(u["thumbnails"]), (u, ctx) => {
64
+ return (0, function_1.pipe)(ImageFieldValidator.validate(u, ctx), (0, Either_1.chain)((mainImage) => {
65
+ const { id, dimensions, edit, credit, alt, ...maybeThumbnails } = mainImage;
66
+ return (0, function_1.pipe)(ThumbnailsValidator.validate(maybeThumbnails, ctx), (0, Either_1.map)((thumbnails) => {
67
+ var _a;
68
+ return ({
69
+ ...encodeImageField(mainImage, mainImage, (_a = field === null || field === void 0 ? void 0 : field.config) === null || _a === void 0 ? void 0 : _a.constraint),
70
+ thumbnails: encodeThumbnails(mainImage, thumbnails, field),
71
+ });
72
+ }));
43
73
  }));
44
74
  }, t.identity);
45
- exports.ImportImage = (0, ImportContent_1.ImportContent)("Image", (0, validators_1.NullOrElse)(ImageFieldWithThumbnails));
75
+ const ImportImage = (field) => (0, ImportContent_1.ImportContent)("Image", (0, validators_1.NullOrElse)(ImageFieldWithThumbnails(field)));
76
+ exports.ImportImage = ImportImage;
@@ -25,12 +25,42 @@ 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
- } | null;
31
- }, {
30
+ type: string;
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
+ }, ({
32
47
  embed_url: string;
33
- } | undefined, unknown> | import("io-ts").Type<{
48
+ type: string;
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<{
34
64
  type: "GeoPoint";
35
65
  value: {
36
66
  latitude: number;
@@ -41,67 +71,11 @@ export declare const ImportNestable: {
41
71
  longitude: number;
42
72
  } | undefined, unknown> | import("io-ts").Type<{
43
73
  type: "Image";
44
- value: ({
45
- id: string;
46
- } & {
47
- edit?: {
48
- x: number;
49
- y: number;
50
- width: number;
51
- height: number;
52
- zoom: number;
53
- background: string;
54
- };
55
- credit?: string | null;
56
- alt?: string | null;
57
- } & {
58
- thumbnails: {
59
- [x: string]: {
60
- id: string;
61
- } & {
62
- edit?: {
63
- x: number;
64
- y: number;
65
- width: number;
66
- height: number;
67
- zoom: number;
68
- background: string;
69
- };
70
- credit?: string | null;
71
- alt?: string | null;
72
- };
73
- };
74
+ value: (import("./Image").ImageField & {
75
+ thumbnails: Record<string, import("./Image").ImageField>;
74
76
  }) | null;
75
- }, ({
76
- id: string;
77
- } & {
78
- edit?: {
79
- x: number;
80
- y: number;
81
- width: number;
82
- height: number;
83
- zoom: number;
84
- background: string;
85
- };
86
- credit?: string | null;
87
- alt?: string | null;
88
- } & {
89
- thumbnails: {
90
- [x: string]: {
91
- id: string;
92
- } & {
93
- edit?: {
94
- x: number;
95
- y: number;
96
- width: number;
97
- height: number;
98
- zoom: number;
99
- background: string;
100
- };
101
- credit?: string | null;
102
- alt?: string | null;
103
- };
104
- };
77
+ }, (import("./Image").ImageField & {
78
+ thumbnails: Record<string, import("./Image").ImageField>;
105
79
  }) | undefined, unknown> | import("io-ts").Type<{
106
80
  type: "Link";
107
81
  value: ({
@@ -159,36 +133,8 @@ export declare const ImportNestable: {
159
133
  } | null;
160
134
  }> | import("fp-ts/lib/Either").Right<{
161
135
  type: "Image";
162
- value: ({
163
- id: string;
164
- } & {
165
- edit?: {
166
- x: number;
167
- y: number;
168
- width: number;
169
- height: number;
170
- zoom: number;
171
- background: string;
172
- };
173
- credit?: string | null;
174
- alt?: string | null;
175
- } & {
176
- thumbnails: {
177
- [x: string]: {
178
- id: string;
179
- } & {
180
- edit?: {
181
- x: number;
182
- y: number;
183
- width: number;
184
- height: number;
185
- zoom: number;
186
- background: string;
187
- };
188
- credit?: string | null;
189
- alt?: string | null;
190
- };
191
- };
136
+ value: (import("./Image").ImageField & {
137
+ thumbnails: Record<string, import("./Image").ImageField>;
192
138
  }) | null;
193
139
  }> | import("fp-ts/lib/Either").Right<{
194
140
  type: "GeoPoint";
@@ -198,9 +144,24 @@ export declare const ImportNestable: {
198
144
  } | null;
199
145
  }> | import("fp-ts/lib/Either").Right<{
200
146
  type: "Embed";
201
- value: {
147
+ value: ({
202
148
  embed_url: string;
203
- } | null;
149
+ type: string;
150
+ } & {
151
+ version?: string | number | null;
152
+ title?: string;
153
+ author_name?: string | null;
154
+ author_url?: string | null;
155
+ provider_name?: string | null;
156
+ provider_url?: string | null;
157
+ cache_age?: string | number | null;
158
+ thumbnail_url?: string | null;
159
+ thumbnail_width?: number | null;
160
+ thumbnail_height?: number | null;
161
+ html?: string | null;
162
+ } & {
163
+ all: unknown;
164
+ }) | null;
204
165
  }> | import("fp-ts/lib/Either").Right<{
205
166
  type: "Date";
206
167
  value: Date | null;
@@ -24,7 +24,7 @@ exports.ImportNestable = {
24
24
  Embed_1.ImportEmbed.is(u) ||
25
25
  Link_1.ImportLink.is(u) ||
26
26
  GeoPoint_1.ImportGeoPoint.is(u) ||
27
- Image_1.ImportImage.is(u));
27
+ (0, Image_1.ImportImage)().is(u));
28
28
  },
29
29
  decode: (field) => {
30
30
  return (content) => {
@@ -49,7 +49,7 @@ exports.ImportNestable = {
49
49
  case "Link":
50
50
  return Link_1.ImportLink;
51
51
  case "Image":
52
- return Image_1.ImportImage;
52
+ return (0, Image_1.ImportImage)(field);
53
53
  case "GeoPoint":
54
54
  return GeoPoint_1.ImportGeoPoint;
55
55
  default:
@@ -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) => O): Record<string, O>;
8
+ export declare function mapValues<T, O>(record: Record<string, T>, fn: (value: T, key: string) => O): Record<string, O>;
@@ -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, key) }), {});
41
41
  }
42
42
  exports.mapValues = mapValues;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-alpha.7",
3
+ "version": "2.2.0-alpha.9",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -1,5 +1,7 @@
1
+ export type AssetId = string
2
+
1
3
  export type Asset = {
2
- id: string
4
+ id: AssetId
3
5
  last_modified: string
4
6
  kind: "image" | "all"
5
7
  filename?: string
@@ -15,8 +17,10 @@ export type Asset = {
15
17
  }
16
18
 
17
19
  export const getAssetOrThrow =
18
- (assets: Record<string, Asset>) => (assetId: string) => {
20
+ (assets: Record<AssetId, Asset>) => (assetId: AssetId) => {
19
21
  const asset = assets[assetId]
20
- if (!asset) throw new Error(`Missing asset with id '${assetId}'`)
22
+ if (!asset) {
23
+ throw new Error(`Missing asset with id '${assetId}'!`)
24
+ }
21
25
  return asset
22
26
  }
@@ -1,3 +1,2 @@
1
1
  export * from "./Asset"
2
- export * from "./Embed"
3
2
  export * from "./WidgetKey"
@@ -8,6 +8,7 @@ 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"
11
12
 
12
13
  export const CustomTypeFormat = {
13
14
  page: "page",
@@ -194,3 +195,15 @@ export function filterMissingSharedSlices(
194
195
  return widget
195
196
  })
196
197
  }
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,5 +1,4 @@
1
- import type { Asset } from "../../common"
2
- import type { Embed } from "../../common/Embed"
1
+ import type { Asset, AssetId } from "../../common"
3
2
  import type { Document, WidgetContent } from "../../content"
4
3
  import type { ImportDocument } from "../validators"
5
4
  import type { ImportField } from "../validators/fields/ImportField"
@@ -7,12 +6,11 @@ import { convertNestableWidget, uidConverter } from "./fields"
7
6
 
8
7
  export function convertImportToContent(
9
8
  document: ImportDocument,
10
- assets: Record<string, Asset>,
11
- embeds: Record<string, Embed>,
9
+ assets: Record<AssetId, Asset>,
12
10
  ): Document {
13
11
  return Object.entries(document).reduce<Document>(
14
12
  (acc, [fieldKey, fieldValue]) => {
15
- const newFieldValue = convertWidget(fieldValue, assets, embeds)
13
+ const newFieldValue = convertWidget(fieldValue, assets)
16
14
  return newFieldValue ? { ...acc, [fieldKey]: newFieldValue } : acc
17
15
  },
18
16
  {},
@@ -21,13 +19,12 @@ export function convertImportToContent(
21
19
 
22
20
  function convertWidget(
23
21
  field: ImportField,
24
- assets: Record<string, Asset>,
25
- embeds: Record<string, Embed>,
22
+ assets: Record<AssetId, Asset>,
26
23
  ): WidgetContent | undefined {
27
24
  switch (field.type) {
28
25
  case "UID":
29
26
  return uidConverter(field.value)
30
27
  default:
31
- return convertNestableWidget(field, assets, embeds)
28
+ return convertNestableWidget(field, assets)
32
29
  }
33
30
  }
@@ -1,37 +1,15 @@
1
- import type { Embed } from "../../../../common"
2
- import { getEmbedOrThrow } from "../../../../common"
3
1
  import type { EmbedContent } from "../../../../content"
4
- import { withOptionals } from "../../../../utils/Objects"
5
2
  import type { ImportEmbed } from "../../../validators"
6
3
 
7
4
  export const embedConverter = (
8
5
  field: ImportEmbed["value"],
9
- embeds: Record<string, Embed>,
10
6
  ): EmbedContent | undefined => {
11
7
  if (field === null) return
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
- )
8
+
9
+ return {
10
+ ...field,
11
+ __TYPE__: "EmbedContent",
12
+ }
13
+
14
+ return
37
15
  }
@@ -1,53 +1,73 @@
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 { mapValues, withOptionals } from "../../../../utils/Objects"
4
+ import { withOptionals } from "../../../../utils/Objects"
4
5
  import type { ImageField, ImportImage } from "../../../validators"
5
6
 
6
- function convertImage(field: ImageField, image: Asset) {
7
+ function convertImage(imageField: ImageField, image: Asset) {
7
8
  return withOptionals<Omit<ImageContent, "__TYPE__" | "thumbnails">>(
8
9
  {
9
10
  origin: {
10
- id: field.id,
11
+ id: image.id,
11
12
  url: image.origin_url,
13
+ // All images returned form Asset API should have width and height properties.
12
14
  width: image.width ?? 0,
13
15
  height: image.height ?? 0,
14
16
  },
15
- width: field.edit?.width ?? image.width ?? 0,
16
- height: field.edit?.height ?? image.height ?? 0,
17
+ // All images returned form Asset API should have width and height properties.
18
+ // Image width and height will be only applied if there are no constraints and user didn't overwrite it
19
+ width: imageField?.dimensions?.width ?? image.width ?? 0,
20
+ height: imageField?.dimensions?.height ?? image.height ?? 0,
21
+ // If edit is not provided, we crop constraint width and height from the left upper corner.
22
+ // WARN: If constraints are greater than image dimensions cut outside the image (background will fill extra space).
17
23
  edit: {
18
- zoom: field.edit?.zoom ?? 1,
24
+ zoom: imageField.edit?.zoom ?? 1,
19
25
  crop: {
20
- x: field.edit?.x ?? 0,
21
- y: field.edit?.y ?? 0,
26
+ x: imageField.edit?.x ?? 0,
27
+ y: imageField.edit?.y ?? 0,
22
28
  },
23
- background: field.edit?.background ?? "transparent",
29
+ background:
30
+ imageField.edit?.background ??
31
+ (image.extension === "png" ? "transparent" : "#ffffff"),
24
32
  },
25
33
  url: image.url,
26
34
  },
27
35
  [
28
- ["alt", field.alt || image.alt],
29
- ["credits", field.credit || image.credits],
36
+ ["alt", imageField?.alt || image.alt],
37
+ ["credits", imageField?.credit || image.credits],
30
38
  ],
31
39
  )
32
40
  }
33
41
 
42
+ function convertThumbnails(
43
+ imageField: Exclude<ImportImage["value"], null>,
44
+ assets: Record<AssetId, Asset>,
45
+ ) {
46
+ return Object.entries(imageField.thumbnails).reduce(
47
+ (acc, [thumbnailName, thumbnail]) => {
48
+ return {
49
+ ...acc,
50
+ [thumbnailName]: convertImage(
51
+ thumbnail,
52
+ getAssetOrThrow(assets)(thumbnail.id),
53
+ ),
54
+ }
55
+ },
56
+ {},
57
+ )
58
+ }
59
+
60
+ // All assets from `imageField` must be present in `assets`
61
+ // If not then function will throw an error
34
62
  export const imageConverter = (
35
- field: ImportImage["value"],
36
- assets: Record<string, Asset>,
63
+ imageField: ImportImage["value"],
64
+ assets: Record<AssetId, Asset>,
37
65
  ): ImageContent | undefined => {
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
42
- }
43
-
44
- if (!field) return
66
+ if (!imageField) return
45
67
 
46
68
  return {
47
- ...convertImage(field, getImageById(field.id)),
48
- thumbnails: mapValues(field.thumbnails, (thumbnail) =>
49
- convertImage(thumbnail, getImageById(thumbnail.id)),
50
- ),
51
- __TYPE__: "ImageContent",
69
+ ...convertImage(imageField, getAssetOrThrow(assets)(imageField.id)),
70
+ thumbnails: convertThumbnails(imageField, assets),
71
+ __TYPE__: "ImageContent" as const,
52
72
  }
53
73
  }
@@ -1,5 +1,4 @@
1
- import type { Asset } from "../../../../common"
2
- import type { Embed } from "../../../../common/Embed"
1
+ import type { Asset, AssetId } from "../../../../common"
3
2
  import type { WidgetContent } from "../../../../content"
4
3
  import type { ImportNestable } from "../../../validators"
5
4
  import {
@@ -18,8 +17,7 @@ import {
18
17
 
19
18
  export function convertNestableWidget(
20
19
  field: ImportNestable,
21
- assets: Record<string, Asset>,
22
- embeds: Record<string, Embed>,
20
+ assets: Record<AssetId, Asset>,
23
21
  ): WidgetContent | undefined {
24
22
  switch (field.type) {
25
23
  case "Boolean":
@@ -37,7 +35,7 @@ export function convertNestableWidget(
37
35
  case "Timestamp":
38
36
  return timestampConverter(field.value)
39
37
  case "Embed":
40
- return embedConverter(field.value, embeds)
38
+ return embedConverter(field.value)
41
39
  case "GeoPoint":
42
40
  return geopointConverter(field.value)
43
41
  case "Link":
@@ -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,