@prismicio/client 7.1.1 → 7.3.0
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/dist/createClient.cjs +24 -1
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +7 -0
- package/dist/createClient.js +24 -1
- package/dist/createClient.js.map +1 -1
- package/dist/errors/ForbiddenError.cjs.map +1 -1
- package/dist/errors/ForbiddenError.d.ts +1 -1
- package/dist/errors/ForbiddenError.js.map +1 -1
- package/dist/errors/NotFoundError.cjs.map +1 -1
- package/dist/errors/NotFoundError.d.ts +1 -1
- package/dist/errors/NotFoundError.js.map +1 -1
- package/dist/errors/ParsingError.cjs.map +1 -1
- package/dist/errors/ParsingError.d.ts +1 -1
- package/dist/errors/ParsingError.js.map +1 -1
- package/dist/errors/RefExpiredError.cjs +7 -0
- package/dist/errors/RefExpiredError.cjs.map +1 -0
- package/dist/errors/RefExpiredError.d.ts +8 -0
- package/dist/errors/RefExpiredError.js +7 -0
- package/dist/errors/RefExpiredError.js.map +1 -0
- package/dist/errors/RefNotFoundError.cjs +7 -0
- package/dist/errors/RefNotFoundError.cjs.map +1 -0
- package/dist/errors/RefNotFoundError.d.ts +8 -0
- package/dist/errors/RefNotFoundError.js +7 -0
- package/dist/errors/RefNotFoundError.js.map +1 -0
- package/dist/helpers/asHTML.cjs +43 -50
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.d.ts +31 -6
- package/dist/helpers/asHTML.js +42 -49
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.d.ts +1 -1
- package/dist/helpers/asImageSrc.d.ts +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.d.ts +1 -1
- package/dist/helpers/asImageWidthSrcSet.js +1 -1
- package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/helpers/asLinkAttrs.d.ts +1 -1
- package/dist/helpers/asText.cjs +2 -2
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.js +1 -1
- package/dist/helpers/asText.js.map +1 -1
- package/dist/helpers/isFilled.d.ts +10 -13
- package/dist/index.cjs +8 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +28 -2
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/lib/serializerHelpers.cjs +91 -35
- package/dist/lib/serializerHelpers.cjs.map +1 -1
- package/dist/lib/serializerHelpers.d.ts +8 -8
- package/dist/lib/serializerHelpers.js +91 -35
- package/dist/lib/serializerHelpers.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/richtext/asText.cjs +13 -0
- package/dist/richtext/asText.cjs.map +1 -0
- package/dist/richtext/asText.d.ts +12 -0
- package/dist/richtext/asText.js +13 -0
- package/dist/richtext/asText.js.map +1 -0
- package/dist/richtext/asTree.cjs +123 -0
- package/dist/richtext/asTree.cjs.map +1 -0
- package/dist/richtext/asTree.d.ts +14 -0
- package/dist/richtext/asTree.js +123 -0
- package/dist/richtext/asTree.js.map +1 -0
- package/dist/richtext/composeSerializers.cjs +17 -0
- package/dist/richtext/composeSerializers.cjs.map +1 -0
- package/dist/richtext/composeSerializers.d.ts +16 -0
- package/dist/richtext/composeSerializers.js +17 -0
- package/dist/richtext/composeSerializers.js.map +1 -0
- package/dist/richtext/index.d.ts +7 -0
- package/dist/richtext/serialize.cjs +19 -0
- package/dist/richtext/serialize.cjs.map +1 -0
- package/dist/richtext/serialize.d.ts +19 -0
- package/dist/richtext/serialize.js +19 -0
- package/dist/richtext/serialize.js.map +1 -0
- package/dist/richtext/types.cjs +11 -0
- package/dist/richtext/types.cjs.map +1 -0
- package/dist/richtext/types.d.ts +71 -0
- package/dist/richtext/types.js +11 -0
- package/dist/richtext/types.js.map +1 -0
- package/dist/richtext/wrapMapSerializer.cjs +24 -0
- package/dist/richtext/wrapMapSerializer.cjs.map +1 -0
- package/dist/richtext/wrapMapSerializer.d.ts +15 -0
- package/dist/richtext/wrapMapSerializer.js +24 -0
- package/dist/richtext/wrapMapSerializer.js.map +1 -0
- package/dist/richtext.cjs +15 -0
- package/dist/richtext.cjs.map +1 -0
- package/dist/richtext.js +15 -0
- package/dist/richtext.js.map +1 -0
- package/dist/types/value/image.d.ts +9 -0
- package/dist/types/value/linkToMedia.d.ts +1 -0
- package/dist/types/value/richText.cjs.map +1 -1
- package/dist/types/value/richText.d.ts +7 -0
- package/dist/types/value/richText.js.map +1 -1
- package/dist/types/value/sharedSlice.d.ts +1 -1
- package/dist/types/value/slice.d.ts +1 -1
- package/package.json +34 -15
- package/src/createClient.ts +55 -4
- package/src/errors/ForbiddenError.ts +5 -3
- package/src/errors/NotFoundError.ts +3 -1
- package/src/errors/ParsingError.ts +3 -1
- package/src/errors/RefExpiredError.ts +14 -0
- package/src/errors/RefNotFoundError.ts +14 -0
- package/src/helpers/asHTML.ts +166 -76
- package/src/helpers/asImageWidthSrcSet.ts +2 -0
- package/src/helpers/asText.ts +2 -2
- package/src/index.ts +9 -4
- package/src/lib/serializerHelpers.ts +140 -67
- package/src/richtext/asText.ts +27 -0
- package/src/richtext/asTree.ts +211 -0
- package/src/richtext/composeSerializers.ts +36 -0
- package/src/richtext/index.ts +14 -0
- package/src/richtext/serialize.ts +54 -0
- package/src/richtext/types.ts +153 -0
- package/src/richtext/wrapMapSerializer.ts +46 -0
- package/src/types/value/image.ts +9 -0
- package/src/types/value/linkToMedia.ts +1 -0
- package/src/types/value/richText.ts +7 -0
- package/src/types/value/sharedSlice.ts +1 -1
- package/src/types/value/slice.ts +1 -1
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RTAnyNode,
|
|
3
|
+
RTEmNode,
|
|
4
|
+
RTEmbedNode,
|
|
5
|
+
RTHeading1Node,
|
|
6
|
+
RTHeading2Node,
|
|
7
|
+
RTHeading3Node,
|
|
8
|
+
RTHeading4Node,
|
|
9
|
+
RTHeading5Node,
|
|
10
|
+
RTHeading6Node,
|
|
11
|
+
RTImageNode,
|
|
12
|
+
RTLabelNode,
|
|
13
|
+
RTLinkNode,
|
|
14
|
+
RTListItemNode,
|
|
15
|
+
RTListNode,
|
|
16
|
+
RTOListItemNode,
|
|
17
|
+
RTOListNode,
|
|
18
|
+
RTParagraphNode,
|
|
19
|
+
RTPreformattedNode,
|
|
20
|
+
RTSpanNode,
|
|
21
|
+
RTStrongNode,
|
|
22
|
+
RichTextNodeType,
|
|
23
|
+
} from "../types/value/richText";
|
|
24
|
+
|
|
25
|
+
// Serializers
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Serializes a node from a rich text or title field with a function
|
|
29
|
+
*
|
|
30
|
+
* @typeParam ReturnType - Return type of the function serializer
|
|
31
|
+
*
|
|
32
|
+
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
|
|
33
|
+
*/
|
|
34
|
+
export type RichTextFunctionSerializer<ReturnType> = (
|
|
35
|
+
type: (typeof RichTextNodeType)[keyof typeof RichTextNodeType],
|
|
36
|
+
node: RTAnyNode,
|
|
37
|
+
text: string | undefined,
|
|
38
|
+
children: ReturnType[],
|
|
39
|
+
key: string,
|
|
40
|
+
) => ReturnType | null | undefined;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Map serializer's tag function serializer, can be helpful for typing those
|
|
44
|
+
* handlers
|
|
45
|
+
*
|
|
46
|
+
* @typeParam ReturnType - Return type of the tag serializer
|
|
47
|
+
*/
|
|
48
|
+
export type RichTextMapSerializerFunction<
|
|
49
|
+
ReturnType,
|
|
50
|
+
Node extends RTAnyNode = RTAnyNode,
|
|
51
|
+
TextType = string | undefined,
|
|
52
|
+
> = (payload: {
|
|
53
|
+
type: Node["type"];
|
|
54
|
+
node: Node;
|
|
55
|
+
text: TextType;
|
|
56
|
+
children: ReturnType[];
|
|
57
|
+
key: string;
|
|
58
|
+
}) => ReturnType | null | undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Serializes a node from a rich text or title field with a map
|
|
62
|
+
*
|
|
63
|
+
* @remarks
|
|
64
|
+
* This type of serializer needs to be processed through
|
|
65
|
+
* {@link wrapMapSerializer} before being used with {@link serialize}
|
|
66
|
+
*
|
|
67
|
+
* @typeParam ReturnType - Return type of the map serializer
|
|
68
|
+
*
|
|
69
|
+
* @see Templating rich text and title fields from Prismic {@link https://prismic.io/docs/technologies/templating-rich-text-and-title-fields-javascript}
|
|
70
|
+
*/
|
|
71
|
+
export type RichTextMapSerializer<ReturnType> = {
|
|
72
|
+
heading1?: RichTextMapSerializerFunction<
|
|
73
|
+
ReturnType,
|
|
74
|
+
RTHeading1Node,
|
|
75
|
+
undefined
|
|
76
|
+
>;
|
|
77
|
+
heading2?: RichTextMapSerializerFunction<
|
|
78
|
+
ReturnType,
|
|
79
|
+
RTHeading2Node,
|
|
80
|
+
undefined
|
|
81
|
+
>;
|
|
82
|
+
heading3?: RichTextMapSerializerFunction<
|
|
83
|
+
ReturnType,
|
|
84
|
+
RTHeading3Node,
|
|
85
|
+
undefined
|
|
86
|
+
>;
|
|
87
|
+
heading4?: RichTextMapSerializerFunction<
|
|
88
|
+
ReturnType,
|
|
89
|
+
RTHeading4Node,
|
|
90
|
+
undefined
|
|
91
|
+
>;
|
|
92
|
+
heading5?: RichTextMapSerializerFunction<
|
|
93
|
+
ReturnType,
|
|
94
|
+
RTHeading5Node,
|
|
95
|
+
undefined
|
|
96
|
+
>;
|
|
97
|
+
heading6?: RichTextMapSerializerFunction<
|
|
98
|
+
ReturnType,
|
|
99
|
+
RTHeading6Node,
|
|
100
|
+
undefined
|
|
101
|
+
>;
|
|
102
|
+
paragraph?: RichTextMapSerializerFunction<
|
|
103
|
+
ReturnType,
|
|
104
|
+
RTParagraphNode,
|
|
105
|
+
undefined
|
|
106
|
+
>;
|
|
107
|
+
preformatted?: RichTextMapSerializerFunction<
|
|
108
|
+
ReturnType,
|
|
109
|
+
RTPreformattedNode,
|
|
110
|
+
undefined
|
|
111
|
+
>;
|
|
112
|
+
strong?: RichTextMapSerializerFunction<ReturnType, RTStrongNode, string>;
|
|
113
|
+
em?: RichTextMapSerializerFunction<ReturnType, RTEmNode, string>;
|
|
114
|
+
listItem?: RichTextMapSerializerFunction<
|
|
115
|
+
ReturnType,
|
|
116
|
+
RTListItemNode,
|
|
117
|
+
undefined
|
|
118
|
+
>;
|
|
119
|
+
oListItem?: RichTextMapSerializerFunction<
|
|
120
|
+
ReturnType,
|
|
121
|
+
RTOListItemNode,
|
|
122
|
+
undefined
|
|
123
|
+
>;
|
|
124
|
+
list?: RichTextMapSerializerFunction<ReturnType, RTListNode, undefined>;
|
|
125
|
+
oList?: RichTextMapSerializerFunction<ReturnType, RTOListNode, undefined>;
|
|
126
|
+
image?: RichTextMapSerializerFunction<ReturnType, RTImageNode, undefined>;
|
|
127
|
+
embed?: RichTextMapSerializerFunction<ReturnType, RTEmbedNode, undefined>;
|
|
128
|
+
hyperlink?: RichTextMapSerializerFunction<ReturnType, RTLinkNode, string>;
|
|
129
|
+
label?: RichTextMapSerializerFunction<ReturnType, RTLabelNode, string>;
|
|
130
|
+
span?: RichTextMapSerializerFunction<ReturnType, RTSpanNode, string>;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Tree
|
|
134
|
+
export interface Tree {
|
|
135
|
+
key: string;
|
|
136
|
+
children: TreeNode[];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface TreeNode {
|
|
140
|
+
key: string;
|
|
141
|
+
type: (typeof RichTextNodeType)[keyof typeof RichTextNodeType];
|
|
142
|
+
text?: string;
|
|
143
|
+
node: RTAnyNode;
|
|
144
|
+
children: TreeNode[];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Helpers
|
|
148
|
+
export const RichTextReversedNodeType = {
|
|
149
|
+
[RichTextNodeType.listItem]: "listItem",
|
|
150
|
+
[RichTextNodeType.oListItem]: "oListItem",
|
|
151
|
+
[RichTextNodeType.list]: "list",
|
|
152
|
+
[RichTextNodeType.oList]: "oList",
|
|
153
|
+
} as const;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RichTextFunctionSerializer,
|
|
3
|
+
RichTextMapSerializer,
|
|
4
|
+
RichTextReversedNodeType,
|
|
5
|
+
} from "./types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Wraps a map serializer into a regular function serializer
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This is a low level helper mainly intended to be used by higher level
|
|
12
|
+
* packages Most users aren't expected to this function directly
|
|
13
|
+
*
|
|
14
|
+
* @typeParam SerializerReturnType - Return type of the map serializer
|
|
15
|
+
*
|
|
16
|
+
* @param mapSerializer - Map serializer to wrap
|
|
17
|
+
*
|
|
18
|
+
* @returns A regular function serializer
|
|
19
|
+
*/
|
|
20
|
+
export const wrapMapSerializer = <SerializerReturnType>(
|
|
21
|
+
mapSerializer: RichTextMapSerializer<SerializerReturnType>,
|
|
22
|
+
): RichTextFunctionSerializer<SerializerReturnType> => {
|
|
23
|
+
return (type, node, text, children, key) => {
|
|
24
|
+
const tagSerializer: RichTextMapSerializer<SerializerReturnType>[keyof RichTextMapSerializer<SerializerReturnType>] =
|
|
25
|
+
mapSerializer[
|
|
26
|
+
(RichTextReversedNodeType[
|
|
27
|
+
type as keyof typeof RichTextReversedNodeType
|
|
28
|
+
] || type) as keyof RichTextMapSerializer<SerializerReturnType>
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
if (tagSerializer) {
|
|
32
|
+
return tagSerializer({
|
|
33
|
+
// @ts-expect-error cannot type check here
|
|
34
|
+
type,
|
|
35
|
+
// @ts-expect-error cannot type check here
|
|
36
|
+
node,
|
|
37
|
+
// @ts-expect-error cannot type check here
|
|
38
|
+
text,
|
|
39
|
+
// @ts-expect-error cannot type check here
|
|
40
|
+
children,
|
|
41
|
+
// @ts-expect-error cannot type check here
|
|
42
|
+
key,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
};
|
package/src/types/value/image.ts
CHANGED
|
@@ -12,18 +12,27 @@ export type ImageFieldImage<State extends FieldState = FieldState> =
|
|
|
12
12
|
State extends "empty" ? EmptyImageFieldImage : FilledImageFieldImage;
|
|
13
13
|
|
|
14
14
|
export interface FilledImageFieldImage {
|
|
15
|
+
id: string;
|
|
15
16
|
url: string;
|
|
16
17
|
dimensions: {
|
|
17
18
|
width: number;
|
|
18
19
|
height: number;
|
|
19
20
|
};
|
|
21
|
+
edit: {
|
|
22
|
+
x: number;
|
|
23
|
+
y: number;
|
|
24
|
+
zoom: number;
|
|
25
|
+
background: string;
|
|
26
|
+
};
|
|
20
27
|
alt: string | null;
|
|
21
28
|
copyright: string | null;
|
|
22
29
|
}
|
|
23
30
|
|
|
24
31
|
export interface EmptyImageFieldImage {
|
|
32
|
+
id?: null;
|
|
25
33
|
url?: null;
|
|
26
34
|
dimensions?: null;
|
|
35
|
+
edit?: null;
|
|
27
36
|
alt?: null;
|
|
28
37
|
copyright?: null;
|
|
29
38
|
}
|
|
@@ -153,6 +153,7 @@ export interface RTLabelNode extends RTSpanNodeBase {
|
|
|
153
153
|
*/
|
|
154
154
|
export type RTImageNode = {
|
|
155
155
|
type: typeof RichTextNodeType.image;
|
|
156
|
+
id: string;
|
|
156
157
|
url: string;
|
|
157
158
|
alt: string | null;
|
|
158
159
|
copyright: string | null;
|
|
@@ -160,6 +161,12 @@ export type RTImageNode = {
|
|
|
160
161
|
width: number;
|
|
161
162
|
height: number;
|
|
162
163
|
};
|
|
164
|
+
edit: {
|
|
165
|
+
x: number;
|
|
166
|
+
y: number;
|
|
167
|
+
zoom: number;
|
|
168
|
+
background: string;
|
|
169
|
+
};
|
|
163
170
|
linkTo?:
|
|
164
171
|
| FilledContentRelationshipField
|
|
165
172
|
| FilledLinkToWebField
|