@prismicio/types-internal 2.2.0-alpha.4 → 2.2.0-alpha.5

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.
@@ -13,3 +13,4 @@ export declare type Asset = {
13
13
  credits?: string;
14
14
  alt?: string;
15
15
  };
16
+ export declare const getAssetOrThrow: (assets: Record<string, Asset>) => (assetId: string) => Asset;
@@ -1,2 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getAssetOrThrow = void 0;
4
+ const getAssetOrThrow = (assets) => (assetId) => {
5
+ const asset = assets[assetId];
6
+ if (!asset)
7
+ throw new Error(`Missing asset with id '${assetId}'`);
8
+ return asset;
9
+ };
10
+ exports.getAssetOrThrow = getAssetOrThrow;
@@ -1,3 +1,4 @@
1
+ import type { Asset } from "../../../../common";
1
2
  import type { LinkContent } from "../../../../content";
2
3
  import type { ImportLink } from "../../../validators";
3
- export declare const linkConverter: (field: ImportLink["value"]) => LinkContent | undefined;
4
+ export declare const linkConverter: (field: ImportLink["value"], assets: Record<string, Asset>) => LinkContent | undefined;
@@ -1,31 +1,70 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.linkConverter = void 0;
4
- const linkConverter = (field) => {
4
+ const common_1 = require("../../../../common");
5
+ const webLinkConverter = (field) => ({
6
+ value: {
7
+ url: field.url,
8
+ target: field.target,
9
+ __TYPE__: "ExternalLink",
10
+ },
11
+ __TYPE__: "LinkContent",
12
+ });
13
+ const documentLinkConverter = (field) => ({
14
+ value: {
15
+ id: field.id,
16
+ __TYPE__: "DocumentLink",
17
+ },
18
+ __TYPE__: "LinkContent",
19
+ });
20
+ const fileLinkConverter = (field, asset) => {
21
+ var _a, _b;
22
+ return ({
23
+ value: {
24
+ id: field.id,
25
+ url: asset.url,
26
+ name: (_a = asset.filename) !== null && _a !== void 0 ? _a : "unknown.jpg",
27
+ kind: asset.kind,
28
+ size: (_b = asset.size) !== null && _b !== void 0 ? _b : "0",
29
+ __TYPE__: "FileLink",
30
+ },
31
+ __TYPE__: "LinkContent",
32
+ });
33
+ };
34
+ const imageLinkConverter = (field, asset) => {
35
+ var _a, _b, _c, _d;
36
+ return ({
37
+ value: {
38
+ id: field.id,
39
+ url: asset.url,
40
+ height: String((_a = asset.height) !== null && _a !== void 0 ? _a : 1),
41
+ width: String((_b = asset.width) !== null && _b !== void 0 ? _b : 1),
42
+ name: (_c = asset.filename) !== null && _c !== void 0 ? _c : "unknown.jpg",
43
+ kind: asset.kind,
44
+ size: (_d = asset.size) !== null && _d !== void 0 ? _d : "0",
45
+ __TYPE__: "ImageLink",
46
+ },
47
+ __TYPE__: "LinkContent",
48
+ });
49
+ };
50
+ const linkConverter = (field, assets) => {
5
51
  if (field === null) {
6
52
  return;
7
53
  }
8
54
  switch (field.link_type) {
9
55
  case "Web":
10
- return {
11
- value: {
12
- url: field.url,
13
- target: field.target,
14
- __TYPE__: "ExternalLink",
15
- },
16
- __TYPE__: "LinkContent",
17
- };
56
+ return webLinkConverter(field);
18
57
  case "Document":
19
- return {
20
- value: {
21
- id: field.id,
22
- __TYPE__: "DocumentLink",
23
- },
24
- __TYPE__: "LinkContent",
25
- };
26
- // TODO: https://linear.app/prismic/issue/AGE-90/[content-validation-and-error-management]-link-to-media
27
- case "Media":
28
- return undefined;
58
+ return documentLinkConverter(field);
59
+ case "Media": {
60
+ const asset = (0, common_1.getAssetOrThrow)(assets)(field.id);
61
+ switch (asset.kind) {
62
+ case "all":
63
+ return fileLinkConverter(field, asset);
64
+ case "image":
65
+ return imageLinkConverter(field, asset);
66
+ }
67
+ }
29
68
  }
30
69
  };
31
70
  exports.linkConverter = linkConverter;
@@ -23,7 +23,7 @@ function convertNestableWidget(field, assets) {
23
23
  case "GeoPoint":
24
24
  return (0, _1.geopointConverter)(field.value);
25
25
  case "Link":
26
- return (0, _1.linkConverter)(field.value);
26
+ return (0, _1.linkConverter)(field.value, assets);
27
27
  case "Image":
28
28
  return (0, _1.imageConverter)(field.value, assets);
29
29
  default:
@@ -1,5 +1,22 @@
1
1
  import type { TypeOf } from "io-ts";
2
2
  import * as t from "io-ts";
3
+ declare const WebLink: t.IntersectionC<[t.TypeC<{
4
+ link_type: t.LiteralC<"Web">;
5
+ url: t.StringC;
6
+ }>, t.PartialC<{
7
+ target: t.StringC;
8
+ }>]>;
9
+ export declare type WebLink = t.TypeOf<typeof WebLink>;
10
+ declare const DocumentLink: t.TypeC<{
11
+ link_type: t.LiteralC<"Document">;
12
+ id: t.Type<string, string, unknown>;
13
+ }>;
14
+ export declare type DocumentLink = t.TypeOf<typeof DocumentLink>;
15
+ declare const MediaLink: t.TypeC<{
16
+ link_type: t.LiteralC<"Media">;
17
+ id: t.StringC;
18
+ }>;
19
+ export declare type MediaLink = t.TypeOf<typeof MediaLink>;
3
20
  export declare const ImportLink: t.Type<{
4
21
  type: "Link";
5
22
  value: ({
@@ -18,3 +35,4 @@ export declare const ImportLink: t.Type<{
18
35
  link_type: "Document" | "Web" | "Media";
19
36
  } | undefined, unknown>;
20
37
  export declare type ImportLink = TypeOf<typeof ImportLink>;
38
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/types-internal",
3
- "version": "2.2.0-alpha.4",
3
+ "version": "2.2.0-alpha.5",
4
4
  "description": "Prismic types for Custom Types and Prismic Data",
5
5
  "keywords": [
6
6
  "typescript",
@@ -13,3 +13,10 @@ export type Asset = {
13
13
  credits?: string
14
14
  alt?: string
15
15
  }
16
+
17
+ export const getAssetOrThrow =
18
+ (assets: Record<string, Asset>) => (assetId: string) => {
19
+ const asset = assets[assetId]
20
+ if (!asset) throw new Error(`Missing asset with id '${assetId}'`)
21
+ return asset
22
+ }
@@ -1,8 +1,59 @@
1
+ import type { Asset } from "../../../../common"
2
+ import { getAssetOrThrow } from "../../../../common"
1
3
  import type { LinkContent } from "../../../../content"
2
- import type { ImportLink } from "../../../validators"
4
+ import type {
5
+ DocumentLink,
6
+ ImportLink,
7
+ MediaLink,
8
+ WebLink,
9
+ } from "../../../validators"
10
+
11
+ const webLinkConverter = (field: WebLink): LinkContent => ({
12
+ value: {
13
+ url: field.url,
14
+ target: field.target,
15
+ __TYPE__: "ExternalLink",
16
+ },
17
+ __TYPE__: "LinkContent",
18
+ })
19
+
20
+ const documentLinkConverter = (field: DocumentLink): LinkContent => ({
21
+ value: {
22
+ id: field.id,
23
+ __TYPE__: "DocumentLink",
24
+ },
25
+ __TYPE__: "LinkContent",
26
+ })
27
+
28
+ const fileLinkConverter = (field: MediaLink, asset: Asset): LinkContent => ({
29
+ value: {
30
+ id: field.id,
31
+ url: asset.url,
32
+ name: asset.filename ?? "unknown.jpg",
33
+ kind: asset.kind,
34
+ size: asset.size ?? "0",
35
+ __TYPE__: "FileLink",
36
+ },
37
+ __TYPE__: "LinkContent",
38
+ })
39
+
40
+ const imageLinkConverter = (field: MediaLink, asset: Asset): LinkContent => ({
41
+ value: {
42
+ id: field.id,
43
+ url: asset.url,
44
+ height: String(asset.height ?? 1),
45
+ width: String(asset.width ?? 1),
46
+ name: asset.filename ?? "unknown.jpg",
47
+ kind: asset.kind,
48
+ size: asset.size ?? "0",
49
+ __TYPE__: "ImageLink",
50
+ },
51
+ __TYPE__: "LinkContent",
52
+ })
3
53
 
4
54
  export const linkConverter = (
5
55
  field: ImportLink["value"],
56
+ assets: Record<string, Asset>,
6
57
  ): LinkContent | undefined => {
7
58
  if (field === null) {
8
59
  return
@@ -10,24 +61,17 @@ export const linkConverter = (
10
61
 
11
62
  switch (field.link_type) {
12
63
  case "Web":
13
- return {
14
- value: {
15
- url: field.url,
16
- target: field.target,
17
- __TYPE__: "ExternalLink",
18
- },
19
- __TYPE__: "LinkContent",
20
- }
64
+ return webLinkConverter(field)
21
65
  case "Document":
22
- return {
23
- value: {
24
- id: field.id,
25
- __TYPE__: "DocumentLink",
26
- },
27
- __TYPE__: "LinkContent",
66
+ return documentLinkConverter(field)
67
+ case "Media": {
68
+ const asset = getAssetOrThrow(assets)(field.id)
69
+ switch (asset.kind) {
70
+ case "all":
71
+ return fileLinkConverter(field, asset)
72
+ case "image":
73
+ return imageLinkConverter(field, asset)
28
74
  }
29
- // TODO: https://linear.app/prismic/issue/AGE-90/[content-validation-and-error-management]-link-to-media
30
- case "Media":
31
- return undefined
75
+ }
32
76
  }
33
77
  }
@@ -39,7 +39,7 @@ export function convertNestableWidget(
39
39
  case "GeoPoint":
40
40
  return geopointConverter(field.value)
41
41
  case "Link":
42
- return linkConverter(field.value)
42
+ return linkConverter(field.value, assets)
43
43
  case "Image":
44
44
  return imageConverter(field.value, assets)
45
45
  default:
@@ -22,16 +22,19 @@ const WebLink = t.intersection([
22
22
  target: String,
23
23
  }),
24
24
  ])
25
+ export type WebLink = t.TypeOf<typeof WebLink>
25
26
 
26
27
  const DocumentLink = t.type({
27
28
  link_type: t.literal("Document"),
28
29
  id: DocumentId,
29
30
  })
31
+ export type DocumentLink = t.TypeOf<typeof DocumentLink>
30
32
 
31
33
  const MediaLink = t.type({
32
34
  link_type: t.literal("Media"),
33
35
  id: String,
34
36
  })
37
+ export type MediaLink = t.TypeOf<typeof MediaLink>
35
38
 
36
39
  const Link = LinkTypeValidator.pipe(t.union([WebLink, DocumentLink, MediaLink]))
37
40