@dotcms/client 1.0.0 → 1.0.1

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 (54) hide show
  1. package/.eslintrc.json +18 -0
  2. package/CLAUDE.md +253 -0
  3. package/MIGRATION.md +329 -0
  4. package/README.md +250 -434
  5. package/jest.config.ts +15 -0
  6. package/package.json +8 -25
  7. package/project.json +73 -0
  8. package/src/lib/client/client.spec.ts +147 -0
  9. package/src/lib/client/client.ts +125 -0
  10. package/src/lib/client/content/builders/collection/collection.spec.ts +514 -0
  11. package/src/lib/client/content/builders/collection/{collection.d.ts → collection.ts} +210 -19
  12. package/src/lib/client/content/builders/query/lucene-syntax/{Equals.d.ts → Equals.ts} +45 -11
  13. package/src/lib/client/content/builders/query/lucene-syntax/{Field.d.ts → Field.ts} +13 -5
  14. package/src/lib/client/content/builders/query/lucene-syntax/{NotOperand.d.ts → NotOperand.ts} +13 -5
  15. package/src/lib/client/content/builders/query/lucene-syntax/{Operand.d.ts → Operand.ts} +21 -7
  16. package/src/lib/client/content/builders/query/query.spec.ts +159 -0
  17. package/src/lib/client/content/builders/query/{query.d.ts → query.ts} +16 -5
  18. package/src/lib/client/content/builders/query/utils/{index.d.ts → index.ts} +49 -12
  19. package/src/lib/client/content/{content-api.d.ts → content-api.ts} +14 -4
  20. package/src/lib/client/content/shared/{const.d.ts → const.ts} +5 -3
  21. package/src/lib/client/content/shared/{types.d.ts → types.ts} +18 -2
  22. package/src/lib/client/content/shared/{utils.d.ts → utils.ts} +9 -1
  23. package/src/lib/client/models/{index.d.ts → index.ts} +8 -1
  24. package/src/lib/client/navigation/navigation-api.spec.ts +167 -0
  25. package/src/lib/client/navigation/navigation-api.ts +62 -0
  26. package/src/lib/client/page/page-api.spec.ts +359 -0
  27. package/src/lib/client/page/page-api.ts +197 -0
  28. package/src/lib/client/page/utils.ts +291 -0
  29. package/src/lib/utils/graphql/transforms.spec.ts +250 -0
  30. package/src/lib/utils/graphql/transforms.ts +128 -0
  31. package/tsconfig.json +22 -0
  32. package/tsconfig.lib.json +13 -0
  33. package/tsconfig.spec.json +9 -0
  34. package/index.cjs.d.ts +0 -1
  35. package/index.cjs.default.js +0 -1
  36. package/index.cjs.js +0 -1591
  37. package/index.cjs.mjs +0 -2
  38. package/index.esm.d.ts +0 -1
  39. package/index.esm.js +0 -1589
  40. package/internal.cjs.d.ts +0 -1
  41. package/internal.cjs.default.js +0 -1
  42. package/internal.cjs.js +0 -85
  43. package/internal.cjs.mjs +0 -2
  44. package/internal.esm.d.ts +0 -1
  45. package/internal.esm.js +0 -83
  46. package/src/lib/client/client.d.ts +0 -56
  47. package/src/lib/client/navigation/navigation-api.d.ts +0 -14
  48. package/src/lib/client/page/page-api.d.ts +0 -95
  49. package/src/lib/client/page/utils.d.ts +0 -41
  50. package/src/lib/utils/graphql/transforms.d.ts +0 -13
  51. /package/src/{index.d.ts → index.ts} +0 -0
  52. /package/src/{internal.d.ts → internal.ts} +0 -0
  53. /package/src/lib/client/content/builders/query/lucene-syntax/{index.d.ts → index.ts} +0 -0
  54. /package/src/lib/utils/{index.d.ts → index.ts} +0 -0
package/index.esm.js DELETED
@@ -1,1589 +0,0 @@
1
- import consola$1, { consola } from 'consola';
2
- import { graphqlToPageEntity } from './internal.esm.js';
3
-
4
- /******************************************************************************
5
- Copyright (c) Microsoft Corporation.
6
-
7
- Permission to use, copy, modify, and/or distribute this software for any
8
- purpose with or without fee is hereby granted.
9
-
10
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
- PERFORMANCE OF THIS SOFTWARE.
17
- ***************************************************************************** */
18
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
19
-
20
-
21
- function __classPrivateFieldGet(receiver, state, kind, f) {
22
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
23
- 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");
24
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
25
- }
26
-
27
- function __classPrivateFieldSet(receiver, state, value, kind, f) {
28
- if (kind === "m") throw new TypeError("Private method is not writable");
29
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
30
- 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");
31
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
32
- }
33
-
34
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
35
- var e = new Error(message);
36
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
37
- };
38
-
39
- /**
40
- * Default variant identifier used in the application.
41
- */
42
- /**
43
- * Fields that should not be formatted when sanitizing the query.
44
- * These fields are essential for maintaining the integrity of the content type.
45
- */
46
- const CONTENT_TYPE_MAIN_FIELDS = ['live', 'variant', 'contentType', 'languageId'];
47
- /**
48
- * URL endpoint for the content API search functionality.
49
- */
50
- const CONTENT_API_URL = '/api/content/_search';
51
-
52
- /**
53
- * @description
54
- * Sanitizes the query for the given content type.
55
- * It replaces the fields that are not content type fields with the correct format.
56
- * Example: +field: -> +contentTypeVar.field:
57
- *
58
- * @example
59
- *
60
- * ```ts
61
- * const query = '+field: value';
62
- * const contentType = 'contentTypeVar';
63
- * const sanitizedQuery = sanitizeQueryForContentType(query, contentType); // Output: '+contentTypeVar.field: value'
64
- * ```
65
- *
66
- * @export
67
- * @param {string} query - The query string to be sanitized.
68
- * @param {string} contentType - The content type to be used for formatting the fields.
69
- * @returns {string} The sanitized query string.
70
- */
71
- function sanitizeQueryForContentType(query, contentType) {
72
- return query.replace(/\+([^+:]*?):/g, (original, field) => {
73
- return !CONTENT_TYPE_MAIN_FIELDS.includes(field) // Fields that are not content type fields
74
- ? `+${contentType}.${field}:` // Should have this format: +contentTypeVar.field:
75
- : original; // Return the field if it is a content type field
76
- });
77
- }
78
-
79
- var _Field_query;
80
- /**
81
- * The `Field` class is used to build a query with a specific field.
82
- * A Lucene Field is a key used to search for a specific value in a document.
83
- *
84
- * @export
85
- * @class Field
86
- */
87
- class Field {
88
- /**
89
- * Creates an instance of the `Field` class.
90
- *
91
- * @param {string} query - The initial query string.
92
- */
93
- constructor(query) {
94
- this.query = query;
95
- _Field_query.set(this, '');
96
- __classPrivateFieldSet(this, _Field_query, this.query, "f");
97
- }
98
- /**
99
- * Appends a term to the query that should be included in the search.
100
- *
101
- * @example
102
- * ```typescript
103
- * const field = new Field("+myField");
104
- * field.equals("myValue");
105
- * ```
106
- *
107
- * @param {string} term - The term that should be included in the search.
108
- * @return {Equals} - An instance of `Equals`.
109
- * @memberof Field
110
- */
111
- equals(term) {
112
- return buildEquals(__classPrivateFieldGet(this, _Field_query, "f"), term);
113
- }
114
- }
115
- _Field_query = new WeakMap();
116
-
117
- var _NotOperand_query;
118
- /**
119
- * 'NotOperand' Is a Typescript class that provides the ability to use the NOT operand in the lucene query string.
120
- *
121
- * @export
122
- * @class NotOperand
123
- */
124
- class NotOperand {
125
- constructor(query) {
126
- this.query = query;
127
- _NotOperand_query.set(this, '');
128
- __classPrivateFieldSet(this, _NotOperand_query, this.query, "f");
129
- }
130
- /**
131
- * This method appends to the query a term that should be included in the search.
132
- *
133
- * @example
134
- * ```typescript
135
- * const notOperand = new NotOperand("+myField");
136
- * notOperand.equals("myValue");
137
- * ```
138
- *
139
- * @param {string} term - The term that should be included in the search.
140
- * @return {*} {Equals} - An instance of Equals.
141
- * @memberof NotOperand
142
- */
143
- equals(term) {
144
- return buildEquals(__classPrivateFieldGet(this, _NotOperand_query, "f"), term);
145
- }
146
- }
147
- _NotOperand_query = new WeakMap();
148
-
149
- var _Operand_query;
150
- /**
151
- * 'Operand' Is a Typescript class that provides the ability to use operands in the lucene query string.}
152
- * An operand is a logical operator used to join two or more conditions in a query.
153
- *
154
- * @export
155
- * @class Operand
156
- */
157
- class Operand {
158
- constructor(query) {
159
- this.query = query;
160
- _Operand_query.set(this, '');
161
- __classPrivateFieldSet(this, _Operand_query, this.query, "f");
162
- }
163
- /**
164
- * This method appends to the query a term that should be excluded in the search.
165
- *
166
- * Ex: "-myValue"
167
- *
168
- * @param {string} field - The field that should be excluded in the search.
169
- * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
170
- * @memberof Operand
171
- */
172
- excludeField(field) {
173
- return buildExcludeField(__classPrivateFieldGet(this, _Operand_query, "f"), field);
174
- }
175
- /**
176
- * This method appends to the query a field that should be included in the search.
177
- *
178
- * Ex: "+myField:"
179
- *
180
- * @param {string} field - The field that should be included in the search.
181
- * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
182
- * @memberof Operand
183
- */
184
- field(field) {
185
- return buildField(__classPrivateFieldGet(this, _Operand_query, "f"), field);
186
- }
187
- /**
188
- * This method appends to the query a term that should be included in the search.
189
- *
190
- * Ex: myValue or "My value"
191
- *
192
- * @param {string} term - The term that should be included in the search.
193
- * @return {*} {Equals} - An instance of Equals.
194
- * @memberof Operand
195
- */
196
- equals(term) {
197
- return buildEquals(__classPrivateFieldGet(this, _Operand_query, "f"), term);
198
- }
199
- }
200
- _Operand_query = new WeakMap();
201
-
202
- /**
203
- * Enum for common Operands
204
- *
205
- * @export
206
- * @enum {number}
207
- */
208
- var OPERAND;
209
- (function (OPERAND) {
210
- OPERAND["OR"] = "OR";
211
- OPERAND["AND"] = "AND";
212
- OPERAND["NOT"] = "NOT";
213
- })(OPERAND || (OPERAND = {}));
214
- /**
215
- * This function removes extra spaces from a string.
216
- *
217
- * @example
218
- * ```ts
219
- * sanitizeQuery(" my query "); // Output: "my query"
220
- * ```
221
- *
222
- * @export
223
- * @param {string} str
224
- * @return {*} {string}
225
- */
226
- function sanitizeQuery(str) {
227
- return str.replace(/\s{2,}/g, ' ').trim();
228
- }
229
- /**
230
- * This function sanitizes a term by adding quotes if it contains spaces.
231
- * In lucene, a term with spaces should be enclosed in quotes.
232
- *
233
- * @example
234
- * ```ts
235
- * sanitizePhrases(`my term`); // Output: `"my term"`
236
- * sanitizePhrases(`myterm`); // Output: `myterm`
237
- * ```
238
- *
239
- * @export
240
- * @param {string} term
241
- * @return {*} {string}
242
- */
243
- function sanitizePhrases(term) {
244
- return term.includes(' ') ? `'${term}'` : term;
245
- }
246
- /**
247
- * This function builds a term to be used in a lucene query.
248
- * We need to sanitize the term before adding it to the query.
249
- *
250
- * @example
251
- * ```ts
252
- * const equals = buildEquals("+myField: ", "myValue"); // Current query: "+myField: myValue"
253
- * ```
254
- *
255
- * @export
256
- * @param {string} query
257
- * @param {string} term
258
- * @return {*} {Equals}
259
- */
260
- function buildEquals(query, term) {
261
- const newQuery = query + sanitizePhrases(term);
262
- return new Equals(newQuery);
263
- }
264
- /**
265
- * This function builds a term to be used in a lucene query.
266
- * We need to sanitize the raw query before adding it to the query.
267
- *
268
- * @example
269
- * ```ts
270
- * const query = "+myField: myValue";
271
- * const field = buildRawEquals(query, "-myField2: myValue2"); // Current query: "+myField: myValue -myField2: myValue"
272
- * ```
273
- *
274
- * @export
275
- * @param {string} query
276
- * @param {string} raw
277
- * @return {*} {Equals}
278
- */
279
- function buildRawEquals(query, raw) {
280
- const newQuery = query + ` ${raw}`;
281
- return new Equals(sanitizeQuery(newQuery));
282
- }
283
- /**
284
- * This function builds a field to be used in a lucene query.
285
- * We need to format the field before adding it to the query.
286
- *
287
- * @example
288
- * ```ts
289
- * const field = buildField("+myField: ", "myValue"); // Current query: "+myField: myValue"
290
- * ```
291
- *
292
- * @export
293
- * @param {string} query
294
- * @param {string} field
295
- * @return {*} {Field}
296
- */
297
- function buildField(query, field) {
298
- const newQuery = query + ` +${field}:`;
299
- return new Field(newQuery);
300
- }
301
- /**
302
- * This function builds an exclude field to be used in a lucene query.
303
- * We need to format the field before adding it to the query.
304
- *
305
- * @example
306
- * ```ts
307
- * const query = "+myField: myValue";
308
- * const field = buildExcludeField(query, "myField2"); // Current query: "+myField: myValue -myField2:"
309
- * ```
310
- *
311
- * @export
312
- * @param {string} query
313
- * @param {string} field
314
- * @return {*} {Field}
315
- */
316
- function buildExcludeField(query, field) {
317
- const newQuery = query + ` -${field}:`;
318
- return new Field(newQuery);
319
- }
320
- /**
321
- * This function builds an operand to be used in a lucene query.
322
- * We need to format the operand before adding it to the query.
323
- *
324
- * @example
325
- * <caption>E.g. Using the AND operand</caption>
326
- * ```ts
327
- * const query = "+myField: myValue";
328
- * const field = buildOperand(query, OPERAND.AND); // Current query: "+myField: myValue AND"
329
- * ```
330
- * @example
331
- * <caption>E.g. Using the OR operand</caption>
332
- * ```ts
333
- * const query = "+myField: myValue";
334
- * const field = buildOperand(query, OPERAND.OR); // Current query: "+myField: myValue OR"
335
- * ```
336
- * @export
337
- * @param {string} query
338
- * @param {OPERAND} operand
339
- * @return {*} {Operand}
340
- */
341
- function buildOperand(query, operand) {
342
- const newQuery = query + ` ${operand} `;
343
- return new Operand(newQuery);
344
- }
345
- /**
346
- * This function builds a NOT operand to be used in a lucene query.
347
- * We need to format the operand before adding it to the query.
348
- *
349
- * @example
350
- * ```ts
351
- * const query = "+myField: myValue";
352
- * const field = buildNotOperand(query); // Current query: "+myField: myValue NOT"
353
- * ```
354
- *
355
- * @export
356
- * @param {string} query
357
- * @return {*} {NotOperand}
358
- */
359
- function buildNotOperand(query) {
360
- const newQuery = query + ` ${OPERAND.NOT} `;
361
- return new NotOperand(newQuery);
362
- }
363
-
364
- var _Equals_query;
365
- /**
366
- * 'Equal' Is a Typescript class that provides the ability to use terms in the lucene query string.
367
- * A term is a value used to search for a specific value in a document. It can be a word or a phrase.
368
- *
369
- * Ex: myValue or "My Value"
370
- *
371
- * @export
372
- * @class Equal
373
- */
374
- class Equals {
375
- constructor(query) {
376
- this.query = query;
377
- _Equals_query.set(this, '');
378
- __classPrivateFieldSet(this, _Equals_query, this.query, "f");
379
- }
380
- /**
381
- * This method appends to the query a term that should be excluded in the search.
382
- *
383
- * @example
384
- * ```ts
385
- * const equals = new Equals("+myField: myValue");
386
- * equals.excludeField("myField2").equals("myValue2"); // Current query: "+myField: myValue -myField2: myValue2"
387
- * ```
388
- *
389
- * @param {string} field - The field that should be excluded in the search.
390
- * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
391
- * @memberof Equal
392
- */
393
- excludeField(field) {
394
- return buildExcludeField(__classPrivateFieldGet(this, _Equals_query, "f"), field);
395
- }
396
- /**
397
- * This method appends to the query a field that should be included in the search.
398
- *
399
- * @example
400
- * ```ts
401
- * const equals = new Equals("+myField: myValue");
402
- * equals.field("myField2").equals("myValue2"); // Current query: "+myField: myValue +myField2: myValue2"
403
- *```
404
- * @param {string} field - The field that should be included in the search.
405
- * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
406
- * @memberof Equal
407
- */
408
- field(field) {
409
- return buildField(__classPrivateFieldGet(this, _Equals_query, "f"), field);
410
- }
411
- /**
412
- * This method appends to the query an operand to use logic operators in the query.
413
- *
414
- * @example
415
- * @example
416
- * ```ts
417
- * const equals = new Equals("+myField: myValue");
418
- * equals.or().field("myField2").equals("myValue2"); // Current query: "+myField: myValue OR +myField2: myValue2"
419
- * ```
420
- *
421
- * @return {*} {Operand} - An instance of a Lucene Operand. An operand is a logical operator used to combine terms or phrases in a query.
422
- * @memberof Equal
423
- */
424
- or() {
425
- return buildOperand(__classPrivateFieldGet(this, _Equals_query, "f"), OPERAND.OR);
426
- }
427
- /**
428
- * This method appends to the query an operand to use logic operators in the query.
429
- *
430
- * @example
431
- * ```ts
432
- * const equals = new Equals("+myField: myValue");
433
- * equals.and().field("myField2").equals("myValue2"); // Current query: "+myField: myValue AND +myField2: myValue2"
434
- * ```
435
- *
436
- * @return {*} {Operand} - An instance of a Lucene Operand. An operand is a logical operator used to combine terms or phrases in a query.
437
- * @memberof Equal
438
- */
439
- and() {
440
- return buildOperand(__classPrivateFieldGet(this, _Equals_query, "f"), OPERAND.AND);
441
- }
442
- /**
443
- * This method appends to the query an operand to use logic operators in the query.
444
- *
445
- * @example
446
- * ```ts
447
- * const equals = new Equals("+myField: myValue");
448
- * equals.not().field("myField").equals("myValue2"); // Current query: "+myField: myValue NOT +myField: myValue2"
449
- * ```
450
- *
451
- * @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.
452
- * @memberof Equal
453
- */
454
- not() {
455
- return buildNotOperand(__classPrivateFieldGet(this, _Equals_query, "f"));
456
- }
457
- /**
458
- * This method allows to pass a raw query string to the query builder.
459
- * This raw query should end in a Lucene Equal.
460
- * This method is useful when you want to append a complex query or an already written query to the query builder.
461
- *
462
- * @example
463
- * ```ts
464
- * // This builds the follow raw query "+myField: value AND (someOtherValue OR anotherValue)"
465
- * const equals = new Equals("+myField: value");
466
- * equals.raw("+myField2: value2"); // Current query: "+myField: value +myField2: anotherValue"
467
- * ```
468
- *
469
- * @param {string} query - A raw query string.
470
- * @return {*} {Equal} - An instance of a Lucene Equal. A term is a value used to search for a specific value in a document.
471
- * @memberof QueryBuilder
472
- */
473
- raw(query) {
474
- return buildRawEquals(__classPrivateFieldGet(this, _Equals_query, "f"), query);
475
- }
476
- /**
477
- * This method returns the final query string.
478
- *
479
- * @example
480
- * ```ts
481
- * const equals = new Equals("+myField: myValue");
482
- * equals.field("myField2").equals("myValue2").build(); // Returns "+myField: myValue +myField2: myValue2"
483
- * ```
484
- *
485
- * @return {*} {string} - The final query string.
486
- * @memberof Equal
487
- */
488
- build() {
489
- return sanitizeQuery(__classPrivateFieldGet(this, _Equals_query, "f"));
490
- }
491
- }
492
- _Equals_query = new WeakMap();
493
-
494
- var _QueryBuilder_query;
495
- /**
496
- * 'QueryBuilder' Is a Typescript class that provides the ability to build a query string using the Lucene syntax in a more readable way.
497
- * @example
498
- * ```ts
499
- * const qb = new QueryBuilder();
500
- * const query = qb
501
- * .field('contentType')
502
- * .equals('Blog')
503
- * .field('conhost')
504
- * .equals('my-super-cool-site')
505
- * .build(); // Output: `+contentType:Blog +conhost:my-super-cool-site"`
506
- * ```
507
- *
508
- * @example
509
- * ```ts
510
- * const qb = new QueryBuilder();
511
- * const query = qb
512
- * .field('contentType')
513
- * .equals('Blog')
514
- * .field('title')
515
- * .equals('Football')
516
- * .excludeField('summary')
517
- * .equals('Lionel Messi')
518
- * .build(); // Output: `+contentType:Blog +title:Football -summary:"Lionel Messi"`
519
- * ```
520
- * @export
521
- * @class QueryBuilder
522
- */
523
- class QueryBuilder {
524
- constructor() {
525
- _QueryBuilder_query.set(this, '');
526
- }
527
- /**
528
- * This method appends to the query a field that should be included in the search.
529
- *
530
- * @example
531
- * ```ts
532
- * const qb = new QueryBuilder();
533
- * qb.field("+myField: ", "myValue"); // Current query: "+myField: myValue"
534
- * ```
535
- *
536
- * @param {string} field - The field that should be included in the search.
537
- * @return {*} {Field} - An instance of a Lucene Field. A field is a key used to search for a specific value in a document.
538
- * @memberof QueryBuilder
539
- */
540
- field(field) {
541
- return buildField(__classPrivateFieldGet(this, _QueryBuilder_query, "f"), field);
542
- }
543
- /**
544
- * This method appends to the query a field that should be excluded from the search.
545
- *
546
- * @example
547
- * ```ts
548
- * const qb = new QueryBuilder();
549
- * qb.excludeField("myField").equals("myValue"); // Current query: "-myField: myValue"
550
- * ```
551
- *
552
- * @param {string} field - The field that should be excluded from the search.
553
- * @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.
554
- * @memberof QueryBuilder
555
- */
556
- excludeField(field) {
557
- return buildExcludeField(__classPrivateFieldGet(this, _QueryBuilder_query, "f"), field);
558
- }
559
- /**
560
- * This method allows to pass a raw query string to the query builder.
561
- * This raw query should end in Equals.
562
- * This method is useful when you want to append a complex query or an already written query to the query builder.
563
- *
564
- * @example
565
- * ```ts
566
- * const qb = new QueryBuilder();
567
- * qb.raw("+myField: value AND (someOtherValue OR anotherValue)"); // Current query: "+myField: value AND (someOtherValue OR anotherValue)"
568
- * ```
569
- *
570
- * @param {string} query - A raw query string.
571
- * @return {*} {Equals} - An instance of Equals. A term is a value used to search for a specific value in a document.
572
- * @memberof QueryBuilder
573
- */
574
- raw(query) {
575
- return buildRawEquals(__classPrivateFieldGet(this, _QueryBuilder_query, "f"), query);
576
- }
577
- }
578
- _QueryBuilder_query = new WeakMap();
579
-
580
- 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;
581
- /**
582
- * Creates a Builder to filter and fetch content from the content API for a specific content type.
583
- *
584
- * @export
585
- * @class CollectionBuilder
586
- * @template T Represents the type of the content type to fetch. Defaults to unknown.
587
- */
588
- class CollectionBuilder {
589
- /**
590
- * Creates an instance of CollectionBuilder.
591
- * @param {ClientOptions} requestOptions Options for the client request.
592
- * @param {string} serverUrl The server URL.
593
- * @param {string} contentType The content type to fetch.
594
- * @memberof CollectionBuilder
595
- */
596
- constructor(requestOptions, serverUrl, contentType) {
597
- _CollectionBuilder_page.set(this, 1);
598
- _CollectionBuilder_limit.set(this, 10);
599
- _CollectionBuilder_depth.set(this, 0);
600
- _CollectionBuilder_render.set(this, false);
601
- _CollectionBuilder_sortBy.set(this, void 0);
602
- _CollectionBuilder_contentType.set(this, void 0);
603
- _CollectionBuilder_defaultQuery.set(this, void 0);
604
- _CollectionBuilder_query.set(this, void 0);
605
- _CollectionBuilder_rawQuery.set(this, void 0);
606
- _CollectionBuilder_languageId.set(this, 1);
607
- _CollectionBuilder_draft.set(this, false);
608
- _CollectionBuilder_serverUrl.set(this, void 0);
609
- _CollectionBuilder_requestOptions.set(this, void 0);
610
- __classPrivateFieldSet(this, _CollectionBuilder_requestOptions, requestOptions, "f");
611
- __classPrivateFieldSet(this, _CollectionBuilder_serverUrl, serverUrl, "f");
612
- __classPrivateFieldSet(this, _CollectionBuilder_contentType, contentType, "f");
613
- // Build the default query with the contentType field
614
- __classPrivateFieldSet(this, _CollectionBuilder_defaultQuery, new QueryBuilder().field('contentType').equals(__classPrivateFieldGet(this, _CollectionBuilder_contentType, "f")), "f");
615
- }
616
- /**
617
- * Returns the sort query in the format: field order, field order, ...
618
- *
619
- * @readonly
620
- * @private
621
- * @memberof CollectionBuilder
622
- */
623
- get sort() {
624
- return __classPrivateFieldGet(this, _CollectionBuilder_sortBy, "f")?.map((sort) => `${sort.field} ${sort.order}`).join(',');
625
- }
626
- /**
627
- * Returns the offset for pagination.
628
- *
629
- * @readonly
630
- * @private
631
- * @memberof CollectionBuilder
632
- */
633
- get offset() {
634
- return __classPrivateFieldGet(this, _CollectionBuilder_limit, "f") * (__classPrivateFieldGet(this, _CollectionBuilder_page, "f") - 1);
635
- }
636
- /**
637
- * Returns the full URL for the content API.
638
- *
639
- * @readonly
640
- * @private
641
- * @memberof CollectionBuilder
642
- */
643
- get url() {
644
- return `${__classPrivateFieldGet(this, _CollectionBuilder_serverUrl, "f")}${CONTENT_API_URL}`;
645
- }
646
- /**
647
- * Returns the current query built.
648
- *
649
- * @readonly
650
- * @private
651
- * @memberof CollectionBuilder
652
- */
653
- get currentQuery() {
654
- return __classPrivateFieldGet(this, _CollectionBuilder_query, "f") ?? __classPrivateFieldGet(this, _CollectionBuilder_defaultQuery, "f");
655
- }
656
- /**
657
- * Filters the content by the specified language ID.
658
- *
659
- * @example
660
- * ```typescript
661
- * const client = new DotCMSClient(config);
662
- * const collectionBuilder = client.content.getCollection("Blog");
663
- * collectionBuilder.language(1);
664
- * ```
665
- *
666
- * @param {number | string} languageId The language ID to filter the content by.
667
- * @return {CollectionBuilder} A CollectionBuilder instance.
668
- * @memberof CollectionBuilder
669
- */
670
- language(languageId) {
671
- __classPrivateFieldSet(this, _CollectionBuilder_languageId, languageId, "f");
672
- return this;
673
- }
674
- /**
675
- * Setting this to true will server side render (using velocity) any widgets that are returned by the content query.
676
- *
677
- * More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
678
- *
679
- * @return {CollectionBuilder} A CollectionBuilder instance.
680
- * @memberof CollectionBuilder
681
- */
682
- render() {
683
- __classPrivateFieldSet(this, _CollectionBuilder_render, true, "f");
684
- return this;
685
- }
686
- /**
687
- * Sorts the content by the specified fields and orders.
688
- *
689
- * @example
690
- * ```typescript
691
- * const client = new DotCMSClient(config);
692
- * const collectionBuilder = client.content.getCollection("Blog");
693
- * const sortBy = [{ field: 'title', order: 'asc' }, { field: 'modDate', order: 'desc' }];
694
- * collectionBuilder("Blog").sortBy(sortBy);
695
- * ```
696
- *
697
- * @param {SortBy[]} sortBy Array of constraints to sort the content by.
698
- * @return {CollectionBuilder} A CollectionBuilder instance.
699
- * @memberof CollectionBuilder
700
- */
701
- sortBy(sortBy) {
702
- __classPrivateFieldSet(this, _CollectionBuilder_sortBy, sortBy, "f");
703
- return this;
704
- }
705
- /**
706
- * Sets the maximum amount of content to fetch.
707
- *
708
- * @param {number} limit The maximum amount of content to fetch.
709
- * @return {CollectionBuilder} A CollectionBuilder instance.
710
- * @memberof CollectionBuilder
711
- */
712
- limit(limit) {
713
- __classPrivateFieldSet(this, _CollectionBuilder_limit, limit, "f");
714
- return this;
715
- }
716
- /**
717
- * Sets the page number to fetch.
718
- *
719
- * @param {number} page The page number to fetch.
720
- * @return {CollectionBuilder} A CollectionBuilder instance.
721
- * @memberof CollectionBuilder
722
- */
723
- page(page) {
724
- __classPrivateFieldSet(this, _CollectionBuilder_page, page, "f");
725
- return this;
726
- }
727
- query(arg) {
728
- if (typeof arg === 'string') {
729
- __classPrivateFieldSet(this, _CollectionBuilder_rawQuery, arg, "f");
730
- return this;
731
- }
732
- if (typeof arg !== 'function') {
733
- 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.`);
734
- }
735
- const builtQuery = arg(new QueryBuilder());
736
- // This can be use in Javascript so we cannot rely on the type checking
737
- if (builtQuery instanceof Equals) {
738
- __classPrivateFieldSet(this, _CollectionBuilder_query, builtQuery.raw(this.currentQuery.build()), "f");
739
- }
740
- else {
741
- 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.');
742
- }
743
- return this;
744
- }
745
- /**
746
- * Retrieves draft content.
747
- * @example
748
- * ```ts
749
- * const client = new DotCMSClient(config);
750
- * const collectionBuilder = client.content.getCollection("Blog");
751
- * collectionBuilder
752
- * .draft() // This will retrieve draft/working content
753
- * .then((response) => // Your code here })
754
- * .catch((error) => // Your code here })
755
- * ```
756
- *
757
- * @return {CollectionBuilder} A CollectionBuilder instance.
758
- * @memberof CollectionBuilder
759
- */
760
- draft() {
761
- __classPrivateFieldSet(this, _CollectionBuilder_draft, true, "f");
762
- return this;
763
- }
764
- /**
765
- * Filters the content by a variant ID for [Experiments](https://www.dotcms.com/docs/latest/experiments-and-a-b-testing)
766
- *
767
- * More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
768
- *
769
- * @example
770
- * ```ts
771
- * const client = new DotCMSClient(config);
772
- * const collectionBuilder = client.content.getCollection("Blog");
773
- * collectionBuilder
774
- * .variant("YOUR_VARIANT_ID")
775
- * .then((response) => // Your code here })
776
- * .catch((error) => // Your code here })
777
- * ```
778
- *
779
- * @param {string} variantId A string that represents a variant ID.
780
- * @return {CollectionBuilder} A CollectionBuilder instance.
781
- * @memberof CollectionBuilder
782
- */
783
- variant(variantId) {
784
- __classPrivateFieldSet(this, _CollectionBuilder_query, this.currentQuery.field('variant').equals(variantId), "f");
785
- return this;
786
- }
787
- /**
788
- * Sets the depth of the relationships of the content.
789
- * Specifies the depth of related content to return in the results.
790
- *
791
- * More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
792
- *
793
- * @example
794
- * ```ts
795
- * const client = new DotCMSClient(config);
796
- * const collectionBuilder = client.content.getCollection("Blog");
797
- * collectionBuilder
798
- * .depth(1)
799
- * .then((response) => // Your code here })
800
- * .catch((error) => // Your code here })
801
- * ```
802
- *
803
- * @param {number} depth The depth of the relationships of the content.
804
- * @return {CollectionBuilder} A CollectionBuilder instance.
805
- * @memberof CollectionBuilder
806
- */
807
- depth(depth) {
808
- if (depth < 0 || depth > 3) {
809
- throw new Error('Depth value must be between 0 and 3');
810
- }
811
- __classPrivateFieldSet(this, _CollectionBuilder_depth, depth, "f");
812
- return this;
813
- }
814
- /**
815
- * Executes the fetch and returns a promise that resolves to the content or rejects with an error.
816
- *
817
- * @example
818
- * ```ts
819
- * const client = new DotCMSClient(config);
820
- * const collectionBuilder = client.content.getCollection("Blog");
821
- * collectionBuilder
822
- * .limit(10)
823
- * .then((response) => // Your code here })
824
- * .catch((error) => // Your code here })
825
- * ```
826
- *
827
- * @param {OnFullfilled} [onfulfilled] A callback that is called when the fetch is successful.
828
- * @param {OnRejected} [onrejected] A callback that is called when the fetch fails.
829
- * @return {Promise<GetCollectionResponse<T> | GetCollectionError>} A promise that resolves to the content or rejects with an error.
830
- * @memberof CollectionBuilder
831
- */
832
- then(onfulfilled, onrejected) {
833
- return this.fetch().then(async (response) => {
834
- const data = await response.json();
835
- if (response.ok) {
836
- const formattedResponse = this.formatResponse(data);
837
- const finalResponse = typeof onfulfilled === 'function'
838
- ? onfulfilled(formattedResponse)
839
- : formattedResponse;
840
- return finalResponse;
841
- }
842
- else {
843
- return {
844
- status: response.status,
845
- ...data
846
- };
847
- }
848
- }, onrejected);
849
- }
850
- /**
851
- * Formats the response to the desired format.
852
- *
853
- * @private
854
- * @param {GetCollectionRawResponse<T>} data The raw response data.
855
- * @return {GetCollectionResponse<T>} The formatted response.
856
- * @memberof CollectionBuilder
857
- */
858
- formatResponse(data) {
859
- const contentlets = data.entity.jsonObjectView.contentlets;
860
- const total = data.entity.resultsSize;
861
- const mappedResponse = {
862
- contentlets,
863
- total,
864
- page: __classPrivateFieldGet(this, _CollectionBuilder_page, "f"),
865
- size: contentlets.length
866
- };
867
- return __classPrivateFieldGet(this, _CollectionBuilder_sortBy, "f")
868
- ? {
869
- ...mappedResponse,
870
- sortedBy: __classPrivateFieldGet(this, _CollectionBuilder_sortBy, "f")
871
- }
872
- : mappedResponse;
873
- }
874
- /**
875
- * Calls the content API to fetch the content.
876
- *
877
- * @private
878
- * @return {Promise<Response>} The fetch response.
879
- * @memberof CollectionBuilder
880
- */
881
- fetch() {
882
- const finalQuery = this.currentQuery
883
- .field('languageId')
884
- .equals(__classPrivateFieldGet(this, _CollectionBuilder_languageId, "f").toString())
885
- .field('live')
886
- .equals((!__classPrivateFieldGet(this, _CollectionBuilder_draft, "f")).toString())
887
- .build();
888
- const sanitizedQuery = sanitizeQueryForContentType(finalQuery, __classPrivateFieldGet(this, _CollectionBuilder_contentType, "f"));
889
- const query = __classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f") ? `${sanitizedQuery} ${__classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f")}` : sanitizedQuery;
890
- return fetch(this.url, {
891
- ...__classPrivateFieldGet(this, _CollectionBuilder_requestOptions, "f"),
892
- method: 'POST',
893
- headers: {
894
- ...__classPrivateFieldGet(this, _CollectionBuilder_requestOptions, "f").headers,
895
- 'Content-Type': 'application/json'
896
- },
897
- body: JSON.stringify({
898
- query,
899
- render: __classPrivateFieldGet(this, _CollectionBuilder_render, "f"),
900
- sort: this.sort,
901
- limit: __classPrivateFieldGet(this, _CollectionBuilder_limit, "f"),
902
- offset: this.offset,
903
- depth: __classPrivateFieldGet(this, _CollectionBuilder_depth, "f")
904
- //userId: This exist but we currently don't use it
905
- //allCategoriesInfo: This exist but we currently don't use it
906
- })
907
- });
908
- }
909
- }
910
- _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();
911
-
912
- var _Content_requestOptions, _Content_serverUrl;
913
- /**
914
- * Creates a builder to filter and fetch a collection of content items.
915
- * @param contentType - The content type to retrieve.
916
- * @returns A CollectionBuilder instance for chaining filters and executing the query.
917
- * @template T - The type of the content items (defaults to unknown).
918
- *
919
- * @example Fetch blog posts with async/await
920
- * ```typescript
921
- * const response = await client.content
922
- * .getCollection<BlogPost>('Blog')
923
- * .limit(10)
924
- * .page(2)
925
- * .sortBy([{ field: 'title', order: 'asc' }])
926
- * .query(q => q.field('author').equals('John Doe'))
927
- * .depth(1)
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
- * .then(response => console.log(response.contentlets))
942
- * .catch(error => console.error(error));
943
- * ```
944
- *
945
- * @example Using a custom type
946
- * ```typescript
947
- * interface BlogPost {
948
- * summary: string;
949
- * author: string;
950
- * title: string;
951
- * }
952
- *
953
- * const posts = await client.content
954
- * .getCollection<BlogPost>('Blog')
955
- * .limit(10)
956
- *
957
- * posts.contentlets.forEach(post => {
958
- * console.log(post.title, post.author, post.summary);
959
- * });
960
- * ```
961
- */
962
- class Content {
963
- /**
964
- * Creates an instance of Content.
965
- * @param {RequestOptions} requestOptions - The options for the client request.
966
- * @param {string} serverUrl - The server URL.
967
- */
968
- constructor(requestOptions, serverUrl) {
969
- _Content_requestOptions.set(this, void 0);
970
- _Content_serverUrl.set(this, void 0);
971
- __classPrivateFieldSet(this, _Content_requestOptions, requestOptions, "f");
972
- __classPrivateFieldSet(this, _Content_serverUrl, serverUrl, "f");
973
- }
974
- /**
975
- * Takes a content type and returns a builder to filter and fetch the collection.
976
- * @param {string} contentType - The content type to get the collection.
977
- * @return {CollectionBuilder<T>} CollectionBuilder to filter and fetch the collection.
978
- * @template T - Represents the type of the content type to fetch. Defaults to unknown.
979
- * @memberof Content
980
- *
981
- * @example
982
- * ```javascript
983
- * // Using await and async
984
- * const collectionResponse = await client.content
985
- * .getCollection('Blog')
986
- * .limit(10)
987
- * .page(2)
988
- * .sortBy([{ field: 'title', order: 'asc' }])
989
- * .query((queryBuilder) => queryBuilder.field('author').equals('John Doe'))
990
- * .depth(1);
991
- * ```
992
- * @example
993
- * ```javascript
994
- * // Using then and catch
995
- * client.content
996
- * .getCollection('Blog')
997
- * .limit(10)
998
- * .page(2)
999
- * .sortBy([{ field: 'title', order: 'asc' }])
1000
- * .query((queryBuilder) => queryBuilder.field('author').equals('John Doe'))
1001
- * .depth(1)
1002
- * .then((response) => {
1003
- * console.log(response.contentlets);
1004
- * })
1005
- * .catch((error) => {
1006
- * console.error(error);
1007
- * });
1008
- * ```
1009
- * @example
1010
- * ```typescript
1011
- * // Using a specific type for your content
1012
- *
1013
- * type Blog = {
1014
- * summary: string;
1015
- * author: string;
1016
- * title: string;
1017
- * };
1018
- *
1019
- * client.content
1020
- * .getCollection<Blog>('Blog')
1021
- * .limit(10)
1022
- * .page(2)
1023
- * .sortBy([{ field: 'title', order: 'asc' }])
1024
- * .query((queryBuilder) => queryBuilder.field('author').equals('John Doe'))
1025
- * .depth(1)
1026
- * .then((response) => {
1027
- * response.contentlets.forEach((blog) => {
1028
- * console.log(blog.title);
1029
- * console.log(blog.author);
1030
- * console.log(blog.summary);
1031
- * });
1032
- * })
1033
- * .catch((error) => {
1034
- * console.error(error);
1035
- * });
1036
- * ```
1037
- *
1038
- */
1039
- getCollection(contentType) {
1040
- return new CollectionBuilder(__classPrivateFieldGet(this, _Content_requestOptions, "f"), __classPrivateFieldGet(this, _Content_serverUrl, "f"), contentType);
1041
- }
1042
- }
1043
- _Content_requestOptions = new WeakMap(), _Content_serverUrl = new WeakMap();
1044
-
1045
- class NavigationClient {
1046
- constructor(config, requestOptions) {
1047
- this.requestOptions = requestOptions;
1048
- this.BASE_URL = `${config?.dotcmsUrl}/api/v1/nav`;
1049
- }
1050
- /**
1051
- * Retrieves information about the dotCMS file and folder tree.
1052
- * @param {NavigationApiOptions} options - The options for the Navigation API call. Defaults to `{ depth: 0, path: '/', languageId: 1 }`.
1053
- * @returns {Promise<DotCMSNavigationItem[]>} - A Promise that resolves to the response from the DotCMS API.
1054
- * @throws {Error} - Throws an error if the options are not valid.
1055
- */
1056
- async get(path, params) {
1057
- if (!path) {
1058
- throw new Error("The 'path' parameter is required for the Navigation API");
1059
- }
1060
- const navParams = params ? this.mapToBackendParams(params) : {};
1061
- const urlParams = new URLSearchParams(navParams).toString();
1062
- const parsedPath = path.replace(/^\/+/, '/').replace(/\/+$/, '/');
1063
- const url = `${this.BASE_URL}${parsedPath}${urlParams ? `?${urlParams}` : ''}`;
1064
- const response = await fetch(url, this.requestOptions);
1065
- if (!response.ok) {
1066
- throw new Error(`Failed to fetch navigation data: ${response.statusText} - ${response.status}`);
1067
- }
1068
- return response.json().then((data) => data.entity);
1069
- }
1070
- mapToBackendParams(params) {
1071
- const backendParams = {};
1072
- if (params.depth) {
1073
- backendParams['depth'] = String(params.depth);
1074
- }
1075
- if (params.languageId) {
1076
- backendParams['language_id'] = String(params.languageId);
1077
- }
1078
- return backendParams;
1079
- }
1080
- }
1081
-
1082
- /**
1083
- * A record of HTTP status codes and their corresponding error messages.
1084
- *
1085
- * @type {Record<number, string>}
1086
- * @property {string} 401 - Unauthorized. Check the token and try again.
1087
- * @property {string} 403 - Forbidden. Check the permissions and try again.
1088
- * @property {string} 404 - Not Found. Check the URL and try again.
1089
- * @property {string} 500 - Internal Server Error. Try again later.
1090
- * @property {string} 502 - Bad Gateway. Try again later.
1091
- * @property {string} 503 - Service Unavailable. Try again later.
1092
- */
1093
- const ErrorMessages = {
1094
- 401: 'Unauthorized. Check the token and try again.',
1095
- 403: 'Forbidden. Check the permissions and try again.',
1096
- 404: 'Not Found. Check the URL and try again.',
1097
- 500: 'Internal Server Error. Try again later.',
1098
- 502: 'Bad Gateway. Try again later.',
1099
- 503: 'Service Unavailable. Try again later.'
1100
- };
1101
-
1102
- const DEFAULT_PAGE_CONTENTLETS_CONTENT = `
1103
- publishDate
1104
- inode
1105
- identifier
1106
- archived
1107
- urlMap
1108
- urlMap
1109
- locked
1110
- contentType
1111
- creationDate
1112
- modDate
1113
- title
1114
- baseType
1115
- working
1116
- live
1117
- publishUser {
1118
- firstName
1119
- lastName
1120
- }
1121
- owner {
1122
- lastName
1123
- }
1124
- conLanguage {
1125
- language
1126
- languageCode
1127
- }
1128
- modUser {
1129
- firstName
1130
- lastName
1131
- }
1132
- `;
1133
- /**
1134
- * Builds a GraphQL query for retrieving page content from DotCMS.
1135
- *
1136
- * @param {string} pageQuery - Custom fragment fields to include in the ClientPage fragment
1137
- * @param {string} additionalQueries - Additional GraphQL queries to include in the main query
1138
- * @returns {string} Complete GraphQL query string for page content
1139
- */
1140
- const buildPageQuery = ({ page, fragments, additionalQueries }) => {
1141
- if (!page) {
1142
- consola.warn("[DotCMS Client]: No page query was found, so we're loading all content using _map. This might slow things down. For better performance, we recommend adding a specific query in the page attribute.");
1143
- }
1144
- return `
1145
- fragment DotCMSPage on DotPage {
1146
- publishDate
1147
- type
1148
- httpsRequired
1149
- inode
1150
- path
1151
- identifier
1152
- hasTitleImage
1153
- sortOrder
1154
- extension
1155
- canRead
1156
- pageURI
1157
- canEdit
1158
- archived
1159
- friendlyName
1160
- workingInode
1161
- url
1162
- pageURI
1163
- hasLiveVersion
1164
- deleted
1165
- pageUrl
1166
- shortyWorking
1167
- mimeType
1168
- locked
1169
- stInode
1170
- contentType
1171
- creationDate
1172
- liveInode
1173
- name
1174
- shortyLive
1175
- modDate
1176
- title
1177
- baseType
1178
- working
1179
- canLock
1180
- live
1181
- isContentlet
1182
- statusIcons
1183
- canEdit
1184
- canLock
1185
- canRead
1186
- canEdit
1187
- canLock
1188
- canRead
1189
- runningExperimentId
1190
- urlContentMap {
1191
- _map
1192
- }
1193
- host {
1194
- identifier
1195
- hostName
1196
- googleMap
1197
- archived
1198
- contentType
1199
- }
1200
- vanityUrl {
1201
- action
1202
- forwardTo
1203
- uri
1204
- }
1205
- conLanguage {
1206
- id
1207
- language
1208
- languageCode
1209
- }
1210
- template {
1211
- drawed
1212
- anonymous
1213
- theme
1214
- identifier
1215
- }
1216
- containers {
1217
- path
1218
- identifier
1219
- maxContentlets
1220
- containerStructures {
1221
- id
1222
- code
1223
- structureId
1224
- containerId
1225
- contentTypeVar
1226
- containerInode
1227
- }
1228
- containerContentlets {
1229
- uuid
1230
- contentlets {
1231
- ${page ? DEFAULT_PAGE_CONTENTLETS_CONTENT : '_map'}
1232
- }
1233
- }
1234
- }
1235
- layout {
1236
- header
1237
- footer
1238
- body {
1239
- rows {
1240
- columns {
1241
- leftOffset
1242
- styleClass
1243
- width
1244
- left
1245
- containers {
1246
- identifier
1247
- uuid
1248
- }
1249
- }
1250
- }
1251
- }
1252
- }
1253
- viewAs {
1254
- visitor {
1255
- persona {
1256
- modDate
1257
- inode
1258
- name
1259
- identifier
1260
- keyTag
1261
- photo {
1262
- versionPath
1263
- }
1264
- }
1265
- }
1266
- persona {
1267
- modDate
1268
- inode
1269
- name
1270
- identifier
1271
- keyTag
1272
- photo {
1273
- versionPath
1274
- }
1275
- }
1276
- language {
1277
- id
1278
- languageCode
1279
- countryCode
1280
- language
1281
- country
1282
- }
1283
- }
1284
- }
1285
-
1286
- ${page ? ` fragment ClientPage on DotPage { ${page} } ` : ''}
1287
-
1288
- ${fragments ? fragments.join('\n\n') : ''}
1289
-
1290
- query PageContent($url: String!, $languageId: String, $mode: String, $personaId: String, $fireRules: Boolean, $publishDate: String, $siteId: String, $variantName: String) {
1291
- page: page(url: $url, languageId: $languageId, pageMode: $mode, persona: $personaId, fireRules: $fireRules, publishDate: $publishDate, site: $siteId, variantName: $variantName) {
1292
- ...DotCMSPage
1293
- ${page ? '...ClientPage' : ''}
1294
- }
1295
-
1296
- ${additionalQueries}
1297
- }
1298
- `;
1299
- };
1300
- /**
1301
- * Converts a record of query strings into a single GraphQL query string.
1302
- *
1303
- * @param {Record<string, string>} queryData - Object containing named query strings
1304
- * @returns {string} Combined query string or empty string if no queryData provided
1305
- */
1306
- function buildQuery(queryData) {
1307
- if (!queryData)
1308
- return '';
1309
- return Object.entries(queryData)
1310
- .map(([key, query]) => `${key}: ${query}`)
1311
- .join(' ');
1312
- }
1313
- /**
1314
- * Filters response data to include only specified keys.
1315
- *
1316
- * @param {Record<string, string>} responseData - Original response data object
1317
- * @param {string[]} keys - Array of keys to extract from the response data
1318
- * @returns {Record<string, string>} New object containing only the specified keys
1319
- */
1320
- function mapResponseData(responseData, keys) {
1321
- return keys.reduce((accumulator, key) => {
1322
- if (responseData[key] !== undefined) {
1323
- accumulator[key] = responseData[key];
1324
- }
1325
- return accumulator;
1326
- }, {});
1327
- }
1328
- /**
1329
- * Executes a GraphQL query against the DotCMS API.
1330
- *
1331
- * @param {Object} options - Options for the fetch request
1332
- * @param {string} options.body - GraphQL query string
1333
- * @param {Record<string, string>} options.headers - HTTP headers for the request
1334
- * @returns {Promise<any>} Parsed JSON response from the GraphQL API
1335
- * @throws {Error} If the HTTP response is not successful
1336
- */
1337
- async function fetchGraphQL({ baseURL, body, headers }) {
1338
- const url = new URL(baseURL);
1339
- url.pathname = '/api/v1/graphql';
1340
- const response = await fetch(url.toString(), {
1341
- method: 'POST',
1342
- body,
1343
- headers
1344
- });
1345
- if (!response.ok) {
1346
- const error = {
1347
- status: response.status,
1348
- message: ErrorMessages[response.status] || response.statusText
1349
- };
1350
- throw error;
1351
- }
1352
- return await response.json();
1353
- }
1354
-
1355
- /**
1356
- * Client for interacting with the DotCMS Page API.
1357
- * Provides methods to retrieve and manipulate pages.
1358
- */
1359
- class PageClient {
1360
- /**
1361
- * Creates a new PageClient instance.
1362
- *
1363
- * @param {DotCMSClientConfig} config - Configuration options for the DotCMS client
1364
- * @param {RequestOptions} requestOptions - Options for fetch requests including authorization headers
1365
- * @example
1366
- * ```typescript
1367
- * const pageClient = new PageClient(
1368
- * {
1369
- * dotcmsUrl: 'https://demo.dotcms.com',
1370
- * authToken: 'your-auth-token',
1371
- * siteId: 'demo.dotcms.com'
1372
- * },
1373
- * {
1374
- * headers: {
1375
- * Authorization: 'Bearer your-auth-token'
1376
- * }
1377
- * }
1378
- * );
1379
- * ```
1380
- */
1381
- constructor(config, requestOptions) {
1382
- this.requestOptions = requestOptions;
1383
- this.siteId = config.siteId || '';
1384
- this.dotcmsUrl = config.dotcmsUrl;
1385
- }
1386
- /**
1387
- * Retrieves a page from DotCMS using GraphQL.
1388
- *
1389
- * @param {string} url - The URL of the page to retrieve
1390
- * @param {DotCMSPageRequestParams} [options] - Options for the request
1391
- * @template T - The type of the page and content, defaults to DotCMSBasicPage and Record<string, unknown> | unknown
1392
- * @returns {Promise<DotCMSComposedPageResponse<T>>} A Promise that resolves to the page data
1393
- *
1394
- * @example Using GraphQL
1395
- * ```typescript
1396
- * const page = await pageClient.get<{ page: MyPageWithBanners; content: { blogPosts: { blogTitle: string } } }>(
1397
- * '/index',
1398
- * {
1399
- * languageId: '1',
1400
- * mode: 'LIVE',
1401
- * graphql: {
1402
- * page: `
1403
- * containers {
1404
- * containerContentlets {
1405
- * contentlets {
1406
- * ... on Banner {
1407
- * ...bannerFragment
1408
- * }
1409
- * }
1410
- * }
1411
- * `,
1412
- * content: {
1413
- * blogPosts: `
1414
- * BlogCollection(limit: 3) {
1415
- * ...blogFragment
1416
- * }
1417
- * `,
1418
- * },
1419
- * fragments: [
1420
- * `
1421
- * fragment bannerFragment on Banner {
1422
- * caption
1423
- * }
1424
- * `,
1425
- * `
1426
- * fragment blogFragment on Blog {
1427
- * title
1428
- * urlTitle
1429
- * }
1430
- * `
1431
- * ]
1432
- * }
1433
- * });
1434
- * ```
1435
- */
1436
- async get(url, options) {
1437
- const { languageId = '1', mode = 'LIVE', siteId = this.siteId, fireRules = false, personaId, publishDate, variantName, graphql = {} } = options || {};
1438
- const { page, content = {}, variables, fragments } = graphql;
1439
- const contentQuery = buildQuery(content);
1440
- const completeQuery = buildPageQuery({
1441
- page,
1442
- fragments,
1443
- additionalQueries: contentQuery
1444
- });
1445
- const requestVariables = {
1446
- // The url is expected to have a leading slash to comply on VanityURL Matching, some frameworks like Angular will not add the leading slash
1447
- url: url.startsWith('/') ? url : `/${url}`,
1448
- mode,
1449
- languageId,
1450
- personaId,
1451
- fireRules,
1452
- publishDate,
1453
- siteId,
1454
- variantName,
1455
- ...variables
1456
- };
1457
- const requestHeaders = this.requestOptions.headers;
1458
- const requestBody = JSON.stringify({ query: completeQuery, variables: requestVariables });
1459
- try {
1460
- const { data, errors } = await fetchGraphQL({
1461
- baseURL: this.dotcmsUrl,
1462
- body: requestBody,
1463
- headers: requestHeaders
1464
- });
1465
- if (errors) {
1466
- errors.forEach((error) => {
1467
- consola$1.error('[DotCMS GraphQL Error]: ', error.message);
1468
- });
1469
- }
1470
- const pageResponse = graphqlToPageEntity(data);
1471
- if (!pageResponse) {
1472
- throw new Error('No page data found');
1473
- }
1474
- const contentResponse = mapResponseData(data, Object.keys(content));
1475
- return {
1476
- pageAsset: pageResponse,
1477
- content: contentResponse,
1478
- graphql: {
1479
- query: completeQuery,
1480
- variables: requestVariables
1481
- }
1482
- };
1483
- }
1484
- catch (error) {
1485
- const errorMessage = {
1486
- error,
1487
- message: 'Failed to retrieve page data',
1488
- graphql: {
1489
- query: completeQuery,
1490
- variables: requestVariables
1491
- }
1492
- };
1493
- throw errorMessage;
1494
- }
1495
- }
1496
- }
1497
-
1498
- /**
1499
- * Parses a string into a URL object.
1500
- *
1501
- * @param url - The URL string to parse
1502
- * @returns A URL object if parsing is successful, undefined otherwise
1503
- */
1504
- function parseURL(url) {
1505
- try {
1506
- return new URL(url);
1507
- }
1508
- catch {
1509
- consola.error('[DotCMS Client]: Invalid URL:', url);
1510
- return undefined;
1511
- }
1512
- }
1513
- /**
1514
- * Default configuration for the DotCMS client.
1515
- */
1516
- const defaultConfig = {
1517
- dotcmsUrl: '',
1518
- authToken: '',
1519
- requestOptions: {}
1520
- };
1521
- /**
1522
- * Client for interacting with the DotCMS REST API.
1523
- * Provides access to content, page, and navigation functionality.
1524
- */
1525
- class DotCMSClient {
1526
- /**
1527
- * Creates a new DotCMS client instance.
1528
- *
1529
- * @param config - Configuration options for the client
1530
- * @throws Warning if dotcmsUrl is invalid or authToken is missing
1531
- */
1532
- constructor(config = defaultConfig) {
1533
- this.config = config;
1534
- this.requestOptions = this.createAuthenticatedRequestOptions(this.config);
1535
- // Initialize clients
1536
- this.page = new PageClient(this.config, this.requestOptions);
1537
- this.nav = new NavigationClient(this.config, this.requestOptions);
1538
- this.content = new Content(this.requestOptions, this.config.dotcmsUrl);
1539
- }
1540
- /**
1541
- * Creates request options with authentication headers.
1542
- *
1543
- * @param config - The client configuration
1544
- * @returns Request options with authorization headers
1545
- */
1546
- createAuthenticatedRequestOptions(config) {
1547
- return {
1548
- ...config.requestOptions,
1549
- headers: {
1550
- ...config.requestOptions?.headers,
1551
- Authorization: `Bearer ${config.authToken}`
1552
- }
1553
- };
1554
- }
1555
- }
1556
- /**
1557
- * Creates and returns a new DotCMS client instance.
1558
- *
1559
- * @param config - Configuration options for the client
1560
- * @returns A configured DotCMS client instance
1561
- * @example
1562
- * ```typescript
1563
- * const client = dotCMSCreateClient({
1564
- * dotcmsUrl: 'https://demo.dotcms.com',
1565
- * authToken: 'your-auth-token'
1566
- * });
1567
- *
1568
- * // Use the client to fetch content
1569
- * const pages = await client.page.get('/about-us');
1570
- * ```
1571
- */
1572
- const createDotCMSClient = (clientConfig) => {
1573
- const { dotcmsUrl, authToken } = clientConfig || {};
1574
- const instanceUrl = parseURL(dotcmsUrl)?.origin;
1575
- if (!instanceUrl) {
1576
- throw new TypeError("Invalid configuration - 'dotcmsUrl' must be a valid URL");
1577
- }
1578
- if (!authToken) {
1579
- throw new TypeError("Invalid configuration - 'authToken' is required");
1580
- }
1581
- const config = {
1582
- ...clientConfig,
1583
- authToken,
1584
- dotcmsUrl: instanceUrl
1585
- };
1586
- return new DotCMSClient(config);
1587
- };
1588
-
1589
- export { createDotCMSClient };