@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/LICENSE.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
©
|
|
1
|
+
© 2023 Uniform Systems, Inc. All Rights Reserved.
|
|
2
2
|
See details of Uniform Systems, Inc. Master Subscription Agreement here: https://uniform.dev/eula
|
package/dist/index.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ declare class ContentfulClientList {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/** The default shape of the Contentful entry. Note that this can change if the query is altered. */
|
|
31
|
-
|
|
31
|
+
type ContentfulEntryResult<TFields> = {
|
|
32
32
|
/**
|
|
33
33
|
* The shape of the `fields` that the Contentful REST API is expected to return for this entry
|
|
34
34
|
* https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/entries/entry
|
|
@@ -40,14 +40,14 @@ declare type ContentfulEntryResult<TFields> = {
|
|
|
40
40
|
sys: Partial<Pick<Sys, 'id' | 'type'>>;
|
|
41
41
|
};
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
type EnhancerValue = ContentfulEntryResult<unknown> | ContentfulEntryResult<unknown>[] | null;
|
|
44
44
|
declare const contentfulRichTextToHtmlEnhancer: ComponentParameterEnhancerFunction<EnhancerValue>;
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
type EntrySelectorParameterValue = {
|
|
47
47
|
entryId: string;
|
|
48
48
|
source?: string;
|
|
49
49
|
} | string | null | undefined;
|
|
50
|
-
|
|
50
|
+
type CreateContentfulQueryOptions<TContext extends EnhancerContext = EnhancerContext> = {
|
|
51
51
|
/** Canvas parameter name being queried for. Not defined if using batching. */
|
|
52
52
|
parameterName?: string;
|
|
53
53
|
/** Canvas parameter value being fetched. Not defined if using batching. */
|
|
@@ -60,8 +60,8 @@ declare type CreateContentfulQueryOptions<TContext extends EnhancerContext = Enh
|
|
|
60
60
|
context: TContext;
|
|
61
61
|
};
|
|
62
62
|
/** The default shape of the result value of the Contentful enhancer. Note that this can change if the query is altered. */
|
|
63
|
-
|
|
64
|
-
|
|
63
|
+
type ContentfulEnhancerResult<TFields> = ContentfulEntryResult<TFields> | null;
|
|
64
|
+
type CreateContentfulEnhancerOptions = {
|
|
65
65
|
/** Either a list of Contentful clients for use with multi-space/environment-enabled Canvas projects.
|
|
66
66
|
* Or a single Contentful client for use with legacy Canvas data.*/
|
|
67
67
|
client: ContentfulClientApi | ContentfulClientList;
|
|
@@ -78,11 +78,11 @@ declare type CreateContentfulEnhancerOptions = {
|
|
|
78
78
|
declare const CANVAS_CONTENTFUL_PARAMETER_TYPES: readonly string[];
|
|
79
79
|
declare function createContentfulEnhancer({ client, previewClient, createQuery, useBatching, limitPolicy, }: CreateContentfulEnhancerOptions): ComponentParameterEnhancer<EntrySelectorParameterValue, ContentfulEnhancerResult<unknown>>;
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
type ContentfulMultiEntryParameterValue = {
|
|
82
82
|
entries: string[];
|
|
83
83
|
source: string;
|
|
84
84
|
} | null | undefined;
|
|
85
|
-
|
|
85
|
+
type CreateContentfulMultiEntryQueryOptions<TContext extends EnhancerContext = EnhancerContext> = {
|
|
86
86
|
/** Canvas parameter name being queried for */
|
|
87
87
|
parameterName: string;
|
|
88
88
|
/** Canvas parameter value being fetched */
|
|
@@ -95,8 +95,8 @@ declare type CreateContentfulMultiEntryQueryOptions<TContext extends EnhancerCon
|
|
|
95
95
|
context: TContext;
|
|
96
96
|
};
|
|
97
97
|
/** The default shape of the result value of the Contentful Multi Entry enhancer. Note that this can change if the query is altered. */
|
|
98
|
-
|
|
99
|
-
|
|
98
|
+
type ContentfulMultiEntryEnhancerResult<TFields> = ContentfulEntryResult<TFields>[] | null;
|
|
99
|
+
type CreateContentfulMultiEntryEnhancerOptions = {
|
|
100
100
|
/** Either a list of Contentful clients for use with multi-space/environment-enabled Canvas projects. */
|
|
101
101
|
clients: ContentfulClientList;
|
|
102
102
|
/** Creates the Contentful client's query params for specific parameters. See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters */
|
|
@@ -107,7 +107,7 @@ declare const CANVAS_CONTENTFUL_MULTI_PARAMETER_TYPES: readonly string[];
|
|
|
107
107
|
declare function createContentfulMultiEnhancer({ clients, createQuery, limitPolicy, }: CreateContentfulMultiEntryEnhancerOptions): ComponentParameterEnhancer<ContentfulMultiEntryParameterValue, ContentfulMultiEntryEnhancerResult<unknown>>;
|
|
108
108
|
|
|
109
109
|
declare const CANVAS_CONTENTFUL_QUERY_PARAMETER_TYPES: readonly string[];
|
|
110
|
-
|
|
110
|
+
type ContentfulQueryParameterValue = {
|
|
111
111
|
source: string;
|
|
112
112
|
contentType: string;
|
|
113
113
|
count: number;
|
|
@@ -115,8 +115,8 @@ declare type ContentfulQueryParameterValue = {
|
|
|
115
115
|
sortOrder?: 'asc' | 'desc';
|
|
116
116
|
} | null | undefined;
|
|
117
117
|
/** The default shape of the result value of the Contentful Query enhancer. Note that this can change if the query is altered. */
|
|
118
|
-
|
|
119
|
-
|
|
118
|
+
type ContentfulQueryEnhancerResult<TFields> = ContentfulEntryResult<TFields>[] | null;
|
|
119
|
+
type CreateContentfulQueryApiQueryOptions<TContext extends EnhancerContext = EnhancerContext> = {
|
|
120
120
|
/** Canvas parameter name being queried for */
|
|
121
121
|
parameterName: string;
|
|
122
122
|
/** Canvas parameter value being fetched */
|
|
@@ -128,7 +128,7 @@ declare type CreateContentfulQueryApiQueryOptions<TContext extends EnhancerConte
|
|
|
128
128
|
/** The enhancer context provided to the enhance() function */
|
|
129
129
|
context: TContext;
|
|
130
130
|
};
|
|
131
|
-
|
|
131
|
+
type CreateContentfulQueryEnhancerOptions = {
|
|
132
132
|
/** Either a list of Contentful clients for use with multi-space/environment-enabled Canvas projects. */
|
|
133
133
|
clients: ContentfulClientList;
|
|
134
134
|
/** Creates the Contentful client's query params for specific parameters. See https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters */
|
package/dist/index.esm.js
CHANGED
|
@@ -1 +1,499 @@
|
|
|
1
|
-
|
|
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
|
+
};
|