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