@uniformdev/canvas-contentful 12.2.0 → 12.2.1-alpha.107
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/LICENSE.txt +1 -1
- package/README.md +3 -3
- package/dist/index.d.ts +140 -0
- package/dist/index.esm.js +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +24 -24
- package/dist/cjs/ContentfulClientList.d.ts +0 -26
- package/dist/cjs/ContentfulClientList.js +0 -1
- package/dist/cjs/contentfulRichTextToHtmlEnhancer.d.ts +0 -3
- package/dist/cjs/contentfulRichTextToHtmlEnhancer.js +0 -1
- package/dist/cjs/createContentfulEnhancer.d.ts +0 -47
- package/dist/cjs/createContentfulEnhancer.js +0 -1
- package/dist/cjs/index.d.ts +0 -3
- package/dist/cjs/index.js +0 -1
- package/dist/esm/ContentfulClientList.d.ts +0 -26
- package/dist/esm/ContentfulClientList.js +0 -1
- package/dist/esm/contentfulRichTextToHtmlEnhancer.d.ts +0 -3
- package/dist/esm/contentfulRichTextToHtmlEnhancer.js +0 -1
- package/dist/esm/createContentfulEnhancer.d.ts +0 -47
- package/dist/esm/createContentfulEnhancer.js +0 -1
- package/dist/esm/index.d.ts +0 -3
- package/dist/esm/index.js +0 -1
package/LICENSE.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
© 2021 Uniform Systems, Inc. All Rights Reserved.
|
|
1
|
+
© 2021 Uniform Systems, Inc. All Rights Reserved.
|
|
2
2
|
See details of Uniform Systems, Inc. Master Subscription Agreement here: https://uniform.dev/eula
|
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
Contentful data enhancers for Uniform Canvas
|
|
2
|
-
|
|
3
|
-
part of the [Uniform Platform](https://uniform.app). See our [documentation](https://docs.uniform.app) for more details.
|
|
1
|
+
Contentful data enhancers for Uniform Canvas
|
|
2
|
+
|
|
3
|
+
part of the [Uniform Platform](https://uniform.app). See our [documentation](https://docs.uniform.app) for more details.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { EnhancerContext, ComponentParameter, ComponentInstance, LimitPolicy, ComponentParameterEnhancer, ComponentParameterEnhancerFunction } from '@uniformdev/canvas';
|
|
2
|
+
import { ContentfulClientApi, Sys } from 'contentful';
|
|
3
|
+
|
|
4
|
+
interface AddClientArgs {
|
|
5
|
+
/**
|
|
6
|
+
* The Contentful source public ID that this client maps to in the composition data.
|
|
7
|
+
* This is used to enable multiple Contentful spaces/environments as data sources.
|
|
8
|
+
* If unspecified, the client will be the default source that is used when no source public ID
|
|
9
|
+
* is in the data, or the source ID is 'default'.
|
|
10
|
+
*/
|
|
11
|
+
source?: string;
|
|
12
|
+
/** The Contentful client instance to use when fetching published data */
|
|
13
|
+
client: ContentfulClientApi;
|
|
14
|
+
/**
|
|
15
|
+
* The Contentful client instance to use when fetching preview data.
|
|
16
|
+
* If the preview client is not passed, it defaults to the client.
|
|
17
|
+
*/
|
|
18
|
+
previewClient?: ContentfulClientApi;
|
|
19
|
+
}
|
|
20
|
+
declare class ContentfulClientList {
|
|
21
|
+
private _clients;
|
|
22
|
+
constructor(clients?: AddClientArgs[] | AddClientArgs);
|
|
23
|
+
addClient({ source, client, previewClient }: AddClientArgs): void;
|
|
24
|
+
getClient({ source, isPreviewClient, }: {
|
|
25
|
+
source?: string;
|
|
26
|
+
isPreviewClient?: boolean;
|
|
27
|
+
}): ContentfulClientApi | undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The default shape of the Contentful entry. Note that this can change if the query is altered. */
|
|
31
|
+
declare type ContentfulEntryResult<TFields> = {
|
|
32
|
+
/**
|
|
33
|
+
* The shape of the `fields` that the Contentful REST API is expected to return for this entry
|
|
34
|
+
* https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/entries/entry
|
|
35
|
+
*
|
|
36
|
+
* These should line up with the fields in your content model(s) that are allowed for this component.
|
|
37
|
+
*/
|
|
38
|
+
fields: TFields;
|
|
39
|
+
/** System fields returned by the Contentful API. If you modify the query parameters to fetch more than fields, you may get more sys data than this. */
|
|
40
|
+
sys: Partial<Pick<Sys, 'id' | 'type'>>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
declare type EntrySelectorParameterValue = {
|
|
44
|
+
entryId: string;
|
|
45
|
+
source?: string;
|
|
46
|
+
} | string | null | undefined;
|
|
47
|
+
declare type CreateContentfulQueryOptions<TContext extends EnhancerContext = EnhancerContext> = {
|
|
48
|
+
/** Canvas parameter name being queried for. Not defined if using batching. */
|
|
49
|
+
parameterName?: string;
|
|
50
|
+
/** Canvas parameter value being fetched. Not defined if using batching. */
|
|
51
|
+
parameter?: ComponentParameter<EntrySelectorParameterValue>;
|
|
52
|
+
/** Component containing the parameter being fetched. Not defined if using batching. */
|
|
53
|
+
component?: ComponentInstance;
|
|
54
|
+
/** The default Contentful query expression (select fields + include 1 layer of references) */
|
|
55
|
+
defaultQuery: any;
|
|
56
|
+
/** The enhancer context provided to the enhance() function */
|
|
57
|
+
context: TContext;
|
|
58
|
+
};
|
|
59
|
+
/** The default shape of the result value of the Contentful enhancer. Note that this can change if the query is altered. */
|
|
60
|
+
declare type ContentfulEnhancerResult<TFields> = ContentfulEntryResult<TFields> | null;
|
|
61
|
+
declare type CreateContentfulEnhancerOptions = {
|
|
62
|
+
/** Either a list of Contentful clients for use with multi-space/environment-enabled Canvas projects.
|
|
63
|
+
* Or a single Contentful client for use with legacy Canvas data.*/
|
|
64
|
+
client: ContentfulClientApi | ContentfulClientList;
|
|
65
|
+
/** @deprecated Contentful client instance to use for fetching preview content.
|
|
66
|
+
* This client is _only_ relevant when the `client` property is a single Contentful client intended for use with
|
|
67
|
+
* legacy Canvas data. Conversely, if you use a `ContentfulClientList` for the `client` property, any value for `previewClient`
|
|
68
|
+
* will be ignored. To avoid deprecated use, you should switch to using a `ContentfulClientList` for the `client` property. */
|
|
69
|
+
previewClient?: ContentfulClientApi;
|
|
70
|
+
/** Creates the Contentful client's query params for specific parameters. See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters */
|
|
71
|
+
createQuery?: (options: CreateContentfulQueryOptions) => any | undefined;
|
|
72
|
+
useBatching?: boolean;
|
|
73
|
+
limitPolicy?: LimitPolicy;
|
|
74
|
+
};
|
|
75
|
+
declare const CANVAS_CONTENTFUL_PARAMETER_TYPES: readonly string[];
|
|
76
|
+
declare function createContentfulEnhancer({ client, previewClient, createQuery, useBatching, limitPolicy, }: CreateContentfulEnhancerOptions): ComponentParameterEnhancer<EntrySelectorParameterValue, ContentfulEnhancerResult<unknown>>;
|
|
77
|
+
|
|
78
|
+
declare type ContentfulMultiEntryParameterValue = {
|
|
79
|
+
entries: string[];
|
|
80
|
+
source: string;
|
|
81
|
+
} | null | undefined;
|
|
82
|
+
declare type CreateContentfulMultiEntryQueryOptions<TContext extends EnhancerContext = EnhancerContext> = {
|
|
83
|
+
/** Canvas parameter name being queried for */
|
|
84
|
+
parameterName: string;
|
|
85
|
+
/** Canvas parameter value being fetched */
|
|
86
|
+
parameter: ComponentParameter<ContentfulMultiEntryParameterValue>;
|
|
87
|
+
/** Component containing the parameter being fetched */
|
|
88
|
+
component: ComponentInstance;
|
|
89
|
+
/** The default Contentful query expression (select fields + include 1 layer of references) */
|
|
90
|
+
defaultQuery: any;
|
|
91
|
+
/** The enhancer context provided to the enhance() function */
|
|
92
|
+
context: TContext;
|
|
93
|
+
};
|
|
94
|
+
/** The default shape of the result value of the Contentful Multi Entry enhancer. Note that this can change if the query is altered. */
|
|
95
|
+
declare type ContentfulMultiEntryEnhancerResult<TFields> = ContentfulEntryResult<TFields>[] | null;
|
|
96
|
+
declare type CreateContentfulMultiEntryEnhancerOptions = {
|
|
97
|
+
/** Either a list of Contentful clients for use with multi-space/environment-enabled Canvas projects. */
|
|
98
|
+
clients: ContentfulClientList;
|
|
99
|
+
/** Creates the Contentful client's query params for specific parameters. See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters */
|
|
100
|
+
createQuery?: (options: CreateContentfulMultiEntryQueryOptions) => any | undefined;
|
|
101
|
+
limitPolicy?: LimitPolicy;
|
|
102
|
+
};
|
|
103
|
+
declare const CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES: readonly string[];
|
|
104
|
+
declare function createContentfulMultiEnhancer({ clients, createQuery, limitPolicy, }: CreateContentfulMultiEntryEnhancerOptions): ComponentParameterEnhancer<ContentfulMultiEntryParameterValue, ContentfulMultiEntryEnhancerResult<unknown>>;
|
|
105
|
+
|
|
106
|
+
declare const CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES: readonly string[];
|
|
107
|
+
declare type ContentfulQueryParameterValue = {
|
|
108
|
+
source: string;
|
|
109
|
+
contentType: string;
|
|
110
|
+
count: number;
|
|
111
|
+
sortBy?: string;
|
|
112
|
+
sortOrder?: 'asc' | 'desc';
|
|
113
|
+
} | null | undefined;
|
|
114
|
+
/** The default shape of the result value of the Contentful Query enhancer. Note that this can change if the query is altered. */
|
|
115
|
+
declare type ContentfulQueryEnhancerResult<TFields> = ContentfulEntryResult<TFields>[] | null;
|
|
116
|
+
declare type CreateContentfulQueryApiQueryOptions<TContext extends EnhancerContext = EnhancerContext> = {
|
|
117
|
+
/** Canvas parameter name being queried for */
|
|
118
|
+
parameterName: string;
|
|
119
|
+
/** Canvas parameter value being fetched */
|
|
120
|
+
parameter: ComponentParameter<ContentfulQueryParameterValue>;
|
|
121
|
+
/** Component containing the parameter being fetched */
|
|
122
|
+
component: ComponentInstance;
|
|
123
|
+
/** The default Contentful query expression (select fields + include 1 layer of references) */
|
|
124
|
+
defaultQuery: any;
|
|
125
|
+
/** The enhancer context provided to the enhance() function */
|
|
126
|
+
context: TContext;
|
|
127
|
+
};
|
|
128
|
+
declare type CreateContentfulQueryEnhancerOptions = {
|
|
129
|
+
/** Either a list of Contentful clients for use with multi-space/environment-enabled Canvas projects. */
|
|
130
|
+
clients: ContentfulClientList;
|
|
131
|
+
/** Creates the Contentful client's query params for specific parameters. See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters */
|
|
132
|
+
createQuery?: (options: CreateContentfulQueryApiQueryOptions) => any | undefined;
|
|
133
|
+
limitPolicy?: LimitPolicy;
|
|
134
|
+
};
|
|
135
|
+
declare function createContentfulQueryEnhancer({ clients, createQuery, limitPolicy, }: CreateContentfulQueryEnhancerOptions): ComponentParameterEnhancer<ContentfulQueryParameterValue, ContentfulQueryEnhancerResult<unknown>>;
|
|
136
|
+
|
|
137
|
+
declare type EnhancerValue = ContentfulEntryResult<unknown> | ContentfulEntryResult<unknown>[] | null;
|
|
138
|
+
declare const contentfulRichTextToHtmlEnhancer: ComponentParameterEnhancerFunction<EnhancerValue>;
|
|
139
|
+
|
|
140
|
+
export { AddClientArgs, CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES, CANVAS_CONTENTFUL_PARAMETER_TYPES, CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES, ContentfulClientList, ContentfulEnhancerResult, ContentfulMultiEntryEnhancerResult, ContentfulMultiEntryParameterValue, ContentfulQueryEnhancerResult, ContentfulQueryParameterValue, CreateContentfulEnhancerOptions, CreateContentfulMultiEntryEnhancerOptions, CreateContentfulMultiEntryQueryOptions, CreateContentfulQueryApiQueryOptions, CreateContentfulQueryEnhancerOptions, CreateContentfulQueryOptions, EntrySelectorParameterValue, contentfulRichTextToHtmlEnhancer, createContentfulEnhancer, createContentfulMultiEnhancer, createContentfulQueryEnhancer };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createBatchEnhancer as S,UniqueBatchEntries as _}from"@uniformdev/canvas";var L=class{constructor(t){this._clients={},Array.isArray(t)?t.forEach(n=>this.addClient(n)):t&&this.addClient(t)}addClient({source:t="default",client:n,previewClient:o}){if(this._clients[t])throw new Error(`The source ${t} is always registered`);if(!n)throw new Error("You must provide a Contentful client for the ContentfulClientList");this._clients[t]={client:n,previewClient:o||n}}getClient({source:t="default",isPreviewClient:n}){let o=this._clients[t];if(!!o)return n?o.previewClient:o.client}};import{createLimitPolicy as Q}from"@uniformdev/canvas";var x=Q({throttle:{limit:55,interval:1e3}});function P(e){return typeof e=="string"?e:typeof e=="object"&&e&&"error"in e?e.error:e instanceof Error?e.toString():JSON.stringify(e,null,2)}function V(e,t){if(!!e)return`${t==="desc"?"-":""}${e}`}function A({clients:e,parameterValue:t,parameterName:n,component:o,context:g}){let{source:i="default"}=t,a=e.getClient({source:i,isPreviewClient:g.preview});if(!a)throw new Error(`No Contentful client could be resolved for source key '${i}' referenced in parameter '${n} in component '${o.type}'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.`);return a}var $={select:"fields",include:1},k=Object.freeze(["contentfulEntry"]);function j(e){return e instanceof L}function se({client:e,previewClient:t,createQuery:n,useBatching:o,limitPolicy:g}){if(!e)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the `client` or `clients` property.");let a=(()=>{if(j(e))return e;let s=new L;return s.addClient({client:e,previewClient:t}),s})(),y=g||x;return o?S({handleBatch:async s=>{var c;let l=s.reduce((u,r)=>{let{parameter:f,parameterName:C,component:h,context:m}=r.args,d=f.value;if(!O(d))return u;let E=b({parameterValue:d,parameterName:C,clients:a,component:h,context:m}),p="";if(T(d))p="legacy-group";else{let{source:I="default"}=d;p=I}return u[p]&&Array.isArray(u[p].tasks)?u[p].tasks.push(r):u[p]={client:E,tasks:[r]},u},{});try{console.time("fetch all entries");for await(let[u,r]of Object.entries(l)){let{context:f,component:C}=r.tasks[0].args,h=(c=n==null?void 0:n({component:C,defaultQuery:{...$},context:f}))!=null?c:$,m=new _(r.tasks,E=>T(E.parameter.value)?E.parameter.value:E.parameter.value.entryId),d=Object.keys(m.groups);console.time(`fetch entries ${u}`);try{(await r.client.getEntries({"sys.id[in]":d.join(","),limit:d.length,...h})).items.forEach(p=>{m.resolveKey(p.sys.id,p)}),m.resolveRemaining(null)}finally{console.timeEnd(`fetch entries ${u}`)}}console.timeEnd("fetch all entries")}catch(u){let r=P(u),f=new Error(`Failed loading Contentful entries batch (${s.length}) ${r}`);s.forEach(C=>C.reject(f))}},shouldQueue:({parameter:s})=>R(s),limitPolicy:y}):{enhanceOne:async function({parameter:l,parameterName:c,component:u,context:r}){var f,C;if(R(l)){if(!O(l.value))return null;let h=b({clients:a,parameterName:c,parameterValue:l.value,component:u,context:r}),m=T(l.value)?l.value:l.value.entryId,d=(f=n==null?void 0:n({parameter:l,parameterName:c,component:u,defaultQuery:{...$},context:r}))!=null?f:$;try{return console.time(`fetch entry ${m}`),await h.getEntry(m,d)}catch(E){let p=P(E);throw T(l.value)?new Error(`Failed loading Contentful entry '${l.value}' referenced in parameter '${c}': ${p}`):new Error(`Failed loading Contentful entry '${m}' from source '${(C=l.value.source)!=null?C:"default"}' referenced in parameter '${c}': ${p}`)}finally{console.timeEnd(`fetch entry ${m}`)}}},limitPolicy:y}}function R(e){var t;return e.type===k[0]&&(((t=e.value)==null?void 0:t.entryId)||typeof e.value=="string")}function T(e){return typeof e=="string"}function O(e){return!(!e||!T(e)&&!e.entryId)}function b({clients:e,parameterValue:t,parameterName:n,component:o,context:g}){if(T(t)){let y=e.getClient({isPreviewClient:g.preview});if(!y)throw new Error(`Parameter '${n}' in component '${o.type}' has a value '${t}' that is not compatible with multi-space/environment usage. If you wish to use multiple spaces/environments, you must convert your Canvas component parameters to the multi-space/environment compatible version. Otherwise, you can continue to use your parameters as-is, but must specify one of the clients provided to the Contentful enhancer as the 'default' client by registering it without specifying a source key.`);return y}let{source:i="default"}=t,a=e.getClient({source:i,isPreviewClient:g.preview});if(!a)throw new Error(`No Contentful client could be resolved for source key '${i}' referenced in parameter '${n} in component '${o.type}'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.`);return a}var F={select:"fields",include:1},Y=Object.freeze(["contentfulMultiEntry"]);function Ce({clients:e,createQuery:t,limitPolicy:n}){if(!e)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the ContentfulClientList.");return{enhanceOne:async function({parameter:i,parameterName:a,component:y,context:s}){var l,c;if(B(i)){if(!U(i.value))return null;let u=A({clients:e,parameterName:a,parameterValue:i.value,component:y,context:s}),r=i.value.entries,f=(l=t==null?void 0:t({parameter:i,parameterName:a,component:y,defaultQuery:{...F},context:s}))!=null?l:F;try{return console.time(`fetch entries ${r.join()}`),(await u.getEntries({"sys.id[in]":r.join(),limit:r.length,...f})).items}catch(C){let h=P(C);throw new Error(`Failed loading Contentful entries '${r.join()}' from source '${(c=i.value.source)!=null?c:"default"}' referenced in parameter '${a}': ${h}`)}finally{console.timeEnd(`fetch entries ${r.join()}`)}}},limitPolicy:n||x}}function B(e){return e.type===Y[0]}function U(e){var t;return!(!e||!((t=e.entries)==null?void 0:t.length))}var q=Object.freeze(["contentfulQuery"]),M={select:"fields",include:1};function ye({clients:e,createQuery:t,limitPolicy:n}){if(!e)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the ContentfulClientList.");return{enhanceOne:async function({parameter:i,parameterName:a,component:y,context:s}){var l,c;if(D(i)){if(!z(i.value))return null;let u=A({clients:e,parameterName:a,parameterValue:i.value,component:y,context:s}),r=i.value,f=H(r.count),C=(l=t==null?void 0:t({parameter:i,parameterName:a,component:y,defaultQuery:{...M},context:s}))!=null?l:M,h=`fetch query: ${f} entries with '${r.contentType}' content type`;try{return console.time(h),(await u.getEntries({content_type:r.contentType,order:V(r.sortBy,r.sortOrder),limit:f,...C})).items}catch(m){let d=P(m);throw new Error(`Failed loading Contentful entries with '${r.contentType}' content type from source '${(c=r.source)!=null?c:"default"}' referenced in parameter '${a}': ${d}`)}finally{console.timeEnd(h)}}},limitPolicy:n||x}}function D(e){return e.type===q[0]}function z(e){return!(!e||!e.source||!e.contentType||typeof e.count=="undefined")}function H(e){return!e||e<1?1:e>1e3?1e3:e}import{documentToHtmlString as K}from"@contentful/rich-text-html-renderer";var Ee=({parameter:e})=>{let t=e.value;if(!t)return t;if(!G(t))return N(t),t;for(let n of t)N(n);return t};function N(e){var t;typeof(e==null?void 0:e.fields)=="object"&&Object.entries((t=e.fields)!=null?t:{}).forEach(([n,o])=>{typeof o=="object"&&"nodeType"in o&&o.nodeType==="document"&&(e.fields[n]=K(o))})}function G(e){return Array.isArray(e)}export{Y as CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES,k as CANVAS_CONTENTFUL_PARAMETER_TYPES,q as CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES,L as ContentfulClientList,Ee as contentfulRichTextToHtmlEnhancer,se as createContentfulEnhancer,Ce as createContentfulMultiEnhancer,ye as createContentfulQueryEnhancer};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var q=Object.create;var L=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var H=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty;var O=e=>L(e,"__esModule",{value:!0});var G=(e,t)=>{O(e);for(var n in t)L(e,n,{get:t[n],enumerable:!0})},J=(e,t,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of z(t))!K.call(e,o)&&o!=="default"&&L(e,o,{get:()=>t[o],enumerable:!(n=D(t,o))||n.enumerable});return e},R=e=>J(O(L(e!=null?q(H(e)):{},"default",e&&e.__esModule&&"default"in e?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e);G(exports,{CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES:()=>_,CANVAS_CONTENTFUL_PARAMETER_TYPES:()=>M,CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES:()=>k,ContentfulClientList:()=>T,contentfulRichTextToHtmlEnhancer:()=>le,createContentfulEnhancer:()=>X,createContentfulMultiEnhancer:()=>Z,createContentfulQueryEnhancer:()=>ne});var $=R(require("@uniformdev/canvas"));var T=class{constructor(t){this._clients={},Array.isArray(t)?t.forEach(n=>this.addClient(n)):t&&this.addClient(t)}addClient({source:t="default",client:n,previewClient:o}){if(this._clients[t])throw new Error(`The source ${t} is always registered`);if(!n)throw new Error("You must provide a Contentful client for the ContentfulClientList");this._clients[t]={client:n,previewClient:o||n}}getClient({source:t="default",isPreviewClient:n}){let o=this._clients[t];if(!!o)return n?o.previewClient:o.client}};var b=R(require("@uniformdev/canvas")),w=(0,b.createLimitPolicy)({throttle:{limit:55,interval:1e3}});function P(e){return typeof e=="string"?e:typeof e=="object"&&e&&"error"in e?e.error:e instanceof Error?e.toString():JSON.stringify(e,null,2)}function F(e,t){if(!!e)return`${t==="desc"?"-":""}${e}`}function A({clients:e,parameterValue:t,parameterName:n,component:o,context:g}){let{source:i="default"}=t,a=e.getClient({source:i,isPreviewClient:g.preview});if(!a)throw new Error(`No Contentful client could be resolved for source key '${i}' referenced in parameter '${n} in component '${o.type}'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.`);return a}var V={select:"fields",include:1},M=Object.freeze(["contentfulEntry"]);function W(e){return e instanceof T}function X({client:e,previewClient:t,createQuery:n,useBatching:o,limitPolicy:g}){if(!e)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the `client` or `clients` property.");let a=(()=>{if(W(e))return e;let s=new T;return s.addClient({client:e,previewClient:t}),s})(),y=g||w;return o?(0,$.createBatchEnhancer)({handleBatch:async s=>{var c;let l=s.reduce((u,r)=>{let{parameter:f,parameterName:C,component:h,context:m}=r.args,d=f.value;if(!I(d))return u;let E=Q({parameterValue:d,parameterName:C,clients:a,component:h,context:m}),p="";if(x(d))p="legacy-group";else{let{source:U="default"}=d;p=U}return u[p]&&Array.isArray(u[p].tasks)?u[p].tasks.push(r):u[p]={client:E,tasks:[r]},u},{});try{console.time("fetch all entries");for await(let[u,r]of Object.entries(l)){let{context:f,component:C}=r.tasks[0].args,h=(c=n==null?void 0:n({component:C,defaultQuery:{...V},context:f}))!=null?c:V,m=new $.UniqueBatchEntries(r.tasks,E=>x(E.parameter.value)?E.parameter.value:E.parameter.value.entryId),d=Object.keys(m.groups);console.time(`fetch entries ${u}`);try{(await r.client.getEntries({"sys.id[in]":d.join(","),limit:d.length,...h})).items.forEach(p=>{m.resolveKey(p.sys.id,p)}),m.resolveRemaining(null)}finally{console.timeEnd(`fetch entries ${u}`)}}console.timeEnd("fetch all entries")}catch(u){let r=P(u),f=new Error(`Failed loading Contentful entries batch (${s.length}) ${r}`);s.forEach(C=>C.reject(f))}},shouldQueue:({parameter:s})=>N(s),limitPolicy:y}):{enhanceOne:async function({parameter:l,parameterName:c,component:u,context:r}){var f,C;if(N(l)){if(!I(l.value))return null;let h=Q({clients:a,parameterName:c,parameterValue:l.value,component:u,context:r}),m=x(l.value)?l.value:l.value.entryId,d=(f=n==null?void 0:n({parameter:l,parameterName:c,component:u,defaultQuery:{...V},context:r}))!=null?f:V;try{return console.time(`fetch entry ${m}`),await h.getEntry(m,d)}catch(E){let p=P(E);throw x(l.value)?new Error(`Failed loading Contentful entry '${l.value}' referenced in parameter '${c}': ${p}`):new Error(`Failed loading Contentful entry '${m}' from source '${(C=l.value.source)!=null?C:"default"}' referenced in parameter '${c}': ${p}`)}finally{console.timeEnd(`fetch entry ${m}`)}}},limitPolicy:y}}function N(e){var t;return e.type===M[0]&&(((t=e.value)==null?void 0:t.entryId)||typeof e.value=="string")}function x(e){return typeof e=="string"}function I(e){return!(!e||!x(e)&&!e.entryId)}function Q({clients:e,parameterValue:t,parameterName:n,component:o,context:g}){if(x(t)){let y=e.getClient({isPreviewClient:g.preview});if(!y)throw new Error(`Parameter '${n}' in component '${o.type}' has a value '${t}' that is not compatible with multi-space/environment usage. If you wish to use multiple spaces/environments, you must convert your Canvas component parameters to the multi-space/environment compatible version. Otherwise, you can continue to use your parameters as-is, but must specify one of the clients provided to the Contentful enhancer as the 'default' client by registering it without specifying a source key.`);return y}let{source:i="default"}=t,a=e.getClient({source:i,isPreviewClient:g.preview});if(!a)throw new Error(`No Contentful client could be resolved for source key '${i}' referenced in parameter '${n} in component '${o.type}'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.`);return a}var S={select:"fields",include:1},_=Object.freeze(["contentfulMultiEntry"]);function Z({clients:e,createQuery:t,limitPolicy:n}){if(!e)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the ContentfulClientList.");return{enhanceOne:async function({parameter:i,parameterName:a,component:y,context:s}){var l,c;if(ee(i)){if(!te(i.value))return null;let u=A({clients:e,parameterName:a,parameterValue:i.value,component:y,context:s}),r=i.value.entries,f=(l=t==null?void 0:t({parameter:i,parameterName:a,component:y,defaultQuery:{...S},context:s}))!=null?l:S;try{return console.time(`fetch entries ${r.join()}`),(await u.getEntries({"sys.id[in]":r.join(),limit:r.length,...f})).items}catch(C){let h=P(C);throw new Error(`Failed loading Contentful entries '${r.join()}' from source '${(c=i.value.source)!=null?c:"default"}' referenced in parameter '${a}': ${h}`)}finally{console.timeEnd(`fetch entries ${r.join()}`)}}},limitPolicy:n||w}}function ee(e){return e.type===_[0]}function te(e){var t;return!(!e||!((t=e.entries)==null?void 0:t.length))}var k=Object.freeze(["contentfulQuery"]),j={select:"fields",include:1};function ne({clients:e,createQuery:t,limitPolicy:n}){if(!e)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the ContentfulClientList.");return{enhanceOne:async function({parameter:i,parameterName:a,component:y,context:s}){var l,c;if(re(i)){if(!oe(i.value))return null;let u=A({clients:e,parameterName:a,parameterValue:i.value,component:y,context:s}),r=i.value,f=ie(r.count),C=(l=t==null?void 0:t({parameter:i,parameterName:a,component:y,defaultQuery:{...j},context:s}))!=null?l:j,h=`fetch query: ${f} entries with '${r.contentType}' content type`;try{return console.time(h),(await u.getEntries({content_type:r.contentType,order:F(r.sortBy,r.sortOrder),limit:f,...C})).items}catch(m){let d=P(m);throw new Error(`Failed loading Contentful entries with '${r.contentType}' content type from source '${(c=r.source)!=null?c:"default"}' referenced in parameter '${a}': ${d}`)}finally{console.timeEnd(h)}}},limitPolicy:n||w}}function re(e){return e.type===k[0]}function oe(e){return!(!e||!e.source||!e.contentType||typeof e.count=="undefined")}function ie(e){return!e||e<1?1:e>1e3?1e3:e}var Y=R(require("@contentful/rich-text-html-renderer")),le=({parameter:e})=>{let t=e.value;if(!t)return t;if(!ue(t))return B(t),t;for(let n of t)B(n);return t};function B(e){var t;typeof(e==null?void 0:e.fields)=="object"&&Object.entries((t=e.fields)!=null?t:{}).forEach(([n,o])=>{typeof o=="object"&&"nodeType"in o&&o.nodeType==="document"&&(e.fields[n]=(0,Y.documentToHtmlString)(o))})}function ue(e){return Array.isArray(e)}0&&(module.exports={CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES,CANVAS_CONTENTFUL_PARAMETER_TYPES,CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES,ContentfulClientList,contentfulRichTextToHtmlEnhancer,createContentfulEnhancer,createContentfulMultiEnhancer,createContentfulQueryEnhancer});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createBatchEnhancer as S,UniqueBatchEntries as _}from"@uniformdev/canvas";var L=class{constructor(t){this._clients={},Array.isArray(t)?t.forEach(n=>this.addClient(n)):t&&this.addClient(t)}addClient({source:t="default",client:n,previewClient:o}){if(this._clients[t])throw new Error(`The source ${t} is always registered`);if(!n)throw new Error("You must provide a Contentful client for the ContentfulClientList");this._clients[t]={client:n,previewClient:o||n}}getClient({source:t="default",isPreviewClient:n}){let o=this._clients[t];if(!!o)return n?o.previewClient:o.client}};import{createLimitPolicy as Q}from"@uniformdev/canvas";var x=Q({throttle:{limit:55,interval:1e3}});function P(e){return typeof e=="string"?e:typeof e=="object"&&e&&"error"in e?e.error:e instanceof Error?e.toString():JSON.stringify(e,null,2)}function V(e,t){if(!!e)return`${t==="desc"?"-":""}${e}`}function A({clients:e,parameterValue:t,parameterName:n,component:o,context:g}){let{source:i="default"}=t,a=e.getClient({source:i,isPreviewClient:g.preview});if(!a)throw new Error(`No Contentful client could be resolved for source key '${i}' referenced in parameter '${n} in component '${o.type}'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.`);return a}var $={select:"fields",include:1},k=Object.freeze(["contentfulEntry"]);function j(e){return e instanceof L}function se({client:e,previewClient:t,createQuery:n,useBatching:o,limitPolicy:g}){if(!e)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the `client` or `clients` property.");let a=(()=>{if(j(e))return e;let s=new L;return s.addClient({client:e,previewClient:t}),s})(),y=g||x;return o?S({handleBatch:async s=>{var c;let l=s.reduce((u,r)=>{let{parameter:f,parameterName:C,component:h,context:m}=r.args,d=f.value;if(!O(d))return u;let E=b({parameterValue:d,parameterName:C,clients:a,component:h,context:m}),p="";if(T(d))p="legacy-group";else{let{source:I="default"}=d;p=I}return u[p]&&Array.isArray(u[p].tasks)?u[p].tasks.push(r):u[p]={client:E,tasks:[r]},u},{});try{console.time("fetch all entries");for await(let[u,r]of Object.entries(l)){let{context:f,component:C}=r.tasks[0].args,h=(c=n==null?void 0:n({component:C,defaultQuery:{...$},context:f}))!=null?c:$,m=new _(r.tasks,E=>T(E.parameter.value)?E.parameter.value:E.parameter.value.entryId),d=Object.keys(m.groups);console.time(`fetch entries ${u}`);try{(await r.client.getEntries({"sys.id[in]":d.join(","),limit:d.length,...h})).items.forEach(p=>{m.resolveKey(p.sys.id,p)}),m.resolveRemaining(null)}finally{console.timeEnd(`fetch entries ${u}`)}}console.timeEnd("fetch all entries")}catch(u){let r=P(u),f=new Error(`Failed loading Contentful entries batch (${s.length}) ${r}`);s.forEach(C=>C.reject(f))}},shouldQueue:({parameter:s})=>R(s),limitPolicy:y}):{enhanceOne:async function({parameter:l,parameterName:c,component:u,context:r}){var f,C;if(R(l)){if(!O(l.value))return null;let h=b({clients:a,parameterName:c,parameterValue:l.value,component:u,context:r}),m=T(l.value)?l.value:l.value.entryId,d=(f=n==null?void 0:n({parameter:l,parameterName:c,component:u,defaultQuery:{...$},context:r}))!=null?f:$;try{return console.time(`fetch entry ${m}`),await h.getEntry(m,d)}catch(E){let p=P(E);throw T(l.value)?new Error(`Failed loading Contentful entry '${l.value}' referenced in parameter '${c}': ${p}`):new Error(`Failed loading Contentful entry '${m}' from source '${(C=l.value.source)!=null?C:"default"}' referenced in parameter '${c}': ${p}`)}finally{console.timeEnd(`fetch entry ${m}`)}}},limitPolicy:y}}function R(e){var t;return e.type===k[0]&&(((t=e.value)==null?void 0:t.entryId)||typeof e.value=="string")}function T(e){return typeof e=="string"}function O(e){return!(!e||!T(e)&&!e.entryId)}function b({clients:e,parameterValue:t,parameterName:n,component:o,context:g}){if(T(t)){let y=e.getClient({isPreviewClient:g.preview});if(!y)throw new Error(`Parameter '${n}' in component '${o.type}' has a value '${t}' that is not compatible with multi-space/environment usage. If you wish to use multiple spaces/environments, you must convert your Canvas component parameters to the multi-space/environment compatible version. Otherwise, you can continue to use your parameters as-is, but must specify one of the clients provided to the Contentful enhancer as the 'default' client by registering it without specifying a source key.`);return y}let{source:i="default"}=t,a=e.getClient({source:i,isPreviewClient:g.preview});if(!a)throw new Error(`No Contentful client could be resolved for source key '${i}' referenced in parameter '${n} in component '${o.type}'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.`);return a}var F={select:"fields",include:1},Y=Object.freeze(["contentfulMultiEntry"]);function Ce({clients:e,createQuery:t,limitPolicy:n}){if(!e)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the ContentfulClientList.");return{enhanceOne:async function({parameter:i,parameterName:a,component:y,context:s}){var l,c;if(B(i)){if(!U(i.value))return null;let u=A({clients:e,parameterName:a,parameterValue:i.value,component:y,context:s}),r=i.value.entries,f=(l=t==null?void 0:t({parameter:i,parameterName:a,component:y,defaultQuery:{...F},context:s}))!=null?l:F;try{return console.time(`fetch entries ${r.join()}`),(await u.getEntries({"sys.id[in]":r.join(),limit:r.length,...f})).items}catch(C){let h=P(C);throw new Error(`Failed loading Contentful entries '${r.join()}' from source '${(c=i.value.source)!=null?c:"default"}' referenced in parameter '${a}': ${h}`)}finally{console.timeEnd(`fetch entries ${r.join()}`)}}},limitPolicy:n||x}}function B(e){return e.type===Y[0]}function U(e){var t;return!(!e||!((t=e.entries)==null?void 0:t.length))}var q=Object.freeze(["contentfulQuery"]),M={select:"fields",include:1};function ye({clients:e,createQuery:t,limitPolicy:n}){if(!e)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the ContentfulClientList.");return{enhanceOne:async function({parameter:i,parameterName:a,component:y,context:s}){var l,c;if(D(i)){if(!z(i.value))return null;let u=A({clients:e,parameterName:a,parameterValue:i.value,component:y,context:s}),r=i.value,f=H(r.count),C=(l=t==null?void 0:t({parameter:i,parameterName:a,component:y,defaultQuery:{...M},context:s}))!=null?l:M,h=`fetch query: ${f} entries with '${r.contentType}' content type`;try{return console.time(h),(await u.getEntries({content_type:r.contentType,order:V(r.sortBy,r.sortOrder),limit:f,...C})).items}catch(m){let d=P(m);throw new Error(`Failed loading Contentful entries with '${r.contentType}' content type from source '${(c=r.source)!=null?c:"default"}' referenced in parameter '${a}': ${d}`)}finally{console.timeEnd(h)}}},limitPolicy:n||x}}function D(e){return e.type===q[0]}function z(e){return!(!e||!e.source||!e.contentType||typeof e.count=="undefined")}function H(e){return!e||e<1?1:e>1e3?1e3:e}import{documentToHtmlString as K}from"@contentful/rich-text-html-renderer";var Ee=({parameter:e})=>{let t=e.value;if(!t)return t;if(!G(t))return N(t),t;for(let n of t)N(n);return t};function N(e){var t;typeof(e==null?void 0:e.fields)=="object"&&Object.entries((t=e.fields)!=null?t:{}).forEach(([n,o])=>{typeof o=="object"&&"nodeType"in o&&o.nodeType==="document"&&(e.fields[n]=K(o))})}function G(e){return Array.isArray(e)}export{Y as CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES,k as CANVAS_CONTENTFUL_PARAMETER_TYPES,q as CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES,L as ContentfulClientList,Ee as contentfulRichTextToHtmlEnhancer,se as createContentfulEnhancer,Ce as createContentfulMultiEnhancer,ye as createContentfulQueryEnhancer};
|
package/package.json
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uniformdev/canvas-contentful",
|
|
3
|
-
"version": "12.2.
|
|
3
|
+
"version": "12.2.1-alpha.107+b30768d5",
|
|
4
4
|
"description": "Contentful data enhancers for Uniform Canvas",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
|
-
"main": "./dist/
|
|
7
|
-
"module": "./dist/
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.esm.js",
|
|
8
8
|
"exports": {
|
|
9
|
-
"import":
|
|
10
|
-
|
|
9
|
+
"import": {
|
|
10
|
+
"node": "./dist/index.mjs",
|
|
11
|
+
"default": "./dist/index.esm.js"
|
|
12
|
+
},
|
|
13
|
+
"require": "./dist/index.js"
|
|
11
14
|
},
|
|
12
|
-
"types": "./dist/
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
13
16
|
"sideEffects": false,
|
|
14
17
|
"scripts": {
|
|
15
|
-
"build": "
|
|
16
|
-
"
|
|
17
|
-
"build:cjs": "tsc -p ./tsconfig.cjs.json",
|
|
18
|
-
"dev": "run-p dev:esm dev:cjs",
|
|
19
|
-
"dev:esm": "tsc -w",
|
|
20
|
-
"dev:cjs": "tsc -w -p ./tsconfig.cjs.json",
|
|
18
|
+
"build": "tsup",
|
|
19
|
+
"dev": "tsup --watch",
|
|
21
20
|
"clean": "rimraf dist",
|
|
22
21
|
"test": "jest --maxWorkers=1",
|
|
23
22
|
"lint": "eslint \"src/**/*.{js,ts,tsx}\"",
|
|
24
23
|
"format": "prettier --write \"src/**/*.{js,ts,tsx}\"",
|
|
25
24
|
"ci:verify": "run-p lint test",
|
|
26
|
-
"ci:build": "
|
|
25
|
+
"ci:build": "tsup --minify --clean"
|
|
27
26
|
},
|
|
28
27
|
"dependencies": {
|
|
29
|
-
"@uniformdev/canvas": "^12.2.
|
|
28
|
+
"@uniformdev/canvas": "^12.2.1-alpha.107+b30768d5"
|
|
30
29
|
},
|
|
31
30
|
"peerDependencies": {
|
|
32
31
|
"@contentful/rich-text-html-renderer": ">= 14",
|
|
33
32
|
"contentful": ">= 8"
|
|
34
33
|
},
|
|
35
34
|
"devDependencies": {
|
|
36
|
-
"@contentful/rich-text-html-renderer": "15.
|
|
37
|
-
"@types/jest": "27.0.
|
|
38
|
-
"@types/node": "16.
|
|
39
|
-
"contentful": "9.1.
|
|
40
|
-
"eslint": "
|
|
41
|
-
"eslint-plugin-react": "7.
|
|
42
|
-
"eslint-plugin-react-hooks": "4.
|
|
43
|
-
"jest": "27.
|
|
35
|
+
"@contentful/rich-text-html-renderer": "15.9.1",
|
|
36
|
+
"@types/jest": "27.0.3",
|
|
37
|
+
"@types/node": "16.11.18",
|
|
38
|
+
"contentful": "9.1.5",
|
|
39
|
+
"eslint": "8.4.1",
|
|
40
|
+
"eslint-plugin-react": "7.27.1",
|
|
41
|
+
"eslint-plugin-react-hooks": "4.3.0",
|
|
42
|
+
"jest": "27.4.5",
|
|
44
43
|
"npm-run-all": "4.1.5",
|
|
45
44
|
"rimraf": "3.0.2",
|
|
46
|
-
"ts-jest": "27.
|
|
45
|
+
"ts-jest": "27.1.1",
|
|
46
|
+
"tsup": "5.11.10"
|
|
47
47
|
},
|
|
48
48
|
"files": [
|
|
49
49
|
"/dist"
|
|
@@ -51,5 +51,5 @@
|
|
|
51
51
|
"publishConfig": {
|
|
52
52
|
"access": "public"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "b30768d5d58d4c75b390274042724d50c1ee1041"
|
|
55
55
|
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { ContentfulClientApi } from 'contentful';
|
|
2
|
-
export interface AddClientArgs {
|
|
3
|
-
/**
|
|
4
|
-
* The Contentful source public ID that this client maps to in the composition data.
|
|
5
|
-
* This is used to enable multiple Contentful spaces/environments as data sources.
|
|
6
|
-
* If unspecified, the client will be the default source that is used when no source public ID
|
|
7
|
-
* is in the data, or the source ID is 'default'.
|
|
8
|
-
*/
|
|
9
|
-
source?: string;
|
|
10
|
-
/** The Contentful client instance to use when fetching published data */
|
|
11
|
-
client: ContentfulClientApi;
|
|
12
|
-
/**
|
|
13
|
-
* The Contentful client instance to use when fetching preview data.
|
|
14
|
-
* If the preview client is not passed, it defaults to the client.
|
|
15
|
-
*/
|
|
16
|
-
previewClient?: ContentfulClientApi;
|
|
17
|
-
}
|
|
18
|
-
export declare class ContentfulClientList {
|
|
19
|
-
private _clients;
|
|
20
|
-
constructor(clients?: AddClientArgs[] | AddClientArgs);
|
|
21
|
-
addClient({ source, client, previewClient }: AddClientArgs): void;
|
|
22
|
-
getClient({ source, isPreviewClient, }: {
|
|
23
|
-
source?: string;
|
|
24
|
-
isPreviewClient?: boolean;
|
|
25
|
-
}): ContentfulClientApi | undefined;
|
|
26
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ContentfulClientList=void 0;var ContentfulClientList=function(){function t(t){var e=this;this._clients={},Array.isArray(t)?t.forEach(function(t){return e.addClient(t)}):t&&this.addClient(t)}return t.prototype.addClient=function(t){var e=t.source,i=void 0===e?"default":e,e=t.client,t=t.previewClient;if(this._clients[i])throw new Error("The source "+i+" is always registered");if(!e)throw new Error("You must provide a Contentful client for the ContentfulClientList");this._clients[i]={client:e,previewClient:t||e}},t.prototype.getClient=function(t){var e=t.source,t=t.isPreviewClient,e=this._clients[void 0===e?"default":e];if(e)return t?e.previewClient:e.client},t}();exports.ContentfulClientList=ContentfulClientList;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.contentfulRichTextToHtmlEnhancer=void 0;var rich_text_html_renderer_1=require("@contentful/rich-text-html-renderer"),contentfulRichTextToHtmlEnhancer=function(e){var n=e.parameter;return"object"!=typeof(null===(e=n.value)||void 0===e?void 0:e.fields)||Object.entries(null!==(e=null===(e=n.value)||void 0===e?void 0:e.fields)&&void 0!==e?e:{}).forEach(function(e){var t=e[0],e=e[1];"object"==typeof e&&"nodeType"in e&&"document"===e.nodeType&&(n.value.fields[t]=(0,rich_text_html_renderer_1.documentToHtmlString)(e))}),n.value};exports.contentfulRichTextToHtmlEnhancer=contentfulRichTextToHtmlEnhancer;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { ComponentParameterEnhancer, ComponentInstance, ComponentParameter, LimitPolicy, EnhancerContext } from '@uniformdev/canvas';
|
|
2
|
-
import { ContentfulClientApi, Sys } from 'contentful';
|
|
3
|
-
import { ContentfulClientList } from './ContentfulClientList';
|
|
4
|
-
export declare type EntrySelectorParameterValue = {
|
|
5
|
-
entryId: string;
|
|
6
|
-
source?: string;
|
|
7
|
-
} | string | null | undefined;
|
|
8
|
-
export declare type CreateContentfulQueryOptions<TContext extends EnhancerContext = EnhancerContext> = {
|
|
9
|
-
/** Canvas parameter name being queried for. Not defined if using batching. */
|
|
10
|
-
parameterName?: string;
|
|
11
|
-
/** Canvas parameter value being fetched. Not defined if using batching. */
|
|
12
|
-
parameter?: ComponentParameter<EntrySelectorParameterValue>;
|
|
13
|
-
/** Component containing the parameter being fetched. Not defined if using batching. */
|
|
14
|
-
component?: ComponentInstance;
|
|
15
|
-
/** The default Contentful query expression (select fields + include 1 layer of references) */
|
|
16
|
-
defaultQuery: any;
|
|
17
|
-
/** The enhancer context provided to the enhance() function */
|
|
18
|
-
context: TContext;
|
|
19
|
-
};
|
|
20
|
-
/** The default shape of the result value of the Contentful enhancer. Note that this can change if the query is altered. */
|
|
21
|
-
export declare type ContentfulEnhancerResult<TFields> = {
|
|
22
|
-
/**
|
|
23
|
-
* The shape of the `fields` that the Contentful REST API is expected to return for this entry
|
|
24
|
-
* https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/entries/entry
|
|
25
|
-
*
|
|
26
|
-
* These should line up with the fields in your content model(s) that are allowed for this component.
|
|
27
|
-
*/
|
|
28
|
-
fields: TFields;
|
|
29
|
-
/** System fields returned by the Contentful API. If you modify the query parameters to fetch more than fields, you may get more sys data than this. */
|
|
30
|
-
sys: Partial<Pick<Sys, 'id' | 'type'>>;
|
|
31
|
-
} | null;
|
|
32
|
-
export declare type CreateContentfulEnhancerOptions = {
|
|
33
|
-
/** Either a list of Contentful clients for use with multi-space/environment-enabled Canvas projects.
|
|
34
|
-
* Or a single Contentful client for use with legacy Canvas data.*/
|
|
35
|
-
client: ContentfulClientApi | ContentfulClientList;
|
|
36
|
-
/** @deprecated Contentful client instance to use for fetching preview content.
|
|
37
|
-
* This client is _only_ relevant when the `client` property is a single Contentful client intended for use with
|
|
38
|
-
* legacy Canvas data. Conversely, if you use a `ContentfulClientList` for the `client` property, any value for `previewClient`
|
|
39
|
-
* will be ignored. To avoid deprecated use, you should switch to using a `ContentfulClientList` for the `client` property. */
|
|
40
|
-
previewClient?: ContentfulClientApi;
|
|
41
|
-
/** Creates the Contentful client's query params for specific parameters. See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters */
|
|
42
|
-
createQuery?: (options: CreateContentfulQueryOptions) => any | undefined;
|
|
43
|
-
useBatching?: boolean;
|
|
44
|
-
limitPolicy?: LimitPolicy;
|
|
45
|
-
};
|
|
46
|
-
export declare const CANVAS_CONTENTFUL_PARAMETER_TYPES: readonly string[];
|
|
47
|
-
export declare function createContentfulEnhancer({ client, previewClient, createQuery, useBatching, limitPolicy, }: CreateContentfulEnhancerOptions): ComponentParameterEnhancer<EntrySelectorParameterValue, ContentfulEnhancerResult<unknown>>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var a in t=arguments[r])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,i,s,u){return new(s=s||Promise)(function(r,t){function n(e){try{o(u.next(e))}catch(e){t(e)}}function a(e){try{o(u.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?r(e.value):((t=e.value)instanceof s?t:new s(function(e){e(t)})).then(n,a)}o((u=u.apply(e,i||[])).next())})},__generator=this&&this.__generator||function(r,n){var a,o,i,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},e={next:t(0),throw:t(1),return:t(2)};return"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function t(t){return function(e){return function(t){if(a)throw new TypeError("Generator is already executing.");for(;s;)try{if(a=1,o&&(i=2&t[0]?o.return:t[0]?o.throw||((i=o.return)&&i.call(o),0):o.next)&&!(i=i.call(o,t[1])).done)return i;switch(o=0,(t=i?[2&t[0],i.value]:t)[0]){case 0:case 1:i=t;break;case 4:return s.label++,{value:t[1],done:!1};case 5:s.label++,o=t[1],t=[0];continue;case 7:t=s.ops.pop(),s.trys.pop();continue;default:if(!(i=0<(i=s.trys).length&&i[i.length-1])&&(6===t[0]||2===t[0])){s=0;continue}if(3===t[0]&&(!i||t[1]>i[0]&&t[1]<i[3])){s.label=t[1];break}if(6===t[0]&&s.label<i[1]){s.label=i[1],i=t;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(t);break}i[2]&&s.ops.pop(),s.trys.pop();continue}t=n.call(r,s)}catch(e){t=[6,e],o=0}finally{a=i=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([t,e])}}},__asyncValues=this&&this.__asyncValues||function(i){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var e,t=i[Symbol.asyncIterator];return t?t.call(i):(i="function"==typeof __values?__values(i):i[Symbol.iterator](),e={},r("next"),r("throw"),r("return"),e[Symbol.asyncIterator]=function(){return this},e);function r(o){e[o]=i[o]&&function(a){return new Promise(function(e,t){var r,n;a=i[o](a),r=e,e=t,n=a.done,t=a.value,Promise.resolve(t).then(function(e){r({value:e,done:n})},e)})}}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.createContentfulEnhancer=exports.CANVAS_CONTENTFUL_PARAMETER_TYPES=void 0;var canvas_1=require("@uniformdev/canvas"),ContentfulClientList_1=require("./ContentfulClientList"),defaultQuery={select:"fields",include:1};function isClientList(e){return e instanceof ContentfulClientList_1.ContentfulClientList}function createContentfulEnhancer(e){var t=this,r=e.client,n=e.previewClient,h=e.createQuery,a=e.useBatching,e=e.limitPolicy;if(!r)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the `client` or `clients` property.");var v=function(){if(isClientList(r))return r;var e=new ContentfulClientList_1.ContentfulClientList;return e.addClient({client:r,previewClient:n}),e}(),e=e||(0,canvas_1.createLimitPolicy)({throttle:{limit:55,interval:1e3}});return a?(0,canvas_1.createBatchEnhancer)({handleBatch:function(y){return __awaiter(t,void 0,void 0,function(){var t,r,n,a,o,i,s,u,l,c,f,p;return __generator(this,function(e){switch(e.label){case 0:t=y.reduce(function(e,t){var r=t.args,n=r.parameter,a=r.parameterName,o=r.component,r=r.context,n=n.value;if(!isParameterValueDefined(n))return e;o=resolveClientForParameter({parameterValue:n,parameterName:a,clients:v,component:o,context:r}),r="";return e[r=isLegacyValue(n)?"legacy-group":void 0===(n=n.source)?"default":n]&&Array.isArray(e[r].tasks)?e[r].tasks.push(t):e[r]={client:o,tasks:[t]},e},{}),e.label=1;case 1:e.trys.push([1,17,,18]),console.time("fetch all entries"),e.label=2;case 2:e.trys.push([2,10,11,16]),r=__asyncValues(Object.entries(t)),e.label=3;case 3:return[4,r.next()];case 4:if((n=e.sent()).done)return[3,9];p=n.value,a=p[0],o=p[1],i=o.tasks[0].args.context,i=null!==(p=null==h?void 0:h({defaultQuery:__assign({},defaultQuery),context:i}))&&void 0!==p?p:defaultQuery,s=new canvas_1.UniqueBatchEntries(o.tasks,function(e){return isLegacyValue(e.parameter.value)?e.parameter.value:e.parameter.value.entryId}),p=Object.keys(s.groups),console.time("fetch entries "+a),e.label=5;case 5:return e.trys.push([5,,7,8]),[4,o.client.getEntries(__assign({"sys.id[in]":p.join(","),limit:p.length},i))];case 6:return e.sent().items.forEach(function(e){s.resolveKey(e.sys.id,e)}),s.resolveRemaining(null),[3,8];case 7:return console.timeEnd("fetch entries "+a),[7];case 8:return[3,3];case 9:return[3,16];case 10:return u=e.sent(),c={error:u},[3,16];case 11:return e.trys.push([11,,14,15]),n&&!n.done&&(f=r.return)?[4,f.call(r)]:[3,13];case 12:e.sent(),e.label=13;case 13:return[3,15];case 14:if(c)throw c.error;return[7];case 15:return[7];case 16:return console.timeEnd("fetch all entries"),[3,18];case 17:return u=e.sent(),u=getErrorMessageFromContentfulError(u),l=new Error("Failed loading Contentful entries batch ("+y.length+") "+u),y.forEach(function(e){return e.reject(l)}),[3,18];case 18:return[2]}})})},shouldQueue:function(e){return parameterIsContentfulEntrySelector(e.parameter)},limitPolicy:e}):{enhanceOne:function(e){var o,i=e.parameter,s=e.parameterName,u=e.component,l=e.context;return __awaiter(this,void 0,void 0,function(){var t,r,n,a;return __generator(this,function(e){switch(e.label){case 0:if(!parameterIsContentfulEntrySelector(i))return[3,5];if(!isParameterValueDefined(i.value))return[2,null];t=resolveClientForParameter({clients:v,parameterName:s,parameterValue:i.value,component:u,context:l}),r=isLegacyValue(i.value)?i.value:i.value.entryId,n=null!==(o=null==h?void 0:h({parameter:i,parameterName:s,component:u,defaultQuery:__assign({},defaultQuery),context:l}))&&void 0!==o?o:defaultQuery,e.label=1;case 1:return e.trys.push([1,3,4,5]),console.time("fetch entry "+r),[4,t.getEntry(r,n)];case 2:return[2,e.sent()];case 3:throw a=e.sent(),a=getErrorMessageFromContentfulError(a),isLegacyValue(i.value)?new Error("Failed loading Contentful entry '"+i.value+"' referenced in parameter '"+s+"': "+a):new Error("Failed loading Contentful entry '"+r+"' from source '"+(null!==(o=i.value.source)&&void 0!==o?o:"default")+"' referenced in parameter '"+s+"': "+a);case 4:return console.timeEnd("fetch entry "+r),[7];case 5:return[2]}})})},limitPolicy:e}}function parameterIsContentfulEntrySelector(e){var t;return e.type===exports.CANVAS_CONTENTFUL_PARAMETER_TYPES[0]&&((null===(t=e.value)||void 0===t?void 0:t.entryId)||"string"==typeof e.value)}function getErrorMessageFromContentfulError(e){return"string"==typeof e?e:"object"==typeof e&&e&&"error"in e?e.error:e instanceof Error?e.toString():JSON.stringify(e,null,2)}function isLegacyValue(e){return"string"==typeof e}function isParameterValueDefined(e){return!(!e||!isLegacyValue(e)&&!e.entryId)}function resolveClientForParameter(e){var t=e.clients,r=e.parameterValue,n=e.parameterName,a=e.component,o=e.context;if(isLegacyValue(r)){e=t.getClient({isPreviewClient:o.preview});if(!e)throw new Error("Parameter '"+n+"' in component '"+a.type+"' has a value '"+r+"' that is not compatible with multi-space/environment usage. If you wish to use multiple spaces/environments, you must convert your Canvas component parameters to the multi-space/environment compatible version. Otherwise, you can continue to use your parameters as-is, but must specify one of the clients provided to the Contentful enhancer as the 'default' client by registering it without specifying a source key.");return e}r=r.source,r=void 0===r?"default":r,o=t.getClient({source:r,isPreviewClient:o.preview});if(!o)throw new Error("No Contentful client could be resolved for source key '"+r+"' referenced in parameter '"+n+" in component '"+a.type+"'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.");return o}exports.CANVAS_CONTENTFUL_PARAMETER_TYPES=Object.freeze(["contentfulEntry"]),exports.createContentfulEnhancer=createContentfulEnhancer;
|
package/dist/cjs/index.d.ts
DELETED
package/dist/cjs/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,r,n){void 0===n&&(n=r),Object.defineProperty(e,n,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,n){e[n=void 0===n?r:n]=t[r]}),__exportStar=this&&this.__exportStar||function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||__createBinding(t,e,r)};Object.defineProperty(exports,"__esModule",{value:!0}),__exportStar(require("./createContentfulEnhancer"),exports),__exportStar(require("./contentfulRichTextToHtmlEnhancer"),exports),__exportStar(require("./ContentfulClientList"),exports);
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { ContentfulClientApi } from 'contentful';
|
|
2
|
-
export interface AddClientArgs {
|
|
3
|
-
/**
|
|
4
|
-
* The Contentful source public ID that this client maps to in the composition data.
|
|
5
|
-
* This is used to enable multiple Contentful spaces/environments as data sources.
|
|
6
|
-
* If unspecified, the client will be the default source that is used when no source public ID
|
|
7
|
-
* is in the data, or the source ID is 'default'.
|
|
8
|
-
*/
|
|
9
|
-
source?: string;
|
|
10
|
-
/** The Contentful client instance to use when fetching published data */
|
|
11
|
-
client: ContentfulClientApi;
|
|
12
|
-
/**
|
|
13
|
-
* The Contentful client instance to use when fetching preview data.
|
|
14
|
-
* If the preview client is not passed, it defaults to the client.
|
|
15
|
-
*/
|
|
16
|
-
previewClient?: ContentfulClientApi;
|
|
17
|
-
}
|
|
18
|
-
export declare class ContentfulClientList {
|
|
19
|
-
private _clients;
|
|
20
|
-
constructor(clients?: AddClientArgs[] | AddClientArgs);
|
|
21
|
-
addClient({ source, client, previewClient }: AddClientArgs): void;
|
|
22
|
-
getClient({ source, isPreviewClient, }: {
|
|
23
|
-
source?: string;
|
|
24
|
-
isPreviewClient?: boolean;
|
|
25
|
-
}): ContentfulClientApi | undefined;
|
|
26
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var ContentfulClientList=function(){function t(t){var e=this;this._clients={},Array.isArray(t)?t.forEach(function(t){return e.addClient(t)}):t&&this.addClient(t)}return t.prototype.addClient=function(t){var e=t.source,i=void 0===e?"default":e,e=t.client,t=t.previewClient;if(this._clients[i])throw new Error("The source "+i+" is always registered");if(!e)throw new Error("You must provide a Contentful client for the ContentfulClientList");this._clients[i]={client:e,previewClient:t||e}},t.prototype.getClient=function(t){var e=t.source,t=t.isPreviewClient,e=this._clients[void 0===e?"default":e];if(e)return t?e.previewClient:e.client},t}();export{ContentfulClientList};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{documentToHtmlString}from"@contentful/rich-text-html-renderer";var contentfulRichTextToHtmlEnhancer=function(e){var n=e.parameter;return"object"!=typeof(null===(e=n.value)||void 0===e?void 0:e.fields)||Object.entries(null!==(e=null===(e=n.value)||void 0===e?void 0:e.fields)&&void 0!==e?e:{}).forEach(function(e){var t=e[0],e=e[1];"object"==typeof e&&"nodeType"in e&&"document"===e.nodeType&&(n.value.fields[t]=documentToHtmlString(e))}),n.value};export{contentfulRichTextToHtmlEnhancer};
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { ComponentParameterEnhancer, ComponentInstance, ComponentParameter, LimitPolicy, EnhancerContext } from '@uniformdev/canvas';
|
|
2
|
-
import { ContentfulClientApi, Sys } from 'contentful';
|
|
3
|
-
import { ContentfulClientList } from './ContentfulClientList';
|
|
4
|
-
export declare type EntrySelectorParameterValue = {
|
|
5
|
-
entryId: string;
|
|
6
|
-
source?: string;
|
|
7
|
-
} | string | null | undefined;
|
|
8
|
-
export declare type CreateContentfulQueryOptions<TContext extends EnhancerContext = EnhancerContext> = {
|
|
9
|
-
/** Canvas parameter name being queried for. Not defined if using batching. */
|
|
10
|
-
parameterName?: string;
|
|
11
|
-
/** Canvas parameter value being fetched. Not defined if using batching. */
|
|
12
|
-
parameter?: ComponentParameter<EntrySelectorParameterValue>;
|
|
13
|
-
/** Component containing the parameter being fetched. Not defined if using batching. */
|
|
14
|
-
component?: ComponentInstance;
|
|
15
|
-
/** The default Contentful query expression (select fields + include 1 layer of references) */
|
|
16
|
-
defaultQuery: any;
|
|
17
|
-
/** The enhancer context provided to the enhance() function */
|
|
18
|
-
context: TContext;
|
|
19
|
-
};
|
|
20
|
-
/** The default shape of the result value of the Contentful enhancer. Note that this can change if the query is altered. */
|
|
21
|
-
export declare type ContentfulEnhancerResult<TFields> = {
|
|
22
|
-
/**
|
|
23
|
-
* The shape of the `fields` that the Contentful REST API is expected to return for this entry
|
|
24
|
-
* https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/entries/entry
|
|
25
|
-
*
|
|
26
|
-
* These should line up with the fields in your content model(s) that are allowed for this component.
|
|
27
|
-
*/
|
|
28
|
-
fields: TFields;
|
|
29
|
-
/** System fields returned by the Contentful API. If you modify the query parameters to fetch more than fields, you may get more sys data than this. */
|
|
30
|
-
sys: Partial<Pick<Sys, 'id' | 'type'>>;
|
|
31
|
-
} | null;
|
|
32
|
-
export declare type CreateContentfulEnhancerOptions = {
|
|
33
|
-
/** Either a list of Contentful clients for use with multi-space/environment-enabled Canvas projects.
|
|
34
|
-
* Or a single Contentful client for use with legacy Canvas data.*/
|
|
35
|
-
client: ContentfulClientApi | ContentfulClientList;
|
|
36
|
-
/** @deprecated Contentful client instance to use for fetching preview content.
|
|
37
|
-
* This client is _only_ relevant when the `client` property is a single Contentful client intended for use with
|
|
38
|
-
* legacy Canvas data. Conversely, if you use a `ContentfulClientList` for the `client` property, any value for `previewClient`
|
|
39
|
-
* will be ignored. To avoid deprecated use, you should switch to using a `ContentfulClientList` for the `client` property. */
|
|
40
|
-
previewClient?: ContentfulClientApi;
|
|
41
|
-
/** Creates the Contentful client's query params for specific parameters. See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters */
|
|
42
|
-
createQuery?: (options: CreateContentfulQueryOptions) => any | undefined;
|
|
43
|
-
useBatching?: boolean;
|
|
44
|
-
limitPolicy?: LimitPolicy;
|
|
45
|
-
};
|
|
46
|
-
export declare const CANVAS_CONTENTFUL_PARAMETER_TYPES: readonly string[];
|
|
47
|
-
export declare function createContentfulEnhancer({ client, previewClient, createQuery, useBatching, limitPolicy, }: CreateContentfulEnhancerOptions): ComponentParameterEnhancer<EntrySelectorParameterValue, ContentfulEnhancerResult<unknown>>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var __assign=this&&this.__assign||function(){return(__assign=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var a in t=arguments[r])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e}).apply(this,arguments)},__awaiter=this&&this.__awaiter||function(e,i,u,s){return new(u=u||Promise)(function(r,t){function n(e){try{o(s.next(e))}catch(e){t(e)}}function a(e){try{o(s.throw(e))}catch(e){t(e)}}function o(e){var t;e.done?r(e.value):((t=e.value)instanceof u?t:new u(function(e){e(t)})).then(n,a)}o((s=s.apply(e,i||[])).next())})},__generator=this&&this.__generator||function(r,n){var a,o,i,u={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},e={next:t(0),throw:t(1),return:t(2)};return"function"==typeof Symbol&&(e[Symbol.iterator]=function(){return this}),e;function t(t){return function(e){return function(t){if(a)throw new TypeError("Generator is already executing.");for(;u;)try{if(a=1,o&&(i=2&t[0]?o.return:t[0]?o.throw||((i=o.return)&&i.call(o),0):o.next)&&!(i=i.call(o,t[1])).done)return i;switch(o=0,(t=i?[2&t[0],i.value]:t)[0]){case 0:case 1:i=t;break;case 4:return u.label++,{value:t[1],done:!1};case 5:u.label++,o=t[1],t=[0];continue;case 7:t=u.ops.pop(),u.trys.pop();continue;default:if(!(i=0<(i=u.trys).length&&i[i.length-1])&&(6===t[0]||2===t[0])){u=0;continue}if(3===t[0]&&(!i||t[1]>i[0]&&t[1]<i[3])){u.label=t[1];break}if(6===t[0]&&u.label<i[1]){u.label=i[1],i=t;break}if(i&&u.label<i[2]){u.label=i[2],u.ops.push(t);break}i[2]&&u.ops.pop(),u.trys.pop();continue}t=n.call(r,u)}catch(e){t=[6,e],o=0}finally{a=i=0}if(5&t[0])throw t[1];return{value:t[0]?t[1]:void 0,done:!0}}([t,e])}}},__asyncValues=this&&this.__asyncValues||function(i){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var e,t=i[Symbol.asyncIterator];return t?t.call(i):(i="function"==typeof __values?__values(i):i[Symbol.iterator](),e={},r("next"),r("throw"),r("return"),e[Symbol.asyncIterator]=function(){return this},e);function r(o){e[o]=i[o]&&function(a){return new Promise(function(e,t){var r,n;a=i[o](a),r=e,e=t,n=a.done,t=a.value,Promise.resolve(t).then(function(e){r({value:e,done:n})},e)})}}};import{createBatchEnhancer,UniqueBatchEntries,createLimitPolicy}from"@uniformdev/canvas";import{ContentfulClientList}from"./ContentfulClientList";var defaultQuery={select:"fields",include:1},CANVAS_CONTENTFUL_PARAMETER_TYPES=Object.freeze(["contentfulEntry"]);function isClientList(e){return e instanceof ContentfulClientList}function createContentfulEnhancer(e){var t=this,r=e.client,n=e.previewClient,h=e.createQuery,a=e.useBatching,e=e.limitPolicy;if(!r)throw new Error("No Contentful clients were provided to the enhancer. You must provide at least one client via the `client` or `clients` property.");var m=function(){if(isClientList(r))return r;var e=new ContentfulClientList;return e.addClient({client:r,previewClient:n}),e}(),e=e||createLimitPolicy({throttle:{limit:55,interval:1e3}});return a?createBatchEnhancer({handleBatch:function(p){return __awaiter(t,void 0,void 0,function(){var t,r,n,a,o,i,u,s,l,c,f,y;return __generator(this,function(e){switch(e.label){case 0:t=p.reduce(function(e,t){var r=t.args,n=r.parameter,a=r.parameterName,o=r.component,r=r.context,n=n.value;if(!isParameterValueDefined(n))return e;o=resolveClientForParameter({parameterValue:n,parameterName:a,clients:m,component:o,context:r}),r="";return e[r=isLegacyValue(n)?"legacy-group":void 0===(n=n.source)?"default":n]&&Array.isArray(e[r].tasks)?e[r].tasks.push(t):e[r]={client:o,tasks:[t]},e},{}),e.label=1;case 1:e.trys.push([1,17,,18]),console.time("fetch all entries"),e.label=2;case 2:e.trys.push([2,10,11,16]),r=__asyncValues(Object.entries(t)),e.label=3;case 3:return[4,r.next()];case 4:if((n=e.sent()).done)return[3,9];y=n.value,a=y[0],o=y[1],i=o.tasks[0].args.context,i=null!==(y=null==h?void 0:h({defaultQuery:__assign({},defaultQuery),context:i}))&&void 0!==y?y:defaultQuery,u=new UniqueBatchEntries(o.tasks,function(e){return isLegacyValue(e.parameter.value)?e.parameter.value:e.parameter.value.entryId}),y=Object.keys(u.groups),console.time("fetch entries "+a),e.label=5;case 5:return e.trys.push([5,,7,8]),[4,o.client.getEntries(__assign({"sys.id[in]":y.join(","),limit:y.length},i))];case 6:return e.sent().items.forEach(function(e){u.resolveKey(e.sys.id,e)}),u.resolveRemaining(null),[3,8];case 7:return console.timeEnd("fetch entries "+a),[7];case 8:return[3,3];case 9:return[3,16];case 10:return s=e.sent(),c={error:s},[3,16];case 11:return e.trys.push([11,,14,15]),n&&!n.done&&(f=r.return)?[4,f.call(r)]:[3,13];case 12:e.sent(),e.label=13;case 13:return[3,15];case 14:if(c)throw c.error;return[7];case 15:return[7];case 16:return console.timeEnd("fetch all entries"),[3,18];case 17:return s=e.sent(),s=getErrorMessageFromContentfulError(s),l=new Error("Failed loading Contentful entries batch ("+p.length+") "+s),p.forEach(function(e){return e.reject(l)}),[3,18];case 18:return[2]}})})},shouldQueue:function(e){return parameterIsContentfulEntrySelector(e.parameter)},limitPolicy:e}):{enhanceOne:function(e){var o,i=e.parameter,u=e.parameterName,s=e.component,l=e.context;return __awaiter(this,void 0,void 0,function(){var t,r,n,a;return __generator(this,function(e){switch(e.label){case 0:if(!parameterIsContentfulEntrySelector(i))return[3,5];if(!isParameterValueDefined(i.value))return[2,null];t=resolveClientForParameter({clients:m,parameterName:u,parameterValue:i.value,component:s,context:l}),r=isLegacyValue(i.value)?i.value:i.value.entryId,n=null!==(o=null==h?void 0:h({parameter:i,parameterName:u,component:s,defaultQuery:__assign({},defaultQuery),context:l}))&&void 0!==o?o:defaultQuery,e.label=1;case 1:return e.trys.push([1,3,4,5]),console.time("fetch entry "+r),[4,t.getEntry(r,n)];case 2:return[2,e.sent()];case 3:throw a=e.sent(),a=getErrorMessageFromContentfulError(a),isLegacyValue(i.value)?new Error("Failed loading Contentful entry '"+i.value+"' referenced in parameter '"+u+"': "+a):new Error("Failed loading Contentful entry '"+r+"' from source '"+(null!==(o=i.value.source)&&void 0!==o?o:"default")+"' referenced in parameter '"+u+"': "+a);case 4:return console.timeEnd("fetch entry "+r),[7];case 5:return[2]}})})},limitPolicy:e}}function parameterIsContentfulEntrySelector(e){var t;return e.type===CANVAS_CONTENTFUL_PARAMETER_TYPES[0]&&((null===(t=e.value)||void 0===t?void 0:t.entryId)||"string"==typeof e.value)}function getErrorMessageFromContentfulError(e){return"string"==typeof e?e:"object"==typeof e&&e&&"error"in e?e.error:e instanceof Error?e.toString():JSON.stringify(e,null,2)}function isLegacyValue(e){return"string"==typeof e}function isParameterValueDefined(e){return!(!e||!isLegacyValue(e)&&!e.entryId)}function resolveClientForParameter(e){var t=e.clients,r=e.parameterValue,n=e.parameterName,a=e.component,o=e.context;if(isLegacyValue(r)){e=t.getClient({isPreviewClient:o.preview});if(!e)throw new Error("Parameter '"+n+"' in component '"+a.type+"' has a value '"+r+"' that is not compatible with multi-space/environment usage. If you wish to use multiple spaces/environments, you must convert your Canvas component parameters to the multi-space/environment compatible version. Otherwise, you can continue to use your parameters as-is, but must specify one of the clients provided to the Contentful enhancer as the 'default' client by registering it without specifying a source key.");return e}r=r.source,r=void 0===r?"default":r,o=t.getClient({source:r,isPreviewClient:o.preview});if(!o)throw new Error("No Contentful client could be resolved for source key '"+r+"' referenced in parameter '"+n+" in component '"+a.type+"'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.");return o}export{CANVAS_CONTENTFUL_PARAMETER_TYPES,createContentfulEnhancer};
|
package/dist/esm/index.d.ts
DELETED
package/dist/esm/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export*from"./createContentfulEnhancer";export*from"./contentfulRichTextToHtmlEnhancer";export*from"./ContentfulClientList";
|