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

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 +31 -21
  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 +46 -31
  20. package/lib/import/validators/fields/nestable/Image.js +51 -24
  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 +98 -47
  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
@@ -1,8 +1,14 @@
1
- import * as Either from "fp-ts/Either"
1
+ import * as Either from "fp-ts/lib/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"
4
5
 
5
6
  import { EmptyObjectOrElse } from "../../../../validators"
7
+ import {
8
+ NumberOrNull,
9
+ String,
10
+ StringOrNull,
11
+ } from "../../../../validators/BasicTypes"
6
12
  import { ImportContent } from "../ImportContent"
7
13
 
8
14
  type URL = string
@@ -32,23 +38,49 @@ const EmbedUrl = new t.Type<URL, URL, unknown>(
32
38
  t.identity,
33
39
  )
34
40
 
35
- const EmbedProto = t.type({
36
- embed_url: EmbedUrl,
37
- })
41
+ const EmbedProto = t.exact(
42
+ t.intersection([
43
+ t.type({
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
+ )
38
68
  type EmbedProto = t.TypeOf<typeof EmbedProto>
39
69
 
40
- type Embed = EmbedProto
41
- const Embed = new t.Type<Embed>(
70
+ type Embed = EmbedProto & {
71
+ all: unknown
72
+ }
73
+ const embedValue = new t.Type<Embed>(
42
74
  "ImportEmbedValue",
43
- (u: unknown): u is Embed => EmbedProto.is(u),
75
+ (u: unknown): u is Embed => EmbedProto.is(u) && "all" in u,
44
76
  (u: unknown) => {
45
77
  return pipe(
46
78
  EmbedProto.decode(u),
47
- Either.map((parsed) => ({ embed_url: parsed.embed_url })),
79
+ Either.map((parsed) => ({ ...parsed, all: u })),
48
80
  )
49
81
  },
50
82
  t.identity,
51
83
  )
52
84
 
53
- export const ImportEmbed = ImportContent("Embed", EmptyObjectOrElse(Embed))
85
+ export const ImportEmbed = ImportContent("Embed", EmptyObjectOrElse(embedValue))
54
86
  export type ImportEmbed = t.TypeOf<typeof ImportEmbed>
@@ -3,23 +3,24 @@ import { pipe } from "fp-ts/lib/function"
3
3
  import type { Validation } from "io-ts"
4
4
  import * as t from "io-ts"
5
5
 
6
- import { withOptionals } from "../../../../utils/Objects"
6
+ import type { Image } from "../../../../customtypes"
7
+ import type ImageConstraint from "../../../../customtypes/widgets/shared/ImageConstraint"
7
8
  import { AnyObject, NullOrElse, Number } from "../../../../validators"
8
- import { String, StringOrNull } from "../../../../validators/BasicTypes"
9
+ import { String, StringOrNull } from "../../../../validators"
9
10
  import { ImportContent } from "../ImportContent"
10
11
 
11
- const ImageFieldCodec = AnyObject.pipe(
12
+ const ImageFieldValidator = AnyObject.pipe(
12
13
  t.intersection([
13
14
  t.type({
14
15
  id: String,
15
16
  }),
16
17
  t.partial({
18
+ width: Number,
19
+ height: Number,
17
20
  edit: AnyObject.pipe(
18
- t.strict({
21
+ t.partial({
19
22
  x: Number,
20
23
  y: Number,
21
- width: Number,
22
- height: Number,
23
24
  zoom: Number,
24
25
  background: String,
25
26
  }),
@@ -30,48 +31,98 @@ const ImageFieldCodec = AnyObject.pipe(
30
31
  ]),
31
32
  )
32
33
 
33
- const ThumbnailsCodec = t.record(t.string, ImageFieldCodec)
34
- type Thumbnails = t.TypeOf<typeof ThumbnailsCodec>
34
+ const ThumbnailsValidator = t.record(t.string, ImageFieldValidator)
35
35
 
36
- export type ImageField = t.TypeOf<typeof ImageFieldCodec>
36
+ export type ImageField = {
37
+ id: string
38
+ width?: number
39
+ height?: number
40
+ edit: {
41
+ x: number
42
+ y: number
43
+ zoom: number
44
+ background?: string
45
+ }
46
+ credit?: string | null
47
+ alt?: string | null
48
+ }
49
+ type ImageFieldWithThumbnails = ImageField & {
50
+ thumbnails: Record<string, ImageField>
51
+ }
37
52
 
38
- type ImageFieldWithThumbnails = ImageField & { thumbnails: Thumbnails }
53
+ const encodeImageField = (
54
+ image: t.TypeOf<typeof ImageFieldValidator> | undefined,
55
+ mainImage: t.TypeOf<typeof ImageFieldValidator>,
56
+ constraints?: ImageConstraint,
57
+ ): ImageField => {
58
+ const background = image?.edit?.background
59
+ const width = constraints?.width ?? image?.width
60
+ const height = constraints?.height ?? image?.height
61
+ const alt = image?.alt
62
+ const credit = image?.credit
39
63
 
40
- const ImageFieldWithThumbnails = new t.Type(
41
- "ImageFieldWithThumbnails",
42
- (u: unknown): u is ImageFieldWithThumbnails =>
43
- ImageFieldCodec.is(u) &&
44
- "thumbnails" in u &&
45
- ThumbnailsCodec.is(u["thumbnails"]),
46
- (u: unknown, ctx): Validation<ImageFieldWithThumbnails> => {
47
- return pipe(
48
- ImageFieldCodec.validate(u, ctx),
49
- chain((proto) => {
50
- const { id, edit, credit, alt, ...maybeThumbnails } = proto
51
- return pipe(
52
- ThumbnailsCodec.validate(maybeThumbnails, ctx),
53
- map((thumbnails) =>
54
- withOptionals<ImageFieldWithThumbnails>(
55
- {
56
- id,
57
- thumbnails,
58
- },
59
- [
60
- ["edit", edit],
61
- ["credit", credit],
62
- ["alt", alt],
63
- ],
64
- ),
65
- ),
66
- )
67
- }),
68
- )
69
- },
70
- t.identity,
71
- )
64
+ return {
65
+ id: image?.id ?? mainImage?.id,
66
+ edit: {
67
+ x: image?.edit?.x ?? 0,
68
+ y: image?.edit?.y ?? 0,
69
+ zoom: image?.edit?.zoom ?? 1,
70
+ ...(background !== undefined ? { background } : {}),
71
+ },
72
+ ...(width !== undefined ? { width } : {}),
73
+ ...(height !== undefined ? { height } : {}),
74
+ ...(alt !== undefined ? { alt } : {}),
75
+ ...(credit !== undefined ? { credit } : {}),
76
+ }
77
+ }
72
78
 
73
- export const ImportImage = ImportContent(
74
- "Image",
75
- NullOrElse(ImageFieldWithThumbnails),
76
- )
77
- export type ImportImage = t.TypeOf<typeof ImportImage>
79
+ const encodeThumbnails = (
80
+ mainImage: t.TypeOf<typeof ImageFieldValidator>,
81
+ thumbnails: t.TypeOf<typeof ThumbnailsValidator>,
82
+ field?: Image,
83
+ ) =>
84
+ field?.config?.thumbnails?.reduce(
85
+ (acc, thumbnail) => ({
86
+ ...acc,
87
+ [thumbnail.name]: encodeImageField(
88
+ thumbnails[thumbnail.name],
89
+ mainImage,
90
+ thumbnail,
91
+ ),
92
+ }),
93
+ {},
94
+ ) ?? {}
95
+
96
+ const ImageFieldWithThumbnails = (field?: Image) =>
97
+ new t.Type<ImageFieldWithThumbnails>(
98
+ "ImageFieldWithThumbnails",
99
+ (u: unknown): u is ImageFieldWithThumbnails =>
100
+ ImageFieldValidator.is(u) &&
101
+ "thumbnails" in u &&
102
+ ThumbnailsValidator.is(u["thumbnails"]),
103
+ (u: unknown, ctx): Validation<ImageFieldWithThumbnails> => {
104
+ return pipe(
105
+ ImageFieldValidator.validate(u, ctx),
106
+ chain((mainImage) => {
107
+ const { id, width, height, edit, credit, alt, ...maybeThumbnails } =
108
+ mainImage
109
+ return pipe(
110
+ ThumbnailsValidator.validate(maybeThumbnails, ctx),
111
+ map((thumbnails) => ({
112
+ ...encodeImageField(
113
+ mainImage,
114
+ mainImage,
115
+ field?.config?.constraint,
116
+ ),
117
+ thumbnails: encodeThumbnails(mainImage, thumbnails, field),
118
+ })),
119
+ )
120
+ }),
121
+ )
122
+ },
123
+ t.identity,
124
+ )
125
+
126
+ export const ImportImage = (field?: Image) =>
127
+ ImportContent("Image", NullOrElse(ImageFieldWithThumbnails(field)))
128
+ export type ImportImage = t.TypeOf<ReturnType<typeof ImportImage>>
@@ -37,7 +37,7 @@ export const ImportNestable = {
37
37
  ImportEmbed.is(u) ||
38
38
  ImportLink.is(u) ||
39
39
  ImportGeoPoint.is(u) ||
40
- ImportImage.is(u)
40
+ ImportImage().is(u)
41
41
  )
42
42
  },
43
43
  decode: (field: NestableWidget) => {
@@ -63,7 +63,7 @@ export const ImportNestable = {
63
63
  case "Link":
64
64
  return ImportLink
65
65
  case "Image":
66
- return ImportImage
66
+ return ImportImage(field)
67
67
  case "GeoPoint":
68
68
  return ImportGeoPoint
69
69
  default:
@@ -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
  }
@@ -1,23 +0,0 @@
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
- }