@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.
Files changed (45) hide show
  1. package/dist/createClient.cjs +39 -18
  2. package/dist/createClient.cjs.map +1 -1
  3. package/dist/createClient.d.ts +32 -8
  4. package/dist/createClient.js +39 -18
  5. package/dist/createClient.js.map +1 -1
  6. package/dist/helpers/asHTML.cjs +17 -4
  7. package/dist/helpers/asHTML.cjs.map +1 -1
  8. package/dist/helpers/asHTML.d.ts +55 -13
  9. package/dist/helpers/asHTML.js +17 -4
  10. package/dist/helpers/asHTML.js.map +1 -1
  11. package/dist/helpers/asImagePixelDensitySrcSet.cjs +2 -2
  12. package/dist/helpers/asImagePixelDensitySrcSet.cjs.map +1 -1
  13. package/dist/helpers/asImagePixelDensitySrcSet.d.ts +2 -2
  14. package/dist/helpers/asImagePixelDensitySrcSet.js +2 -2
  15. package/dist/helpers/asImagePixelDensitySrcSet.js.map +1 -1
  16. package/dist/helpers/asImageSrc.cjs +2 -2
  17. package/dist/helpers/asImageSrc.cjs.map +1 -1
  18. package/dist/helpers/asImageSrc.d.ts +2 -2
  19. package/dist/helpers/asImageSrc.js +2 -2
  20. package/dist/helpers/asImageSrc.js.map +1 -1
  21. package/dist/helpers/asImageWidthSrcSet.cjs +2 -2
  22. package/dist/helpers/asImageWidthSrcSet.cjs.map +1 -1
  23. package/dist/helpers/asImageWidthSrcSet.d.ts +2 -2
  24. package/dist/helpers/asImageWidthSrcSet.js +2 -2
  25. package/dist/helpers/asImageWidthSrcSet.js.map +1 -1
  26. package/dist/helpers/asLink.cjs +12 -3
  27. package/dist/helpers/asLink.cjs.map +1 -1
  28. package/dist/helpers/asLink.d.ts +55 -15
  29. package/dist/helpers/asLink.js +12 -3
  30. package/dist/helpers/asLink.js.map +1 -1
  31. package/dist/helpers/asText.cjs +11 -2
  32. package/dist/helpers/asText.cjs.map +1 -1
  33. package/dist/helpers/asText.d.ts +44 -10
  34. package/dist/helpers/asText.js +11 -2
  35. package/dist/helpers/asText.js.map +1 -1
  36. package/dist/package.json.cjs +1 -1
  37. package/dist/package.json.js +1 -1
  38. package/package.json +1 -1
  39. package/src/createClient.ts +105 -31
  40. package/src/helpers/asHTML.ts +99 -25
  41. package/src/helpers/asImagePixelDensitySrcSet.ts +3 -3
  42. package/src/helpers/asImageSrc.ts +3 -3
  43. package/src/helpers/asImageWidthSrcSet.ts +3 -3
  44. package/src/helpers/asLink.ts +102 -18
  45. 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 params - An object of Imgix URL API parameters. The `pixelDensities`
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
- params: AsImagePixelDensitySrcSetConfig = {},
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 } = params;
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 params - An object of Imgix URL API parameters to transform the image.
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
- params: ImgixURLParams = {},
34
+ config: ImgixURLParams = {},
35
35
  ): AsImageSrcReturnType<Field> => {
36
36
  if (field && isImageThumbnailFilled(field)) {
37
- return buildURL(field.url, params) as AsImageSrcReturnType<Field>;
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 params - An object of Imgix URL API parameters. The `widths` parameter
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
- params: AsImageWidthSrcSetConfig = {},
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
- } = params;
92
+ } = config;
93
93
  const {
94
94
  url,
95
95
  dimensions,
@@ -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
- * Resolves any type of Link field or Prismic document to a URL.
42
- *
43
- * @typeParam LinkResolverFunctionReturnType - Link Resolver function return
44
- * type
45
- * @typeParam Field - Link field or Prismic document to resolve to a URL
46
- * @param linkFieldOrDocument - Any kind of Link field or a document to resolve
47
- * @param linkResolver - An optional Link Resolver function. Without it, you are
48
- * expected to use the `routes` options from the API
49
- *
50
- * @returns Resolved URL or, if the provided Link field or document is empty, `null`
51
- * @see Prismic Link Resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}
52
- * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}
53
- */
54
- export const asLink = <
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
- linkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null,
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<
@@ -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
- * Serializes a Rich Text or Title field to a plain text string
13
- *
14
- * @param richTextField - A Rich Text or Title field from Prismic
15
- * @param separator - Separator used to join each element, defaults to a space
16
- *
17
- * @returns Plain text equivalent of the provided Rich Text or Title field
18
- * @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
19
- */
20
- export const asText = <Field extends RichTextField | null | undefined>(
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
- separator?: string,
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
- return baseAsText(richTextField, separator) as AsTextReturnType<Field>;
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
  }