@dotcms/client 0.0.1-beta.2 → 0.0.1-beta.21

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.
Files changed (47) hide show
  1. package/README.md +164 -36
  2. package/index.cjs.js +102 -1238
  3. package/index.esm.js +86 -1221
  4. package/next.cjs.d.ts +1 -0
  5. package/next.cjs.default.js +1 -0
  6. package/next.cjs.js +575 -0
  7. package/next.cjs.mjs +2 -0
  8. package/next.esm.d.ts +1 -0
  9. package/next.esm.js +573 -0
  10. package/package.json +32 -7
  11. package/src/index.d.ts +6 -6
  12. package/src/lib/client/client.d.ts +84 -0
  13. package/src/lib/client/content/builders/collection/collection.d.ts +1 -1
  14. package/src/lib/client/content/content-api.d.ts +1 -1
  15. package/src/lib/client/content/shared/types.d.ts +2 -2
  16. package/src/lib/client/models/types.d.ts +573 -10
  17. package/src/lib/client/navigation/navigation-api.d.ts +31 -0
  18. package/src/lib/client/page/page-api.d.ts +172 -0
  19. package/src/lib/client/page/utils.d.ts +41 -0
  20. package/src/lib/{editor → deprecated/editor}/models/client.model.d.ts +13 -0
  21. package/src/lib/{editor → deprecated/editor}/sdk-editor.d.ts +1 -1
  22. package/src/lib/{client → deprecated}/sdk-js-client.d.ts +1 -1
  23. package/src/lib/utils/page/common-utils.d.ts +1 -1
  24. package/src/next.d.ts +1 -0
  25. package/src/types.d.ts +2 -0
  26. package/transforms.cjs.js +1149 -0
  27. package/transforms.esm.js +1143 -0
  28. package/types.cjs.d.ts +1 -0
  29. package/types.cjs.default.js +1 -0
  30. package/types.cjs.js +2 -0
  31. package/types.cjs.mjs +2 -0
  32. package/types.esm.d.ts +1 -0
  33. package/types.esm.js +1 -0
  34. /package/src/lib/{query-builder → client/content/builders/query}/lucene-syntax/Equals.d.ts +0 -0
  35. /package/src/lib/{query-builder → client/content/builders/query}/lucene-syntax/Field.d.ts +0 -0
  36. /package/src/lib/{query-builder → client/content/builders/query}/lucene-syntax/NotOperand.d.ts +0 -0
  37. /package/src/lib/{query-builder → client/content/builders/query}/lucene-syntax/Operand.d.ts +0 -0
  38. /package/src/lib/{query-builder → client/content/builders/query}/lucene-syntax/index.d.ts +0 -0
  39. /package/src/lib/{query-builder/sdk-query-builder.d.ts → client/content/builders/query/query.d.ts} +0 -0
  40. /package/src/lib/{query-builder → client/content/builders/query}/utils/index.d.ts +0 -0
  41. /package/src/lib/{editor → deprecated/editor}/listeners/listeners.d.ts +0 -0
  42. /package/src/lib/{editor → deprecated/editor}/models/editor.model.d.ts +0 -0
  43. /package/src/lib/{editor → deprecated/editor}/models/inline-event.model.d.ts +0 -0
  44. /package/src/lib/{editor → deprecated/editor}/models/listeners.model.d.ts +0 -0
  45. /package/src/lib/{editor → deprecated/editor}/sdk-editor-vtl.d.ts +0 -0
  46. /package/src/lib/{editor → deprecated/editor}/utils/editor.utils.d.ts +0 -0
  47. /package/src/lib/{editor → deprecated/editor}/utils/traditional-vtl.utils.d.ts +0 -0
@@ -0,0 +1,1149 @@
1
+ 'use strict';
2
+
3
+ /******************************************************************************
4
+ Copyright (c) Microsoft Corporation.
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
16
+ ***************************************************************************** */
17
+ /* global Reflect, Promise, SuppressedError, Symbol */
18
+
19
+
20
+ function __classPrivateFieldGet(receiver, state, kind, f) {
21
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
22
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
23
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
24
+ }
25
+
26
+ function __classPrivateFieldSet(receiver, state, value, kind, f) {
27
+ if (kind === "m") throw new TypeError("Private method is not writable");
28
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
29
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
30
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
31
+ }
32
+
33
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
34
+ var e = new Error(message);
35
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
36
+ };
37
+
38
+ /**
39
+ * Default variant identifier used in the application.
40
+ */
41
+ /**
42
+ * Fields that should not be formatted when sanitizing the query.
43
+ * These fields are essential for maintaining the integrity of the content type.
44
+ */
45
+ const CONTENT_TYPE_MAIN_FIELDS = ['live', 'variant', 'contentType', 'languageId'];
46
+ /**
47
+ * URL endpoint for the content API search functionality.
48
+ */
49
+ const CONTENT_API_URL = '/api/content/_search';
50
+
51
+ /**
52
+ * @description
53
+ * Sanitizes the query for the given content type.
54
+ * It replaces the fields that are not content type fields with the correct format.
55
+ * Example: +field: -> +contentTypeVar.field:
56
+ *
57
+ * @example
58
+ *
59
+ * ```ts
60
+ * const query = '+field: value';
61
+ * const contentType = 'contentTypeVar';
62
+ * const sanitizedQuery = sanitizeQueryForContentType(query, contentType); // Output: '+contentTypeVar.field: value'
63
+ * ```
64
+ *
65
+ * @export
66
+ * @param {string} query - The query string to be sanitized.
67
+ * @param {string} contentType - The content type to be used for formatting the fields.
68
+ * @returns {string} The sanitized query string.
69
+ */
70
+ function sanitizeQueryForContentType(query, contentType) {
71
+ return query.replace(/\+([^+:]*?):/g, (original, field) => {
72
+ return !CONTENT_TYPE_MAIN_FIELDS.includes(field) // Fields that are not content type fields
73
+ ? `+${contentType}.${field}:` // Should have this format: +contentTypeVar.field:
74
+ : original; // Return the field if it is a content type field
75
+ });
76
+ }
77
+
78
+ var _Field_query;
79
+ /**
80
+ * The `Field` class is used to build a query with a specific field.
81
+ * A Lucene Field is a key used to search for a specific value in a document.
82
+ *
83
+ * @export
84
+ * @class Field
85
+ */
86
+ class Field {
87
+ /**
88
+ * Creates an instance of the `Field` class.
89
+ *
90
+ * @param {string} query - The initial query string.
91
+ */
92
+ constructor(query) {
93
+ this.query = query;
94
+ _Field_query.set(this, '');
95
+ __classPrivateFieldSet(this, _Field_query, this.query, "f");
96
+ }
97
+ /**
98
+ * Appends a term to the query that should be included in the search.
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * const field = new Field("+myField");
103
+ * field.equals("myValue");
104
+ * ```
105
+ *
106
+ * @param {string} term - The term that should be included in the search.
107
+ * @return {Equals} - An instance of `Equals`.
108
+ * @memberof Field
109
+ */
110
+ equals(term) {
111
+ return buildEquals(__classPrivateFieldGet(this, _Field_query, "f"), term);
112
+ }
113
+ }
114
+ _Field_query = new WeakMap();
115
+
116
+ var _NotOperand_query;
117
+ /**
118
+ * 'NotOperand' Is a Typescript class that provides the ability to use the NOT operand in the lucene query string.
119
+ *
120
+ * @export
121
+ * @class NotOperand
122
+ */
123
+ class NotOperand {
124
+ constructor(query) {
125
+ this.query = query;
126
+ _NotOperand_query.set(this, '');
127
+ __classPrivateFieldSet(this, _NotOperand_query, this.query, "f");
128
+ }
129
+ /**
130
+ * This method appends to the query a term that should be included in the search.
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * const notOperand = new NotOperand("+myField");
135
+ * notOperand.equals("myValue");
136
+ * ```
137
+ *
138
+ * @param {string} term - The term that should be included in the search.
139
+ * @return {*} {Equals} - An instance of Equals.
140
+ * @memberof NotOperand
141
+ */
142
+ equals(term) {
143
+ return buildEquals(__classPrivateFieldGet(this, _NotOperand_query, "f"), term);
144
+ }
145
+ }
146
+ _NotOperand_query = new WeakMap();
147
+
148
+ var _Operand_query;
149
+ /**
150
+ * 'Operand' Is a Typescript class that provides the ability to use operands in the lucene query string.}
151
+ * An operand is a logical operator used to join two or more conditions in a query.
152
+ *
153
+ * @export
154
+ * @class Operand
155
+ */
156
+ class Operand {
157
+ constructor(query) {
158
+ this.query = query;
159
+ _Operand_query.set(this, '');
160
+ __classPrivateFieldSet(this, _Operand_query, this.query, "f");
161
+ }
162
+ /**
163
+ * This method appends to the query a term that should be excluded in the search.
164
+ *
165
+ * Ex: "-myValue"
166
+ *
167
+ * @param {string} field - The field that should be excluded in the search.
168
+ * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
169
+ * @memberof Operand
170
+ */
171
+ excludeField(field) {
172
+ return buildExcludeField(__classPrivateFieldGet(this, _Operand_query, "f"), field);
173
+ }
174
+ /**
175
+ * This method appends to the query a field that should be included in the search.
176
+ *
177
+ * Ex: "+myField:"
178
+ *
179
+ * @param {string} field - The field that should be included in the search.
180
+ * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
181
+ * @memberof Operand
182
+ */
183
+ field(field) {
184
+ return buildField(__classPrivateFieldGet(this, _Operand_query, "f"), field);
185
+ }
186
+ /**
187
+ * This method appends to the query a term that should be included in the search.
188
+ *
189
+ * Ex: myValue or "My value"
190
+ *
191
+ * @param {string} term - The term that should be included in the search.
192
+ * @return {*} {Equals} - An instance of Equals.
193
+ * @memberof Operand
194
+ */
195
+ equals(term) {
196
+ return buildEquals(__classPrivateFieldGet(this, _Operand_query, "f"), term);
197
+ }
198
+ }
199
+ _Operand_query = new WeakMap();
200
+
201
+ /**
202
+ * Enum for common Operands
203
+ *
204
+ * @export
205
+ * @enum {number}
206
+ */
207
+ var OPERAND;
208
+ (function (OPERAND) {
209
+ OPERAND["OR"] = "OR";
210
+ OPERAND["AND"] = "AND";
211
+ OPERAND["NOT"] = "NOT";
212
+ })(OPERAND || (OPERAND = {}));
213
+ /**
214
+ * This function removes extra spaces from a string.
215
+ *
216
+ * @example
217
+ * ```ts
218
+ * sanitizeQuery(" my query "); // Output: "my query"
219
+ * ```
220
+ *
221
+ * @export
222
+ * @param {string} str
223
+ * @return {*} {string}
224
+ */
225
+ function sanitizeQuery(str) {
226
+ return str.replace(/\s{2,}/g, ' ').trim();
227
+ }
228
+ /**
229
+ * This function sanitizes a term by adding quotes if it contains spaces.
230
+ * In lucene, a term with spaces should be enclosed in quotes.
231
+ *
232
+ * @example
233
+ * ```ts
234
+ * sanitizePhrases(`my term`); // Output: `"my term"`
235
+ * sanitizePhrases(`myterm`); // Output: `myterm`
236
+ * ```
237
+ *
238
+ * @export
239
+ * @param {string} term
240
+ * @return {*} {string}
241
+ */
242
+ function sanitizePhrases(term) {
243
+ return term.includes(' ') ? `'${term}'` : term;
244
+ }
245
+ /**
246
+ * This function builds a term to be used in a lucene query.
247
+ * We need to sanitize the term before adding it to the query.
248
+ *
249
+ * @example
250
+ * ```ts
251
+ * const equals = buildEquals("+myField: ", "myValue"); // Current query: "+myField: myValue"
252
+ * ```
253
+ *
254
+ * @export
255
+ * @param {string} query
256
+ * @param {string} term
257
+ * @return {*} {Equals}
258
+ */
259
+ function buildEquals(query, term) {
260
+ const newQuery = query + sanitizePhrases(term);
261
+ return new Equals(newQuery);
262
+ }
263
+ /**
264
+ * This function builds a term to be used in a lucene query.
265
+ * We need to sanitize the raw query before adding it to the query.
266
+ *
267
+ * @example
268
+ * ```ts
269
+ * const query = "+myField: myValue";
270
+ * const field = buildRawEquals(query, "-myField2: myValue2"); // Current query: "+myField: myValue -myField2: myValue"
271
+ * ```
272
+ *
273
+ * @export
274
+ * @param {string} query
275
+ * @param {string} raw
276
+ * @return {*} {Equals}
277
+ */
278
+ function buildRawEquals(query, raw) {
279
+ const newQuery = query + ` ${raw}`;
280
+ return new Equals(sanitizeQuery(newQuery));
281
+ }
282
+ /**
283
+ * This function builds a field to be used in a lucene query.
284
+ * We need to format the field before adding it to the query.
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * const field = buildField("+myField: ", "myValue"); // Current query: "+myField: myValue"
289
+ * ```
290
+ *
291
+ * @export
292
+ * @param {string} query
293
+ * @param {string} field
294
+ * @return {*} {Field}
295
+ */
296
+ function buildField(query, field) {
297
+ const newQuery = query + ` +${field}:`;
298
+ return new Field(newQuery);
299
+ }
300
+ /**
301
+ * This function builds an exclude field to be used in a lucene query.
302
+ * We need to format the field before adding it to the query.
303
+ *
304
+ * @example
305
+ * ```ts
306
+ * const query = "+myField: myValue";
307
+ * const field = buildExcludeField(query, "myField2"); // Current query: "+myField: myValue -myField2:"
308
+ * ```
309
+ *
310
+ * @export
311
+ * @param {string} query
312
+ * @param {string} field
313
+ * @return {*} {Field}
314
+ */
315
+ function buildExcludeField(query, field) {
316
+ const newQuery = query + ` -${field}:`;
317
+ return new Field(newQuery);
318
+ }
319
+ /**
320
+ * This function builds an operand to be used in a lucene query.
321
+ * We need to format the operand before adding it to the query.
322
+ *
323
+ * @example
324
+ * <caption>E.g. Using the AND operand</caption>
325
+ * ```ts
326
+ * const query = "+myField: myValue";
327
+ * const field = buildOperand(query, OPERAND.AND); // Current query: "+myField: myValue AND"
328
+ * ```
329
+ * @example
330
+ * <caption>E.g. Using the OR operand</caption>
331
+ * ```ts
332
+ * const query = "+myField: myValue";
333
+ * const field = buildOperand(query, OPERAND.OR); // Current query: "+myField: myValue OR"
334
+ * ```
335
+ * @export
336
+ * @param {string} query
337
+ * @param {OPERAND} operand
338
+ * @return {*} {Operand}
339
+ */
340
+ function buildOperand(query, operand) {
341
+ const newQuery = query + ` ${operand} `;
342
+ return new Operand(newQuery);
343
+ }
344
+ /**
345
+ * This function builds a NOT operand to be used in a lucene query.
346
+ * We need to format the operand before adding it to the query.
347
+ *
348
+ * @example
349
+ * ```ts
350
+ * const query = "+myField: myValue";
351
+ * const field = buildNotOperand(query); // Current query: "+myField: myValue NOT"
352
+ * ```
353
+ *
354
+ * @export
355
+ * @param {string} query
356
+ * @return {*} {NotOperand}
357
+ */
358
+ function buildNotOperand(query) {
359
+ const newQuery = query + ` ${OPERAND.NOT} `;
360
+ return new NotOperand(newQuery);
361
+ }
362
+
363
+ var _Equals_query;
364
+ /**
365
+ * 'Equal' Is a Typescript class that provides the ability to use terms in the lucene query string.
366
+ * A term is a value used to search for a specific value in a document. It can be a word or a phrase.
367
+ *
368
+ * Ex: myValue or "My Value"
369
+ *
370
+ * @export
371
+ * @class Equal
372
+ */
373
+ class Equals {
374
+ constructor(query) {
375
+ this.query = query;
376
+ _Equals_query.set(this, '');
377
+ __classPrivateFieldSet(this, _Equals_query, this.query, "f");
378
+ }
379
+ /**
380
+ * This method appends to the query a term that should be excluded in the search.
381
+ *
382
+ * @example
383
+ * ```ts
384
+ * const equals = new Equals("+myField: myValue");
385
+ * equals.excludeField("myField2").equals("myValue2"); // Current query: "+myField: myValue -myField2: myValue2"
386
+ * ```
387
+ *
388
+ * @param {string} field - The field that should be excluded in the search.
389
+ * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
390
+ * @memberof Equal
391
+ */
392
+ excludeField(field) {
393
+ return buildExcludeField(__classPrivateFieldGet(this, _Equals_query, "f"), field);
394
+ }
395
+ /**
396
+ * This method appends to the query a field that should be included in the search.
397
+ *
398
+ * @example
399
+ * ```ts
400
+ * const equals = new Equals("+myField: myValue");
401
+ * equals.field("myField2").equals("myValue2"); // Current query: "+myField: myValue +myField2: myValue2"
402
+ *```
403
+ * @param {string} field - The field that should be included in the search.
404
+ * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
405
+ * @memberof Equal
406
+ */
407
+ field(field) {
408
+ return buildField(__classPrivateFieldGet(this, _Equals_query, "f"), field);
409
+ }
410
+ /**
411
+ * This method appends to the query an operand to use logic operators in the query.
412
+ *
413
+ * @example
414
+ * @example
415
+ * ```ts
416
+ * const equals = new Equals("+myField: myValue");
417
+ * equals.or().field("myField2").equals("myValue2"); // Current query: "+myField: myValue OR +myField2: myValue2"
418
+ * ```
419
+ *
420
+ * @return {*} {Operand} - An instance of a Lucene Operand. An operand is a logical operator used to combine terms or phrases in a query.
421
+ * @memberof Equal
422
+ */
423
+ or() {
424
+ return buildOperand(__classPrivateFieldGet(this, _Equals_query, "f"), OPERAND.OR);
425
+ }
426
+ /**
427
+ * This method appends to the query an operand to use logic operators in the query.
428
+ *
429
+ * @example
430
+ * ```ts
431
+ * const equals = new Equals("+myField: myValue");
432
+ * equals.and().field("myField2").equals("myValue2"); // Current query: "+myField: myValue AND +myField2: myValue2"
433
+ * ```
434
+ *
435
+ * @return {*} {Operand} - An instance of a Lucene Operand. An operand is a logical operator used to combine terms or phrases in a query.
436
+ * @memberof Equal
437
+ */
438
+ and() {
439
+ return buildOperand(__classPrivateFieldGet(this, _Equals_query, "f"), OPERAND.AND);
440
+ }
441
+ /**
442
+ * This method appends to the query an operand to use logic operators in the query.
443
+ *
444
+ * @example
445
+ * ```ts
446
+ * const equals = new Equals("+myField: myValue");
447
+ * equals.not().field("myField").equals("myValue2"); // Current query: "+myField: myValue NOT +myField: myValue2"
448
+ * ```
449
+ *
450
+ * @return {*} {NotOperand} - An instance of a Lucene Not Operand. A not operand is a logical operator used to exclude terms or phrases in a query.
451
+ * @memberof Equal
452
+ */
453
+ not() {
454
+ return buildNotOperand(__classPrivateFieldGet(this, _Equals_query, "f"));
455
+ }
456
+ /**
457
+ * This method allows to pass a raw query string to the query builder.
458
+ * This raw query should end in a Lucene Equal.
459
+ * This method is useful when you want to append a complex query or an already written query to the query builder.
460
+ *
461
+ * @example
462
+ * ```ts
463
+ * // This builds the follow raw query "+myField: value AND (someOtherValue OR anotherValue)"
464
+ * const equals = new Equals("+myField: value");
465
+ * equals.raw("+myField2: value2"); // Current query: "+myField: value +myField2: anotherValue"
466
+ * ```
467
+ *
468
+ * @param {string} query - A raw query string.
469
+ * @return {*} {Equal} - An instance of a Lucene Equal. A term is a value used to search for a specific value in a document.
470
+ * @memberof QueryBuilder
471
+ */
472
+ raw(query) {
473
+ return buildRawEquals(__classPrivateFieldGet(this, _Equals_query, "f"), query);
474
+ }
475
+ /**
476
+ * This method returns the final query string.
477
+ *
478
+ * @example
479
+ * ```ts
480
+ * const equals = new Equals("+myField: myValue");
481
+ * equals.field("myField2").equals("myValue2").build(); // Returns "+myField: myValue +myField2: myValue2"
482
+ * ```
483
+ *
484
+ * @return {*} {string} - The final query string.
485
+ * @memberof Equal
486
+ */
487
+ build() {
488
+ return sanitizeQuery(__classPrivateFieldGet(this, _Equals_query, "f"));
489
+ }
490
+ }
491
+ _Equals_query = new WeakMap();
492
+
493
+ var _QueryBuilder_query;
494
+ /**
495
+ * 'QueryBuilder' Is a Typescript class that provides the ability to build a query string using the Lucene syntax in a more readable way.
496
+ * @example
497
+ * ```ts
498
+ * const qb = new QueryBuilder();
499
+ * const query = qb
500
+ * .field('contentType')
501
+ * .equals('Blog')
502
+ * .field('conhost')
503
+ * .equals('my-super-cool-site')
504
+ * .build(); // Output: `+contentType:Blog +conhost:my-super-cool-site"`
505
+ * ```
506
+ *
507
+ * @example
508
+ * ```ts
509
+ * const qb = new QueryBuilder();
510
+ * const query = qb
511
+ * .field('contentType')
512
+ * .equals('Blog')
513
+ * .field('title')
514
+ * .equals('Football')
515
+ * .excludeField('summary')
516
+ * .equals('Lionel Messi')
517
+ * .build(); // Output: `+contentType:Blog +title:Football -summary:"Lionel Messi"`
518
+ * ```
519
+ * @export
520
+ * @class QueryBuilder
521
+ */
522
+ class QueryBuilder {
523
+ constructor() {
524
+ _QueryBuilder_query.set(this, '');
525
+ }
526
+ /**
527
+ * This method appends to the query a field that should be included in the search.
528
+ *
529
+ * @example
530
+ * ```ts
531
+ * const qb = new QueryBuilder();
532
+ * qb.field("+myField: ", "myValue"); // Current query: "+myField: myValue"
533
+ * ```
534
+ *
535
+ * @param {string} field - The field that should be included in the search.
536
+ * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
537
+ * @memberof QueryBuilder
538
+ */
539
+ field(field) {
540
+ return buildField(__classPrivateFieldGet(this, _QueryBuilder_query, "f"), field);
541
+ }
542
+ /**
543
+ * This method appends to the query a field that should be excluded from the search.
544
+ *
545
+ * @example
546
+ * ```ts
547
+ * const qb = new QueryBuilder();
548
+ * qb.excludeField("myField").equals("myValue"); // Current query: "-myField: myValue"
549
+ * ```
550
+ *
551
+ * @param {string} field - The field that should be excluded from the search.
552
+ * @return {*} {Field} - An instance of a Lucene Exclude Field. An exclude field is a key used to exclude for a specific value in a document.
553
+ * @memberof QueryBuilder
554
+ */
555
+ excludeField(field) {
556
+ return buildExcludeField(__classPrivateFieldGet(this, _QueryBuilder_query, "f"), field);
557
+ }
558
+ /**
559
+ * This method allows to pass a raw query string to the query builder.
560
+ * This raw query should end in Equals.
561
+ * This method is useful when you want to append a complex query or an already written query to the query builder.
562
+ *
563
+ * @example
564
+ * ```ts
565
+ * const qb = new QueryBuilder();
566
+ * qb.raw("+myField: value AND (someOtherValue OR anotherValue)"); // Current query: "+myField: value AND (someOtherValue OR anotherValue)"
567
+ * ```
568
+ *
569
+ * @param {string} query - A raw query string.
570
+ * @return {*} {Equals} - An instance of Equals. A term is a value used to search for a specific value in a document.
571
+ * @memberof QueryBuilder
572
+ */
573
+ raw(query) {
574
+ return buildRawEquals(__classPrivateFieldGet(this, _QueryBuilder_query, "f"), query);
575
+ }
576
+ }
577
+ _QueryBuilder_query = new WeakMap();
578
+
579
+ var _CollectionBuilder_page, _CollectionBuilder_limit, _CollectionBuilder_depth, _CollectionBuilder_render, _CollectionBuilder_sortBy, _CollectionBuilder_contentType, _CollectionBuilder_defaultQuery, _CollectionBuilder_query, _CollectionBuilder_rawQuery, _CollectionBuilder_languageId, _CollectionBuilder_draft, _CollectionBuilder_serverUrl, _CollectionBuilder_requestOptions;
580
+ /**
581
+ * Creates a Builder to filter and fetch content from the content API for a specific content type.
582
+ *
583
+ * @export
584
+ * @class CollectionBuilder
585
+ * @template T Represents the type of the content type to fetch. Defaults to unknown.
586
+ */
587
+ class CollectionBuilder {
588
+ /**
589
+ * Creates an instance of CollectionBuilder.
590
+ * @param {ClientOptions} requestOptions Options for the client request.
591
+ * @param {string} serverUrl The server URL.
592
+ * @param {string} contentType The content type to fetch.
593
+ * @memberof CollectionBuilder
594
+ */
595
+ constructor(requestOptions, serverUrl, contentType) {
596
+ _CollectionBuilder_page.set(this, 1);
597
+ _CollectionBuilder_limit.set(this, 10);
598
+ _CollectionBuilder_depth.set(this, 0);
599
+ _CollectionBuilder_render.set(this, false);
600
+ _CollectionBuilder_sortBy.set(this, void 0);
601
+ _CollectionBuilder_contentType.set(this, void 0);
602
+ _CollectionBuilder_defaultQuery.set(this, void 0);
603
+ _CollectionBuilder_query.set(this, void 0);
604
+ _CollectionBuilder_rawQuery.set(this, void 0);
605
+ _CollectionBuilder_languageId.set(this, 1);
606
+ _CollectionBuilder_draft.set(this, false);
607
+ _CollectionBuilder_serverUrl.set(this, void 0);
608
+ _CollectionBuilder_requestOptions.set(this, void 0);
609
+ __classPrivateFieldSet(this, _CollectionBuilder_requestOptions, requestOptions, "f");
610
+ __classPrivateFieldSet(this, _CollectionBuilder_serverUrl, serverUrl, "f");
611
+ __classPrivateFieldSet(this, _CollectionBuilder_contentType, contentType, "f");
612
+ // Build the default query with the contentType field
613
+ __classPrivateFieldSet(this, _CollectionBuilder_defaultQuery, new QueryBuilder().field('contentType').equals(__classPrivateFieldGet(this, _CollectionBuilder_contentType, "f")), "f");
614
+ }
615
+ /**
616
+ * Returns the sort query in the format: field order, field order, ...
617
+ *
618
+ * @readonly
619
+ * @private
620
+ * @memberof CollectionBuilder
621
+ */
622
+ get sort() {
623
+ return __classPrivateFieldGet(this, _CollectionBuilder_sortBy, "f")?.map((sort) => `${sort.field} ${sort.order}`).join(',');
624
+ }
625
+ /**
626
+ * Returns the offset for pagination.
627
+ *
628
+ * @readonly
629
+ * @private
630
+ * @memberof CollectionBuilder
631
+ */
632
+ get offset() {
633
+ return __classPrivateFieldGet(this, _CollectionBuilder_limit, "f") * (__classPrivateFieldGet(this, _CollectionBuilder_page, "f") - 1);
634
+ }
635
+ /**
636
+ * Returns the full URL for the content API.
637
+ *
638
+ * @readonly
639
+ * @private
640
+ * @memberof CollectionBuilder
641
+ */
642
+ get url() {
643
+ return `${__classPrivateFieldGet(this, _CollectionBuilder_serverUrl, "f")}${CONTENT_API_URL}`;
644
+ }
645
+ /**
646
+ * Returns the current query built.
647
+ *
648
+ * @readonly
649
+ * @private
650
+ * @memberof CollectionBuilder
651
+ */
652
+ get currentQuery() {
653
+ return __classPrivateFieldGet(this, _CollectionBuilder_query, "f") ?? __classPrivateFieldGet(this, _CollectionBuilder_defaultQuery, "f");
654
+ }
655
+ /**
656
+ * Filters the content by the specified language ID.
657
+ *
658
+ * @example
659
+ * ```typescript
660
+ * const client = new DotCMSClient(config);
661
+ * const collectionBuilder = client.content.getCollection("Blog");
662
+ * collectionBuilder.language(1);
663
+ * ```
664
+ *
665
+ * @param {number | string} languageId The language ID to filter the content by.
666
+ * @return {CollectionBuilder} A CollectionBuilder instance.
667
+ * @memberof CollectionBuilder
668
+ */
669
+ language(languageId) {
670
+ __classPrivateFieldSet(this, _CollectionBuilder_languageId, languageId, "f");
671
+ return this;
672
+ }
673
+ /**
674
+ * Setting this to true will server side render (using velocity) any widgets that are returned by the content query.
675
+ *
676
+ * More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
677
+ *
678
+ * @return {CollectionBuilder} A CollectionBuilder instance.
679
+ * @memberof CollectionBuilder
680
+ */
681
+ render() {
682
+ __classPrivateFieldSet(this, _CollectionBuilder_render, true, "f");
683
+ return this;
684
+ }
685
+ /**
686
+ * Sorts the content by the specified fields and orders.
687
+ *
688
+ * @example
689
+ * ```typescript
690
+ * const client = new DotCMSClient(config);
691
+ * const collectionBuilder = client.content.getCollection("Blog");
692
+ * const sortBy = [{ field: 'title', order: 'asc' }, { field: 'modDate', order: 'desc' }];
693
+ * collectionBuilder("Blog").sortBy(sortBy);
694
+ * ```
695
+ *
696
+ * @param {SortBy[]} sortBy Array of constraints to sort the content by.
697
+ * @return {CollectionBuilder} A CollectionBuilder instance.
698
+ * @memberof CollectionBuilder
699
+ */
700
+ sortBy(sortBy) {
701
+ __classPrivateFieldSet(this, _CollectionBuilder_sortBy, sortBy, "f");
702
+ return this;
703
+ }
704
+ /**
705
+ * Sets the maximum amount of content to fetch.
706
+ *
707
+ * @param {number} limit The maximum amount of content to fetch.
708
+ * @return {CollectionBuilder} A CollectionBuilder instance.
709
+ * @memberof CollectionBuilder
710
+ */
711
+ limit(limit) {
712
+ __classPrivateFieldSet(this, _CollectionBuilder_limit, limit, "f");
713
+ return this;
714
+ }
715
+ /**
716
+ * Sets the page number to fetch.
717
+ *
718
+ * @param {number} page The page number to fetch.
719
+ * @return {CollectionBuilder} A CollectionBuilder instance.
720
+ * @memberof CollectionBuilder
721
+ */
722
+ page(page) {
723
+ __classPrivateFieldSet(this, _CollectionBuilder_page, page, "f");
724
+ return this;
725
+ }
726
+ query(arg) {
727
+ if (typeof arg === 'string') {
728
+ __classPrivateFieldSet(this, _CollectionBuilder_rawQuery, arg, "f");
729
+ return this;
730
+ }
731
+ if (typeof arg !== 'function') {
732
+ throw new Error(`Parameter for query method should be a buildQuery function or a string.\nExample:\nclient.content.getCollection('Activity').query((queryBuilder) => queryBuilder.field('title').equals('Hello World'))\nor\nclient.content.getCollection('Activity').query('+Activity.title:"Hello World"') \nSee documentation for more information.`);
733
+ }
734
+ const builtQuery = arg(new QueryBuilder());
735
+ // This can be use in Javascript so we cannot rely on the type checking
736
+ if (builtQuery instanceof Equals) {
737
+ __classPrivateFieldSet(this, _CollectionBuilder_query, builtQuery.raw(this.currentQuery.build()), "f");
738
+ }
739
+ else {
740
+ throw new Error('Provided query is not valid. A query should end in an equals method call.\nExample:\n(queryBuilder) => queryBuilder.field("title").equals("Hello World")\nSee documentation for more information.');
741
+ }
742
+ return this;
743
+ }
744
+ /**
745
+ * Retrieves draft content.
746
+ * @example
747
+ * ```ts
748
+ * const client = new DotCMSClient(config);
749
+ * const collectionBuilder = client.content.getCollection("Blog");
750
+ * collectionBuilder
751
+ * .draft() // This will retrieve draft/working content
752
+ * .then((response) => // Your code here })
753
+ * .catch((error) => // Your code here })
754
+ * ```
755
+ *
756
+ * @return {CollectionBuilder} A CollectionBuilder instance.
757
+ * @memberof CollectionBuilder
758
+ */
759
+ draft() {
760
+ __classPrivateFieldSet(this, _CollectionBuilder_draft, true, "f");
761
+ return this;
762
+ }
763
+ /**
764
+ * Filters the content by a variant ID for [Experiments](https://www.dotcms.com/docs/latest/experiments-and-a-b-testing)
765
+ *
766
+ * More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
767
+ *
768
+ * @example
769
+ * ```ts
770
+ * const client = new DotCMSClient(config);
771
+ * const collectionBuilder = client.content.getCollection("Blog");
772
+ * collectionBuilder
773
+ * .variant("YOUR_VARIANT_ID")
774
+ * .then((response) => // Your code here })
775
+ * .catch((error) => // Your code here })
776
+ * ```
777
+ *
778
+ * @param {string} variantId A string that represents a variant ID.
779
+ * @return {CollectionBuilder} A CollectionBuilder instance.
780
+ * @memberof CollectionBuilder
781
+ */
782
+ variant(variantId) {
783
+ __classPrivateFieldSet(this, _CollectionBuilder_query, this.currentQuery.field('variant').equals(variantId), "f");
784
+ return this;
785
+ }
786
+ /**
787
+ * Sets the depth of the relationships of the content.
788
+ * Specifies the depth of related content to return in the results.
789
+ *
790
+ * More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
791
+ *
792
+ * @example
793
+ * ```ts
794
+ * const client = new DotCMSClient(config);
795
+ * const collectionBuilder = client.content.getCollection("Blog");
796
+ * collectionBuilder
797
+ * .depth(1)
798
+ * .then((response) => // Your code here })
799
+ * .catch((error) => // Your code here })
800
+ * ```
801
+ *
802
+ * @param {number} depth The depth of the relationships of the content.
803
+ * @return {CollectionBuilder} A CollectionBuilder instance.
804
+ * @memberof CollectionBuilder
805
+ */
806
+ depth(depth) {
807
+ if (depth < 0 || depth > 3) {
808
+ throw new Error('Depth value must be between 0 and 3');
809
+ }
810
+ __classPrivateFieldSet(this, _CollectionBuilder_depth, depth, "f");
811
+ return this;
812
+ }
813
+ /**
814
+ * Executes the fetch and returns a promise that resolves to the content or rejects with an error.
815
+ *
816
+ * @example
817
+ * ```ts
818
+ * const client = new DotCMSClient(config);
819
+ * const collectionBuilder = client.content.getCollection("Blog");
820
+ * collectionBuilder
821
+ * .limit(10)
822
+ * .then((response) => // Your code here })
823
+ * .catch((error) => // Your code here })
824
+ * ```
825
+ *
826
+ * @param {OnFullfilled} [onfulfilled] A callback that is called when the fetch is successful.
827
+ * @param {OnRejected} [onrejected] A callback that is called when the fetch fails.
828
+ * @return {Promise<GetCollectionResponse<T> | GetCollectionError>} A promise that resolves to the content or rejects with an error.
829
+ * @memberof CollectionBuilder
830
+ */
831
+ then(onfulfilled, onrejected) {
832
+ return this.fetch().then(async (response) => {
833
+ const data = await response.json();
834
+ if (response.ok) {
835
+ const formattedResponse = this.formatResponse(data);
836
+ const finalResponse = typeof onfulfilled === 'function'
837
+ ? onfulfilled(formattedResponse)
838
+ : formattedResponse;
839
+ return finalResponse;
840
+ }
841
+ else {
842
+ return {
843
+ status: response.status,
844
+ ...data
845
+ };
846
+ }
847
+ }, onrejected);
848
+ }
849
+ /**
850
+ * Formats the response to the desired format.
851
+ *
852
+ * @private
853
+ * @param {GetCollectionRawResponse<T>} data The raw response data.
854
+ * @return {GetCollectionResponse<T>} The formatted response.
855
+ * @memberof CollectionBuilder
856
+ */
857
+ formatResponse(data) {
858
+ const contentlets = data.entity.jsonObjectView.contentlets;
859
+ const total = data.entity.resultsSize;
860
+ const mappedResponse = {
861
+ contentlets,
862
+ total,
863
+ page: __classPrivateFieldGet(this, _CollectionBuilder_page, "f"),
864
+ size: contentlets.length
865
+ };
866
+ return __classPrivateFieldGet(this, _CollectionBuilder_sortBy, "f")
867
+ ? {
868
+ ...mappedResponse,
869
+ sortedBy: __classPrivateFieldGet(this, _CollectionBuilder_sortBy, "f")
870
+ }
871
+ : mappedResponse;
872
+ }
873
+ /**
874
+ * Calls the content API to fetch the content.
875
+ *
876
+ * @private
877
+ * @return {Promise<Response>} The fetch response.
878
+ * @memberof CollectionBuilder
879
+ */
880
+ fetch() {
881
+ const finalQuery = this.currentQuery
882
+ .field('languageId')
883
+ .equals(__classPrivateFieldGet(this, _CollectionBuilder_languageId, "f").toString())
884
+ .field('live')
885
+ .equals((!__classPrivateFieldGet(this, _CollectionBuilder_draft, "f")).toString())
886
+ .build();
887
+ const sanitizedQuery = sanitizeQueryForContentType(finalQuery, __classPrivateFieldGet(this, _CollectionBuilder_contentType, "f"));
888
+ const query = __classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f") ? `${sanitizedQuery} ${__classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f")}` : sanitizedQuery;
889
+ return fetch(this.url, {
890
+ ...__classPrivateFieldGet(this, _CollectionBuilder_requestOptions, "f"),
891
+ method: 'POST',
892
+ headers: {
893
+ ...__classPrivateFieldGet(this, _CollectionBuilder_requestOptions, "f").headers,
894
+ 'Content-Type': 'application/json'
895
+ },
896
+ body: JSON.stringify({
897
+ query,
898
+ render: __classPrivateFieldGet(this, _CollectionBuilder_render, "f"),
899
+ sort: this.sort,
900
+ limit: __classPrivateFieldGet(this, _CollectionBuilder_limit, "f"),
901
+ offset: this.offset,
902
+ depth: __classPrivateFieldGet(this, _CollectionBuilder_depth, "f")
903
+ //userId: This exist but we currently don't use it
904
+ //allCategoriesInfo: This exist but we currently don't use it
905
+ })
906
+ });
907
+ }
908
+ }
909
+ _CollectionBuilder_page = new WeakMap(), _CollectionBuilder_limit = new WeakMap(), _CollectionBuilder_depth = new WeakMap(), _CollectionBuilder_render = new WeakMap(), _CollectionBuilder_sortBy = new WeakMap(), _CollectionBuilder_contentType = new WeakMap(), _CollectionBuilder_defaultQuery = new WeakMap(), _CollectionBuilder_query = new WeakMap(), _CollectionBuilder_rawQuery = new WeakMap(), _CollectionBuilder_languageId = new WeakMap(), _CollectionBuilder_draft = new WeakMap(), _CollectionBuilder_serverUrl = new WeakMap(), _CollectionBuilder_requestOptions = new WeakMap();
910
+
911
+ var _Content_requestOptions, _Content_serverUrl;
912
+ /**
913
+ * Creates a builder to filter and fetch a collection of content items.
914
+ * @param contentType - The content type to retrieve.
915
+ * @returns A CollectionBuilder instance for chaining filters and executing the query.
916
+ * @template T - The type of the content items (defaults to unknown).
917
+ *
918
+ * @example Fetch blog posts with async/await
919
+ * ```typescript
920
+ * const response = await client.content
921
+ * .getCollection<BlogPost>('Blog')
922
+ * .limit(10)
923
+ * .page(2)
924
+ * .sortBy([{ field: 'title', order: 'asc' }])
925
+ * .query(q => q.field('author').equals('John Doe'))
926
+ * .depth(1)
927
+ * .fetch();
928
+ *
929
+ * console.log(response.contentlets);
930
+ * ```
931
+ *
932
+ * @example Fetch blog posts with Promise chain
933
+ * ```typescript
934
+ * client.content
935
+ * .getCollection<BlogPost>('Blog')
936
+ * .limit(10)
937
+ * .page(2)
938
+ * .sortBy([{ field: 'title', order: 'asc' }])
939
+ * .query(q => q.field('author').equals('John Doe'))
940
+ * .depth(1)
941
+ * .fetch()
942
+ * .then(response => console.log(response.contentlets))
943
+ * .catch(error => console.error(error));
944
+ * ```
945
+ *
946
+ * @example Using a custom type
947
+ * ```typescript
948
+ * interface BlogPost {
949
+ * summary: string;
950
+ * author: string;
951
+ * title: string;
952
+ * }
953
+ *
954
+ * const posts = await client.content
955
+ * .getCollection<BlogPost>('Blog')
956
+ * .limit(10)
957
+ * .fetch();
958
+ *
959
+ * posts.contentlets.forEach(post => {
960
+ * console.log(post.title, post.author, post.summary);
961
+ * });
962
+ * ```
963
+ */
964
+ class Content {
965
+ /**
966
+ * Creates an instance of Content.
967
+ * @param {ClientOptions} requestOptions - The options for the client request.
968
+ * @param {string} serverUrl - The server URL.
969
+ */
970
+ constructor(requestOptions, serverUrl) {
971
+ _Content_requestOptions.set(this, void 0);
972
+ _Content_serverUrl.set(this, void 0);
973
+ __classPrivateFieldSet(this, _Content_requestOptions, requestOptions, "f");
974
+ __classPrivateFieldSet(this, _Content_serverUrl, serverUrl, "f");
975
+ }
976
+ /**
977
+ * Takes a content type and returns a builder to filter and fetch the collection.
978
+ * @param {string} contentType - The content type to get the collection.
979
+ * @return {CollectionBuilder<T>} CollectionBuilder to filter and fetch the collection.
980
+ * @template T - Represents the type of the content type to fetch. Defaults to unknown.
981
+ * @memberof Content
982
+ *
983
+ * @example
984
+ * ```javascript
985
+ * // Using await and async
986
+ * const collectionResponse = await client.content
987
+ * .getCollection('Blog')
988
+ * .limit(10)
989
+ * .page(2)
990
+ * .sortBy([{ field: 'title', order: 'asc' }])
991
+ * .query((queryBuilder) => queryBuilder.field('author').equals('John Doe'))
992
+ * .depth(1);
993
+ * ```
994
+ * @example
995
+ * ```javascript
996
+ * // Using then and catch
997
+ * client.content
998
+ * .getCollection('Blog')
999
+ * .limit(10)
1000
+ * .page(2)
1001
+ * .sortBy([{ field: 'title', order: 'asc' }])
1002
+ * .query((queryBuilder) => queryBuilder.field('author').equals('John Doe'))
1003
+ * .depth(1)
1004
+ * .then((response) => {
1005
+ * console.log(response.contentlets);
1006
+ * })
1007
+ * .catch((error) => {
1008
+ * console.error(error);
1009
+ * });
1010
+ * ```
1011
+ * @example
1012
+ * ```typescript
1013
+ * // Using a specific type for your content
1014
+ *
1015
+ * type Blog = {
1016
+ * summary: string;
1017
+ * author: string;
1018
+ * title: string;
1019
+ * };
1020
+ *
1021
+ * client.content
1022
+ * .getCollection<Blog>('Blog')
1023
+ * .limit(10)
1024
+ * .page(2)
1025
+ * .sortBy([{ field: 'title', order: 'asc' }])
1026
+ * .query((queryBuilder) => queryBuilder.field('author').equals('John Doe'))
1027
+ * .depth(1)
1028
+ * .then((response) => {
1029
+ * response.contentlets.forEach((blog) => {
1030
+ * console.log(blog.title);
1031
+ * console.log(blog.author);
1032
+ * console.log(blog.summary);
1033
+ * });
1034
+ * })
1035
+ * .catch((error) => {
1036
+ * console.error(error);
1037
+ * });
1038
+ * ```
1039
+ *
1040
+ */
1041
+ getCollection(contentType) {
1042
+ return new CollectionBuilder(__classPrivateFieldGet(this, _Content_requestOptions, "f"), __classPrivateFieldGet(this, _Content_serverUrl, "f"), contentType);
1043
+ }
1044
+ }
1045
+ _Content_requestOptions = new WeakMap(), _Content_serverUrl = new WeakMap();
1046
+
1047
+ /**
1048
+ * A record of HTTP status codes and their corresponding error messages.
1049
+ *
1050
+ * @type {Record<number, string>}
1051
+ * @property {string} 401 - Unauthorized. Check the token and try again.
1052
+ * @property {string} 403 - Forbidden. Check the permissions and try again.
1053
+ * @property {string} 404 - Not Found. Check the URL and try again.
1054
+ * @property {string} 500 - Internal Server Error. Try again later.
1055
+ * @property {string} 502 - Bad Gateway. Try again later.
1056
+ * @property {string} 503 - Service Unavailable. Try again later.
1057
+ */
1058
+ const ErrorMessages = {
1059
+ 401: 'Unauthorized. Check the token and try again.',
1060
+ 403: 'Forbidden. Check the permissions and try again.',
1061
+ 404: 'Not Found. Check the URL and try again.',
1062
+ 500: 'Internal Server Error. Try again later.',
1063
+ 502: 'Bad Gateway. Try again later.',
1064
+ 503: 'Service Unavailable. Try again later.'
1065
+ };
1066
+
1067
+ /**
1068
+ * Transforms a GraphQL Page response to a Page Entity.
1069
+ *
1070
+ * @param {GraphQLPageResponse} graphQLPageResponse - The GraphQL Page response object.
1071
+ * @returns {object|null} The transformed Page Entity or null if the page is not present.
1072
+ *
1073
+ * @example
1074
+ * ```ts
1075
+ * const pageEntity = graphqlToPageEntity(graphQLPageResponse);
1076
+ * ```
1077
+ */
1078
+ const graphqlToPageEntity = (graphQLPageResponse) => {
1079
+ const { page } = graphQLPageResponse;
1080
+ // If there is no page, return null
1081
+ if (!page) {
1082
+ return null;
1083
+ }
1084
+ const { layout, template, containers, urlContentMap, viewAs, site, _map, ...pageAsset } = page;
1085
+ const data = (_map || {});
1086
+ // To prevent type errors, we cast the urlContentMap to an object
1087
+ const urlContentMapObject = urlContentMap;
1088
+ // Extract the _map data from the urlContentMap object
1089
+ const urlContentMapData = urlContentMapObject?.['_map'];
1090
+ return {
1091
+ layout,
1092
+ template,
1093
+ viewAs,
1094
+ site,
1095
+ page: {
1096
+ ...data,
1097
+ ...pageAsset
1098
+ },
1099
+ containers: parseContainers(containers),
1100
+ urlContentMap: urlContentMapData
1101
+ };
1102
+ };
1103
+ /**
1104
+ * Parses the containers from the GraphQL response.
1105
+ *
1106
+ * @param {Array<Record<string, unknown>>} [containers=[]] - The containers array from the GraphQL response.
1107
+ * @returns {Record<string, unknown>} The parsed containers.
1108
+ */
1109
+ const parseContainers = (containers = []) => {
1110
+ return containers.reduce((acc, container) => {
1111
+ const { path, identifier, containerStructures, containerContentlets, ...rest } = container;
1112
+ const key = (path || identifier);
1113
+ acc[key] = {
1114
+ containerStructures,
1115
+ container: {
1116
+ path,
1117
+ identifier,
1118
+ ...rest
1119
+ },
1120
+ contentlets: parseContentletsToUuidMap(containerContentlets)
1121
+ };
1122
+ return acc;
1123
+ }, {});
1124
+ };
1125
+ /**
1126
+ * Parses the contentlets from the GraphQL response.
1127
+ *
1128
+ * @param {Array<Record<string, unknown>>} containerContentlets - The contentlets array from the GraphQL response.
1129
+ * @returns {Record<string, Array<Record<string, unknown>>>} The parsed contentlets mapped by UUID.
1130
+ */
1131
+ const parseContentletsToUuidMap = (containerContentlets = []) => {
1132
+ return containerContentlets.reduce((acc, containerContentlet) => {
1133
+ const { uuid, contentlets } = containerContentlet;
1134
+ // TODO: This is a temporary solution, we need to find a better way to handle this.
1135
+ acc[uuid] = contentlets.map(({ _map = {}, ...rest }) => {
1136
+ return {
1137
+ ..._map,
1138
+ ...rest
1139
+ };
1140
+ });
1141
+ return acc;
1142
+ }, {});
1143
+ };
1144
+
1145
+ exports.Content = Content;
1146
+ exports.ErrorMessages = ErrorMessages;
1147
+ exports.__classPrivateFieldGet = __classPrivateFieldGet;
1148
+ exports.__classPrivateFieldSet = __classPrivateFieldSet;
1149
+ exports.graphqlToPageEntity = graphqlToPageEntity;