@prismicio/client 7.0.0-alpha.2 → 7.0.0-alpha.4
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 +39 -18
- package/dist/createClient.cjs.map +1 -1
- package/dist/createClient.d.ts +32 -8
- package/dist/createClient.js +39 -18
- package/dist/createClient.js.map +1 -1
- package/dist/helpers/asHTML.cjs +17 -4
- package/dist/helpers/asHTML.cjs.map +1 -1
- package/dist/helpers/asHTML.d.ts +55 -13
- package/dist/helpers/asHTML.js +17 -4
- package/dist/helpers/asHTML.js.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.cjs +2 -2
- package/dist/helpers/asImagePixelDensitySrcSet.cjs.map +1 -1
- package/dist/helpers/asImagePixelDensitySrcSet.d.ts +2 -2
- package/dist/helpers/asImagePixelDensitySrcSet.js +2 -2
- package/dist/helpers/asImagePixelDensitySrcSet.js.map +1 -1
- package/dist/helpers/asImageSrc.cjs +2 -2
- package/dist/helpers/asImageSrc.cjs.map +1 -1
- package/dist/helpers/asImageSrc.d.ts +2 -2
- package/dist/helpers/asImageSrc.js +2 -2
- package/dist/helpers/asImageSrc.js.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.cjs +2 -2
- package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
- package/dist/helpers/asImageWidthSrcSet.d.ts +2 -2
- package/dist/helpers/asImageWidthSrcSet.js +2 -2
- package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
- package/dist/helpers/asLink.cjs +12 -3
- package/dist/helpers/asLink.cjs.map +1 -1
- package/dist/helpers/asLink.d.ts +55 -15
- package/dist/helpers/asLink.js +12 -3
- package/dist/helpers/asLink.js.map +1 -1
- package/dist/helpers/asText.cjs +11 -2
- package/dist/helpers/asText.cjs.map +1 -1
- package/dist/helpers/asText.d.ts +44 -10
- package/dist/helpers/asText.js +11 -2
- package/dist/helpers/asText.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/package.json +1 -1
- package/src/createClient.ts +105 -31
- package/src/helpers/asHTML.ts +99 -25
- package/src/helpers/asImagePixelDensitySrcSet.ts +3 -3
- package/src/helpers/asImageSrc.ts +3 -3
- package/src/helpers/asImageWidthSrcSet.ts +3 -3
- package/src/helpers/asLink.ts +102 -18
- package/src/helpers/asText.ts +71 -12
|
@@ -65,7 +65,7 @@ type AsImagePixelDensitySrcSetReturnType<
|
|
|
65
65
|
*
|
|
66
66
|
* @param field - Image field (or one of its responsive views) from which to get
|
|
67
67
|
* an image URL.
|
|
68
|
-
* @param
|
|
68
|
+
* @param config - An object of Imgix URL API parameters. The `pixelDensities`
|
|
69
69
|
* parameter defines the resulting `srcset` widths.
|
|
70
70
|
*
|
|
71
71
|
* @returns A `srcset` attribute value for the Image field with Imgix URL
|
|
@@ -76,12 +76,12 @@ export const asImagePixelDensitySrcSet = <
|
|
|
76
76
|
Field extends ImageFieldImage | null | undefined,
|
|
77
77
|
>(
|
|
78
78
|
field: Field,
|
|
79
|
-
|
|
79
|
+
config: AsImagePixelDensitySrcSetConfig = {},
|
|
80
80
|
): AsImagePixelDensitySrcSetReturnType<Field> => {
|
|
81
81
|
if (field && isImageThumbnailFilled(field)) {
|
|
82
82
|
// We are using destructuring to omit `pixelDensities` from the
|
|
83
83
|
// object we will pass to `buildURL()`.
|
|
84
|
-
const { pixelDensities = DEFAULT_PIXEL_DENSITIES, ...imgixParams } =
|
|
84
|
+
const { pixelDensities = DEFAULT_PIXEL_DENSITIES, ...imgixParams } = config;
|
|
85
85
|
|
|
86
86
|
return {
|
|
87
87
|
src: buildURL(field.url, imgixParams),
|
|
@@ -23,7 +23,7 @@ type AsImageSrcReturnType<Field extends ImageFieldImage | null | undefined> =
|
|
|
23
23
|
*
|
|
24
24
|
* @param field - Image field (or one of its responsive views) from which to get
|
|
25
25
|
* an image URL.
|
|
26
|
-
* @param
|
|
26
|
+
* @param config - An object of Imgix URL API parameters to transform the image.
|
|
27
27
|
*
|
|
28
28
|
* @returns The Image field's image URL with transformations applied (if given).
|
|
29
29
|
* If the Image field is empty, `null` is returned.
|
|
@@ -31,10 +31,10 @@ type AsImageSrcReturnType<Field extends ImageFieldImage | null | undefined> =
|
|
|
31
31
|
*/
|
|
32
32
|
export const asImageSrc = <Field extends ImageFieldImage | null | undefined>(
|
|
33
33
|
field: Field,
|
|
34
|
-
|
|
34
|
+
config: ImgixURLParams = {},
|
|
35
35
|
): AsImageSrcReturnType<Field> => {
|
|
36
36
|
if (field && isImageThumbnailFilled(field)) {
|
|
37
|
-
return buildURL(field.url,
|
|
37
|
+
return buildURL(field.url, config) as AsImageSrcReturnType<Field>;
|
|
38
38
|
} else {
|
|
39
39
|
return null as AsImageSrcReturnType<Field>;
|
|
40
40
|
}
|
|
@@ -68,7 +68,7 @@ type AsImageWidthSrcSetConfig = Omit<BuildWidthSrcSetParams, "widths"> & {
|
|
|
68
68
|
*
|
|
69
69
|
* @param field - Image field (or one of its responsive views) from which to get
|
|
70
70
|
* an image URL.
|
|
71
|
-
* @param
|
|
71
|
+
* @param config - An object of Imgix URL API parameters. The `widths` parameter
|
|
72
72
|
* defines the resulting `srcset` widths. Pass `"thumbnails"` to automatically
|
|
73
73
|
* use the field's responsive views.
|
|
74
74
|
*
|
|
@@ -80,7 +80,7 @@ export const asImageWidthSrcSet = <
|
|
|
80
80
|
Field extends ImageFieldImage | null | undefined,
|
|
81
81
|
>(
|
|
82
82
|
field: Field,
|
|
83
|
-
|
|
83
|
+
config: AsImageWidthSrcSetConfig = {},
|
|
84
84
|
): AsImageWidthSrcSetReturnType<Field> => {
|
|
85
85
|
if (field && isFilled.imageThumbnail(field)) {
|
|
86
86
|
// We are using destructuring to omit `widths` from the object
|
|
@@ -89,7 +89,7 @@ export const asImageWidthSrcSet = <
|
|
|
89
89
|
widths = DEFAULT_WIDTHS,
|
|
90
90
|
// eslint-disable-next-line prefer-const
|
|
91
91
|
...imgixParams
|
|
92
|
-
} =
|
|
92
|
+
} = config;
|
|
93
93
|
const {
|
|
94
94
|
url,
|
|
95
95
|
dimensions,
|
package/src/helpers/asLink.ts
CHANGED
|
@@ -19,6 +19,28 @@ export type LinkResolverFunction<ReturnType = string | null | undefined> = (
|
|
|
19
19
|
linkToDocumentField: FilledContentRelationshipField,
|
|
20
20
|
) => ReturnType;
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Configuration that determines the output of `asLink()`.
|
|
24
|
+
*/
|
|
25
|
+
type AsLinkConfig<LinkResolverFunctionReturnType = string | null | undefined> =
|
|
26
|
+
{
|
|
27
|
+
/**
|
|
28
|
+
* An optional Link Resolver function. Without it, you are
|
|
29
|
+
* expected to use the `routes` options from the API.
|
|
30
|
+
*/
|
|
31
|
+
linkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
35
|
+
/**
|
|
36
|
+
* @deprecated Use object-style configuration instead.
|
|
37
|
+
*/
|
|
38
|
+
type AsLinkDeprecatedTupleConfig<
|
|
39
|
+
LinkResolverFunctionReturnType = string | null | undefined,
|
|
40
|
+
> = [
|
|
41
|
+
linkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null,
|
|
42
|
+
];
|
|
43
|
+
|
|
22
44
|
/**
|
|
23
45
|
* The return type of `asLink()`.
|
|
24
46
|
*/
|
|
@@ -37,21 +59,66 @@ export type AsLinkReturnType<
|
|
|
37
59
|
? LinkResolverFunctionReturnType | string | null
|
|
38
60
|
: null;
|
|
39
61
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
62
|
+
// TODO: Remove overload when we remove support for deprecated tuple-style configuration.
|
|
63
|
+
export const asLink: {
|
|
64
|
+
/**
|
|
65
|
+
* Resolves any type of Link field or Prismic document to a URL.
|
|
66
|
+
*
|
|
67
|
+
* @typeParam LinkResolverFunctionReturnType - Link Resolver function return
|
|
68
|
+
* type
|
|
69
|
+
* @typeParam Field - Link field or Prismic document to resolve to a URL
|
|
70
|
+
* @param linkFieldOrDocument - Any kind of Link field or a document to resolve
|
|
71
|
+
* @param config - Configuration that determines the output of `asLink()`
|
|
72
|
+
*
|
|
73
|
+
* @returns Resolved URL or, if the provided Link field or document is empty, `null`
|
|
74
|
+
* @see Prismic Link Resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}
|
|
75
|
+
* @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}
|
|
76
|
+
*/
|
|
77
|
+
<
|
|
78
|
+
LinkResolverFunctionReturnType = string | null | undefined,
|
|
79
|
+
Field extends LinkField | PrismicDocument | null | undefined =
|
|
80
|
+
| LinkField
|
|
81
|
+
| PrismicDocument
|
|
82
|
+
| null
|
|
83
|
+
| undefined,
|
|
84
|
+
>(
|
|
85
|
+
linkFieldOrDocument: Field,
|
|
86
|
+
config?: AsLinkConfig<LinkResolverFunctionReturnType>,
|
|
87
|
+
): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Resolves any type of Link field or Prismic document to a URL.
|
|
91
|
+
*
|
|
92
|
+
* @typeParam LinkResolverFunctionReturnType - Link Resolver function return
|
|
93
|
+
* type
|
|
94
|
+
* @typeParam Field - Link field or Prismic document to resolve to a URL
|
|
95
|
+
* @param linkFieldOrDocument - Any kind of Link field or a document to resolve
|
|
96
|
+
* @param linkResolver - An optional Link Resolver function. Without it, you are
|
|
97
|
+
* expected to use the `routes` options from the API
|
|
98
|
+
*
|
|
99
|
+
* @returns Resolved URL or, if the provided Link field or document is empty, `null`
|
|
100
|
+
* @see Prismic Link Resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}
|
|
101
|
+
* @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}
|
|
102
|
+
*
|
|
103
|
+
* @deprecated Use object-style configuration instead.
|
|
104
|
+
*
|
|
105
|
+
* ```ts
|
|
106
|
+
* asLink(field);
|
|
107
|
+
* asLink(field, { linkResolver });
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
<
|
|
111
|
+
LinkResolverFunctionReturnType = string | null | undefined,
|
|
112
|
+
Field extends LinkField | PrismicDocument | null | undefined =
|
|
113
|
+
| LinkField
|
|
114
|
+
| PrismicDocument
|
|
115
|
+
| null
|
|
116
|
+
| undefined,
|
|
117
|
+
>(
|
|
118
|
+
linkFieldOrDocument: Field,
|
|
119
|
+
...config: AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>
|
|
120
|
+
): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;
|
|
121
|
+
} = <
|
|
55
122
|
LinkResolverFunctionReturnType = string | null | undefined,
|
|
56
123
|
Field extends LinkField | PrismicDocument | null | undefined =
|
|
57
124
|
| LinkField
|
|
@@ -60,7 +127,10 @@ export const asLink = <
|
|
|
60
127
|
| undefined,
|
|
61
128
|
>(
|
|
62
129
|
linkFieldOrDocument: Field,
|
|
63
|
-
|
|
130
|
+
// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.
|
|
131
|
+
...configObjectOrTuple:
|
|
132
|
+
| [config?: AsLinkConfig<LinkResolverFunctionReturnType>]
|
|
133
|
+
| AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>
|
|
64
134
|
): AsLinkReturnType<LinkResolverFunctionReturnType, Field> => {
|
|
65
135
|
if (!linkFieldOrDocument) {
|
|
66
136
|
return null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;
|
|
@@ -78,6 +148,20 @@ export const asLink = <
|
|
|
78
148
|
: documentToLinkField(linkFieldOrDocument)
|
|
79
149
|
) as LinkField;
|
|
80
150
|
|
|
151
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
152
|
+
const [configObjectOrLinkResolver] = configObjectOrTuple;
|
|
153
|
+
let config: AsLinkConfig<LinkResolverFunctionReturnType>;
|
|
154
|
+
if (
|
|
155
|
+
typeof configObjectOrLinkResolver === "function" ||
|
|
156
|
+
configObjectOrLinkResolver == null
|
|
157
|
+
) {
|
|
158
|
+
config = {
|
|
159
|
+
linkResolver: configObjectOrLinkResolver,
|
|
160
|
+
};
|
|
161
|
+
} else {
|
|
162
|
+
config = { ...configObjectOrLinkResolver };
|
|
163
|
+
}
|
|
164
|
+
|
|
81
165
|
switch (linkField.link_type) {
|
|
82
166
|
case LinkType.Media:
|
|
83
167
|
case LinkType.Web:
|
|
@@ -87,9 +171,9 @@ export const asLink = <
|
|
|
87
171
|
>;
|
|
88
172
|
|
|
89
173
|
case LinkType.Document: {
|
|
90
|
-
if ("id" in linkField && linkResolver) {
|
|
174
|
+
if ("id" in linkField && config.linkResolver) {
|
|
91
175
|
// When using Link Resolver...
|
|
92
|
-
const resolvedURL = linkResolver(linkField);
|
|
176
|
+
const resolvedURL = config.linkResolver(linkField);
|
|
93
177
|
|
|
94
178
|
if (resolvedURL != null) {
|
|
95
179
|
return resolvedURL as AsLinkReturnType<
|
package/src/helpers/asText.ts
CHANGED
|
@@ -2,27 +2,86 @@ import { asText as baseAsText } from "@prismicio/richtext";
|
|
|
2
2
|
|
|
3
3
|
import { RichTextField } from "../types/value/richText";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Configuration that determines the output of `asText()`.
|
|
7
|
+
*/
|
|
8
|
+
type AsTextConfig = {
|
|
9
|
+
/**
|
|
10
|
+
* Separator used to join each element.
|
|
11
|
+
*
|
|
12
|
+
* @defaultValue ` ` (a space)
|
|
13
|
+
*/
|
|
14
|
+
separator?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated Use object-style configuration instead.
|
|
20
|
+
*/
|
|
21
|
+
type AsTextDeprecatedTupleConfig = [separator?: string];
|
|
22
|
+
|
|
5
23
|
/**
|
|
6
24
|
* The return type of `asText()`.
|
|
7
25
|
*/
|
|
8
26
|
type AsTextReturnType<Field extends RichTextField | null | undefined> =
|
|
9
27
|
Field extends RichTextField ? string : null;
|
|
10
28
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
29
|
+
export const asText: {
|
|
30
|
+
/**
|
|
31
|
+
* Serializes a Rich Text or Title field to a plain text string.
|
|
32
|
+
*
|
|
33
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
34
|
+
* @param config - Configuration that determines the output of `asText()`
|
|
35
|
+
*
|
|
36
|
+
* @returns Plain text equivalent of the provided Rich Text or Title field
|
|
37
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
38
|
+
*/
|
|
39
|
+
<Field extends RichTextField | null | undefined>(
|
|
40
|
+
richTextField: Field,
|
|
41
|
+
config?: AsTextConfig,
|
|
42
|
+
): AsTextReturnType<Field>;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Serializes a Rich Text or Title field to a plain text string.
|
|
46
|
+
*
|
|
47
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
48
|
+
* @param separator - Separator used to join each element, defaults to a space
|
|
49
|
+
*
|
|
50
|
+
* @returns Plain text equivalent of the provided Rich Text or Title field
|
|
51
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
52
|
+
*
|
|
53
|
+
* @deprecated Use object-style configuration instead.
|
|
54
|
+
*
|
|
55
|
+
* ```ts
|
|
56
|
+
* asText(field);
|
|
57
|
+
* asText(field, { separator });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
<Field extends RichTextField | null | undefined>(
|
|
61
|
+
richTextField: Field,
|
|
62
|
+
...config: AsTextDeprecatedTupleConfig
|
|
63
|
+
): AsTextReturnType<Field>;
|
|
64
|
+
} = <Field extends RichTextField | null | undefined>(
|
|
21
65
|
richTextField: Field,
|
|
22
|
-
|
|
66
|
+
// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.
|
|
67
|
+
...configObjectOrTuple: [config?: AsTextConfig] | AsTextDeprecatedTupleConfig
|
|
23
68
|
): AsTextReturnType<Field> => {
|
|
24
69
|
if (richTextField) {
|
|
25
|
-
|
|
70
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
71
|
+
const [configObjectOrSeparator] = configObjectOrTuple;
|
|
72
|
+
let config: AsTextConfig;
|
|
73
|
+
if (typeof configObjectOrSeparator === "string") {
|
|
74
|
+
config = {
|
|
75
|
+
separator: configObjectOrSeparator,
|
|
76
|
+
};
|
|
77
|
+
} else {
|
|
78
|
+
config = { ...configObjectOrSeparator };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return baseAsText(
|
|
82
|
+
richTextField,
|
|
83
|
+
config.separator,
|
|
84
|
+
) as AsTextReturnType<Field>;
|
|
26
85
|
} else {
|
|
27
86
|
return null as AsTextReturnType<Field>;
|
|
28
87
|
}
|