@prismicio/api-renderer
Advanced tools
Comparing version 3.4.0 to 3.5.0-alpha.0
@@ -5,2 +5,7 @@ import type { ApiDocument } from "./ApiDocument"; | ||
import type UrlRewriter from "./URLRewriter"; | ||
/** | ||
* Context object passed to renderers. | ||
* | ||
* If the Extension is not provided, the renderers will render broken/empty document links and integration fields. | ||
*/ | ||
interface RenderContext { | ||
@@ -16,3 +21,3 @@ Extension: { | ||
}; | ||
}; | ||
} | undefined; | ||
LinkResolver: { | ||
@@ -19,0 +24,0 @@ buildUrl(params: { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const IntegrationFieldRenderer = (ctx) => ({ | ||
renderV1(content) { | ||
try { | ||
return { | ||
type: "IntegrationFields", | ||
value: ctx.Extension.encoders.integrationFields.encode(content.value), | ||
}; | ||
} | ||
catch (e) { | ||
//todo report error somewhere | ||
return { | ||
value: {}, | ||
}; | ||
} | ||
}, | ||
renderV2(_def, content) { | ||
try { | ||
return ctx.Extension.encoders.integrationFields.encode(content.value); | ||
} | ||
catch (e) { | ||
//todo report error somewhere | ||
return {}; | ||
} | ||
}, | ||
renderDefault(_def) { | ||
return null; | ||
}, | ||
renderMocks(def) { | ||
return this.renderDefault(def); | ||
}, | ||
}); | ||
const IntegrationFieldRenderer = (ctx) => { | ||
var _a; | ||
const integrationFieldsEncoder = (_a = ctx.Extension) === null || _a === void 0 ? void 0 : _a.encoders.integrationFields; | ||
return { | ||
renderV1(content) { | ||
if (!integrationFieldsEncoder) | ||
return { | ||
value: {}, | ||
}; | ||
try { | ||
return { | ||
type: "IntegrationFields", | ||
value: integrationFieldsEncoder.encode(content.value), | ||
}; | ||
} | ||
catch (e) { | ||
//todo report error somewhere | ||
return { | ||
value: {}, | ||
}; | ||
} | ||
}, | ||
renderV2(_def, content) { | ||
if (!integrationFieldsEncoder) | ||
return {}; | ||
try { | ||
return integrationFieldsEncoder.encode(content.value); | ||
} | ||
catch (e) { | ||
//todo report error somewhere | ||
return {}; | ||
} | ||
}, | ||
renderDefault(_def) { | ||
return null; | ||
}, | ||
renderMocks(def) { | ||
return this.renderDefault(def); | ||
}, | ||
}; | ||
}; | ||
exports.default = IntegrationFieldRenderer; |
@@ -26,59 +26,66 @@ "use strict"; | ||
} | ||
const DocumentLinkRenderer = (ctx) => ({ | ||
renderV1(link, fetch) { | ||
//todo check fetch type | ||
try { | ||
if (content_1.FilledDocumentLinkContent.is(link)) { | ||
const id = ctx.Extension.DocEncoder.encodeDocId(link.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [link.text]); | ||
const DocumentLinkRenderer = (ctx) => { | ||
const extension = ctx.Extension; | ||
return { | ||
renderV1(link, fetch) { | ||
if (!extension) | ||
return brokenLinkV1(link); | ||
//todo check fetch type | ||
try { | ||
if (content_1.FilledDocumentLinkContent.is(link)) { | ||
const id = extension.DocEncoder.encodeDocId(link.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [link.text]); | ||
return { | ||
type: "Link.document", | ||
value: extension.encoders.documentLinks.encode(id), | ||
}; | ||
} | ||
return { | ||
type: "Link.document", | ||
value: ctx.Extension.encoders.documentLinks.encode(id), | ||
value: { | ||
...(link.text && { text: link.text }), | ||
}, | ||
}; | ||
} | ||
return { | ||
type: "Link.document", | ||
value: { | ||
...(link.text && { text: link.text }), | ||
}, | ||
}; | ||
} | ||
catch (e) { | ||
return brokenLinkV1(link); | ||
} | ||
}, | ||
renderV2(def, link, fetch) { | ||
//todo check fetch type | ||
try { | ||
const linkText = def.allowText ? link.text : undefined; | ||
catch (e) { | ||
return brokenLinkV1(link); | ||
} | ||
}, | ||
renderV2(def, link, fetch) { | ||
if (!extension) | ||
return brokenLinkV2(link, def); | ||
//todo check fetch type | ||
try { | ||
const linkText = def.allowText ? link.text : undefined; | ||
if (content_1.FilledDocumentLinkContent.is(link)) { | ||
const id = extension.DocEncoder.encodeDocId(link.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [linkText]); | ||
return extension.encoders.documentLinks.encode(id); | ||
} | ||
return { | ||
link_type: "Document", | ||
...(linkText && { text: linkText }), | ||
}; | ||
} | ||
catch (e) { | ||
return brokenLinkV2(link, def); | ||
} | ||
}, | ||
renderMocks(def, link) { | ||
if (content_1.FilledDocumentLinkContent.is(link)) { | ||
const id = ctx.Extension.DocEncoder.encodeDocId(link.id, fetch === null || fetch === void 0 ? void 0 : fetch.uuid, [linkText]); | ||
return ctx.Extension.encoders.documentLinks.encode(id); | ||
const type = (models_1.DocumentLinkDef.is(def) && def.customtypes[0]) || "mock"; | ||
return { | ||
id: link.id, | ||
...(def.allowText && link.text && { text: link.text }), | ||
link_type: "Document", | ||
type, | ||
tags: [], | ||
lang: "en-us", | ||
slug: null, | ||
first_publication_date: "1970-01-01T00:00:01+0000", | ||
last_publication_date: "1970-01-01T01:00:00+0000", | ||
}; | ||
} | ||
return { | ||
link_type: "Document", | ||
...(linkText && { text: linkText }), | ||
}; | ||
} | ||
catch (e) { | ||
return brokenLinkV2(link, def); | ||
} | ||
}, | ||
renderMocks(def, link) { | ||
if (content_1.FilledDocumentLinkContent.is(link)) { | ||
const type = (models_1.DocumentLinkDef.is(def) && def.customtypes[0]) || "mock"; | ||
return { | ||
id: link.id, | ||
...(def.allowText && link.text && { text: link.text }), | ||
link_type: "Document", | ||
type, | ||
tags: [], | ||
lang: "en-us", | ||
slug: null, | ||
first_publication_date: "1970-01-01T00:00:01+0000", | ||
last_publication_date: "1970-01-01T01:00:00+0000", | ||
}; | ||
} | ||
return brokenLinkV2(link, def); | ||
}, | ||
}); | ||
}, | ||
}; | ||
}; | ||
exports.default = DocumentLinkRenderer; |
{ | ||
"name": "@prismicio/api-renderer", | ||
"version": "3.4.0", | ||
"version": "3.5.0-alpha.0", | ||
"description": "Prismic renderers for the content API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -6,17 +6,24 @@ import type { ApiDocument } from "./ApiDocument" | ||
/** | ||
* Context object passed to renderers. | ||
* | ||
* If the Extension is not provided, the renderers will render broken/empty document links and integration fields. | ||
*/ | ||
interface RenderContext { | ||
Extension: { | ||
DocEncoder: { | ||
encodeDocId( | ||
documentId: string, | ||
fetchUuidOrRelationName: string | undefined, | ||
additionalData: (string | undefined)[], | ||
): string | ||
} | ||
encoders: { | ||
documentLinks: IDEncoder | ||
documentUrls: IDEncoder | ||
integrationFields: IDEncoder | ||
} | ||
} | ||
Extension: | ||
| { | ||
DocEncoder: { | ||
encodeDocId( | ||
documentId: string, | ||
fetchUuidOrRelationName: string | undefined, | ||
additionalData: (string | undefined)[], | ||
): string | ||
} | ||
encoders: { | ||
documentLinks: IDEncoder | ||
documentUrls: IDEncoder | ||
integrationFields: IDEncoder | ||
} | ||
} | ||
| undefined | ||
LinkResolver: { | ||
@@ -23,0 +30,0 @@ buildUrl(params: { |
@@ -7,37 +7,45 @@ import type { IntegrationFieldContent } from "@prismicio/types-internal/lib/content" | ||
const IntegrationFieldRenderer: (ctx: RenderContext) => Renderer<IntegrationField, IntegrationFieldContent> = ( | ||
ctx, | ||
) => ({ | ||
renderV1(content: IntegrationFieldContent): unknown { | ||
try { | ||
return { | ||
type: "IntegrationFields", | ||
value: ctx.Extension.encoders.integrationFields.encode(content.value), | ||
const IntegrationFieldRenderer: (ctx: RenderContext) => Renderer<IntegrationField, IntegrationFieldContent> = (ctx) => { | ||
const integrationFieldsEncoder = ctx.Extension?.encoders.integrationFields | ||
return { | ||
renderV1(content: IntegrationFieldContent): unknown { | ||
if (!integrationFieldsEncoder) | ||
return { | ||
value: {}, | ||
} | ||
try { | ||
return { | ||
type: "IntegrationFields", | ||
value: integrationFieldsEncoder.encode(content.value), | ||
} | ||
} catch (e) { | ||
//todo report error somewhere | ||
return { | ||
value: {}, | ||
} | ||
} | ||
} catch (e) { | ||
//todo report error somewhere | ||
return { | ||
value: {}, | ||
} | ||
} | ||
}, | ||
}, | ||
renderV2(_def: IntegrationField, content: IntegrationFieldContent): unknown { | ||
try { | ||
return ctx.Extension.encoders.integrationFields.encode(content.value) | ||
} catch (e) { | ||
//todo report error somewhere | ||
return {} | ||
} | ||
}, | ||
renderV2(_def: IntegrationField, content: IntegrationFieldContent): unknown { | ||
if (!integrationFieldsEncoder) return {} | ||
renderDefault(_def: IntegrationField): unknown { | ||
return null | ||
}, | ||
try { | ||
return integrationFieldsEncoder.encode(content.value) | ||
} catch (e) { | ||
//todo report error somewhere | ||
return {} | ||
} | ||
}, | ||
renderMocks(def: IntegrationField): unknown { | ||
return this.renderDefault(def) | ||
}, | ||
}) | ||
renderDefault(_def: IntegrationField): unknown { | ||
return null | ||
}, | ||
renderMocks(def: IntegrationField): unknown { | ||
return this.renderDefault(def) | ||
}, | ||
} | ||
} | ||
export default IntegrationFieldRenderer |
@@ -32,67 +32,74 @@ import { type DocumentLinkContent, FilledDocumentLinkContent } from "@prismicio/types-internal/lib/content" | ||
ctx, | ||
) => ({ | ||
renderV1(link: DocumentLinkContent, fetch: DocRelation | undefined): unknown { | ||
//todo check fetch type | ||
try { | ||
if (FilledDocumentLinkContent.is(link)) { | ||
const id = ctx.Extension.DocEncoder.encodeDocId(link.id, fetch?.uuid, [link.text]) | ||
) => { | ||
const extension = ctx.Extension | ||
return { | ||
renderV1(link: DocumentLinkContent, fetch: DocRelation | undefined): unknown { | ||
if (!extension) return brokenLinkV1(link) | ||
//todo check fetch type | ||
try { | ||
if (FilledDocumentLinkContent.is(link)) { | ||
const id = extension.DocEncoder.encodeDocId(link.id, fetch?.uuid, [link.text]) | ||
return { | ||
type: "Link.document", | ||
value: extension.encoders.documentLinks.encode(id), | ||
} | ||
} | ||
return { | ||
type: "Link.document", | ||
value: ctx.Extension.encoders.documentLinks.encode(id), | ||
value: { | ||
...(link.text && { text: link.text }), | ||
}, | ||
} | ||
} catch (e) { | ||
return brokenLinkV1(link) | ||
} | ||
}, | ||
return { | ||
type: "Link.document", | ||
value: { | ||
...(link.text && { text: link.text }), | ||
}, | ||
} | ||
} catch (e) { | ||
return brokenLinkV1(link) | ||
} | ||
}, | ||
renderV2(def: DocumentLinkDef | AnyLinkDef, link: DocumentLinkContent, fetch: DocRelation | undefined): unknown { | ||
if (!extension) return brokenLinkV2(link, def) | ||
renderV2(def: DocumentLinkDef | AnyLinkDef, link: DocumentLinkContent, fetch: DocRelation | undefined): unknown { | ||
//todo check fetch type | ||
try { | ||
const linkText = def.allowText ? link.text : undefined | ||
//todo check fetch type | ||
try { | ||
const linkText = def.allowText ? link.text : undefined | ||
if (FilledDocumentLinkContent.is(link)) { | ||
const id = ctx.Extension.DocEncoder.encodeDocId(link.id, fetch?.uuid, [linkText]) | ||
if (FilledDocumentLinkContent.is(link)) { | ||
const id = extension.DocEncoder.encodeDocId(link.id, fetch?.uuid, [linkText]) | ||
return ctx.Extension.encoders.documentLinks.encode(id) | ||
} | ||
return extension.encoders.documentLinks.encode(id) | ||
} | ||
return { | ||
link_type: "Document", | ||
...(linkText && { text: linkText }), | ||
return { | ||
link_type: "Document", | ||
...(linkText && { text: linkText }), | ||
} | ||
} catch (e) { | ||
return brokenLinkV2(link, def) | ||
} | ||
} catch (e) { | ||
return brokenLinkV2(link, def) | ||
} | ||
}, | ||
}, | ||
renderMocks(def: DocumentLinkDef | AnyLinkDef, link: DocumentLinkContent): unknown { | ||
if (FilledDocumentLinkContent.is(link)) { | ||
const type = (DocumentLinkDef.is(def) && def.customtypes[0]) || "mock" | ||
renderMocks(def: DocumentLinkDef | AnyLinkDef, link: DocumentLinkContent): unknown { | ||
if (FilledDocumentLinkContent.is(link)) { | ||
const type = (DocumentLinkDef.is(def) && def.customtypes[0]) || "mock" | ||
return { | ||
id: link.id, | ||
...(def.allowText && link.text && { text: link.text }), | ||
link_type: "Document", | ||
type, | ||
tags: [], | ||
lang: "en-us", | ||
slug: null, | ||
first_publication_date: "1970-01-01T00:00:01+0000", | ||
last_publication_date: "1970-01-01T01:00:00+0000", | ||
return { | ||
id: link.id, | ||
...(def.allowText && link.text && { text: link.text }), | ||
link_type: "Document", | ||
type, | ||
tags: [], | ||
lang: "en-us", | ||
slug: null, | ||
first_publication_date: "1970-01-01T00:00:01+0000", | ||
last_publication_date: "1970-01-01T01:00:00+0000", | ||
} | ||
} | ||
} | ||
return brokenLinkV2(link, def) | ||
}, | ||
}) | ||
return brokenLinkV2(link, def) | ||
}, | ||
} | ||
} | ||
export default DocumentLinkRenderer |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1052222
24939
2