@dotcms/client 1.0.6-next.2 → 1.0.6-next.3

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.cjs.js CHANGED
@@ -45,7 +45,13 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
45
45
  * Fields that should not be formatted when sanitizing the query.
46
46
  * These fields are essential for maintaining the integrity of the content type.
47
47
  */
48
- const CONTENT_TYPE_MAIN_FIELDS = ['live', 'variant', 'contentType', 'languageId'];
48
+ const CONTENT_TYPE_MAIN_FIELDS = [
49
+ 'live',
50
+ 'variant',
51
+ 'contentType',
52
+ 'languageId',
53
+ 'conhost'
54
+ ];
49
55
  /**
50
56
  * URL endpoint for the content API search functionality.
51
57
  */
@@ -77,6 +83,58 @@ function sanitizeQueryForContentType(query, contentType) {
77
83
  : original; // Return the field if it is a content type field
78
84
  });
79
85
  }
86
+ /**
87
+ * @description
88
+ * Determines whether a site ID constraint should be added to a query based on existing constraints.
89
+ *
90
+ * The site ID constraint is added only when:
91
+ * - Query doesn't already contain a positive site constraint (+conhost)
92
+ * - Query doesn't explicitly exclude the specified site ID (-conhost:siteId)
93
+ * - Site ID is provided and configured
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * const query = '+contentType:Blog +languageId:1';
98
+ * const siteId = '123';
99
+ * const shouldAdd = shouldAddSiteIdConstraint(query, siteId); // true
100
+ * ```
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * const query = '+contentType:Blog -conhost:123';
105
+ * const siteId = '123';
106
+ * const shouldAdd = shouldAddSiteIdConstraint(query, siteId); // false (explicitly excluded)
107
+ * ```
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const query = '+contentType:Blog +conhost:456';
112
+ * const siteId = '123';
113
+ * const shouldAdd = shouldAddSiteIdConstraint(query, siteId); // false (already has constraint)
114
+ * ```
115
+ *
116
+ * @export
117
+ * @param {string} query - The Lucene query string to analyze
118
+ * @param {string | number | null | undefined} siteId - The site ID to check for
119
+ * @returns {boolean} True if site ID constraint should be added, false otherwise
120
+ */
121
+ function shouldAddSiteIdConstraint(query, siteId) {
122
+ // No site ID configured
123
+ if (!siteId) {
124
+ return false;
125
+ }
126
+ // Query already contains a positive site constraint
127
+ const hasExistingSiteConstraint = /\+conhost/gi.test(query);
128
+ if (hasExistingSiteConstraint) {
129
+ return false;
130
+ }
131
+ // Query explicitly excludes this specific site ID
132
+ const hasThisSiteIdExclusion = new RegExp(`-conhost:${siteId}`, 'gi').test(query);
133
+ if (hasThisSiteIdExclusion) {
134
+ return false;
135
+ }
136
+ return true;
137
+ }
80
138
 
81
139
  var _Field_query;
82
140
  /**
@@ -579,7 +637,7 @@ class QueryBuilder {
579
637
  }
580
638
  _QueryBuilder_query = new WeakMap();
581
639
 
582
- 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;
640
+ 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_config;
583
641
  /**
584
642
  * Creates a Builder to filter and fetch content from the content API for a specific content type.
585
643
  *
@@ -591,11 +649,11 @@ class CollectionBuilder {
591
649
  /**
592
650
  * Creates an instance of CollectionBuilder.
593
651
  * @param {ClientOptions} requestOptions Options for the client request.
594
- * @param {string} serverUrl The server URL.
652
+ * @param {DotCMSClientConfig} config The client configuration.
595
653
  * @param {string} contentType The content type to fetch.
596
654
  * @memberof CollectionBuilder
597
655
  */
598
- constructor(requestOptions, serverUrl, contentType) {
656
+ constructor(requestOptions, config, contentType) {
599
657
  _CollectionBuilder_page.set(this, 1);
600
658
  _CollectionBuilder_limit.set(this, 10);
601
659
  _CollectionBuilder_depth.set(this, 0);
@@ -607,10 +665,10 @@ class CollectionBuilder {
607
665
  _CollectionBuilder_rawQuery.set(this, void 0);
608
666
  _CollectionBuilder_languageId.set(this, 1);
609
667
  _CollectionBuilder_draft.set(this, false);
610
- _CollectionBuilder_serverUrl.set(this, void 0);
611
668
  _CollectionBuilder_requestOptions.set(this, void 0);
669
+ _CollectionBuilder_config.set(this, void 0);
612
670
  __classPrivateFieldSet(this, _CollectionBuilder_requestOptions, requestOptions, "f");
613
- __classPrivateFieldSet(this, _CollectionBuilder_serverUrl, serverUrl, "f");
671
+ __classPrivateFieldSet(this, _CollectionBuilder_config, config, "f");
614
672
  __classPrivateFieldSet(this, _CollectionBuilder_contentType, contentType, "f");
615
673
  // Build the default query with the contentType field
616
674
  __classPrivateFieldSet(this, _CollectionBuilder_defaultQuery, new QueryBuilder().field('contentType').equals(__classPrivateFieldGet(this, _CollectionBuilder_contentType, "f")), "f");
@@ -643,7 +701,17 @@ class CollectionBuilder {
643
701
  * @memberof CollectionBuilder
644
702
  */
645
703
  get url() {
646
- return `${__classPrivateFieldGet(this, _CollectionBuilder_serverUrl, "f")}${CONTENT_API_URL}`;
704
+ return `${__classPrivateFieldGet(this, _CollectionBuilder_config, "f").dotcmsUrl}${CONTENT_API_URL}`;
705
+ }
706
+ /**
707
+ * Returns the site ID from the configuration.
708
+ *
709
+ * @readonly
710
+ * @private
711
+ * @memberof CollectionBuilder
712
+ */
713
+ get siteId() {
714
+ return __classPrivateFieldGet(this, _CollectionBuilder_config, "f").siteId;
647
715
  }
648
716
  /**
649
717
  * Returns the current query built.
@@ -881,12 +949,7 @@ class CollectionBuilder {
881
949
  * @memberof CollectionBuilder
882
950
  */
883
951
  fetch() {
884
- const finalQuery = this.currentQuery
885
- .field('languageId')
886
- .equals(__classPrivateFieldGet(this, _CollectionBuilder_languageId, "f").toString())
887
- .field('live')
888
- .equals((!__classPrivateFieldGet(this, _CollectionBuilder_draft, "f")).toString())
889
- .build();
952
+ const finalQuery = this.getFinalQuery();
890
953
  const sanitizedQuery = sanitizeQueryForContentType(finalQuery, __classPrivateFieldGet(this, _CollectionBuilder_contentType, "f"));
891
954
  const query = __classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f") ? `${sanitizedQuery} ${__classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f")}` : sanitizedQuery;
892
955
  return fetch(this.url, {
@@ -908,10 +971,62 @@ class CollectionBuilder {
908
971
  })
909
972
  });
910
973
  }
974
+ /**
975
+ * Builds the final Lucene query string by combining the base query with required system constraints.
976
+ *
977
+ * This method constructs the complete query by:
978
+ * 1. Adding language ID filter to ensure content matches the specified language
979
+ * 2. Adding live/draft status filter based on the draft flag
980
+ * 3. Optionally adding site ID constraint if conditions are met
981
+ *
982
+ * Site ID constraint is added only when:
983
+ * - Query doesn't already contain a positive site constraint (+conhost)
984
+ * - Query doesn't explicitly exclude the current site ID (-conhost:currentSiteId)
985
+ * - Site ID is configured in the system
986
+ *
987
+ * @private
988
+ * @returns {string} The complete Lucene query string ready for the Content API
989
+ * @memberof CollectionBuilder
990
+ *
991
+ * @example
992
+ * // For live content in language 1 with site ID 123:
993
+ * // Returns: "+contentType:Blog +languageId:1 +live:true +conhost:123"
994
+ *
995
+ * @example
996
+ * // For draft content without site constraint:
997
+ * // Returns: "+contentType:Blog +languageId:1 +live:false"
998
+ *
999
+ * @example
1000
+ * // For content with explicit exclusion of current site (site ID 123):
1001
+ * // Query: "+contentType:Blog -conhost:123"
1002
+ * // Returns: "+contentType:Blog -conhost:123 +languageId:1 +live:true" (no site ID added)
1003
+ *
1004
+ * @example
1005
+ * // For content with exclusion of different site (site ID 456, current is 123):
1006
+ * // Query: "+contentType:Blog -conhost:456"
1007
+ * // Returns: "+contentType:Blog -conhost:456 +languageId:1 +live:true +conhost:123" (site ID still added)
1008
+ */
1009
+ getFinalQuery() {
1010
+ // Build base query with language and live/draft constraints
1011
+ const baseQuery = this.currentQuery
1012
+ .field('languageId')
1013
+ .equals(__classPrivateFieldGet(this, _CollectionBuilder_languageId, "f").toString())
1014
+ .field('live')
1015
+ .equals((!__classPrivateFieldGet(this, _CollectionBuilder_draft, "f")).toString())
1016
+ .build();
1017
+ // Check if site ID constraint should be added using utility function
1018
+ const shouldAddSiteId = shouldAddSiteIdConstraint(baseQuery, this.siteId);
1019
+ // Add site ID constraint if needed
1020
+ if (shouldAddSiteId) {
1021
+ const queryWithSiteId = `${baseQuery} +conhost:${this.siteId}`;
1022
+ return sanitizeQuery(queryWithSiteId);
1023
+ }
1024
+ return baseQuery;
1025
+ }
911
1026
  }
912
- _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();
1027
+ _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_config = new WeakMap();
913
1028
 
914
- var _Content_requestOptions, _Content_serverUrl;
1029
+ var _Content_requestOptions, _Content_config;
915
1030
  /**
916
1031
  * Creates a builder to filter and fetch a collection of content items.
917
1032
  * @param contentType - The content type to retrieve.
@@ -967,11 +1082,11 @@ class Content {
967
1082
  * @param {RequestOptions} requestOptions - The options for the client request.
968
1083
  * @param {string} serverUrl - The server URL.
969
1084
  */
970
- constructor(requestOptions, serverUrl) {
1085
+ constructor(config, requestOptions) {
971
1086
  _Content_requestOptions.set(this, void 0);
972
- _Content_serverUrl.set(this, void 0);
1087
+ _Content_config.set(this, void 0);
973
1088
  __classPrivateFieldSet(this, _Content_requestOptions, requestOptions, "f");
974
- __classPrivateFieldSet(this, _Content_serverUrl, serverUrl, "f");
1089
+ __classPrivateFieldSet(this, _Content_config, config, "f");
975
1090
  }
976
1091
  /**
977
1092
  * Takes a content type and returns a builder to filter and fetch the collection.
@@ -1039,10 +1154,10 @@ class Content {
1039
1154
  *
1040
1155
  */
1041
1156
  getCollection(contentType) {
1042
- return new CollectionBuilder(__classPrivateFieldGet(this, _Content_requestOptions, "f"), __classPrivateFieldGet(this, _Content_serverUrl, "f"), contentType);
1157
+ return new CollectionBuilder(__classPrivateFieldGet(this, _Content_requestOptions, "f"), __classPrivateFieldGet(this, _Content_config, "f"), contentType);
1043
1158
  }
1044
1159
  }
1045
- _Content_requestOptions = new WeakMap(), _Content_serverUrl = new WeakMap();
1160
+ _Content_requestOptions = new WeakMap(), _Content_config = new WeakMap();
1046
1161
 
1047
1162
  class NavigationClient {
1048
1163
  constructor(config, requestOptions) {
@@ -1538,7 +1653,7 @@ class DotCMSClient {
1538
1653
  // Initialize clients
1539
1654
  this.page = new PageClient(this.config, this.requestOptions);
1540
1655
  this.nav = new NavigationClient(this.config, this.requestOptions);
1541
- this.content = new Content(this.requestOptions, this.config.dotcmsUrl);
1656
+ this.content = new Content(this.config, this.requestOptions);
1542
1657
  }
1543
1658
  /**
1544
1659
  * Creates request options with authentication headers.
package/index.esm.js CHANGED
@@ -43,7 +43,13 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
43
43
  * Fields that should not be formatted when sanitizing the query.
44
44
  * These fields are essential for maintaining the integrity of the content type.
45
45
  */
46
- const CONTENT_TYPE_MAIN_FIELDS = ['live', 'variant', 'contentType', 'languageId'];
46
+ const CONTENT_TYPE_MAIN_FIELDS = [
47
+ 'live',
48
+ 'variant',
49
+ 'contentType',
50
+ 'languageId',
51
+ 'conhost'
52
+ ];
47
53
  /**
48
54
  * URL endpoint for the content API search functionality.
49
55
  */
@@ -75,6 +81,58 @@ function sanitizeQueryForContentType(query, contentType) {
75
81
  : original; // Return the field if it is a content type field
76
82
  });
77
83
  }
84
+ /**
85
+ * @description
86
+ * Determines whether a site ID constraint should be added to a query based on existing constraints.
87
+ *
88
+ * The site ID constraint is added only when:
89
+ * - Query doesn't already contain a positive site constraint (+conhost)
90
+ * - Query doesn't explicitly exclude the specified site ID (-conhost:siteId)
91
+ * - Site ID is provided and configured
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * const query = '+contentType:Blog +languageId:1';
96
+ * const siteId = '123';
97
+ * const shouldAdd = shouldAddSiteIdConstraint(query, siteId); // true
98
+ * ```
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * const query = '+contentType:Blog -conhost:123';
103
+ * const siteId = '123';
104
+ * const shouldAdd = shouldAddSiteIdConstraint(query, siteId); // false (explicitly excluded)
105
+ * ```
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * const query = '+contentType:Blog +conhost:456';
110
+ * const siteId = '123';
111
+ * const shouldAdd = shouldAddSiteIdConstraint(query, siteId); // false (already has constraint)
112
+ * ```
113
+ *
114
+ * @export
115
+ * @param {string} query - The Lucene query string to analyze
116
+ * @param {string | number | null | undefined} siteId - The site ID to check for
117
+ * @returns {boolean} True if site ID constraint should be added, false otherwise
118
+ */
119
+ function shouldAddSiteIdConstraint(query, siteId) {
120
+ // No site ID configured
121
+ if (!siteId) {
122
+ return false;
123
+ }
124
+ // Query already contains a positive site constraint
125
+ const hasExistingSiteConstraint = /\+conhost/gi.test(query);
126
+ if (hasExistingSiteConstraint) {
127
+ return false;
128
+ }
129
+ // Query explicitly excludes this specific site ID
130
+ const hasThisSiteIdExclusion = new RegExp(`-conhost:${siteId}`, 'gi').test(query);
131
+ if (hasThisSiteIdExclusion) {
132
+ return false;
133
+ }
134
+ return true;
135
+ }
78
136
 
79
137
  var _Field_query;
80
138
  /**
@@ -577,7 +635,7 @@ class QueryBuilder {
577
635
  }
578
636
  _QueryBuilder_query = new WeakMap();
579
637
 
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;
638
+ 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_config;
581
639
  /**
582
640
  * Creates a Builder to filter and fetch content from the content API for a specific content type.
583
641
  *
@@ -589,11 +647,11 @@ class CollectionBuilder {
589
647
  /**
590
648
  * Creates an instance of CollectionBuilder.
591
649
  * @param {ClientOptions} requestOptions Options for the client request.
592
- * @param {string} serverUrl The server URL.
650
+ * @param {DotCMSClientConfig} config The client configuration.
593
651
  * @param {string} contentType The content type to fetch.
594
652
  * @memberof CollectionBuilder
595
653
  */
596
- constructor(requestOptions, serverUrl, contentType) {
654
+ constructor(requestOptions, config, contentType) {
597
655
  _CollectionBuilder_page.set(this, 1);
598
656
  _CollectionBuilder_limit.set(this, 10);
599
657
  _CollectionBuilder_depth.set(this, 0);
@@ -605,10 +663,10 @@ class CollectionBuilder {
605
663
  _CollectionBuilder_rawQuery.set(this, void 0);
606
664
  _CollectionBuilder_languageId.set(this, 1);
607
665
  _CollectionBuilder_draft.set(this, false);
608
- _CollectionBuilder_serverUrl.set(this, void 0);
609
666
  _CollectionBuilder_requestOptions.set(this, void 0);
667
+ _CollectionBuilder_config.set(this, void 0);
610
668
  __classPrivateFieldSet(this, _CollectionBuilder_requestOptions, requestOptions, "f");
611
- __classPrivateFieldSet(this, _CollectionBuilder_serverUrl, serverUrl, "f");
669
+ __classPrivateFieldSet(this, _CollectionBuilder_config, config, "f");
612
670
  __classPrivateFieldSet(this, _CollectionBuilder_contentType, contentType, "f");
613
671
  // Build the default query with the contentType field
614
672
  __classPrivateFieldSet(this, _CollectionBuilder_defaultQuery, new QueryBuilder().field('contentType').equals(__classPrivateFieldGet(this, _CollectionBuilder_contentType, "f")), "f");
@@ -641,7 +699,17 @@ class CollectionBuilder {
641
699
  * @memberof CollectionBuilder
642
700
  */
643
701
  get url() {
644
- return `${__classPrivateFieldGet(this, _CollectionBuilder_serverUrl, "f")}${CONTENT_API_URL}`;
702
+ return `${__classPrivateFieldGet(this, _CollectionBuilder_config, "f").dotcmsUrl}${CONTENT_API_URL}`;
703
+ }
704
+ /**
705
+ * Returns the site ID from the configuration.
706
+ *
707
+ * @readonly
708
+ * @private
709
+ * @memberof CollectionBuilder
710
+ */
711
+ get siteId() {
712
+ return __classPrivateFieldGet(this, _CollectionBuilder_config, "f").siteId;
645
713
  }
646
714
  /**
647
715
  * Returns the current query built.
@@ -879,12 +947,7 @@ class CollectionBuilder {
879
947
  * @memberof CollectionBuilder
880
948
  */
881
949
  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();
950
+ const finalQuery = this.getFinalQuery();
888
951
  const sanitizedQuery = sanitizeQueryForContentType(finalQuery, __classPrivateFieldGet(this, _CollectionBuilder_contentType, "f"));
889
952
  const query = __classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f") ? `${sanitizedQuery} ${__classPrivateFieldGet(this, _CollectionBuilder_rawQuery, "f")}` : sanitizedQuery;
890
953
  return fetch(this.url, {
@@ -906,10 +969,62 @@ class CollectionBuilder {
906
969
  })
907
970
  });
908
971
  }
972
+ /**
973
+ * Builds the final Lucene query string by combining the base query with required system constraints.
974
+ *
975
+ * This method constructs the complete query by:
976
+ * 1. Adding language ID filter to ensure content matches the specified language
977
+ * 2. Adding live/draft status filter based on the draft flag
978
+ * 3. Optionally adding site ID constraint if conditions are met
979
+ *
980
+ * Site ID constraint is added only when:
981
+ * - Query doesn't already contain a positive site constraint (+conhost)
982
+ * - Query doesn't explicitly exclude the current site ID (-conhost:currentSiteId)
983
+ * - Site ID is configured in the system
984
+ *
985
+ * @private
986
+ * @returns {string} The complete Lucene query string ready for the Content API
987
+ * @memberof CollectionBuilder
988
+ *
989
+ * @example
990
+ * // For live content in language 1 with site ID 123:
991
+ * // Returns: "+contentType:Blog +languageId:1 +live:true +conhost:123"
992
+ *
993
+ * @example
994
+ * // For draft content without site constraint:
995
+ * // Returns: "+contentType:Blog +languageId:1 +live:false"
996
+ *
997
+ * @example
998
+ * // For content with explicit exclusion of current site (site ID 123):
999
+ * // Query: "+contentType:Blog -conhost:123"
1000
+ * // Returns: "+contentType:Blog -conhost:123 +languageId:1 +live:true" (no site ID added)
1001
+ *
1002
+ * @example
1003
+ * // For content with exclusion of different site (site ID 456, current is 123):
1004
+ * // Query: "+contentType:Blog -conhost:456"
1005
+ * // Returns: "+contentType:Blog -conhost:456 +languageId:1 +live:true +conhost:123" (site ID still added)
1006
+ */
1007
+ getFinalQuery() {
1008
+ // Build base query with language and live/draft constraints
1009
+ const baseQuery = this.currentQuery
1010
+ .field('languageId')
1011
+ .equals(__classPrivateFieldGet(this, _CollectionBuilder_languageId, "f").toString())
1012
+ .field('live')
1013
+ .equals((!__classPrivateFieldGet(this, _CollectionBuilder_draft, "f")).toString())
1014
+ .build();
1015
+ // Check if site ID constraint should be added using utility function
1016
+ const shouldAddSiteId = shouldAddSiteIdConstraint(baseQuery, this.siteId);
1017
+ // Add site ID constraint if needed
1018
+ if (shouldAddSiteId) {
1019
+ const queryWithSiteId = `${baseQuery} +conhost:${this.siteId}`;
1020
+ return sanitizeQuery(queryWithSiteId);
1021
+ }
1022
+ return baseQuery;
1023
+ }
909
1024
  }
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();
1025
+ _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_config = new WeakMap();
911
1026
 
912
- var _Content_requestOptions, _Content_serverUrl;
1027
+ var _Content_requestOptions, _Content_config;
913
1028
  /**
914
1029
  * Creates a builder to filter and fetch a collection of content items.
915
1030
  * @param contentType - The content type to retrieve.
@@ -965,11 +1080,11 @@ class Content {
965
1080
  * @param {RequestOptions} requestOptions - The options for the client request.
966
1081
  * @param {string} serverUrl - The server URL.
967
1082
  */
968
- constructor(requestOptions, serverUrl) {
1083
+ constructor(config, requestOptions) {
969
1084
  _Content_requestOptions.set(this, void 0);
970
- _Content_serverUrl.set(this, void 0);
1085
+ _Content_config.set(this, void 0);
971
1086
  __classPrivateFieldSet(this, _Content_requestOptions, requestOptions, "f");
972
- __classPrivateFieldSet(this, _Content_serverUrl, serverUrl, "f");
1087
+ __classPrivateFieldSet(this, _Content_config, config, "f");
973
1088
  }
974
1089
  /**
975
1090
  * Takes a content type and returns a builder to filter and fetch the collection.
@@ -1037,10 +1152,10 @@ class Content {
1037
1152
  *
1038
1153
  */
1039
1154
  getCollection(contentType) {
1040
- return new CollectionBuilder(__classPrivateFieldGet(this, _Content_requestOptions, "f"), __classPrivateFieldGet(this, _Content_serverUrl, "f"), contentType);
1155
+ return new CollectionBuilder(__classPrivateFieldGet(this, _Content_requestOptions, "f"), __classPrivateFieldGet(this, _Content_config, "f"), contentType);
1041
1156
  }
1042
1157
  }
1043
- _Content_requestOptions = new WeakMap(), _Content_serverUrl = new WeakMap();
1158
+ _Content_requestOptions = new WeakMap(), _Content_config = new WeakMap();
1044
1159
 
1045
1160
  class NavigationClient {
1046
1161
  constructor(config, requestOptions) {
@@ -1536,7 +1651,7 @@ class DotCMSClient {
1536
1651
  // Initialize clients
1537
1652
  this.page = new PageClient(this.config, this.requestOptions);
1538
1653
  this.nav = new NavigationClient(this.config, this.requestOptions);
1539
- this.content = new Content(this.requestOptions, this.config.dotcmsUrl);
1654
+ this.content = new Content(this.config, this.requestOptions);
1540
1655
  }
1541
1656
  /**
1542
1657
  * Creates request options with authentication headers.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcms/client",
3
- "version": "1.0.6-next.2",
3
+ "version": "1.0.6-next.3",
4
4
  "description": "Official JavaScript library for interacting with DotCMS REST APIs.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,3 +1,4 @@
1
+ import { DotCMSClientConfig } from '@dotcms/types';
1
2
  import { GetCollectionResponse, BuildQuery, SortBy, GetCollectionError, OnFullfilled, OnRejected } from '../../shared/types';
2
3
  export type ClientOptions = Omit<RequestInit, 'body' | 'method'>;
3
4
  /**
@@ -12,11 +13,11 @@ export declare class CollectionBuilder<T = unknown> {
12
13
  /**
13
14
  * Creates an instance of CollectionBuilder.
14
15
  * @param {ClientOptions} requestOptions Options for the client request.
15
- * @param {string} serverUrl The server URL.
16
+ * @param {DotCMSClientConfig} config The client configuration.
16
17
  * @param {string} contentType The content type to fetch.
17
18
  * @memberof CollectionBuilder
18
19
  */
19
- constructor(requestOptions: ClientOptions, serverUrl: string, contentType: string);
20
+ constructor(requestOptions: ClientOptions, config: DotCMSClientConfig, contentType: string);
20
21
  /**
21
22
  * Returns the sort query in the format: field order, field order, ...
22
23
  *
@@ -41,6 +42,14 @@ export declare class CollectionBuilder<T = unknown> {
41
42
  * @memberof CollectionBuilder
42
43
  */
43
44
  private get url();
45
+ /**
46
+ * Returns the site ID from the configuration.
47
+ *
48
+ * @readonly
49
+ * @private
50
+ * @memberof CollectionBuilder
51
+ */
52
+ private get siteId();
44
53
  /**
45
54
  * Returns the current query built.
46
55
  *
@@ -223,4 +232,40 @@ export declare class CollectionBuilder<T = unknown> {
223
232
  * @memberof CollectionBuilder
224
233
  */
225
234
  private fetch;
235
+ /**
236
+ * Builds the final Lucene query string by combining the base query with required system constraints.
237
+ *
238
+ * This method constructs the complete query by:
239
+ * 1. Adding language ID filter to ensure content matches the specified language
240
+ * 2. Adding live/draft status filter based on the draft flag
241
+ * 3. Optionally adding site ID constraint if conditions are met
242
+ *
243
+ * Site ID constraint is added only when:
244
+ * - Query doesn't already contain a positive site constraint (+conhost)
245
+ * - Query doesn't explicitly exclude the current site ID (-conhost:currentSiteId)
246
+ * - Site ID is configured in the system
247
+ *
248
+ * @private
249
+ * @returns {string} The complete Lucene query string ready for the Content API
250
+ * @memberof CollectionBuilder
251
+ *
252
+ * @example
253
+ * // For live content in language 1 with site ID 123:
254
+ * // Returns: "+contentType:Blog +languageId:1 +live:true +conhost:123"
255
+ *
256
+ * @example
257
+ * // For draft content without site constraint:
258
+ * // Returns: "+contentType:Blog +languageId:1 +live:false"
259
+ *
260
+ * @example
261
+ * // For content with explicit exclusion of current site (site ID 123):
262
+ * // Query: "+contentType:Blog -conhost:123"
263
+ * // Returns: "+contentType:Blog -conhost:123 +languageId:1 +live:true" (no site ID added)
264
+ *
265
+ * @example
266
+ * // For content with exclusion of different site (site ID 456, current is 123):
267
+ * // Query: "+contentType:Blog -conhost:456"
268
+ * // Returns: "+contentType:Blog -conhost:456 +languageId:1 +live:true +conhost:123" (site ID still added)
269
+ */
270
+ private getFinalQuery;
226
271
  }
@@ -1,4 +1,4 @@
1
- import { RequestOptions } from '@dotcms/types';
1
+ import { DotCMSClientConfig, RequestOptions } from '@dotcms/types';
2
2
  import { CollectionBuilder } from './builders/collection/collection';
3
3
  /**
4
4
  * Creates a builder to filter and fetch a collection of content items.
@@ -56,7 +56,7 @@ export declare class Content {
56
56
  * @param {RequestOptions} requestOptions - The options for the client request.
57
57
  * @param {string} serverUrl - The server URL.
58
58
  */
59
- constructor(requestOptions: RequestOptions, serverUrl: string);
59
+ constructor(config: DotCMSClientConfig, requestOptions: RequestOptions);
60
60
  /**
61
61
  * Takes a content type and returns a builder to filter and fetch the collection.
62
62
  * @param {string} contentType - The content type to get the collection.
@@ -18,3 +18,39 @@
18
18
  * @returns {string} The sanitized query string.
19
19
  */
20
20
  export declare function sanitizeQueryForContentType(query: string, contentType: string): string;
21
+ /**
22
+ * @description
23
+ * Determines whether a site ID constraint should be added to a query based on existing constraints.
24
+ *
25
+ * The site ID constraint is added only when:
26
+ * - Query doesn't already contain a positive site constraint (+conhost)
27
+ * - Query doesn't explicitly exclude the specified site ID (-conhost:siteId)
28
+ * - Site ID is provided and configured
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * const query = '+contentType:Blog +languageId:1';
33
+ * const siteId = '123';
34
+ * const shouldAdd = shouldAddSiteIdConstraint(query, siteId); // true
35
+ * ```
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * const query = '+contentType:Blog -conhost:123';
40
+ * const siteId = '123';
41
+ * const shouldAdd = shouldAddSiteIdConstraint(query, siteId); // false (explicitly excluded)
42
+ * ```
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * const query = '+contentType:Blog +conhost:456';
47
+ * const siteId = '123';
48
+ * const shouldAdd = shouldAddSiteIdConstraint(query, siteId); // false (already has constraint)
49
+ * ```
50
+ *
51
+ * @export
52
+ * @param {string} query - The Lucene query string to analyze
53
+ * @param {string | number | null | undefined} siteId - The site ID to check for
54
+ * @returns {boolean} True if site ID constraint should be added, false otherwise
55
+ */
56
+ export declare function shouldAddSiteIdConstraint(query: string, siteId: string | number | null | undefined): boolean;