@prismicio/types-internal 2.2.0-alpha.2 → 2.2.0-alpha.3

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 (56) hide show
  1. package/lib/common/Asset.d.ts +15 -0
  2. package/lib/common/Asset.js +2 -0
  3. package/lib/common/index.d.ts +1 -0
  4. package/lib/common/index.js +1 -0
  5. package/lib/content/Document.d.ts +40 -40
  6. package/lib/content/fields/GroupContent.d.ts +12 -12
  7. package/lib/content/fields/WidgetContent.d.ts +56 -56
  8. package/lib/content/fields/nestable/NestableContent.d.ts +7 -7
  9. package/lib/content/fields/nestable/RichTextContent/Blocks.d.ts +12 -12
  10. package/lib/content/fields/nestable/RichTextContent/index.d.ts +9 -9
  11. package/lib/content/fields/slices/Slice/CompositeSliceContent.d.ts +14 -14
  12. package/lib/content/fields/slices/Slice/RepeatableContent.d.ts +5 -5
  13. package/lib/content/fields/slices/Slice/SharedSliceContent.d.ts +14 -14
  14. package/lib/content/fields/slices/Slice/SimpleSliceContent.d.ts +14 -14
  15. package/lib/content/fields/slices/Slice/index.d.ts +30 -30
  16. package/lib/content/fields/slices/SliceItem.d.ts +30 -30
  17. package/lib/content/fields/slices/SlicesContent.d.ts +42 -42
  18. package/lib/import/converters/Document.d.ts +2 -2
  19. package/lib/import/converters/Document.js +4 -4
  20. package/lib/import/converters/fields/nestable/GeooPoint.d.ts +3 -0
  21. package/lib/import/converters/fields/nestable/GeooPoint.js +15 -0
  22. package/lib/import/converters/fields/nestable/Image.d.ts +2 -16
  23. package/lib/import/converters/fields/nestable/Image.js +11 -11
  24. package/lib/import/converters/fields/nestable/Nestable.d.ts +2 -2
  25. package/lib/import/converters/fields/nestable/Nestable.js +4 -2
  26. package/lib/import/converters/fields/nestable/index.d.ts +1 -0
  27. package/lib/import/converters/fields/nestable/index.js +1 -0
  28. package/lib/import/validators/fields/ImportField.d.ts +105 -6
  29. package/lib/import/validators/fields/nestable/GeoPoint.d.ts +13 -0
  30. package/lib/import/validators/fields/nestable/GeoPoint.js +13 -0
  31. package/lib/import/validators/fields/nestable/Image.d.ts +24 -3
  32. package/lib/import/validators/fields/nestable/Nestable.d.ts +108 -8
  33. package/lib/import/validators/fields/nestable/Nestable.js +7 -3
  34. package/lib/import/validators/fields/nestable/index.d.ts +1 -0
  35. package/lib/import/validators/fields/nestable/index.js +1 -0
  36. package/lib/validators/NumberRange.d.ts +32 -0
  37. package/lib/validators/NumberRange.js +40 -0
  38. package/lib/validators/function.d.ts +20 -0
  39. package/lib/validators/function.js +41 -1
  40. package/lib/validators/index.d.ts +1 -0
  41. package/lib/validators/index.js +3 -1
  42. package/package.json +1 -1
  43. package/src/common/Asset.ts +15 -0
  44. package/src/common/index.ts +1 -0
  45. package/src/import/converters/Document.ts +6 -5
  46. package/src/import/converters/fields/nestable/GeooPoint.ts +16 -0
  47. package/src/import/converters/fields/nestable/Image.ts +7 -22
  48. package/src/import/converters/fields/nestable/Nestable.ts +6 -3
  49. package/src/import/converters/fields/nestable/index.ts +1 -0
  50. package/src/import/validators/fields/nestable/GeoPoint.ts +21 -0
  51. package/src/import/validators/fields/nestable/Image.ts +3 -16
  52. package/src/import/validators/fields/nestable/Nestable.ts +6 -1
  53. package/src/import/validators/fields/nestable/index.ts +1 -0
  54. package/src/validators/NumberRange.ts +51 -0
  55. package/src/validators/function.ts +44 -0
  56. package/src/validators/index.ts +1 -0
@@ -0,0 +1,16 @@
1
+ import type { GeoPointContent } from "../../../../content"
2
+ import type { ImportGeoPoint } from "../../../validators"
3
+
4
+ export const geopointConverter = (
5
+ field: ImportGeoPoint["value"],
6
+ ): GeoPointContent | undefined => {
7
+ if (field === null) return
8
+
9
+ return {
10
+ position: {
11
+ lat: field.latitude,
12
+ lng: field.longitude,
13
+ },
14
+ __TYPE__: "GeoPointContent",
15
+ }
16
+ }
@@ -1,34 +1,19 @@
1
+ import type { Asset } from "../../../../common"
1
2
  import type { ImageContent } from "../../../../content"
2
3
  import { mapValues, withOptionals } from "../../../../utils/Objects"
3
4
  import type { ImageField, ImportImage } from "../../../validators"
4
5
 
5
- export type Asset = {
6
- id: string
7
- last_modified: string
8
- kind: "image" | "all"
9
- filename?: string
10
- extension?: string
11
- size?: string
12
- origin_url: string
13
- url: string
14
- width?: number
15
- height?: number
16
- notes?: string
17
- credits?: string
18
- alt?: string
19
- }
20
-
21
6
  function convertImage(field: ImageField, image: Asset) {
22
7
  return withOptionals<Omit<ImageContent, "__TYPE__" | "thumbnails">>(
23
8
  {
24
9
  origin: {
25
10
  id: field.id,
26
11
  url: image.origin_url,
27
- width: image.width!,
28
- height: image.height!,
12
+ width: image.width ?? 0,
13
+ height: image.height ?? 0,
29
14
  },
30
- width: field.edit?.width ?? image.width!,
31
- height: field.edit?.height ?? image.height!,
15
+ width: field.edit?.width ?? image.width ?? 0,
16
+ height: field.edit?.height ?? image.height ?? 0,
32
17
  edit: {
33
18
  zoom: field.edit?.zoom ?? 1,
34
19
  crop: {
@@ -48,10 +33,10 @@ function convertImage(field: ImageField, image: Asset) {
48
33
 
49
34
  export const imageConverter = (
50
35
  field: ImportImage["value"],
51
- images: Map<string, Asset>,
36
+ assets: Record<string, Asset>,
52
37
  ): ImageContent | undefined => {
53
38
  const getImageById = (id: string): Asset => {
54
- const image = images.get(id)
39
+ const image = assets[id]
55
40
  if (!image) throw new Error(`Missing asset with id '${id}'`)
56
41
  return image
57
42
  }
@@ -1,11 +1,12 @@
1
+ import type { Asset } from "../../../../common"
1
2
  import type { WidgetContent } from "../../../../content"
2
3
  import type { ImportNestable } from "../../../validators"
3
4
  import {
4
- Asset,
5
5
  booleanConverter,
6
6
  colorConverter,
7
7
  dateConverter,
8
8
  embedConverter,
9
+ geopointConverter,
9
10
  imageConverter,
10
11
  numberConverter,
11
12
  selectConverter,
@@ -15,7 +16,7 @@ import {
15
16
 
16
17
  export function convertNestableWidget(
17
18
  field: ImportNestable,
18
- images: Map<string, Asset>,
19
+ assets: Record<string, Asset>,
19
20
  ): WidgetContent | undefined {
20
21
  switch (field.type) {
21
22
  case "Boolean":
@@ -34,8 +35,10 @@ export function convertNestableWidget(
34
35
  return timestampConverter(field.value)
35
36
  case "Embed":
36
37
  return embedConverter(field.value)
38
+ case "GeoPoint":
39
+ return geopointConverter(field.value)
37
40
  case "Image":
38
- return imageConverter(field.value, images)
41
+ return imageConverter(field.value, assets)
39
42
  default:
40
43
  throw new Error(
41
44
  `Unsupported type of nestable converter ${JSON.stringify(field)}`,
@@ -2,6 +2,7 @@ export * from "./Boolean"
2
2
  export * from "./Color"
3
3
  export * from "./Date"
4
4
  export * from "./Embed"
5
+ export * from "./GeooPoint"
5
6
  export * from "./Image"
6
7
  export * from "./Nestable"
7
8
  export * from "./Number"
@@ -0,0 +1,21 @@
1
+ import type { TypeOf } from "io-ts"
2
+ import * as t from "io-ts"
3
+
4
+ import { EmptyObjectOrElse, NumberRange } from "../../../../validators"
5
+ import { withFallbackMessage } from "../../../../validators/function"
6
+ import { ImportContent } from "../ImportContent"
7
+
8
+ const GeooPoint = withFallbackMessage(
9
+ t.strict({
10
+ latitude: NumberRange(-90, 90, "latitude"),
11
+ longitude: NumberRange(-180, 180, "longitude"),
12
+ }),
13
+ () =>
14
+ "GeoPoint must be an object with the properties `latitude` and `longitude` between the given ranges",
15
+ )
16
+
17
+ export const ImportGeoPoint = ImportContent(
18
+ "GeoPoint",
19
+ EmptyObjectOrElse(GeooPoint),
20
+ )
21
+ export type ImportGeoPoint = TypeOf<typeof ImportGeoPoint>
@@ -31,24 +31,11 @@ const ImageFieldCodec = AnyObject.pipe(
31
31
  )
32
32
 
33
33
  const ThumbnailsCodec = t.record(t.string, ImageFieldCodec)
34
+ type Thumbnails = t.TypeOf<typeof ThumbnailsCodec>
34
35
 
35
- export type ImageField = {
36
- id: string
37
- edit?: {
38
- x: number
39
- y: number
40
- width: number
41
- height: number
42
- zoom: number
43
- background: string
44
- }
45
- alt?: string | null
46
- credit?: string | null
47
- }
36
+ export type ImageField = t.TypeOf<typeof ImageFieldCodec>
48
37
 
49
- type ImageFieldWithThumbnails = ImageField & {
50
- thumbnails: Record<string, ImageField>
51
- }
38
+ type ImageFieldWithThumbnails = ImageField & { thumbnails: Thumbnails }
52
39
 
53
40
  const ImageFieldWithThumbnails = new t.Type(
54
41
  "ImageFieldWithThumbnails",
@@ -1,8 +1,9 @@
1
1
  import type { NestableWidget } from "../../../../customtypes"
2
- import { ImportBoolean } from "."
2
+ import { ImportBoolean } from "./Boolean"
3
3
  import { ImportColor } from "./Color"
4
4
  import { ImportDate } from "./Date"
5
5
  import { ImportEmbed } from "./Embed"
6
+ import { ImportGeoPoint } from "./GeoPoint"
6
7
  import { ImportImage } from "./Image"
7
8
  import { ImportNumber } from "./Number"
8
9
  import { ImportSelect } from "./Select"
@@ -18,6 +19,7 @@ export type ImportNestable =
18
19
  | ImportDate
19
20
  | ImportTimestamp
20
21
  | ImportEmbed
22
+ | ImportGeoPoint
21
23
  | ImportImage
22
24
 
23
25
  export const ImportNestable = {
@@ -31,6 +33,7 @@ export const ImportNestable = {
31
33
  ImportDate.is(u) ||
32
34
  ImportTimestamp.is(u) ||
33
35
  ImportEmbed.is(u) ||
36
+ ImportGeoPoint.is(u) ||
34
37
  ImportImage.is(u)
35
38
  )
36
39
  },
@@ -56,6 +59,8 @@ export const ImportNestable = {
56
59
  return ImportImage
57
60
  case "Embed":
58
61
  return ImportEmbed
62
+ case "GeoPoint":
63
+ return ImportGeoPoint
59
64
  default:
60
65
  throw new Error(`Unsupported type of nestable field ${field.type}`)
61
66
  }
@@ -2,6 +2,7 @@ export * from "./Boolean"
2
2
  export * from "./Color"
3
3
  export * from "./Date"
4
4
  export * from "./Embed"
5
+ export * from "./GeoPoint"
5
6
  export * from "./Image"
6
7
  export * from "./Nestable"
7
8
  export * from "./Number"
@@ -0,0 +1,51 @@
1
+ import * as t from "io-ts"
2
+
3
+ import { Number } from "./BasicTypes"
4
+
5
+ /**
6
+ * Creates a custom runtime type for validating that a number falls within a specified range.
7
+ *
8
+ * @param min - The minimum value of the range.
9
+ * @param max - The maximum value of the range.
10
+ * @param fieldName - The name of the field being validated (used in error messages).
11
+ * @returns A runtime type representing the number range validation.
12
+ *
13
+ * * @example
14
+ * // Creating a custom runtime type for age validation
15
+ * const AgeType = numberInRange(18, 99, 'Age');
16
+ *
17
+ * // Valid age
18
+ * const validAgeResult = AgeType.decode(25);
19
+ * if (t.isRight(validAgeResult)) {
20
+ * console.log('Valid age:', validAgeResult.right); // Output: Valid age: 25
21
+ * } else {
22
+ * console.error('Invalid age:', t.left(validAgeResult).map(t.reporter.report));
23
+ * }
24
+ *
25
+ * @example
26
+ * // Invalid age
27
+ * const invalidAgeResult = AgeType.decode(15);
28
+ * if (t.isRight(invalidAgeResult)) {
29
+ * console.log('Valid age:', invalidAgeResult.right);
30
+ * } else {
31
+ * console.error('Invalid age:', t.left(invalidAgeResult).map(t.reporter.report));
32
+ * }
33
+ */
34
+ export default (min: number, max: number, fieldName: string) =>
35
+ Number.pipe(
36
+ new t.Type<number, number, number>(
37
+ "numberInRange",
38
+ (u: unknown): u is number => Number.is(u),
39
+ (u: number, context) => {
40
+ if (u > max || u < min) {
41
+ return t.failure(
42
+ u,
43
+ context,
44
+ `${fieldName} must be a number between ${min} and ${max}`,
45
+ )
46
+ }
47
+ return t.success(u)
48
+ },
49
+ t.identity,
50
+ ),
51
+ )
@@ -1,6 +1,7 @@
1
1
  import { either } from "fp-ts"
2
2
  import { pipe } from "fp-ts/function"
3
3
  import * as t from "io-ts"
4
+ import { withValidate } from "io-ts-types"
4
5
  import { mapOutput } from "io-ts-types/mapOutput"
5
6
 
6
7
  export function nullable<A, O>(c: t.Type<A, O>) {
@@ -69,3 +70,46 @@ export function addType<A, O extends object, I, T extends string>(
69
70
  ): t.Type<A, O & { __TYPE__: T }, I> {
70
71
  return mapOutput(codec, (o) => ({ ...o, __TYPE__: t } as const))
71
72
  }
73
+
74
+ /**
75
+ * Returns a clone of the given codec that tries to find sub-error with message already set.
76
+ * If there is such error it just returns sub-errors array.
77
+ * If there is no such error it generates new error with given message.
78
+ *
79
+ * @example
80
+ * expect(
81
+ * withFallbackMessage(
82
+ * t.type({age: withMessage(t.number, () => 'Invalid child')}),
83
+ * () => "Invalid parent"
84
+ * )
85
+ * ).toSubsetEqualLeft([{message: "Invalid child"}])
86
+ * expect(
87
+ * withFallbackMessage(
88
+ * t.type({age: t.number}),
89
+ * () => "Invalid parent"
90
+ * )
91
+ * ).toSubsetEqualLeft([{message: "Invalid parent"}])
92
+ */
93
+ export function withFallbackMessage<C extends t.Any>(
94
+ codec: C,
95
+ message: (i: t.InputOf<C>, c: t.Context) => string,
96
+ ): C {
97
+ return withValidate(codec, (i, c) => {
98
+ return either.mapLeft((errors: t.Errors) => {
99
+ if (errors.find((error) => error.message)) {
100
+ return errors
101
+ }
102
+
103
+ return [
104
+ {
105
+ value: i,
106
+ context: c,
107
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
108
+ message: message(i, c),
109
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
110
+ actual: i,
111
+ },
112
+ ]
113
+ })(codec.validate(i, c))
114
+ })
115
+ }
@@ -9,6 +9,7 @@ export { default as IntFromPixels } from "./IntFromPixels"
9
9
  export { default as NonEmptyString } from "./NonEmptyString"
10
10
  export { default as NonEmptyStringOrNull } from "./NonEmptyStringOrNull"
11
11
  export { default as NumberOrNull } from "./NumberOrNull"
12
+ export { default as NumberRange } from "./NumberRange"
12
13
  export { default as StringFromBoolean } from "./StringFromBoolean"
13
14
  export { default as StringFromNumber } from "./StringFromNumber"
14
15
  export { default as StringOrNull } from "./StringOrNull"