@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
package/dist/helpers/asLink.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { LinkType } from "../types/value/link.js";
|
|
2
2
|
import { documentToLinkField } from "./documentToLinkField.js";
|
|
3
|
-
const asLink = (linkFieldOrDocument,
|
|
3
|
+
const asLink = (linkFieldOrDocument, ...configObjectOrTuple) => {
|
|
4
4
|
if (!linkFieldOrDocument) {
|
|
5
5
|
return null;
|
|
6
6
|
}
|
|
@@ -11,13 +11,22 @@ const asLink = (linkFieldOrDocument, linkResolver) => {
|
|
|
11
11
|
// TODO: Remove the `prettier-ignore` comment when this bug is fixed.
|
|
12
12
|
"link_type" in linkFieldOrDocument ? linkFieldOrDocument : documentToLinkField(linkFieldOrDocument)
|
|
13
13
|
);
|
|
14
|
+
const [configObjectOrLinkResolver] = configObjectOrTuple;
|
|
15
|
+
let config;
|
|
16
|
+
if (typeof configObjectOrLinkResolver === "function" || configObjectOrLinkResolver == null) {
|
|
17
|
+
config = {
|
|
18
|
+
linkResolver: configObjectOrLinkResolver
|
|
19
|
+
};
|
|
20
|
+
} else {
|
|
21
|
+
config = { ...configObjectOrLinkResolver };
|
|
22
|
+
}
|
|
14
23
|
switch (linkField.link_type) {
|
|
15
24
|
case LinkType.Media:
|
|
16
25
|
case LinkType.Web:
|
|
17
26
|
return "url" in linkField ? linkField.url : null;
|
|
18
27
|
case LinkType.Document: {
|
|
19
|
-
if ("id" in linkField && linkResolver) {
|
|
20
|
-
const resolvedURL = linkResolver(linkField);
|
|
28
|
+
if ("id" in linkField && config.linkResolver) {
|
|
29
|
+
const resolvedURL = config.linkResolver(linkField);
|
|
21
30
|
if (resolvedURL != null) {
|
|
22
31
|
return resolvedURL;
|
|
23
32
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asLink.js","sources":["../../../src/helpers/asLink.ts"],"sourcesContent":["import type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField, LinkType } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { documentToLinkField } from \"./documentToLinkField\";\n\n/**\n * Resolves a Link to a Prismic document to a URL\n *\n * @typeParam ReturnType - Return type of your link resolver function, useful if\n * you prefer to return a complex object\n * @param linkToDocumentField - A document Link Field to resolve\n *\n * @returns Resolved URL\n * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n */\nexport type LinkResolverFunction<ReturnType = string | null | undefined> = (\n\tlinkToDocumentField: FilledContentRelationshipField,\n) => ReturnType;\n\n/**\n * The return type of `asLink()`.\n */\nexport type AsLinkReturnType<\n\tLinkResolverFunctionReturnType = string | null | undefined,\n\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t| LinkField\n\t\t| PrismicDocument\n\t\t| null\n\t\t| undefined,\n> = Field extends\n\t| FilledLinkToWebField\n\t| FilledLinkToMediaField\n\t| FilledContentRelationshipField\n\t| PrismicDocument\n\t? LinkResolverFunctionReturnType | string | null\n\t: null;\n\n/**\n * Resolves any type of Link field or Prismic document to a URL.\n *\n * @typeParam LinkResolverFunctionReturnType - Link Resolver function return\n * type\n * @typeParam Field - Link field or Prismic document to resolve to a URL\n * @param linkFieldOrDocument - Any kind of Link field or a document to resolve\n * @param linkResolver - An optional Link Resolver function. Without it, you are\n * expected to use the `routes` options from the API\n *\n * @returns Resolved URL or, if the provided Link field or document is empty, `null`\n * @see Prismic Link Resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}\n
|
|
1
|
+
{"version":3,"file":"asLink.js","sources":["../../../src/helpers/asLink.ts"],"sourcesContent":["import type { FilledContentRelationshipField } from \"../types/value/contentRelationship\";\nimport type { PrismicDocument } from \"../types/value/document\";\nimport { FilledLinkToWebField, LinkField, LinkType } from \"../types/value/link\";\nimport type { FilledLinkToMediaField } from \"../types/value/linkToMedia\";\n\nimport { documentToLinkField } from \"./documentToLinkField\";\n\n/**\n * Resolves a Link to a Prismic document to a URL\n *\n * @typeParam ReturnType - Return type of your link resolver function, useful if\n * you prefer to return a complex object\n * @param linkToDocumentField - A document Link Field to resolve\n *\n * @returns Resolved URL\n * @see Prismic link resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n */\nexport type LinkResolverFunction<ReturnType = string | null | undefined> = (\n\tlinkToDocumentField: FilledContentRelationshipField,\n) => ReturnType;\n\n/**\n * Configuration that determines the output of `asLink()`.\n */\ntype AsLinkConfig<LinkResolverFunctionReturnType = string | null | undefined> =\n\t{\n\t\t/**\n\t\t * An optional Link Resolver function. Without it, you are\n\t\t * expected to use the `routes` options from the API.\n\t\t */\n\t\tlinkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null;\n\t};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsLinkDeprecatedTupleConfig<\n\tLinkResolverFunctionReturnType = string | null | undefined,\n> = [\n\tlinkResolver?: LinkResolverFunction<LinkResolverFunctionReturnType> | null,\n];\n\n/**\n * The return type of `asLink()`.\n */\nexport type AsLinkReturnType<\n\tLinkResolverFunctionReturnType = string | null | undefined,\n\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t| LinkField\n\t\t| PrismicDocument\n\t\t| null\n\t\t| undefined,\n> = Field extends\n\t| FilledLinkToWebField\n\t| FilledLinkToMediaField\n\t| FilledContentRelationshipField\n\t| PrismicDocument\n\t? LinkResolverFunctionReturnType | string | null\n\t: null;\n\n// TODO: Remove overload when we remove support for deprecated tuple-style configuration.\nexport const asLink: {\n\t/**\n\t * Resolves any type of Link field or Prismic document to a URL.\n\t *\n\t * @typeParam LinkResolverFunctionReturnType - Link Resolver function return\n\t * type\n\t * @typeParam Field - Link field or Prismic document to resolve to a URL\n\t * @param linkFieldOrDocument - Any kind of Link field or a document to resolve\n\t * @param config - Configuration that determines the output of `asLink()`\n\t *\n\t * @returns Resolved URL or, if the provided Link field or document is empty, `null`\n\t * @see Prismic Link Resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n\t * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}\n\t */\n\t<\n\t\tLinkResolverFunctionReturnType = string | null | undefined,\n\t\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t\t| LinkField\n\t\t\t| PrismicDocument\n\t\t\t| null\n\t\t\t| undefined,\n\t>(\n\t\tlinkFieldOrDocument: Field,\n\t\tconfig?: AsLinkConfig<LinkResolverFunctionReturnType>,\n\t): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\n\t/**\n\t * Resolves any type of Link field or Prismic document to a URL.\n\t *\n\t * @typeParam LinkResolverFunctionReturnType - Link Resolver function return\n\t * type\n\t * @typeParam Field - Link field or Prismic document to resolve to a URL\n\t * @param linkFieldOrDocument - Any kind of Link field or a document to resolve\n\t * @param linkResolver - An optional Link Resolver function. Without it, you are\n\t * expected to use the `routes` options from the API\n\t *\n\t * @returns Resolved URL or, if the provided Link field or document is empty, `null`\n\t * @see Prismic Link Resolver documentation: {@link https://prismic.io/docs/route-resolver#link-resolver}\n\t * @see Prismic API `routes` options documentation: {@link https://prismic.io/docs/route-resolver}\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * ```ts\n\t * asLink(field);\n\t * asLink(field, { linkResolver });\n\t * ```\n\t */\n\t<\n\t\tLinkResolverFunctionReturnType = string | null | undefined,\n\t\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t\t| LinkField\n\t\t\t| PrismicDocument\n\t\t\t| null\n\t\t\t| undefined,\n\t>(\n\t\tlinkFieldOrDocument: Field,\n\t\t...config: AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>\n\t): AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n} = <\n\tLinkResolverFunctionReturnType = string | null | undefined,\n\tField extends LinkField | PrismicDocument | null | undefined =\n\t\t| LinkField\n\t\t| PrismicDocument\n\t\t| null\n\t\t| undefined,\n>(\n\tlinkFieldOrDocument: Field,\n\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple:\n\t\t| [config?: AsLinkConfig<LinkResolverFunctionReturnType>]\n\t\t| AsLinkDeprecatedTupleConfig<LinkResolverFunctionReturnType>\n): AsLinkReturnType<LinkResolverFunctionReturnType, Field> => {\n\tif (!linkFieldOrDocument) {\n\t\treturn null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\t}\n\n\t// Converts document to Link Field if needed\n\tconst linkField =\n\t\t// prettier-ignore\n\t\t(\n\t\t\t// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n\t\t\t// @ts-ignore - Bug in TypeScript 4.9: https://github.com/microsoft/TypeScript/issues/51501\n\t\t\t// TODO: Remove the `prettier-ignore` comment when this bug is fixed.\n\t\t\t\"link_type\" in linkFieldOrDocument\n\t\t\t\t? linkFieldOrDocument\n\t\t\t\t: documentToLinkField(linkFieldOrDocument)\n\t\t) as LinkField;\n\n\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\tconst [configObjectOrLinkResolver] = configObjectOrTuple;\n\tlet config: AsLinkConfig<LinkResolverFunctionReturnType>;\n\tif (\n\t\ttypeof configObjectOrLinkResolver === \"function\" ||\n\t\tconfigObjectOrLinkResolver == null\n\t) {\n\t\tconfig = {\n\t\t\tlinkResolver: configObjectOrLinkResolver,\n\t\t};\n\t} else {\n\t\tconfig = { ...configObjectOrLinkResolver };\n\t}\n\n\tswitch (linkField.link_type) {\n\t\tcase LinkType.Media:\n\t\tcase LinkType.Web:\n\t\t\treturn (\"url\" in linkField ? linkField.url : null) as AsLinkReturnType<\n\t\t\t\tLinkResolverFunctionReturnType,\n\t\t\t\tField\n\t\t\t>;\n\n\t\tcase LinkType.Document: {\n\t\t\tif (\"id\" in linkField && config.linkResolver) {\n\t\t\t\t// When using Link Resolver...\n\t\t\t\tconst resolvedURL = config.linkResolver(linkField);\n\n\t\t\t\tif (resolvedURL != null) {\n\t\t\t\t\treturn resolvedURL as AsLinkReturnType<\n\t\t\t\t\t\tLinkResolverFunctionReturnType,\n\t\t\t\t\t\tField\n\t\t\t\t\t>;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (\"url\" in linkField && linkField.url) {\n\t\t\t\t// When using Route Resolver...\n\t\t\t\treturn linkField.url as AsLinkReturnType<\n\t\t\t\t\tLinkResolverFunctionReturnType,\n\t\t\t\t\tField\n\t\t\t\t>;\n\t\t\t}\n\n\t\t\t// When empty or Link Resolver and Route Resolver are not used...\n\t\t\treturn null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\t\t}\n\n\t\tcase LinkType.Any:\n\t\tdefault:\n\t\t\treturn null as AsLinkReturnType<LinkResolverFunctionReturnType, Field>;\n\t}\n};\n"],"names":[],"mappings":";;AA8Da,MAAA,SA0DT,CAQH,wBAEG,wBAGyD;AAC5D,MAAI,CAAC,qBAAqB;AAClB,WAAA;AAAA,EACP;AAGK,QAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMJ,eAAe,sBACZ,sBACA,oBAAoB,mBAAmB;AAAA;AAItC,QAAA,CAAC,0BAA0B,IAAI;AACjC,MAAA;AACJ,MACC,OAAO,+BAA+B,cACtC,8BAA8B,MAC7B;AACQ,aAAA;AAAA,MACR,cAAc;AAAA,IAAA;AAAA,SAET;AACG,aAAA,EAAE,GAAG;EACd;AAED,UAAQ,UAAU,WAAW;AAAA,IAC5B,KAAK,SAAS;AAAA,IACd,KAAK,SAAS;AACL,aAAA,SAAS,YAAY,UAAU,MAAM;AAAA,IAK9C,KAAK,SAAS,UAAU;AACnB,UAAA,QAAQ,aAAa,OAAO,cAAc;AAEvC,cAAA,cAAc,OAAO,aAAa,SAAS;AAEjD,YAAI,eAAe,MAAM;AACjB,iBAAA;AAAA,QAIP;AAAA,MACD;AAEG,UAAA,SAAS,aAAa,UAAU,KAAK;AAExC,eAAO,UAAU;AAAA,MAIjB;AAGM,aAAA;AAAA,IACP;AAAA,IAED,KAAK,SAAS;AAAA,IACd;AACQ,aAAA;AAAA,EACR;AACF;"}
|
package/dist/helpers/asText.cjs
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const richtext = require("@prismicio/richtext");
|
|
4
|
-
const asText = (richTextField,
|
|
4
|
+
const asText = (richTextField, ...configObjectOrTuple) => {
|
|
5
5
|
if (richTextField) {
|
|
6
|
-
|
|
6
|
+
const [configObjectOrSeparator] = configObjectOrTuple;
|
|
7
|
+
let config;
|
|
8
|
+
if (typeof configObjectOrSeparator === "string") {
|
|
9
|
+
config = {
|
|
10
|
+
separator: configObjectOrSeparator
|
|
11
|
+
};
|
|
12
|
+
} else {
|
|
13
|
+
config = { ...configObjectOrSeparator };
|
|
14
|
+
}
|
|
15
|
+
return richtext.asText(richTextField, config.separator);
|
|
7
16
|
} else {
|
|
8
17
|
return null;
|
|
9
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asText.cjs","sources":["../../../src/helpers/asText.ts"],"sourcesContent":["import { asText as baseAsText } from \"@prismicio/richtext\";\n\nimport { RichTextField } from \"../types/value/richText\";\n\n/**\n * The return type of `asText()`.\n */\ntype AsTextReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\n/**\n * Serializes a Rich Text or Title field to a plain text string\n *\n * @param richTextField - A Rich Text or Title field from Prismic\n * @param separator - Separator used to join each element, defaults to a space\n *\n * @returns Plain text equivalent of the provided Rich Text or Title field\n * @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n
|
|
1
|
+
{"version":3,"file":"asText.cjs","sources":["../../../src/helpers/asText.ts"],"sourcesContent":["import { asText as baseAsText } from \"@prismicio/richtext\";\n\nimport { RichTextField } from \"../types/value/richText\";\n\n/**\n * Configuration that determines the output of `asText()`.\n */\ntype AsTextConfig = {\n\t/**\n\t * Separator used to join each element.\n\t *\n\t * @defaultValue ` ` (a space)\n\t */\n\tseparator?: string;\n};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsTextDeprecatedTupleConfig = [separator?: string];\n\n/**\n * The return type of `asText()`.\n */\ntype AsTextReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\nexport const asText: {\n\t/**\n\t * Serializes a Rich Text or Title field to a plain text string.\n\t *\n\t * @param richTextField - A Rich Text or Title field from Prismic\n\t * @param config - Configuration that determines the output of `asText()`\n\t *\n\t * @returns Plain text equivalent of the provided Rich Text or Title field\n\t * @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\tconfig?: AsTextConfig,\n\t): AsTextReturnType<Field>;\n\n\t/**\n\t * Serializes a Rich Text or Title field to a plain text string.\n\t *\n\t * @param richTextField - A Rich Text or Title field from Prismic\n\t * @param separator - Separator used to join each element, defaults to a space\n\t *\n\t * @returns Plain text equivalent of the provided Rich Text or Title field\n\t * @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * ```ts\n\t * asText(field);\n\t * asText(field, { separator });\n\t * ```\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\t...config: AsTextDeprecatedTupleConfig\n\t): AsTextReturnType<Field>;\n} = <Field extends RichTextField | null | undefined>(\n\trichTextField: Field,\n\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple: [config?: AsTextConfig] | AsTextDeprecatedTupleConfig\n): AsTextReturnType<Field> => {\n\tif (richTextField) {\n\t\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\t\tconst [configObjectOrSeparator] = configObjectOrTuple;\n\t\tlet config: AsTextConfig;\n\t\tif (typeof configObjectOrSeparator === \"string\") {\n\t\t\tconfig = {\n\t\t\t\tseparator: configObjectOrSeparator,\n\t\t\t};\n\t\t} else {\n\t\t\tconfig = { ...configObjectOrSeparator };\n\t\t}\n\n\t\treturn baseAsText(\n\t\t\trichTextField,\n\t\t\tconfig.separator,\n\t\t) as AsTextReturnType<Field>;\n\t} else {\n\t\treturn null as AsTextReturnType<Field>;\n\t}\n};\n"],"names":["baseAsText"],"mappings":";;;AA4Ba,MAAA,SAmCT,CACH,kBAEG,wBACyB;AAC5B,MAAI,eAAe;AAEZ,UAAA,CAAC,uBAAuB,IAAI;AAC9B,QAAA;AACA,QAAA,OAAO,4BAA4B,UAAU;AACvC,eAAA;AAAA,QACR,WAAW;AAAA,MAAA;AAAA,WAEN;AACG,eAAA,EAAE,GAAG;IACd;AAEM,WAAAA,gBACN,eACA,OAAO,SAAS;AAAA,EAAA,OAEX;AACC,WAAA;AAAA,EACP;AACF;;"}
|
package/dist/helpers/asText.d.ts
CHANGED
|
@@ -1,16 +1,50 @@
|
|
|
1
1
|
import { RichTextField } from "../types/value/richText";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Configuration that determines the output of `asText()`.
|
|
4
4
|
*/
|
|
5
|
-
type
|
|
5
|
+
type AsTextConfig = {
|
|
6
|
+
/**
|
|
7
|
+
* Separator used to join each element.
|
|
8
|
+
*
|
|
9
|
+
* @defaultValue ` ` (a space)
|
|
10
|
+
*/
|
|
11
|
+
separator?: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated Use object-style configuration instead.
|
|
15
|
+
*/
|
|
16
|
+
type AsTextDeprecatedTupleConfig = [separator?: string];
|
|
6
17
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @param richTextField - A Rich Text or Title field from Prismic
|
|
10
|
-
* @param separator - Separator used to join each element, defaults to a space
|
|
11
|
-
*
|
|
12
|
-
* @returns Plain text equivalent of the provided Rich Text or Title field
|
|
13
|
-
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
18
|
+
* The return type of `asText()`.
|
|
14
19
|
*/
|
|
15
|
-
|
|
20
|
+
type AsTextReturnType<Field extends RichTextField | null | undefined> = Field extends RichTextField ? string : null;
|
|
21
|
+
export declare const asText: {
|
|
22
|
+
/**
|
|
23
|
+
* Serializes a Rich Text or Title field to a plain text string.
|
|
24
|
+
*
|
|
25
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
26
|
+
* @param config - Configuration that determines the output of `asText()`
|
|
27
|
+
*
|
|
28
|
+
* @returns Plain text equivalent of the provided Rich Text or Title field
|
|
29
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
30
|
+
*/
|
|
31
|
+
<Field extends RichTextField | null | undefined>(richTextField: Field, config?: AsTextConfig): AsTextReturnType<Field>;
|
|
32
|
+
/**
|
|
33
|
+
* Serializes a Rich Text or Title field to a plain text string.
|
|
34
|
+
*
|
|
35
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
36
|
+
* @param separator - Separator used to join each element, defaults to a space
|
|
37
|
+
*
|
|
38
|
+
* @returns Plain text equivalent of the provided Rich Text or Title field
|
|
39
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
40
|
+
*
|
|
41
|
+
* @deprecated Use object-style configuration instead.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* asText(field);
|
|
45
|
+
* asText(field, { separator });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
<Field extends RichTextField | null | undefined>(richTextField: Field, ...config: AsTextDeprecatedTupleConfig): AsTextReturnType<Field>;
|
|
49
|
+
};
|
|
16
50
|
export {};
|
package/dist/helpers/asText.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { asText as asText$1 } from "@prismicio/richtext";
|
|
2
|
-
const asText = (richTextField,
|
|
2
|
+
const asText = (richTextField, ...configObjectOrTuple) => {
|
|
3
3
|
if (richTextField) {
|
|
4
|
-
|
|
4
|
+
const [configObjectOrSeparator] = configObjectOrTuple;
|
|
5
|
+
let config;
|
|
6
|
+
if (typeof configObjectOrSeparator === "string") {
|
|
7
|
+
config = {
|
|
8
|
+
separator: configObjectOrSeparator
|
|
9
|
+
};
|
|
10
|
+
} else {
|
|
11
|
+
config = { ...configObjectOrSeparator };
|
|
12
|
+
}
|
|
13
|
+
return asText$1(richTextField, config.separator);
|
|
5
14
|
} else {
|
|
6
15
|
return null;
|
|
7
16
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asText.js","sources":["../../../src/helpers/asText.ts"],"sourcesContent":["import { asText as baseAsText } from \"@prismicio/richtext\";\n\nimport { RichTextField } from \"../types/value/richText\";\n\n/**\n * The return type of `asText()`.\n */\ntype AsTextReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\n/**\n * Serializes a Rich Text or Title field to a plain text string\n *\n * @param richTextField - A Rich Text or Title field from Prismic\n * @param separator - Separator used to join each element, defaults to a space\n *\n * @returns Plain text equivalent of the provided Rich Text or Title field\n * @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n
|
|
1
|
+
{"version":3,"file":"asText.js","sources":["../../../src/helpers/asText.ts"],"sourcesContent":["import { asText as baseAsText } from \"@prismicio/richtext\";\n\nimport { RichTextField } from \"../types/value/richText\";\n\n/**\n * Configuration that determines the output of `asText()`.\n */\ntype AsTextConfig = {\n\t/**\n\t * Separator used to join each element.\n\t *\n\t * @defaultValue ` ` (a space)\n\t */\n\tseparator?: string;\n};\n\n// TODO: Remove when we remove support for deprecated tuple-style configuration.\n/**\n * @deprecated Use object-style configuration instead.\n */\ntype AsTextDeprecatedTupleConfig = [separator?: string];\n\n/**\n * The return type of `asText()`.\n */\ntype AsTextReturnType<Field extends RichTextField | null | undefined> =\n\tField extends RichTextField ? string : null;\n\nexport const asText: {\n\t/**\n\t * Serializes a Rich Text or Title field to a plain text string.\n\t *\n\t * @param richTextField - A Rich Text or Title field from Prismic\n\t * @param config - Configuration that determines the output of `asText()`\n\t *\n\t * @returns Plain text equivalent of the provided Rich Text or Title field\n\t * @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\tconfig?: AsTextConfig,\n\t): AsTextReturnType<Field>;\n\n\t/**\n\t * Serializes a Rich Text or Title field to a plain text string.\n\t *\n\t * @param richTextField - A Rich Text or Title field from Prismic\n\t * @param separator - Separator used to join each element, defaults to a space\n\t *\n\t * @returns Plain text equivalent of the provided Rich Text or Title field\n\t * @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}\n\t *\n\t * @deprecated Use object-style configuration instead.\n\t *\n\t * ```ts\n\t * asText(field);\n\t * asText(field, { separator });\n\t * ```\n\t */\n\t<Field extends RichTextField | null | undefined>(\n\t\trichTextField: Field,\n\t\t...config: AsTextDeprecatedTupleConfig\n\t): AsTextReturnType<Field>;\n} = <Field extends RichTextField | null | undefined>(\n\trichTextField: Field,\n\t// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.\n\t...configObjectOrTuple: [config?: AsTextConfig] | AsTextDeprecatedTupleConfig\n): AsTextReturnType<Field> => {\n\tif (richTextField) {\n\t\t// TODO: Remove when we remove support for deprecated tuple-style configuration.\n\t\tconst [configObjectOrSeparator] = configObjectOrTuple;\n\t\tlet config: AsTextConfig;\n\t\tif (typeof configObjectOrSeparator === \"string\") {\n\t\t\tconfig = {\n\t\t\t\tseparator: configObjectOrSeparator,\n\t\t\t};\n\t\t} else {\n\t\t\tconfig = { ...configObjectOrSeparator };\n\t\t}\n\n\t\treturn baseAsText(\n\t\t\trichTextField,\n\t\t\tconfig.separator,\n\t\t) as AsTextReturnType<Field>;\n\t} else {\n\t\treturn null as AsTextReturnType<Field>;\n\t}\n};\n"],"names":["baseAsText"],"mappings":";AA4Ba,MAAA,SAmCT,CACH,kBAEG,wBACyB;AAC5B,MAAI,eAAe;AAEZ,UAAA,CAAC,uBAAuB,IAAI;AAC9B,QAAA;AACA,QAAA,OAAO,4BAA4B,UAAU;AACvC,eAAA;AAAA,QACR,WAAW;AAAA,MAAA;AAAA,WAEN;AACG,eAAA,EAAE,GAAG;IACd;AAEM,WAAAA,SACN,eACA,OAAO,SAAS;AAAA,EAAA,OAEX;AACC,WAAA;AAAA,EACP;AACF;"}
|
package/dist/package.json.cjs
CHANGED
package/dist/package.json.js
CHANGED
package/package.json
CHANGED
package/src/createClient.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { appendFilters } from "./lib/appendFilters";
|
|
2
2
|
import { castThunk } from "./lib/castThunk";
|
|
3
|
+
import { devMsg } from "./lib/devMsg";
|
|
3
4
|
import { everyTagFilter } from "./lib/everyTagFilter";
|
|
4
5
|
import { findMasterRef } from "./lib/findMasterRef";
|
|
5
6
|
import { findRefByID } from "./lib/findRefByID";
|
|
@@ -24,6 +25,7 @@ import { LinkResolverFunction, asLink } from "./helpers/asLink";
|
|
|
24
25
|
import { BuildQueryURLArgs, buildQueryURL } from "./buildQueryURL";
|
|
25
26
|
import { filter } from "./filter";
|
|
26
27
|
import { getRepositoryEndpoint } from "./getRepositoryEndpoint";
|
|
28
|
+
import { getRepositoryName } from "./getRepositoryName";
|
|
27
29
|
import { isRepositoryEndpoint } from "./isRepositoryEndpoint";
|
|
28
30
|
|
|
29
31
|
/**
|
|
@@ -88,16 +90,23 @@ export type FetchLike = (
|
|
|
88
90
|
export type AbortSignalLike = any;
|
|
89
91
|
|
|
90
92
|
/**
|
|
91
|
-
*
|
|
93
|
+
* A subset of RequestInit properties to configure a `fetch()` request.
|
|
92
94
|
*/
|
|
93
|
-
|
|
95
|
+
// Only options relevant to the client are included. Extending from the full
|
|
96
|
+
// RequestInit would cause issues, such as accepting Header objects.
|
|
97
|
+
//
|
|
98
|
+
// An interface is used to allow other libraries to augment the type with
|
|
99
|
+
// environment-specific types.
|
|
100
|
+
export interface RequestInitLike extends Pick<RequestInit, "cache"> {
|
|
101
|
+
/**
|
|
102
|
+
* An object literal to set the `fetch()` request's headers.
|
|
103
|
+
*/
|
|
94
104
|
headers?: Record<string, string>;
|
|
95
105
|
|
|
96
106
|
/**
|
|
97
|
-
* An
|
|
98
|
-
* `AbortController` object
|
|
107
|
+
* An AbortSignal to set the `fetch()` request's signal.
|
|
99
108
|
*
|
|
100
|
-
*
|
|
109
|
+
* See: \<https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal\>
|
|
101
110
|
*/
|
|
102
111
|
// NOTE: `AbortSignalLike` is `any`! It is left as `AbortSignalLike`
|
|
103
112
|
// for backwards compatibility (the type is exported) and to signal to
|
|
@@ -257,18 +266,41 @@ export type ClientConfig = {
|
|
|
257
266
|
* Node.js, this function must be provided.
|
|
258
267
|
*/
|
|
259
268
|
fetch?: FetchLike;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Options provided to the client's `fetch()` on all network requests.
|
|
272
|
+
* These options will be merged with internally required options. They
|
|
273
|
+
* can also be overriden on a per-query basis using the query's
|
|
274
|
+
* `fetchOptions` parameter.
|
|
275
|
+
*/
|
|
276
|
+
fetchOptions?: RequestInitLike;
|
|
260
277
|
};
|
|
261
278
|
|
|
262
279
|
/**
|
|
263
|
-
* Parameters for any client method that use `fetch()`.
|
|
264
|
-
* `fetch()` parameters are exposed.
|
|
280
|
+
* Parameters for any client method that use `fetch()`.
|
|
265
281
|
*/
|
|
266
282
|
type FetchParams = {
|
|
283
|
+
/**
|
|
284
|
+
* Options provided to the client's `fetch()` on all network requests.
|
|
285
|
+
* These options will be merged with internally required options. They
|
|
286
|
+
* can also be overriden on a per-query basis using the query's
|
|
287
|
+
* `fetchOptions` parameter.
|
|
288
|
+
*/
|
|
289
|
+
fetchOptions?: RequestInitLike;
|
|
290
|
+
|
|
267
291
|
/**
|
|
268
292
|
* An `AbortSignal` provided by an `AbortController`. This allows the network
|
|
269
293
|
* request to be cancelled if necessary.
|
|
270
294
|
*
|
|
271
295
|
* {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal}
|
|
296
|
+
*
|
|
297
|
+
* @deprecated Move the `signal` parameter into `fetchOptions.signal`:
|
|
298
|
+
*
|
|
299
|
+
* ```typescript
|
|
300
|
+
* client.getByUID("page", "home",{
|
|
301
|
+
* fetchOptions: { signal }
|
|
302
|
+
* });
|
|
303
|
+
* ```
|
|
272
304
|
*/
|
|
273
305
|
signal?: AbortSignalLike;
|
|
274
306
|
};
|
|
@@ -407,6 +439,8 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
407
439
|
*/
|
|
408
440
|
fetchFn: FetchLike;
|
|
409
441
|
|
|
442
|
+
fetchOptions?: RequestInitLike;
|
|
443
|
+
|
|
410
444
|
/**
|
|
411
445
|
* Default parameters that will be sent with each query. These parameters can
|
|
412
446
|
* be overridden on each query if needed.
|
|
@@ -459,15 +493,33 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
459
493
|
*/
|
|
460
494
|
constructor(repositoryNameOrEndpoint: string, options: ClientConfig = {}) {
|
|
461
495
|
if (isRepositoryEndpoint(repositoryNameOrEndpoint)) {
|
|
462
|
-
if (
|
|
463
|
-
|
|
464
|
-
/\.prismic\.io\/(?!api\/v2\/?)
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
496
|
+
if (process.env.NODE_ENV === "development") {
|
|
497
|
+
// Matches non-API v2 `.prismic.io` endpoints, see: https://regex101.com/r/xRsavu/1
|
|
498
|
+
if (/\.prismic\.io\/(?!api\/v2\/?)/i.test(repositoryNameOrEndpoint)) {
|
|
499
|
+
throw new PrismicError(
|
|
500
|
+
"@prismicio/client only supports Prismic Rest API V2. Please provide only the repository name to the first createClient() parameter or use the getRepositoryEndpoint() helper to generate a valid Rest API V2 endpoint URL.",
|
|
501
|
+
undefined,
|
|
502
|
+
undefined,
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const hostname = new URL(
|
|
507
|
+
repositoryNameOrEndpoint,
|
|
508
|
+
).hostname.toLowerCase();
|
|
509
|
+
|
|
510
|
+
// Matches non-.cdn `.prismic.io` endpoints
|
|
511
|
+
if (
|
|
512
|
+
hostname.endsWith(".prismic.io") &&
|
|
513
|
+
!hostname.endsWith(".cdn.prismic.io")
|
|
514
|
+
) {
|
|
515
|
+
const repositoryName = getRepositoryName(repositoryNameOrEndpoint);
|
|
516
|
+
const dotCDNEndpoint = getRepositoryEndpoint(repositoryName);
|
|
517
|
+
console.warn(
|
|
518
|
+
`[@prismicio/client] A non-.cdn endpoint was provided to create a client with (\`${repositoryNameOrEndpoint}\`). Non-.cdn endpoints can have unexpected side-effects and cause performance issues when querying Prismic. Please convert it to the \`.cdn\` alternative (\`${dotCDNEndpoint}\`) or use the repository name directly instead (\`${repositoryName}\`). For more details, see ${devMsg(
|
|
519
|
+
"endpoint-must-use-cdn",
|
|
520
|
+
)}`,
|
|
521
|
+
);
|
|
522
|
+
}
|
|
471
523
|
}
|
|
472
524
|
|
|
473
525
|
this.endpoint = repositoryNameOrEndpoint;
|
|
@@ -478,6 +530,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
478
530
|
this.accessToken = options.accessToken;
|
|
479
531
|
this.routes = options.routes;
|
|
480
532
|
this.brokenRoute = options.brokenRoute;
|
|
533
|
+
this.fetchOptions = options.fetchOptions;
|
|
481
534
|
this.defaultParams = options.defaultParams;
|
|
482
535
|
|
|
483
536
|
if (options.ref) {
|
|
@@ -605,8 +658,7 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
605
658
|
): Promise<TDocument> {
|
|
606
659
|
const actualParams = { ...params };
|
|
607
660
|
if (!(params && params.page) && !params?.pageSize) {
|
|
608
|
-
actualParams.pageSize =
|
|
609
|
-
this.defaultParams?.pageSize == null ? 1 : this.defaultParams?.pageSize;
|
|
661
|
+
actualParams.pageSize = this.defaultParams?.pageSize ?? 1;
|
|
610
662
|
}
|
|
611
663
|
const url = await this.buildQueryURL(actualParams);
|
|
612
664
|
const result = await this.fetch<Query<TDocument>>(url, params);
|
|
@@ -1309,12 +1361,15 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1309
1361
|
*/
|
|
1310
1362
|
async buildQueryURL({
|
|
1311
1363
|
signal,
|
|
1364
|
+
fetchOptions,
|
|
1312
1365
|
...params
|
|
1313
1366
|
}: Partial<BuildQueryURLArgs> & FetchParams = {}): Promise<string> {
|
|
1314
|
-
const ref =
|
|
1367
|
+
const ref =
|
|
1368
|
+
params.ref || (await this.getResolvedRefString({ signal, fetchOptions }));
|
|
1315
1369
|
const integrationFieldsRef =
|
|
1316
1370
|
params.integrationFieldsRef ||
|
|
1317
|
-
(await this.getCachedRepository({ signal }))
|
|
1371
|
+
(await this.getCachedRepository({ signal, fetchOptions }))
|
|
1372
|
+
.integrationFieldsRef ||
|
|
1318
1373
|
undefined;
|
|
1319
1374
|
|
|
1320
1375
|
return buildQueryURL(this.endpoint, {
|
|
@@ -1368,8 +1423,15 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1368
1423
|
"url" in this.refState.httpRequest &&
|
|
1369
1424
|
this.refState.httpRequest.url
|
|
1370
1425
|
) {
|
|
1371
|
-
|
|
1372
|
-
|
|
1426
|
+
// Including "missing-host://" by default
|
|
1427
|
+
// handles a case where Next.js Route Handlers
|
|
1428
|
+
// only provide the pathname and search
|
|
1429
|
+
// parameters in the `url` property
|
|
1430
|
+
// (e.g. `/api/preview?foo=bar`).
|
|
1431
|
+
const searchParams = new URL(
|
|
1432
|
+
this.refState.httpRequest.url,
|
|
1433
|
+
"missing-host://",
|
|
1434
|
+
).searchParams;
|
|
1373
1435
|
|
|
1374
1436
|
documentID = documentID || searchParams.get("documentId");
|
|
1375
1437
|
previewToken = previewToken || searchParams.get("token");
|
|
@@ -1378,12 +1440,13 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1378
1440
|
|
|
1379
1441
|
if (documentID != null && previewToken != null) {
|
|
1380
1442
|
const document = await this.getByID(documentID, {
|
|
1381
|
-
signal: args.signal,
|
|
1382
1443
|
ref: previewToken,
|
|
1383
1444
|
lang: "*",
|
|
1445
|
+
signal: args.signal,
|
|
1446
|
+
fetchOptions: args.fetchOptions,
|
|
1384
1447
|
});
|
|
1385
1448
|
|
|
1386
|
-
const url = asLink(document, args.linkResolver);
|
|
1449
|
+
const url = asLink(document, { linkResolver: args.linkResolver });
|
|
1387
1450
|
|
|
1388
1451
|
if (typeof url === "string") {
|
|
1389
1452
|
return url;
|
|
@@ -1711,6 +1774,19 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1711
1774
|
url: string,
|
|
1712
1775
|
params: FetchParams = {},
|
|
1713
1776
|
): Promise<T> {
|
|
1777
|
+
const requestInit: RequestInitLike = {
|
|
1778
|
+
...this.fetchOptions,
|
|
1779
|
+
...params.fetchOptions,
|
|
1780
|
+
headers: {
|
|
1781
|
+
...this.fetchOptions?.headers,
|
|
1782
|
+
...params.fetchOptions?.headers,
|
|
1783
|
+
},
|
|
1784
|
+
signal:
|
|
1785
|
+
params.fetchOptions?.signal ||
|
|
1786
|
+
params.signal ||
|
|
1787
|
+
this.fetchOptions?.signal,
|
|
1788
|
+
};
|
|
1789
|
+
|
|
1714
1790
|
let job: Promise<FetchJobResult>;
|
|
1715
1791
|
|
|
1716
1792
|
// `fetchJobs` is keyed twice: first by the URL and again by is
|
|
@@ -1718,15 +1794,13 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1718
1794
|
//
|
|
1719
1795
|
// Using two keys allows us to reuse fetch requests for
|
|
1720
1796
|
// equivalent URLs, but eject when we detect unique signals.
|
|
1721
|
-
if (this.fetchJobs[url] && this.fetchJobs[url].has(
|
|
1797
|
+
if (this.fetchJobs[url] && this.fetchJobs[url].has(requestInit.signal)) {
|
|
1722
1798
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1723
|
-
job = this.fetchJobs[url].get(
|
|
1799
|
+
job = this.fetchJobs[url].get(requestInit.signal)!;
|
|
1724
1800
|
} else {
|
|
1725
1801
|
this.fetchJobs[url] = this.fetchJobs[url] || new Map();
|
|
1726
1802
|
|
|
1727
|
-
job = this.fetchFn(url,
|
|
1728
|
-
signal: params.signal,
|
|
1729
|
-
})
|
|
1803
|
+
job = this.fetchFn(url, requestInit)
|
|
1730
1804
|
.then(async (res) => {
|
|
1731
1805
|
// We can assume Prismic REST API responses
|
|
1732
1806
|
// will have a `application/json`
|
|
@@ -1747,14 +1821,14 @@ export class Client<TDocuments extends PrismicDocument = PrismicDocument> {
|
|
|
1747
1821
|
};
|
|
1748
1822
|
})
|
|
1749
1823
|
.finally(() => {
|
|
1750
|
-
this.fetchJobs[url].delete(
|
|
1824
|
+
this.fetchJobs[url].delete(requestInit.signal);
|
|
1751
1825
|
|
|
1752
1826
|
if (this.fetchJobs[url].size === 0) {
|
|
1753
1827
|
delete this.fetchJobs[url];
|
|
1754
1828
|
}
|
|
1755
1829
|
});
|
|
1756
1830
|
|
|
1757
|
-
this.fetchJobs[url].set(
|
|
1831
|
+
this.fetchJobs[url].set(requestInit.signal, job);
|
|
1758
1832
|
}
|
|
1759
1833
|
|
|
1760
1834
|
const res = await job;
|
package/src/helpers/asHTML.ts
CHANGED
|
@@ -122,10 +122,7 @@ export type HTMLRichTextSerializer =
|
|
|
122
122
|
* @internal
|
|
123
123
|
*/
|
|
124
124
|
const createDefaultHTMLRichTextSerializer = (
|
|
125
|
-
linkResolver:
|
|
126
|
-
| LinkResolverFunction<string | null | undefined>
|
|
127
|
-
| undefined
|
|
128
|
-
| null,
|
|
125
|
+
linkResolver: LinkResolverFunction | undefined | null,
|
|
129
126
|
): RichTextFunctionSerializer<string> => {
|
|
130
127
|
return (_type, node, text, children, _key) => {
|
|
131
128
|
switch (node.type) {
|
|
@@ -203,41 +200,118 @@ const wrapMapSerializerWithStringChildren = (
|
|
|
203
200
|
return wrapMapSerializer(modifiedMapSerializer);
|
|
204
201
|
};
|
|
205
202
|
|
|
203
|
+
/**
|
|
204
|
+
* Configuration that determines the output of `asHTML()`.
|
|
205
|
+
*/
|
|
206
|
+
type AsHTMLConfig = {
|
|
207
|
+
/**
|
|
208
|
+
* An optional link resolver function to resolve links.
|
|
209
|
+
* Without it you're expected to use the `routes` options from the API.
|
|
210
|
+
*/
|
|
211
|
+
linkResolver?: LinkResolverFunction | null;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* An optional Rich Text Serializer, unhandled cases will fallback to the default serializer
|
|
215
|
+
*/
|
|
216
|
+
htmlRichTextSerializer?: HTMLRichTextSerializer | null;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
220
|
+
/**
|
|
221
|
+
* @deprecated Use object-style configuration instead.
|
|
222
|
+
*/
|
|
223
|
+
type AsHTMLDeprecatedTupleConfig = [
|
|
224
|
+
linkResolver?: LinkResolverFunction | null,
|
|
225
|
+
htmlRichTextSerializer?: HTMLRichTextSerializer | null,
|
|
226
|
+
];
|
|
227
|
+
|
|
206
228
|
/**
|
|
207
229
|
* The return type of `asHTML()`.
|
|
208
230
|
*/
|
|
209
231
|
type AsHTMLReturnType<Field extends RichTextField | null | undefined> =
|
|
210
232
|
Field extends RichTextField ? string : null;
|
|
211
233
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
234
|
+
// TODO: Remove overload when we remove support for deprecated tuple-style configuration.
|
|
235
|
+
export const asHTML: {
|
|
236
|
+
/**
|
|
237
|
+
* Serializes a Rich Text or Title field to an HTML string.
|
|
238
|
+
*
|
|
239
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
240
|
+
* @param config - Configuration that determines the output of `asHTML()`
|
|
241
|
+
*
|
|
242
|
+
* @returns HTML equivalent of the provided Rich Text or Title field
|
|
243
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
244
|
+
*/
|
|
245
|
+
<Field extends RichTextField | null | undefined>(
|
|
246
|
+
richTextField: Field,
|
|
247
|
+
config?: AsHTMLConfig,
|
|
248
|
+
): AsHTMLReturnType<Field>;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Serializes a Rich Text or Title field to an HTML string.
|
|
252
|
+
*
|
|
253
|
+
* @param richTextField - A Rich Text or Title field from Prismic
|
|
254
|
+
* @param linkResolver - An optional link resolver function to resolve links,
|
|
255
|
+
* without it you're expected to use the `routes` options from the API
|
|
256
|
+
* @param htmlRichTextSerializer - An optional Rich Text Serializer, unhandled cases will fallback
|
|
257
|
+
* to the default serializer
|
|
258
|
+
*
|
|
259
|
+
* @returns HTML equivalent of the provided Rich Text or Title field
|
|
260
|
+
* @see Templating Rich Text and title fields from Prismic {@link https://prismic.io/docs/template-content-vanilla-javascript#rich-text-and-title}
|
|
261
|
+
*
|
|
262
|
+
* @deprecated Use object-style configuration instead.
|
|
263
|
+
*
|
|
264
|
+
* ```ts
|
|
265
|
+
* asHTML(field);
|
|
266
|
+
* asHTML(field, { linkResolver });
|
|
267
|
+
* asHTML(field, { htmlRichTextSerializer });
|
|
268
|
+
* asHTML(field, { linkResolver, htmlRichTextSerializer });
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
<Field extends RichTextField | null | undefined>(
|
|
272
|
+
richTextField: Field,
|
|
273
|
+
...config: AsHTMLDeprecatedTupleConfig
|
|
274
|
+
): AsHTMLReturnType<Field>;
|
|
275
|
+
} = <Field extends RichTextField | null | undefined>(
|
|
225
276
|
richTextField: Field,
|
|
226
|
-
|
|
227
|
-
|
|
277
|
+
// TODO: Rename to `config` when we remove support for deprecated tuple-style configuration.
|
|
278
|
+
...configObjectOrTuple: [config?: AsHTMLConfig] | AsHTMLDeprecatedTupleConfig
|
|
228
279
|
): AsHTMLReturnType<Field> => {
|
|
229
280
|
if (richTextField) {
|
|
281
|
+
// TODO: Remove when we remove support for deprecated tuple-style configuration.
|
|
282
|
+
const [configObjectOrLinkResolver, maybeHTMLRichTextSerializer] =
|
|
283
|
+
configObjectOrTuple;
|
|
284
|
+
let config: AsHTMLConfig;
|
|
285
|
+
if (
|
|
286
|
+
typeof configObjectOrLinkResolver === "function" ||
|
|
287
|
+
configObjectOrLinkResolver == null
|
|
288
|
+
) {
|
|
289
|
+
config = {
|
|
290
|
+
linkResolver: configObjectOrLinkResolver,
|
|
291
|
+
htmlRichTextSerializer: maybeHTMLRichTextSerializer,
|
|
292
|
+
};
|
|
293
|
+
} else {
|
|
294
|
+
config = { ...configObjectOrLinkResolver };
|
|
295
|
+
}
|
|
296
|
+
|
|
230
297
|
let serializer: RichTextFunctionSerializer<string>;
|
|
231
|
-
if (htmlRichTextSerializer) {
|
|
298
|
+
if (config.htmlRichTextSerializer) {
|
|
232
299
|
serializer = composeSerializers(
|
|
233
|
-
typeof htmlRichTextSerializer === "object"
|
|
234
|
-
? wrapMapSerializerWithStringChildren(htmlRichTextSerializer)
|
|
300
|
+
typeof config.htmlRichTextSerializer === "object"
|
|
301
|
+
? wrapMapSerializerWithStringChildren(config.htmlRichTextSerializer)
|
|
235
302
|
: (type, node, text, children, key) =>
|
|
236
|
-
|
|
237
|
-
|
|
303
|
+
// TypeScript doesn't narrow the type correctly here since it is now in a callback function, so we have to cast it here.
|
|
304
|
+
(config.htmlRichTextSerializer as HTMLRichTextFunctionSerializer)(
|
|
305
|
+
type,
|
|
306
|
+
node,
|
|
307
|
+
text,
|
|
308
|
+
children.join(""),
|
|
309
|
+
key,
|
|
310
|
+
),
|
|
311
|
+
createDefaultHTMLRichTextSerializer(config.linkResolver),
|
|
238
312
|
);
|
|
239
313
|
} else {
|
|
240
|
-
serializer = createDefaultHTMLRichTextSerializer(linkResolver);
|
|
314
|
+
serializer = createDefaultHTMLRichTextSerializer(config.linkResolver);
|
|
241
315
|
}
|
|
242
316
|
|
|
243
317
|
return serialize(richTextField, serializer).join(
|