@uniformdev/canvas-contentful 17.7.1-alpha.34 → 18.0.1-alpha.3

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/index.mjs CHANGED
@@ -1 +1,499 @@
1
- var x=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{documentToHtmlString as N}from"@contentful/rich-text-html-renderer";var W=({parameter:e})=>{let t=e.value;if(!t)return t;if(!I(t))return L(t),t;for(let n of t)L(n);return t};function L(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]=N(o))})}function I(e){return Array.isArray(e)}import{createBatchEnhancer as k,UniqueBatchEntries as S}from"@uniformdev/canvas";import{createLimitPolicy as Q}from"@uniformdev/canvas";var v=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 $(e,t){if(!!e)return`${t==="desc"?"-":""}${e}`}function A({clients:e,parameterValue:t,parameterName:n,component:o,context:E}){let{source:i="default"}=t,l=e.getClient({source:i,isPreviewClient:E.preview});if(!l)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 l}var T={select:"fields",include:1},_=Object.freeze(["contentfulEntry"]);function j(e){return e instanceof x}function fe({client:e,previewClient:t,createQuery:n,useBatching:o,limitPolicy:E}){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 l=(()=>{if(j(e))return e;let s=new x;return s.addClient({client:e,previewClient:t}),s})(),y=E||v;return o?k({handleBatch:async s=>{var f;let u=s.reduce((a,r)=>{let{parameter:C,parameterName:c,component:d,context:m}=r.args,h=C.value;if(!O(h))return a;let g=R({parameterValue:h,parameterName:c,clients:l,component:d,context:m}),p="";if(w(h))p="legacy-group";else{let{source:M="default"}=h;p=M}return a[p]&&Array.isArray(a[p].tasks)?a[p].tasks.push(r):a[p]={client:g,tasks:[r]},a},{});try{console.time("fetch all entries");for await(let[a,r]of Object.entries(u)){let{context:C,component:c}=r.tasks[0].args,d=(f=n==null?void 0:n({component:c,defaultQuery:{...T},context:C}))!=null?f:T,m=new S(r.tasks,g=>w(g.parameter.value)?g.parameter.value:g.parameter.value.entryId),h=Object.keys(m.groups);console.time(`fetch entries ${a}`);try{(await r.client.getEntries({"sys.id[in]":h.join(","),limit:h.length,...d})).items.forEach(p=>{m.resolveKey(p.sys.id,p)}),m.resolveRemaining(null)}finally{console.timeEnd(`fetch entries ${a}`)}}console.timeEnd("fetch all entries")}catch(a){let r=P(a),C=new Error(`Failed loading Contentful entries batch (${s.length}) ${r}`);s.forEach(c=>c.reject(C))}},shouldQueue:({parameter:s})=>V(s),limitPolicy:y}):{enhanceOne:async function({parameter:u,parameterName:f,component:a,context:r}){var C,c;if(V(u)){if(!O(u.value))return null;let d=R({clients:l,parameterName:f,parameterValue:u.value,component:a,context:r}),m=w(u.value)?u.value:u.value.entryId,h=(C=n==null?void 0:n({parameter:u,parameterName:f,component:a,defaultQuery:{...T},context:r}))!=null?C:T;try{return console.time(`fetch entry ${m}`),await d.getEntry(m,h)}catch(g){let p=P(g);throw w(u.value)?new Error(`Failed loading Contentful entry '${u.value}' referenced in parameter '${f}': ${p}`):new Error(`Failed loading Contentful entry '${m}' from source '${(c=u.value.source)!=null?c:"default"}' referenced in parameter '${f}': ${p}`)}finally{console.timeEnd(`fetch entry ${m}`)}}},limitPolicy:y}}function V(e){var t;return e.type===_[0]&&(((t=e.value)==null?void 0:t.entryId)||typeof e.value=="string")}function w(e){return typeof e=="string"}function O(e){return!(!e||!w(e)&&!e.entryId)}function R({clients:e,parameterValue:t,parameterName:n,component:o,context:E}){if(w(t)){let y=e.getClient({isPreviewClient:E.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,l=e.getClient({source:i,isPreviewClient:E.preview});if(!l)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 l}var b={select:"fields",include:1},Y=Object.freeze(["contentfulMultiEntry"]);function pe({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:l,component:y,context:s}){var u,f;if(B(i)){if(!U(i.value))return null;let a=A({clients:e,parameterName:l,parameterValue:i.value,component:y,context:s}),r=i.value.entries,C=(u=t==null?void 0:t({parameter:i,parameterName:l,component:y,defaultQuery:{...b},context:s}))!=null?u:b;try{console.time(`fetch entries ${r.join()}`);let c=await a.getEntries({"sys.id[in]":r.join(),limit:r.length,...C});return q(c.items,r)}catch(c){let d=P(c);throw new Error(`Failed loading Contentful entries '${r.join()}' from source '${(f=i.value.source)!=null?f:"default"}' referenced in parameter '${l}': ${d}`)}finally{console.timeEnd(`fetch entries ${r.join()}`)}}},limitPolicy:n||v}}function B(e){return e.type===Y[0]}function U(e){var t;return!(!e||!((t=e.entries)!=null&&t.length))}function q(e,t){var o;if(!e.length||!((o=e[0].sys)!=null&&o.id))return e;let n=[];for(let E of t){let i=e.find(l=>l.sys.id===E);i&&n.push(i)}return n}var D=Object.freeze(["contentfulQuery"]),F={select:"fields",include:1};function he({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:l,component:y,context:s}){var u,f;if(z(i)){if(!K(i.value))return null;let a=A({clients:e,parameterName:l,parameterValue:i.value,component:y,context:s}),r=i.value,C=G(r.count),c=(u=t==null?void 0:t({parameter:i,parameterName:l,component:y,defaultQuery:{...F},context:s}))!=null?u:F,d=`fetch query: ${C} entries with '${r.contentType}' content type`;try{return console.time(d),(await a.getEntries({content_type:r.contentType,order:$(r.sortBy,r.sortOrder),limit:C,...c})).items}catch(m){let h=P(m);throw new Error(`Failed loading Contentful entries with '${r.contentType}' content type from source '${(f=r.source)!=null?f:"default"}' referenced in parameter '${l}': ${h}`)}finally{console.timeEnd(d)}}},limitPolicy:n||v}}function z(e){return e.type===D[0]}function K(e){return!(!e||!e.source||!e.contentType||typeof e.count=="undefined")}function G(e){return!e||e<1?1:e>1e3?1e3:e}export{Y as CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES,_ as CANVAS_CONTENTFUL_PARAMETER_TYPES,D as CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES,x as ContentfulClientList,W as contentfulRichTextToHtmlEnhancer,fe as createContentfulEnhancer,pe as createContentfulMultiEnhancer,he as createContentfulQueryEnhancer};
1
+ // src/ContentfulClientList.ts
2
+ var ContentfulClientList = class {
3
+ constructor(clients) {
4
+ this._clients = {};
5
+ if (Array.isArray(clients)) {
6
+ clients.forEach((client) => this.addClient(client));
7
+ } else if (clients) {
8
+ this.addClient(clients);
9
+ }
10
+ }
11
+ addClient({ source = "default", client, previewClient }) {
12
+ if (this._clients[source]) {
13
+ throw new Error(`The source ${source} is always registered`);
14
+ }
15
+ if (!client) {
16
+ throw new Error("You must provide a Contentful client for the ContentfulClientList");
17
+ }
18
+ this._clients[source] = {
19
+ client,
20
+ previewClient: previewClient || client
21
+ };
22
+ }
23
+ getClient({
24
+ source = "default",
25
+ isPreviewClient
26
+ }) {
27
+ const found = this._clients[source];
28
+ if (!found) {
29
+ return void 0;
30
+ }
31
+ return isPreviewClient ? found.previewClient : found.client;
32
+ }
33
+ };
34
+
35
+ // src/contentfulRichTextToHtmlEnhancer.ts
36
+ import { documentToHtmlString } from "@contentful/rich-text-html-renderer";
37
+ var contentfulRichTextToHtmlEnhancer = ({
38
+ parameter
39
+ }) => {
40
+ const value = parameter.value;
41
+ if (!value) {
42
+ return value;
43
+ }
44
+ if (!isArrayValue(value)) {
45
+ updateEntryFields(value);
46
+ return value;
47
+ }
48
+ for (const entry of value) {
49
+ updateEntryFields(entry);
50
+ }
51
+ return value;
52
+ };
53
+ function updateEntryFields(entry) {
54
+ var _a;
55
+ if (typeof (entry == null ? void 0 : entry.fields) !== "object") {
56
+ return;
57
+ }
58
+ Object.entries((_a = entry.fields) != null ? _a : {}).forEach(([key, value]) => {
59
+ if (typeof value === "object" && "nodeType" in value && value.nodeType === "document") {
60
+ entry.fields[key] = documentToHtmlString(value);
61
+ }
62
+ });
63
+ }
64
+ function isArrayValue(value) {
65
+ return Array.isArray(value);
66
+ }
67
+
68
+ // src/createContentfulEnhancer.ts
69
+ import {
70
+ createBatchEnhancer,
71
+ UniqueBatchEntries
72
+ } from "@uniformdev/canvas";
73
+
74
+ // src/utils.ts
75
+ import { createLimitPolicy } from "@uniformdev/canvas";
76
+ var defaultContentfulLimitPolicy = createLimitPolicy({
77
+ throttle: {
78
+ limit: 55,
79
+ interval: 1e3
80
+ }
81
+ });
82
+ function getErrorMessageFromContentfulError(e) {
83
+ if (typeof e === "string") {
84
+ return e;
85
+ }
86
+ if (typeof e === "object" && e && "error" in e) {
87
+ return e.error;
88
+ }
89
+ if (e instanceof Error) {
90
+ return e.toString();
91
+ }
92
+ return JSON.stringify(e, null, 2);
93
+ }
94
+ function toContentfulOrder(sortBy, sortOrder) {
95
+ if (!sortBy) {
96
+ return void 0;
97
+ }
98
+ return `${sortOrder === "desc" ? "-" : ""}${sortBy}`;
99
+ }
100
+ function resolveClientForParameter({
101
+ clients,
102
+ parameterValue,
103
+ parameterName,
104
+ component,
105
+ context
106
+ }) {
107
+ const { source = "default" } = parameterValue;
108
+ const client = clients.getClient({
109
+ source,
110
+ isPreviewClient: context.preview
111
+ });
112
+ if (!client) {
113
+ throw new Error(
114
+ `No Contentful client could be resolved for source key '${source}' referenced in parameter '${parameterName} in component '${component.type}'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.`
115
+ );
116
+ }
117
+ return client;
118
+ }
119
+
120
+ // src/createContentfulEnhancer.ts
121
+ var defaultQuery = {
122
+ select: "fields",
123
+ include: 1
124
+ };
125
+ var CANVAS_CONTENTFUL_PARAMETER_TYPES = Object.freeze(["contentfulEntry"]);
126
+ function isClientList(client) {
127
+ return client instanceof ContentfulClientList;
128
+ }
129
+ function createContentfulEnhancer({
130
+ client,
131
+ previewClient,
132
+ createQuery,
133
+ useBatching,
134
+ limitPolicy
135
+ }) {
136
+ if (!client) {
137
+ throw new Error(
138
+ "No Contentful clients were provided to the enhancer. You must provide at least one client via the `client` or `clients` property."
139
+ );
140
+ }
141
+ const resolveClients = () => {
142
+ if (isClientList(client)) {
143
+ return client;
144
+ }
145
+ const clientList = new ContentfulClientList();
146
+ clientList.addClient({
147
+ client,
148
+ previewClient
149
+ });
150
+ return clientList;
151
+ };
152
+ const clients = resolveClients();
153
+ const finalLimitPolicy = limitPolicy || defaultContentfulLimitPolicy;
154
+ if (useBatching) {
155
+ return createBatchEnhancer({
156
+ handleBatch: async (queuedTasks) => {
157
+ var _a;
158
+ const taskGroups = queuedTasks.reduce((result, queuedTask) => {
159
+ const { parameter, parameterName, component, context } = queuedTask.args;
160
+ const paramValue = parameter.value;
161
+ if (!isParameterValueDefined(paramValue)) {
162
+ return result;
163
+ }
164
+ const client2 = resolveClientForParameter2({
165
+ parameterValue: paramValue,
166
+ parameterName,
167
+ clients,
168
+ component,
169
+ context
170
+ });
171
+ let groupKey = "";
172
+ if (isLegacyValue(paramValue)) {
173
+ groupKey = "legacy-group";
174
+ } else {
175
+ const { source = "default" } = paramValue;
176
+ groupKey = source;
177
+ }
178
+ if (result[groupKey] && Array.isArray(result[groupKey].tasks)) {
179
+ result[groupKey].tasks.push(queuedTask);
180
+ } else {
181
+ result[groupKey] = {
182
+ client: client2,
183
+ tasks: [queuedTask]
184
+ };
185
+ }
186
+ return result;
187
+ }, {});
188
+ try {
189
+ console.time("fetch all entries");
190
+ for await (const [environmentKey, taskGroup] of Object.entries(taskGroups)) {
191
+ const { context, component } = taskGroup.tasks[0].args;
192
+ const query = (_a = createQuery == null ? void 0 : createQuery({
193
+ component,
194
+ defaultQuery: { ...defaultQuery },
195
+ context
196
+ })) != null ? _a : defaultQuery;
197
+ const uniqueBatchEntries = new UniqueBatchEntries(
198
+ taskGroup.tasks,
199
+ (task) => isLegacyValue(task.parameter.value) ? task.parameter.value : task.parameter.value.entryId
200
+ );
201
+ const idsToResolve = Object.keys(uniqueBatchEntries.groups);
202
+ console.time(`fetch entries ${environmentKey}`);
203
+ try {
204
+ const entries = await taskGroup.client.getEntries({
205
+ "sys.id[in]": idsToResolve.join(","),
206
+ limit: idsToResolve.length,
207
+ ...query
208
+ });
209
+ entries.items.forEach((entry) => {
210
+ uniqueBatchEntries.resolveKey(entry.sys.id, entry);
211
+ });
212
+ uniqueBatchEntries.resolveRemaining(null);
213
+ } finally {
214
+ console.timeEnd(`fetch entries ${environmentKey}`);
215
+ }
216
+ }
217
+ console.timeEnd("fetch all entries");
218
+ } catch (e) {
219
+ const message = getErrorMessageFromContentfulError(e);
220
+ const error = new Error(
221
+ `Failed loading Contentful entries batch (${queuedTasks.length}) ${message}`
222
+ );
223
+ queuedTasks.forEach((task) => task.reject(error));
224
+ }
225
+ },
226
+ shouldQueue: ({ parameter }) => parameterIsContentfulEntrySelector(parameter),
227
+ limitPolicy: finalLimitPolicy
228
+ });
229
+ } else {
230
+ return {
231
+ enhanceOne: async function contentfulEnhancer({ parameter, parameterName, component, context }) {
232
+ var _a, _b;
233
+ if (parameterIsContentfulEntrySelector(parameter)) {
234
+ if (!isParameterValueDefined(parameter.value)) {
235
+ return null;
236
+ }
237
+ const client2 = resolveClientForParameter2({
238
+ clients,
239
+ parameterName,
240
+ parameterValue: parameter.value,
241
+ component,
242
+ context
243
+ });
244
+ const entryId = isLegacyValue(parameter.value) ? parameter.value : parameter.value.entryId;
245
+ const query = (_a = createQuery == null ? void 0 : createQuery({
246
+ parameter,
247
+ parameterName,
248
+ component,
249
+ defaultQuery: { ...defaultQuery },
250
+ context
251
+ })) != null ? _a : defaultQuery;
252
+ try {
253
+ console.time(`fetch entry ${entryId}`);
254
+ const entry = await client2.getEntry(entryId, query);
255
+ return entry;
256
+ } catch (e) {
257
+ const message = getErrorMessageFromContentfulError(e);
258
+ if (isLegacyValue(parameter.value)) {
259
+ throw new Error(
260
+ `Failed loading Contentful entry '${parameter.value}' referenced in parameter '${parameterName}': ${message}`
261
+ );
262
+ } else {
263
+ throw new Error(
264
+ `Failed loading Contentful entry '${entryId}' from source '${(_b = parameter.value.source) != null ? _b : "default"}' referenced in parameter '${parameterName}': ${message}`
265
+ );
266
+ }
267
+ } finally {
268
+ console.timeEnd(`fetch entry ${entryId}`);
269
+ }
270
+ }
271
+ },
272
+ limitPolicy: finalLimitPolicy
273
+ };
274
+ }
275
+ }
276
+ function parameterIsContentfulEntrySelector(parameter) {
277
+ var _a;
278
+ return parameter.type === CANVAS_CONTENTFUL_PARAMETER_TYPES[0] && (((_a = parameter.value) == null ? void 0 : _a.entryId) || typeof parameter.value === "string");
279
+ }
280
+ function isLegacyValue(value) {
281
+ return typeof value === "string";
282
+ }
283
+ function isParameterValueDefined(value) {
284
+ if (!value || !isLegacyValue(value) && !value.entryId) {
285
+ return false;
286
+ }
287
+ return true;
288
+ }
289
+ function resolveClientForParameter2({
290
+ clients,
291
+ parameterValue,
292
+ parameterName,
293
+ component,
294
+ context
295
+ }) {
296
+ if (isLegacyValue(parameterValue)) {
297
+ const client2 = clients.getClient({ isPreviewClient: context.preview });
298
+ if (!client2) {
299
+ throw new Error(
300
+ `Parameter '${parameterName}' in component '${component.type}' has a value '${parameterValue}' 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.`
301
+ );
302
+ }
303
+ return client2;
304
+ }
305
+ const { source = "default" } = parameterValue;
306
+ const client = clients.getClient({
307
+ source,
308
+ isPreviewClient: context.preview
309
+ });
310
+ if (!client) {
311
+ throw new Error(
312
+ `No Contentful client could be resolved for source key '${source}' referenced in parameter '${parameterName} in component '${component.type}'. Ensure that the 'clients' property you are passing to the enhancer has a client instance registered for the source key.`
313
+ );
314
+ }
315
+ return client;
316
+ }
317
+
318
+ // src/createContentfulMultiEnhancer.ts
319
+ var defaultQuery2 = {
320
+ select: "fields",
321
+ include: 1
322
+ };
323
+ var CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES = Object.freeze(["contentfulMultiEntry"]);
324
+ function createContentfulMultiEnhancer({
325
+ clients,
326
+ createQuery,
327
+ limitPolicy
328
+ }) {
329
+ if (!clients) {
330
+ throw new Error(
331
+ "No Contentful clients were provided to the enhancer. You must provide at least one client via the ContentfulClientList."
332
+ );
333
+ }
334
+ const finalLimitPolicy = limitPolicy || defaultContentfulLimitPolicy;
335
+ return {
336
+ enhanceOne: async function contentfulMultiEntryEnhancer({
337
+ parameter,
338
+ parameterName,
339
+ component,
340
+ context
341
+ }) {
342
+ var _a, _b;
343
+ if (parameterIsContentfulMultiEntry(parameter)) {
344
+ if (!isParameterValueDefined2(parameter.value)) {
345
+ return null;
346
+ }
347
+ const client = resolveClientForParameter({
348
+ clients,
349
+ parameterName,
350
+ parameterValue: parameter.value,
351
+ component,
352
+ context
353
+ });
354
+ const entryIds = parameter.value.entries;
355
+ const query = (_a = createQuery == null ? void 0 : createQuery({
356
+ parameter,
357
+ parameterName,
358
+ component,
359
+ defaultQuery: { ...defaultQuery2 },
360
+ context
361
+ })) != null ? _a : defaultQuery2;
362
+ try {
363
+ console.time(`fetch entries ${entryIds.join()}`);
364
+ const entries = await client.getEntries({
365
+ "sys.id[in]": entryIds.join(),
366
+ limit: entryIds.length,
367
+ ...query
368
+ });
369
+ const items = syncEntriesOrder(entries.items, entryIds);
370
+ return items;
371
+ } catch (e) {
372
+ const message = getErrorMessageFromContentfulError(e);
373
+ throw new Error(
374
+ `Failed loading Contentful entries '${entryIds.join()}' from source '${(_b = parameter.value.source) != null ? _b : "default"}' referenced in parameter '${parameterName}': ${message}`
375
+ );
376
+ } finally {
377
+ console.timeEnd(`fetch entries ${entryIds.join()}`);
378
+ }
379
+ }
380
+ },
381
+ limitPolicy: finalLimitPolicy
382
+ };
383
+ }
384
+ function parameterIsContentfulMultiEntry(parameter) {
385
+ return parameter.type === CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES[0];
386
+ }
387
+ function isParameterValueDefined2(value) {
388
+ var _a;
389
+ if (!value || !((_a = value.entries) == null ? void 0 : _a.length)) {
390
+ return false;
391
+ }
392
+ return true;
393
+ }
394
+ function syncEntriesOrder(entries, entryIds) {
395
+ var _a;
396
+ if (!entries.length || !((_a = entries[0].sys) == null ? void 0 : _a.id)) {
397
+ return entries;
398
+ }
399
+ const sorted = [];
400
+ for (const entryId of entryIds) {
401
+ const entry = entries.find((x) => x.sys.id === entryId);
402
+ if (entry) {
403
+ sorted.push(entry);
404
+ }
405
+ }
406
+ return sorted;
407
+ }
408
+
409
+ // src/createContentfulQueryEnhancer.ts
410
+ var CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES = Object.freeze(["contentfulQuery"]);
411
+ var defaultQuery3 = {
412
+ select: "fields",
413
+ include: 1
414
+ };
415
+ function createContentfulQueryEnhancer({
416
+ clients,
417
+ createQuery,
418
+ limitPolicy
419
+ }) {
420
+ if (!clients) {
421
+ throw new Error(
422
+ "No Contentful clients were provided to the enhancer. You must provide at least one client via the ContentfulClientList."
423
+ );
424
+ }
425
+ const finalLimitPolicy = limitPolicy || defaultContentfulLimitPolicy;
426
+ return {
427
+ enhanceOne: async function contentfulQueryEnhancer({ parameter, parameterName, component, context }) {
428
+ var _a, _b;
429
+ if (parameterIsContentfulQuery(parameter)) {
430
+ if (!isParameterValueDefined3(parameter.value)) {
431
+ return null;
432
+ }
433
+ const client = resolveClientForParameter({
434
+ clients,
435
+ parameterName,
436
+ parameterValue: parameter.value,
437
+ component,
438
+ context
439
+ });
440
+ const value = parameter.value;
441
+ const count = getValidCount(value.count);
442
+ const query = (_a = createQuery == null ? void 0 : createQuery({
443
+ parameter,
444
+ parameterName,
445
+ component,
446
+ defaultQuery: { ...defaultQuery3 },
447
+ context
448
+ })) != null ? _a : defaultQuery3;
449
+ const timerLabel = `fetch query: ${count} entries with '${value.contentType}' content type`;
450
+ try {
451
+ console.time(timerLabel);
452
+ const entries = await client.getEntries({
453
+ content_type: value.contentType,
454
+ order: toContentfulOrder(value.sortBy, value.sortOrder),
455
+ limit: count,
456
+ ...query
457
+ });
458
+ return entries.items;
459
+ } catch (e) {
460
+ const message = getErrorMessageFromContentfulError(e);
461
+ throw new Error(
462
+ `Failed loading Contentful entries with '${value.contentType}' content type from source '${(_b = value.source) != null ? _b : "default"}' referenced in parameter '${parameterName}': ${message}`
463
+ );
464
+ } finally {
465
+ console.timeEnd(timerLabel);
466
+ }
467
+ }
468
+ },
469
+ limitPolicy: finalLimitPolicy
470
+ };
471
+ }
472
+ function parameterIsContentfulQuery(parameter) {
473
+ return parameter.type === CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES[0];
474
+ }
475
+ function isParameterValueDefined3(value) {
476
+ if (!value || !value.source || !value.contentType || typeof value.count === "undefined") {
477
+ return false;
478
+ }
479
+ return true;
480
+ }
481
+ function getValidCount(count) {
482
+ if (!count || count < 1) {
483
+ return 1;
484
+ }
485
+ if (count > 1e3) {
486
+ return 1e3;
487
+ }
488
+ return count;
489
+ }
490
+ export {
491
+ CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES,
492
+ CANVAS_CONTENTFUL_PARAMETER_TYPES,
493
+ CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES,
494
+ ContentfulClientList,
495
+ contentfulRichTextToHtmlEnhancer,
496
+ createContentfulEnhancer,
497
+ createContentfulMultiEnhancer,
498
+ createContentfulQueryEnhancer
499
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniformdev/canvas-contentful",
3
- "version": "17.7.1-alpha.34+084da3172",
3
+ "version": "18.0.1-alpha.3+2267c1ac6",
4
4
  "description": "Contentful data enhancers for Uniform Canvas",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "main": "./dist/index.js",
@@ -15,7 +15,7 @@
15
15
  "types": "./dist/index.d.ts",
16
16
  "sideEffects": false,
17
17
  "scripts": {
18
- "build": "tsup --minify",
18
+ "build": "tsup",
19
19
  "dev": "tsup --watch",
20
20
  "clean": "rimraf dist",
21
21
  "test": "jest --maxWorkers=1",
@@ -23,15 +23,15 @@
23
23
  "format": "prettier --write \"src/**/*.{js,ts,tsx}\""
24
24
  },
25
25
  "dependencies": {
26
- "@uniformdev/canvas": "^17.7.1-alpha.34+084da3172"
26
+ "@uniformdev/canvas": "18.0.1-alpha.3+2267c1ac6"
27
27
  },
28
28
  "peerDependencies": {
29
29
  "@contentful/rich-text-html-renderer": ">= 14",
30
30
  "contentful": ">= 8"
31
31
  },
32
32
  "devDependencies": {
33
- "@contentful/rich-text-html-renderer": "15.15.1",
34
- "contentful": "9.2.14"
33
+ "@contentful/rich-text-html-renderer": "16.0.2",
34
+ "contentful": "9.2.15"
35
35
  },
36
36
  "files": [
37
37
  "/dist"
@@ -39,5 +39,5 @@
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
42
- "gitHead": "084da31729798e2c1a1f26ba38c764caabebc958"
42
+ "gitHead": "2267c1ac6f5f6929be6b12d0099905ab46848d0c"
43
43
  }