@dotcms/client 1.2.0-next.2 → 1.2.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { consola } from 'consola';
2
- import { BaseHttpClient, DotHttpError, DotErrorContent, DotErrorNavigation, DotErrorPage } from '@dotcms/types';
2
+ import { BaseHttpClient, DISTANCE_FUNCTIONS, DotHttpError, DotErrorAISearch, DotErrorContent, DotErrorNavigation, DotErrorPage } from '@dotcms/types';
3
3
  import { graphqlToPageEntity } from './internal.esm.js';
4
4
 
5
5
  /**
@@ -145,6 +145,362 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
145
145
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
146
146
  };
147
147
 
148
+ /**
149
+ * Utility functions for AI search parameter mapping and processing
150
+ */
151
+ /**
152
+ * Appends mapped parameters to URLSearchParams based on a mapping configuration.
153
+ * Only includes parameters that have defined values.
154
+ *
155
+ * @param searchParams - The URLSearchParams object to append to
156
+ * @param sourceObject - The source object containing values
157
+ * @param mapping - Array of [targetKey, sourceKey] pairs for parameter mapping, or [key] to use same key
158
+ * @example
159
+ * ```typescript
160
+ * const params = new URLSearchParams();
161
+ * const query = { limit: 10, offset: 0, siteId: 'default', indexName: 'content' };
162
+ * const mapping = [
163
+ * ['searchLimit', 'limit'], // Maps limit -> searchLimit
164
+ * ['searchOffset', 'offset'], // Maps offset -> searchOffset
165
+ * ['site', 'siteId'], // Maps siteId -> site
166
+ * ['indexName'] // Uses indexName -> indexName (same key)
167
+ * ];
168
+ * appendMappedParams(params, query, mapping);
169
+ * // Results in: searchLimit=10&searchOffset=0&site=default&indexName=content
170
+ * ```
171
+ */
172
+ function appendMappedParams(searchParams, sourceObject, mapping) {
173
+ mapping.forEach((item) => {
174
+ const targetKey = item[0];
175
+ const sourceKey = item[1] ?? item[0];
176
+ const value = sourceObject[sourceKey];
177
+ if (value !== undefined) {
178
+ searchParams.append(targetKey, String(value));
179
+ }
180
+ });
181
+ }
182
+
183
+ var _BaseApiClient_requestOptions, _BaseApiClient_config, _BaseApiClient_httpClient;
184
+ /**
185
+ * Base class for all DotCMS API clients.
186
+ * Provides common constructor parameters and properties that all API clients need.
187
+ * Uses JavaScript private fields (#) for true runtime privacy.
188
+ */
189
+ class BaseApiClient {
190
+ /**
191
+ * Creates a new API client instance.
192
+ *
193
+ * @param {DotCMSClientConfig} config - Configuration options for the DotCMS client
194
+ * @param {DotRequestOptions} requestOptions - Options for fetch requests including authorization headers
195
+ * @param {DotHttpClient} httpClient - HTTP client for making requests
196
+ */
197
+ constructor(config, requestOptions = {}, httpClient) {
198
+ /**
199
+ * Request options including authorization headers.
200
+ * @private
201
+ */
202
+ _BaseApiClient_requestOptions.set(this, void 0);
203
+ /**
204
+ * DotCMS client configuration.
205
+ * @private
206
+ */
207
+ _BaseApiClient_config.set(this, void 0);
208
+ /**
209
+ * HTTP client for making requests.
210
+ * @private
211
+ */
212
+ _BaseApiClient_httpClient.set(this, void 0);
213
+ __classPrivateFieldSet(this, _BaseApiClient_config, {
214
+ siteId: '',
215
+ ...config
216
+ }, "f");
217
+ __classPrivateFieldSet(this, _BaseApiClient_requestOptions, {
218
+ ...requestOptions
219
+ }, "f");
220
+ __classPrivateFieldSet(this, _BaseApiClient_httpClient, httpClient, "f");
221
+ }
222
+ /**
223
+ * Gets the request options including authorization headers.
224
+ * @protected
225
+ */
226
+ get requestOptions() {
227
+ return __classPrivateFieldGet(this, _BaseApiClient_requestOptions, "f");
228
+ }
229
+ /**
230
+ * Gets the DotCMS client configuration.
231
+ * @protected
232
+ */
233
+ get config() {
234
+ return __classPrivateFieldGet(this, _BaseApiClient_config, "f");
235
+ }
236
+ /**
237
+ * Gets the HTTP client for making requests.
238
+ * @protected
239
+ */
240
+ get httpClient() {
241
+ return __classPrivateFieldGet(this, _BaseApiClient_httpClient, "f");
242
+ }
243
+ /**
244
+ * Gets the DotCMS URL from config.
245
+ * @protected
246
+ */
247
+ get dotcmsUrl() {
248
+ return __classPrivateFieldGet(this, _BaseApiClient_config, "f").dotcmsUrl;
249
+ }
250
+ /**
251
+ * Gets the site ID from config.
252
+ * @protected
253
+ */
254
+ get siteId() {
255
+ return __classPrivateFieldGet(this, _BaseApiClient_config, "f").siteId || '';
256
+ }
257
+ }
258
+ _BaseApiClient_requestOptions = new WeakMap(), _BaseApiClient_config = new WeakMap(), _BaseApiClient_httpClient = new WeakMap();
259
+
260
+ /**
261
+ * Default values for AI configuration
262
+ */
263
+ const DEFAULT_AI_CONFIG = {
264
+ threshold: 0.5,
265
+ distanceFunction: DISTANCE_FUNCTIONS.cosine,
266
+ responseLength: 1024
267
+ };
268
+ /**
269
+ * Default values for search query
270
+ */
271
+ const DEFAULT_QUERY = {
272
+ limit: 1000,
273
+ offset: 0,
274
+ indexName: 'default'
275
+ };
276
+
277
+ var _AISearch_params, _AISearch_prompt, _AISearch_indexName;
278
+ /**
279
+ * Class for executing AI searches.
280
+ *
281
+ * @template T - The type of the contentlet.
282
+ * @param config - The configuration for the client.
283
+ * @param requestOptions - The request options for the client.
284
+ * @param httpClient - The HTTP client for the client.
285
+ * @param params - The parameters for the search.
286
+ * @param prompt - The prompt for the search.
287
+ */
288
+ class AISearch extends BaseApiClient {
289
+ constructor(config, requestOptions, httpClient, prompt, indexName, params = {}) {
290
+ super(config, requestOptions, httpClient);
291
+ _AISearch_params.set(this, void 0);
292
+ _AISearch_prompt.set(this, void 0);
293
+ _AISearch_indexName.set(this, void 0);
294
+ __classPrivateFieldSet(this, _AISearch_params, params, "f");
295
+ __classPrivateFieldSet(this, _AISearch_prompt, prompt, "f");
296
+ __classPrivateFieldSet(this, _AISearch_indexName, indexName, "f");
297
+ }
298
+ /**
299
+ * Executes the AI search and returns a promise with the search results.
300
+ *
301
+ * @param onfulfilled - Callback function to handle the search results.
302
+ * @param onrejected - Callback function to handle the search error.
303
+ * @returns Promise with the search results or the error.
304
+ * @example
305
+ * ```typescript
306
+ * const results = await client.ai.search('machine learning articles', 'content_index', {
307
+ * query: {
308
+ * limit: 20,
309
+ * contentType: 'BlogPost',
310
+ * languageId: '1' // or 1
311
+ * },
312
+ * config: {
313
+ * threshold: 0.7
314
+ * }
315
+ * });
316
+ * ```
317
+ * @example
318
+ * ```typescript
319
+ * client.ai.search('machine learning articles', 'content_index', {
320
+ * query: {
321
+ * limit: 20,
322
+ * contentType: 'BlogPost',
323
+ * languageId: '1' // or 1
324
+ * },
325
+ * config: {
326
+ * threshold: 0.7
327
+ * }
328
+ * }).then((results) => {
329
+ * console.log(results);
330
+ * }).catch((error) => {
331
+ * console.error(error);
332
+ * });
333
+ * ```
334
+ */
335
+ then(onfulfilled, onrejected) {
336
+ return this.fetch().then((data) => {
337
+ if (typeof onfulfilled === 'function') {
338
+ const result = onfulfilled(data);
339
+ // Ensure we always return a value, fallback to formattedResponse if callback returns undefined
340
+ return result ?? data;
341
+ }
342
+ return data;
343
+ }, (error) => {
344
+ // Wrap error in DotCMSContentError
345
+ let contentError;
346
+ if (error instanceof DotHttpError) {
347
+ contentError = new DotErrorAISearch({
348
+ message: `AI Search failed (fetch): ${error.message}`,
349
+ httpError: error,
350
+ params: __classPrivateFieldGet(this, _AISearch_params, "f"),
351
+ prompt: __classPrivateFieldGet(this, _AISearch_prompt, "f"),
352
+ indexName: __classPrivateFieldGet(this, _AISearch_indexName, "f")
353
+ });
354
+ }
355
+ else {
356
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error';
357
+ contentError = new DotErrorAISearch({
358
+ message: `AI Search failed (fetch): ${errorMessage}`,
359
+ httpError: undefined,
360
+ params: __classPrivateFieldGet(this, _AISearch_params, "f"),
361
+ prompt: __classPrivateFieldGet(this, _AISearch_prompt, "f"),
362
+ indexName: __classPrivateFieldGet(this, _AISearch_indexName, "f")
363
+ });
364
+ }
365
+ if (typeof onrejected === 'function') {
366
+ const result = onrejected(contentError);
367
+ // Ensure we always return a value, fallback to original error if callback returns undefined
368
+ return result ?? contentError;
369
+ }
370
+ // Throw the wrapped error to trigger .catch()
371
+ throw contentError;
372
+ });
373
+ }
374
+ fetch() {
375
+ const searchParams = this.buildSearchParams(__classPrivateFieldGet(this, _AISearch_prompt, "f"), __classPrivateFieldGet(this, _AISearch_params, "f"));
376
+ const url = new URL('/api/v1/ai/search', this.dotcmsUrl);
377
+ url.search = searchParams.toString();
378
+ return this.httpClient.request(url.toString(), {
379
+ ...this.requestOptions,
380
+ headers: {
381
+ ...this.requestOptions.headers
382
+ },
383
+ method: 'GET'
384
+ });
385
+ }
386
+ /**
387
+ * Builds URLSearchParams from the SDK interface, mapping to backend parameter names.
388
+ * Only includes parameters that have values.
389
+ *
390
+ * @param params - Search parameters with SDK naming
391
+ * @returns URLSearchParams with backend parameter names
392
+ * @private
393
+ */
394
+ buildSearchParams(prompt, params = {}) {
395
+ const searchParams = new URLSearchParams();
396
+ const { query = {}, config = {} } = params;
397
+ const combinedQuery = {
398
+ ...DEFAULT_QUERY,
399
+ siteId: this.siteId,
400
+ ...query
401
+ };
402
+ const combinedConfig = {
403
+ ...DEFAULT_AI_CONFIG,
404
+ ...config
405
+ };
406
+ const entriesQueryParameters = [
407
+ ['searchLimit', 'limit'],
408
+ ['searchOffset', 'offset'],
409
+ ['site', 'siteId'],
410
+ ['language', 'languageId'],
411
+ ['contentType']
412
+ ];
413
+ // Map SDK query parameters to backend parameter names
414
+ appendMappedParams(searchParams, combinedQuery, entriesQueryParameters);
415
+ // Map config parameters using the same key names
416
+ appendMappedParams(searchParams, combinedConfig, Object.keys(combinedConfig).map((key) => [key]));
417
+ // Add search-specific parameters
418
+ searchParams.append('indexName', __classPrivateFieldGet(this, _AISearch_indexName, "f"));
419
+ searchParams.append('query', prompt);
420
+ return searchParams;
421
+ }
422
+ }
423
+ _AISearch_params = new WeakMap(), _AISearch_prompt = new WeakMap(), _AISearch_indexName = new WeakMap();
424
+
425
+ /**
426
+ * Client for interacting with the DotCMS AI API.
427
+ * Provides methods to interact with AI features.
428
+ * @experimental This client is experimental and may be subject to change.
429
+ */
430
+ class AIClient extends BaseApiClient {
431
+ /**
432
+ * Creates a new AIClient instance.
433
+ *
434
+ * @param {DotCMSClientConfig} config - Configuration options for the DotCMS client
435
+ * @param {DotRequestOptions} requestOptions - Options for fetch requests including authorization headers
436
+ * @param {DotHttpClient} httpClient - HTTP client for making requests
437
+ * @example
438
+ * ```typescript
439
+ * const aiClient = new AIClient(
440
+ * {
441
+ * dotcmsUrl: 'https://demo.dotcms.com',
442
+ * authToken: 'your-auth-token',
443
+ * siteId: 'demo.dotcms.com'
444
+ * },
445
+ * {
446
+ * headers: {
447
+ * Authorization: 'Bearer your-auth-token'
448
+ * }
449
+ * },
450
+ * httpClient
451
+ * );
452
+ * ```
453
+ */
454
+ constructor(config, requestOptions, httpClient) {
455
+ super(config, requestOptions, httpClient);
456
+ }
457
+ /**
458
+ * Performs an AI-powered search.
459
+ *
460
+ * @param params - Search parameters with query and AI configuration
461
+ * @returns Promise with search results
462
+ * @experimental This method is experimental and may be subject to change.
463
+ * @template T - The type of the contentlet.
464
+ * @param prompt - The prompt for the search.
465
+ * @param indexName - The name of the index you want to search in.
466
+ * @param params - Search parameters with query and AI configuration.
467
+ * @example
468
+ * @example
469
+ * ```typescript
470
+ * const results = await client.ai.search('machine learning articles', 'content_index', {
471
+ * query: {
472
+ * limit: 20,
473
+ * contentType: 'BlogPost',
474
+ * languageId: "1" // or 1
475
+ * },
476
+ * config: {
477
+ * threshold: 0.7
478
+ * }
479
+ * });
480
+ * ```
481
+ * @example
482
+ * ```typescript
483
+ * client.ai.search('machine learning articles', 'content_index', {
484
+ * query: {
485
+ * limit: 20,
486
+ * contentType: 'BlogPost',
487
+ * languageId: "1" // or 1
488
+ * },
489
+ * config: {
490
+ * threshold: 0.7,
491
+ * distanceFunction: DISTANCE_FUNCTIONS.cosine
492
+ * }
493
+ * }).then((results) => {
494
+ * console.log(results);
495
+ * }).catch((error) => {
496
+ * console.error(error);
497
+ * });
498
+ */
499
+ search(prompt, indexName, params = {}) {
500
+ return new AISearch(this.config, this.requestOptions, this.httpClient, prompt, indexName, params);
501
+ }
502
+ }
503
+
148
504
  /**
149
505
  * Default variant identifier used in the application.
150
506
  */
@@ -744,7 +1100,7 @@ class QueryBuilder {
744
1100
  }
745
1101
  _QueryBuilder_query = new WeakMap();
746
1102
 
747
- var _CollectionBuilder_page, _CollectionBuilder_limit, _CollectionBuilder_depth, _CollectionBuilder_render, _CollectionBuilder_sortBy, _CollectionBuilder_contentType, _CollectionBuilder_defaultQuery, _CollectionBuilder_query, _CollectionBuilder_rawQuery, _CollectionBuilder_languageId, _CollectionBuilder_draft, _CollectionBuilder_requestOptions, _CollectionBuilder_httpClient, _CollectionBuilder_config;
1103
+ var _CollectionBuilder_page, _CollectionBuilder_limit, _CollectionBuilder_depth, _CollectionBuilder_render, _CollectionBuilder_sortBy, _CollectionBuilder_contentType, _CollectionBuilder_defaultQuery, _CollectionBuilder_query, _CollectionBuilder_rawQuery, _CollectionBuilder_languageId, _CollectionBuilder_draft;
748
1104
  /**
749
1105
  * Creates a Builder to filter and fetch content from the content API for a specific content type.
750
1106
  *
@@ -752,7 +1108,7 @@ var _CollectionBuilder_page, _CollectionBuilder_limit, _CollectionBuilder_depth,
752
1108
  * @class CollectionBuilder
753
1109
  * @template T Represents the type of the content type to fetch. Defaults to unknown.
754
1110
  */
755
- class CollectionBuilder {
1111
+ class CollectionBuilder extends BaseApiClient {
756
1112
  /**
757
1113
  * Creates an instance of CollectionBuilder.
758
1114
  * @param {ClientOptions} requestOptions Options for the client request.
@@ -762,6 +1118,7 @@ class CollectionBuilder {
762
1118
  * @memberof CollectionBuilder
763
1119
  */
764
1120
  constructor(requestOptions, config, contentType, httpClient) {
1121
+ super(config, requestOptions, httpClient);
765
1122
  _CollectionBuilder_page.set(this, 1);
766
1123
  _CollectionBuilder_limit.set(this, 10);
767
1124
  _CollectionBuilder_depth.set(this, 0);
@@ -773,14 +1130,7 @@ class CollectionBuilder {
773
1130
  _CollectionBuilder_rawQuery.set(this, void 0);
774
1131
  _CollectionBuilder_languageId.set(this, 1);
775
1132
  _CollectionBuilder_draft.set(this, false);
776
- _CollectionBuilder_requestOptions.set(this, void 0);
777
- _CollectionBuilder_httpClient.set(this, void 0);
778
- _CollectionBuilder_config.set(this, void 0);
779
- __classPrivateFieldSet(this, _CollectionBuilder_requestOptions, requestOptions, "f");
780
- __classPrivateFieldSet(this, _CollectionBuilder_config, config, "f");
781
1133
  __classPrivateFieldSet(this, _CollectionBuilder_contentType, contentType, "f");
782
- __classPrivateFieldSet(this, _CollectionBuilder_httpClient, httpClient, "f");
783
- __classPrivateFieldSet(this, _CollectionBuilder_config, config, "f");
784
1134
  // Build the default query with the contentType field
785
1135
  __classPrivateFieldSet(this, _CollectionBuilder_defaultQuery, new QueryBuilder().field('contentType').equals(__classPrivateFieldGet(this, _CollectionBuilder_contentType, "f")), "f");
786
1136
  }
@@ -812,17 +1162,7 @@ class CollectionBuilder {
812
1162
  * @memberof CollectionBuilder
813
1163
  */
814
1164
  get url() {
815
- return `${__classPrivateFieldGet(this, _CollectionBuilder_config, "f").dotcmsUrl}${CONTENT_API_URL}`;
816
- }
817
- /**
818
- * Returns the site ID from the configuration.
819
- *
820
- * @readonly
821
- * @private
822
- * @memberof CollectionBuilder
823
- */
824
- get siteId() {
825
- return __classPrivateFieldGet(this, _CollectionBuilder_config, "f").siteId;
1165
+ return `${this.config.dotcmsUrl}${CONTENT_API_URL}`;
826
1166
  }
827
1167
  /**
828
1168
  * Returns the current query built.
@@ -1074,11 +1414,11 @@ class CollectionBuilder {
1074
1414
  const finalQuery = this.getFinalQuery();
1075
1415
  const sanitizedQuery = sanitizeQueryForContentType(finalQuery, __classPrivateFieldGet(this, _CollectionBuilder_contentType, "f"));
1076
1416
  const query = __classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f") ? `${sanitizedQuery} ${__classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f")}` : sanitizedQuery;
1077
- return __classPrivateFieldGet(this, _CollectionBuilder_httpClient, "f").request(this.url, {
1078
- ...__classPrivateFieldGet(this, _CollectionBuilder_requestOptions, "f"),
1417
+ return this.httpClient.request(this.url, {
1418
+ ...this.requestOptions,
1079
1419
  method: 'POST',
1080
1420
  headers: {
1081
- ...__classPrivateFieldGet(this, _CollectionBuilder_requestOptions, "f").headers,
1421
+ ...this.requestOptions.headers,
1082
1422
  'Content-Type': 'application/json'
1083
1423
  },
1084
1424
  body: JSON.stringify({
@@ -1146,9 +1486,8 @@ class CollectionBuilder {
1146
1486
  return baseQuery;
1147
1487
  }
1148
1488
  }
1149
- _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_requestOptions = new WeakMap(), _CollectionBuilder_httpClient = new WeakMap(), _CollectionBuilder_config = new WeakMap();
1489
+ _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();
1150
1490
 
1151
- var _Content_requestOptions, _Content_httpClient, _Content_config;
1152
1491
  /**
1153
1492
  * Creates a builder to filter and fetch a collection of content items.
1154
1493
  * @param contentType - The content type to retrieve.
@@ -1198,20 +1537,15 @@ var _Content_requestOptions, _Content_httpClient, _Content_config;
1198
1537
  * });
1199
1538
  * ```
1200
1539
  */
1201
- class Content {
1540
+ class Content extends BaseApiClient {
1202
1541
  /**
1203
1542
  * Creates an instance of Content.
1543
+ * @param {DotCMSClientConfig} config - Configuration options for the DotCMS client
1204
1544
  * @param {DotRequestOptions} requestOptions - The options for the client request.
1205
- * @param {string} serverUrl - The server URL.
1206
1545
  * @param {DotHttpClient} httpClient - HTTP client for making requests.
1207
1546
  */
1208
1547
  constructor(config, requestOptions, httpClient) {
1209
- _Content_requestOptions.set(this, void 0);
1210
- _Content_httpClient.set(this, void 0);
1211
- _Content_config.set(this, void 0);
1212
- __classPrivateFieldSet(this, _Content_requestOptions, requestOptions, "f");
1213
- __classPrivateFieldSet(this, _Content_config, config, "f");
1214
- __classPrivateFieldSet(this, _Content_httpClient, httpClient, "f");
1548
+ super(config, requestOptions, httpClient);
1215
1549
  }
1216
1550
  /**
1217
1551
  * Takes a content type and returns a builder to filter and fetch the collection.
@@ -1279,16 +1613,20 @@ class Content {
1279
1613
  *
1280
1614
  */
1281
1615
  getCollection(contentType) {
1282
- return new CollectionBuilder(__classPrivateFieldGet(this, _Content_requestOptions, "f"), __classPrivateFieldGet(this, _Content_config, "f"), contentType, __classPrivateFieldGet(this, _Content_httpClient, "f"));
1616
+ return new CollectionBuilder(this.requestOptions, this.config, contentType, this.httpClient);
1283
1617
  }
1284
1618
  }
1285
- _Content_requestOptions = new WeakMap(), _Content_httpClient = new WeakMap(), _Content_config = new WeakMap();
1286
1619
 
1287
- class NavigationClient {
1620
+ class NavigationClient extends BaseApiClient {
1621
+ /**
1622
+ * Creates a new NavigationClient instance.
1623
+ * @param {DotCMSClientConfig} config - Configuration options for the DotCMS client
1624
+ * @param {DotRequestOptions} requestOptions - Options for fetch requests including authorization headers
1625
+ * @param {DotHttpClient} httpClient - HTTP client for making requests
1626
+ */
1288
1627
  constructor(config, requestOptions, httpClient) {
1289
- this.requestOptions = requestOptions;
1628
+ super(config, requestOptions, httpClient);
1290
1629
  this.BASE_URL = `${config?.dotcmsUrl}/api/v1/nav`;
1291
- this.httpClient = httpClient;
1292
1630
  }
1293
1631
  /**
1294
1632
  * Retrieves information about the dotCMS file and folder tree.
@@ -1586,7 +1924,7 @@ async function fetchGraphQL({ baseURL, body, headers, httpClient }) {
1586
1924
  * Client for interacting with the DotCMS Page API.
1587
1925
  * Provides methods to retrieve and manipulate pages.
1588
1926
  */
1589
- class PageClient {
1927
+ class PageClient extends BaseApiClient {
1590
1928
  /**
1591
1929
  * Creates a new PageClient instance.
1592
1930
  *
@@ -1611,10 +1949,7 @@ class PageClient {
1611
1949
  * ```
1612
1950
  */
1613
1951
  constructor(config, requestOptions, httpClient) {
1614
- this.requestOptions = requestOptions;
1615
- this.siteId = config.siteId || '';
1616
- this.dotcmsUrl = config.dotcmsUrl;
1617
- this.httpClient = httpClient;
1952
+ super(config, requestOptions, httpClient);
1618
1953
  }
1619
1954
  /**
1620
1955
  * Retrieves a page from DotCMS using GraphQL.
@@ -1779,6 +2114,7 @@ class DotCMSClient {
1779
2114
  this.page = new PageClient(this.config, this.requestOptions, this.httpClient);
1780
2115
  this.nav = new NavigationClient(this.config, this.requestOptions, this.httpClient);
1781
2116
  this.content = new Content(this.config, this.requestOptions, this.httpClient);
2117
+ this.ai = new AIClient(this.config, this.requestOptions, this.httpClient);
1782
2118
  }
1783
2119
  /**
1784
2120
  * Creates request options with authentication headers.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcms/client",
3
- "version": "1.2.0-next.2",
3
+ "version": "1.2.0-next.4",
4
4
  "description": "Official JavaScript library for interacting with DotCMS REST APIs.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,77 @@
1
+ import { DotCMSClientConfig, DotRequestOptions, DotHttpClient, DotCMSAISearchParams, DotCMSBasicContentlet } from '@dotcms/types';
2
+ import { AISearch } from './search/search';
3
+ import { BaseApiClient } from '../base/base-api';
4
+ /**
5
+ * Client for interacting with the DotCMS AI API.
6
+ * Provides methods to interact with AI features.
7
+ * @experimental This client is experimental and may be subject to change.
8
+ */
9
+ export declare class AIClient extends BaseApiClient {
10
+ /**
11
+ * Creates a new AIClient instance.
12
+ *
13
+ * @param {DotCMSClientConfig} config - Configuration options for the DotCMS client
14
+ * @param {DotRequestOptions} requestOptions - Options for fetch requests including authorization headers
15
+ * @param {DotHttpClient} httpClient - HTTP client for making requests
16
+ * @example
17
+ * ```typescript
18
+ * const aiClient = new AIClient(
19
+ * {
20
+ * dotcmsUrl: 'https://demo.dotcms.com',
21
+ * authToken: 'your-auth-token',
22
+ * siteId: 'demo.dotcms.com'
23
+ * },
24
+ * {
25
+ * headers: {
26
+ * Authorization: 'Bearer your-auth-token'
27
+ * }
28
+ * },
29
+ * httpClient
30
+ * );
31
+ * ```
32
+ */
33
+ constructor(config: DotCMSClientConfig, requestOptions: DotRequestOptions, httpClient: DotHttpClient);
34
+ /**
35
+ * Performs an AI-powered search.
36
+ *
37
+ * @param params - Search parameters with query and AI configuration
38
+ * @returns Promise with search results
39
+ * @experimental This method is experimental and may be subject to change.
40
+ * @template T - The type of the contentlet.
41
+ * @param prompt - The prompt for the search.
42
+ * @param indexName - The name of the index you want to search in.
43
+ * @param params - Search parameters with query and AI configuration.
44
+ * @example
45
+ * @example
46
+ * ```typescript
47
+ * const results = await client.ai.search('machine learning articles', 'content_index', {
48
+ * query: {
49
+ * limit: 20,
50
+ * contentType: 'BlogPost',
51
+ * languageId: "1" // or 1
52
+ * },
53
+ * config: {
54
+ * threshold: 0.7
55
+ * }
56
+ * });
57
+ * ```
58
+ * @example
59
+ * ```typescript
60
+ * client.ai.search('machine learning articles', 'content_index', {
61
+ * query: {
62
+ * limit: 20,
63
+ * contentType: 'BlogPost',
64
+ * languageId: "1" // or 1
65
+ * },
66
+ * config: {
67
+ * threshold: 0.7,
68
+ * distanceFunction: DISTANCE_FUNCTIONS.cosine
69
+ * }
70
+ * }).then((results) => {
71
+ * console.log(results);
72
+ * }).catch((error) => {
73
+ * console.error(error);
74
+ * });
75
+ */
76
+ search<T extends DotCMSBasicContentlet>(prompt: string, indexName: string, params?: DotCMSAISearchParams): AISearch<T>;
77
+ }
@@ -0,0 +1,65 @@
1
+ import { DotCMSAISearchParams, DotCMSBasicContentlet, DotCMSClientConfig, DotErrorAISearch, DotHttpClient, DotRequestOptions, DotCMSAISearchResponse } from '@dotcms/types';
2
+ import { BaseApiClient } from '../../base/base-api';
3
+ import { OnFullfilled, OnRejected } from '../shared/types';
4
+ /**
5
+ * Class for executing AI searches.
6
+ *
7
+ * @template T - The type of the contentlet.
8
+ * @param config - The configuration for the client.
9
+ * @param requestOptions - The request options for the client.
10
+ * @param httpClient - The HTTP client for the client.
11
+ * @param params - The parameters for the search.
12
+ * @param prompt - The prompt for the search.
13
+ */
14
+ export declare class AISearch<T extends DotCMSBasicContentlet> extends BaseApiClient {
15
+ #private;
16
+ constructor(config: DotCMSClientConfig, requestOptions: DotRequestOptions, httpClient: DotHttpClient, prompt: string, indexName: string, params?: DotCMSAISearchParams);
17
+ /**
18
+ * Executes the AI search and returns a promise with the search results.
19
+ *
20
+ * @param onfulfilled - Callback function to handle the search results.
21
+ * @param onrejected - Callback function to handle the search error.
22
+ * @returns Promise with the search results or the error.
23
+ * @example
24
+ * ```typescript
25
+ * const results = await client.ai.search('machine learning articles', 'content_index', {
26
+ * query: {
27
+ * limit: 20,
28
+ * contentType: 'BlogPost',
29
+ * languageId: '1' // or 1
30
+ * },
31
+ * config: {
32
+ * threshold: 0.7
33
+ * }
34
+ * });
35
+ * ```
36
+ * @example
37
+ * ```typescript
38
+ * client.ai.search('machine learning articles', 'content_index', {
39
+ * query: {
40
+ * limit: 20,
41
+ * contentType: 'BlogPost',
42
+ * languageId: '1' // or 1
43
+ * },
44
+ * config: {
45
+ * threshold: 0.7
46
+ * }
47
+ * }).then((results) => {
48
+ * console.log(results);
49
+ * }).catch((error) => {
50
+ * console.error(error);
51
+ * });
52
+ * ```
53
+ */
54
+ then(onfulfilled?: OnFullfilled<T>, onrejected?: OnRejected): Promise<DotCMSAISearchResponse<T> | DotErrorAISearch>;
55
+ private fetch;
56
+ /**
57
+ * Builds URLSearchParams from the SDK interface, mapping to backend parameter names.
58
+ * Only includes parameters that have values.
59
+ *
60
+ * @param params - Search parameters with SDK naming
61
+ * @returns URLSearchParams with backend parameter names
62
+ * @private
63
+ */
64
+ private buildSearchParams;
65
+ }