@dotcms/client 0.0.1-alpha.37 → 0.0.1-alpha.38

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcms/client",
3
- "version": "0.0.1-alpha.37",
3
+ "version": "0.0.1-alpha.38",
4
4
  "description": "Official JavaScript library for interacting with DotCMS REST APIs.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,27 +1,48 @@
1
1
  import { ClientOptions } from '../../../sdk-js-client';
2
2
  import { GetCollectionResponse, BuildQuery, SortBy, GetCollectionError, OnFullfilled, OnRejected } from '../../shared/types';
3
3
  /**
4
- * Creates a Builder to filter and fetch content from the content API for an specific content type
4
+ * Creates a Builder to filter and fetch content from the content API for a specific content type.
5
5
  *
6
6
  * @export
7
7
  * @class CollectionBuilder
8
- * @template T Represents the type of the content type to fetch. Defaults to unknown
8
+ * @template T Represents the type of the content type to fetch. Defaults to unknown.
9
9
  */
10
10
  export declare class CollectionBuilder<T = unknown> {
11
11
  #private;
12
+ /**
13
+ * Creates an instance of CollectionBuilder.
14
+ * @param {ClientOptions} requestOptions Options for the client request.
15
+ * @param {string} serverUrl The server URL.
16
+ * @param {string} contentType The content type to fetch.
17
+ * @memberof CollectionBuilder
18
+ */
12
19
  constructor(requestOptions: ClientOptions, serverUrl: string, contentType: string);
13
20
  /**
14
- * This method returns the sort query in this format: field order, field order, ...
21
+ * Returns the sort query in the format: field order, field order, ...
15
22
  *
16
23
  * @readonly
17
24
  * @private
18
25
  * @memberof CollectionBuilder
19
26
  */
20
27
  private get sort();
28
+ /**
29
+ * Returns the offset for pagination.
30
+ *
31
+ * @readonly
32
+ * @private
33
+ * @memberof CollectionBuilder
34
+ */
21
35
  private get offset();
36
+ /**
37
+ * Returns the full URL for the content API.
38
+ *
39
+ * @readonly
40
+ * @private
41
+ * @memberof CollectionBuilder
42
+ */
22
43
  private get url();
23
44
  /**
24
- * This method returns the current query built
45
+ * Returns the current query built.
25
46
  *
26
47
  * @readonly
27
48
  * @private
@@ -29,120 +50,177 @@ export declare class CollectionBuilder<T = unknown> {
29
50
  */
30
51
  private get currentQuery();
31
52
  /**
32
- * Takes a language id and filters the content by that language.
33
- *
34
- * The language id defaults to 1
53
+ * Filters the content by the specified language ID.
35
54
  *
36
- *
37
- * @param {number | string} languageId The language id to filter the content by
38
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
55
+ * @example
56
+ * ```typescript
57
+ * const client = new DotCMSClient(config);
58
+ * const collectionBuilder = client.content.getCollection("Blog");
59
+ * collectionBuilder.language(1);
60
+ * ```
61
+ *
62
+ * @param {number | string} languageId The language ID to filter the content by.
63
+ * @return {CollectionBuilder} A CollectionBuilder instance.
39
64
  * @memberof CollectionBuilder
40
65
  */
41
66
  language(languageId: number | string): this;
42
67
  /**
43
- * The retrieved content will have the rendered HTML
68
+ * Setting this to true will server side render (using velocity) any widgets that are returned by the content query.
69
+ *
70
+ * More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
44
71
  *
45
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
72
+ * @return {CollectionBuilder} A CollectionBuilder instance.
46
73
  * @memberof CollectionBuilder
47
74
  */
48
75
  render(): this;
49
76
  /**
50
- * Takes an array of constrains to sort the content by field an specific order
77
+ * Sorts the content by the specified fields and orders.
51
78
  *
52
79
  * @example
53
- * ```javascript
54
- * // This will sort the content by title in ascending order
55
- * // and by modDate in descending order
56
- * const sortBy = [{ field: 'title', order: 'asc' }, { field: 'modDate', order: 'desc' }]
57
- *
58
- * client.content.getCollection("Blog").sortBy(sortBy)
59
- *```
60
- *
61
- * @param {SortBy[]} sortBy Array of constrains to sort the content by
62
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
80
+ * ```typescript
81
+ * const client = new DotCMSClient(config);
82
+ * const collectionBuilder = client.content.getCollection("Blog");
83
+ * const sortBy = [{ field: 'title', order: 'asc' }, { field: 'modDate', order: 'desc' }];
84
+ * collectionBuilder("Blog").sortBy(sortBy);
85
+ * ```
86
+ *
87
+ * @param {SortBy[]} sortBy Array of constraints to sort the content by.
88
+ * @return {CollectionBuilder} A CollectionBuilder instance.
63
89
  * @memberof CollectionBuilder
64
90
  */
65
91
  sortBy(sortBy: SortBy[]): this;
66
92
  /**
67
- * Takes a number that represents the max amount of content to fetch
93
+ * Sets the maximum amount of content to fetch.
68
94
  *
69
- * `limit` is set to 10 by default
70
- *
71
- * @param {number} limit The max amount of content to fetch
72
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
95
+ * @param {number} limit The maximum amount of content to fetch.
96
+ * @return {CollectionBuilder} A CollectionBuilder instance.
73
97
  * @memberof CollectionBuilder
74
98
  */
75
99
  limit(limit: number): this;
76
100
  /**
77
- * Takes a number that represents the page to fetch
101
+ * Sets the page number to fetch.
78
102
  *
79
- * @param {number} page The page to fetch
80
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
103
+ * @param {number} page The page number to fetch.
104
+ * @return {CollectionBuilder} A CollectionBuilder instance.
81
105
  * @memberof CollectionBuilder
82
106
  */
83
107
  page(page: number): this;
84
108
  /**
85
- * Takes a string that represents a {@link https://www.dotcms.com/docs/latest/content-search-syntax#Lucene Lucene Query} that is used to filter the content to fetch.
86
- *
87
- * The string is not validated, so be cautious when using it.
109
+ * Filters the content by a Lucene query string.
88
110
  *
89
- * @param {string} query A {@link https://www.dotcms.com/docs/latest/content-search-syntax#Lucene Lucene Query} String
90
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
111
+ * @param {string} query A Lucene query string.
112
+ * @return {CollectionBuilder} A CollectionBuilder instance.
91
113
  * @memberof CollectionBuilder
92
114
  */
93
115
  query(query: string): this;
94
116
  /**
95
- * Takes a function that recieves a QueryBuilder to buid a query for content filtering.
117
+ * Filters the content by building a query using a QueryBuilder function.
118
+ *
96
119
  * @example
97
- *```javascript
98
- * // This will filter the content by title equals 'Hello World' or 'Hello World 2'
99
- * client.content.getCollection("Activity").query((queryBuilder) =>
120
+ * ```typescript
121
+ * const client = new DotCMSClient(config);
122
+ * const collectionBuilder = client.content.getCollection("Blog");
123
+ * collectionBuilder.query((queryBuilder) =>
100
124
  * queryBuilder.field('title').equals('Hello World').or().equals('Hello World 2')
101
125
  * );
102
- *```
103
- * @param {BuildQuery} buildQuery A function that receives a QueryBuilder instance and returns a valid query
104
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
126
+ * ```
127
+ *
128
+ * @param {BuildQuery} buildQuery A function that receives a QueryBuilder instance and returns a valid query.
129
+ * @return {CollectionBuilder} A CollectionBuilder instance.
105
130
  * @memberof CollectionBuilder
106
131
  */
107
132
  query(buildQuery: BuildQuery): this;
108
133
  /**
109
- * The retrieved content will be draft content
110
- *
111
- * The default value is false to fetch content that is not on draft
112
- *
113
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
134
+ * Retrieves draft content.
135
+ * @example
136
+ * ```ts
137
+ * const client = new DotCMSClient(config);
138
+ * const collectionBuilder = client.content.getCollection("Blog");
139
+ * collectionBuilder
140
+ * .draft() // This will retrieve draft/working content
141
+ * .then((response) => // Your code here })
142
+ * .catch((error) => // Your code here })
143
+ * ```
144
+ *
145
+ * @return {CollectionBuilder} A CollectionBuilder instance.
114
146
  * @memberof CollectionBuilder
115
147
  */
116
148
  draft(): this;
117
149
  /**
118
- * Takes a string that represents a variant ID of content created with the {@link https://www.dotcms.com/docs/latest/experiments-and-a-b-testing A/B Testing} feature
150
+ * Filters the content by a variant ID for [Experiments](https://www.dotcms.com/docs/latest/experiments-and-a-b-testing)
119
151
  *
120
- * `variantId` defaults to "DEFAULT" to fetch content that is not part of an A/B test
152
+ * More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
121
153
  *
122
- * @param {string} variantId A string that represents a variant ID
123
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
154
+ * @example
155
+ * ```ts
156
+ * const client = new DotCMSClient(config);
157
+ * const collectionBuilder = client.content.getCollection("Blog");
158
+ * collectionBuilder
159
+ * .variant("YOUR_VARIANT_ID")
160
+ * .then((response) => // Your code here })
161
+ * .catch((error) => // Your code here })
162
+ * ```
163
+ *
164
+ * @param {string} variantId A string that represents a variant ID.
165
+ * @return {CollectionBuilder} A CollectionBuilder instance.
124
166
  * @memberof CollectionBuilder
125
167
  */
126
168
  variant(variantId: string): this;
127
169
  /**
128
- * Takes a number that represents the depth of the relationships of a content
170
+ * Sets the depth of the relationships of the content.
171
+ * Specifies the depth of related content to return in the results.
129
172
  *
130
- * The `depth` is set to 0 by default and the max supported value is 3.
173
+ * More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
131
174
  *
132
- * @param {number} depth The depth of the relationships of a content
133
- * @return {CollectionBuilder} CollectionBuilder - A CollectionBuilder instance
175
+ * @example
176
+ * ```ts
177
+ * const client = new DotCMSClient(config);
178
+ * const collectionBuilder = client.content.getCollection("Blog");
179
+ * collectionBuilder
180
+ * .depth(1)
181
+ * .then((response) => // Your code here })
182
+ * .catch((error) => // Your code here })
183
+ * ```
184
+ *
185
+ * @param {number} depth The depth of the relationships of the content.
186
+ * @return {CollectionBuilder} A CollectionBuilder instance.
134
187
  * @memberof CollectionBuilder
135
188
  */
136
189
  depth(depth: number): this;
137
190
  /**
138
- * Executes the fetch and returns a promise that resolves to the content or rejects to an error
191
+ * Executes the fetch and returns a promise that resolves to the content or rejects with an error.
139
192
  *
140
- * @param {OnFullfilled} [onfulfilled] A callback that is called when the fetch is successful
141
- * @param {OnRejected} [onrejected] A callback that is called when the fetch fails
142
- * @return {Promise<GetCollectionResponse<T> | GetCollectionError>} A promise that resolves to the content or rejects to an error
193
+ * @example
194
+ * ```ts
195
+ * const client = new DotCMSClient(config);
196
+ * const collectionBuilder = client.content.getCollection("Blog");
197
+ * collectionBuilder
198
+ * .limit(10)
199
+ * .then((response) => // Your code here })
200
+ * .catch((error) => // Your code here })
201
+ * ```
202
+ *
203
+ * @param {OnFullfilled} [onfulfilled] A callback that is called when the fetch is successful.
204
+ * @param {OnRejected} [onrejected] A callback that is called when the fetch fails.
205
+ * @return {Promise<GetCollectionResponse<T> | GetCollectionError>} A promise that resolves to the content or rejects with an error.
143
206
  * @memberof CollectionBuilder
144
207
  */
145
208
  then(onfulfilled?: OnFullfilled<T>, onrejected?: OnRejected): Promise<GetCollectionResponse<T> | GetCollectionError>;
209
+ /**
210
+ * Formats the response to the desired format.
211
+ *
212
+ * @private
213
+ * @param {GetCollectionRawResponse<T>} data The raw response data.
214
+ * @return {GetCollectionResponse<T>} The formatted response.
215
+ * @memberof CollectionBuilder
216
+ */
146
217
  private formatResponse;
218
+ /**
219
+ * Calls the content API to fetch the content.
220
+ *
221
+ * @private
222
+ * @return {Promise<Response>} The fetch response.
223
+ * @memberof CollectionBuilder
224
+ */
147
225
  private fetch;
148
226
  }
@@ -1,16 +1,71 @@
1
1
  import { CollectionBuilder } from './builders/collection/collection';
2
2
  import { ClientOptions } from '../sdk-js-client';
3
3
  /**
4
- * Content classs exposes the content api methods
4
+ * Creates a builder to filter and fetch a collection of content items.
5
+ * @param contentType - The content type to retrieve.
6
+ * @returns A CollectionBuilder instance for chaining filters and executing the query.
7
+ * @template T - The type of the content items (defaults to unknown).
5
8
  *
6
- * @export
7
- * @class Content
9
+ * @example Fetch blog posts with async/await
10
+ * ```typescript
11
+ * const response = await client.content
12
+ * .getCollection<BlogPost>('Blog')
13
+ * .limit(10)
14
+ * .page(2)
15
+ * .sortBy([{ field: 'title', order: 'asc' }])
16
+ * .query(q => q.field('author').equals('John Doe'))
17
+ * .depth(1)
18
+ * .fetch();
19
+ *
20
+ * console.log(response.contentlets);
21
+ * ```
22
+ *
23
+ * @example Fetch blog posts with Promise chain
24
+ * ```typescript
25
+ * client.content
26
+ * .getCollection<BlogPost>('Blog')
27
+ * .limit(10)
28
+ * .page(2)
29
+ * .sortBy([{ field: 'title', order: 'asc' }])
30
+ * .query(q => q.field('author').equals('John Doe'))
31
+ * .depth(1)
32
+ * .fetch()
33
+ * .then(response => console.log(response.contentlets))
34
+ * .catch(error => console.error(error));
35
+ * ```
36
+ *
37
+ * @example Using a custom type
38
+ * ```typescript
39
+ * interface BlogPost {
40
+ * summary: string;
41
+ * author: string;
42
+ * title: string;
43
+ * }
44
+ *
45
+ * const posts = await client.content
46
+ * .getCollection<BlogPost>('Blog')
47
+ * .limit(10)
48
+ * .fetch();
49
+ *
50
+ * posts.contentlets.forEach(post => {
51
+ * console.log(post.title, post.author, post.summary);
52
+ * });
53
+ * ```
8
54
  */
9
55
  export declare class Content {
10
56
  #private;
57
+ /**
58
+ * Creates an instance of Content.
59
+ * @param {ClientOptions} requestOptions - The options for the client request.
60
+ * @param {string} serverUrl - The server URL.
61
+ */
11
62
  constructor(requestOptions: ClientOptions, serverUrl: string);
12
63
  /**
13
- * Takes a content type and returns a builder to filter and fetch the collection
64
+ * Takes a content type and returns a builder to filter and fetch the collection.
65
+ * @param {string} contentType - The content type to get the collection.
66
+ * @return {CollectionBuilder<T>} CollectionBuilder to filter and fetch the collection.
67
+ * @template T - Represents the type of the content type to fetch. Defaults to unknown.
68
+ * @memberof Content
14
69
  *
15
70
  * @example
16
71
  * ```javascript
@@ -42,7 +97,7 @@ export declare class Content {
42
97
  * ```
43
98
  * @example
44
99
  * ```typescript
45
- * // Using an specific type for your content
100
+ * // Using a specific type for your content
46
101
  *
47
102
  * type Blog = {
48
103
  * summary: string;
@@ -69,10 +124,6 @@ export declare class Content {
69
124
  * });
70
125
  * ```
71
126
  *
72
- * @param {string} contentType The content type to get the collection
73
- * @return {CollectionBuilder} CollectionBuilder to filter and fetch the collection
74
- * @template T Represents the type of the content type to fetch. Defaults to unknown
75
- * @memberof Content
76
127
  */
77
128
  getCollection<T = unknown>(contentType: string): CollectionBuilder<T>;
78
129
  }
@@ -1,3 +1,13 @@
1
+ /**
2
+ * Default variant identifier used in the application.
3
+ */
1
4
  export declare const DEFAULT_VARIANT_ID = "DEFAULT";
5
+ /**
6
+ * Fields that should not be formatted when sanitizing the query.
7
+ * These fields are essential for maintaining the integrity of the content type.
8
+ */
2
9
  export declare const CONTENT_TYPE_MAIN_FIELDS: string[];
10
+ /**
11
+ * URL endpoint for the content API search functionality.
12
+ */
3
13
  export declare const CONTENT_API_URL = "/api/content/_search";
@@ -1,10 +1,29 @@
1
1
  import { Equals } from '../../../query-builder/lucene-syntax';
2
2
  import { QueryBuilder } from '../../../query-builder/sdk-query-builder';
3
+ /**
4
+ * Model to sort by fields.
5
+ */
3
6
  export type SortBy = {
7
+ /**
8
+ * The field to sort by.
9
+ */
4
10
  field: string;
11
+ /**
12
+ * The order of sorting: 'asc' for ascending, 'desc' for descending.
13
+ */
5
14
  order: 'asc' | 'desc';
6
15
  };
16
+ /**
17
+ * Callback to build a query.
18
+ *
19
+ * @callback BuildQuery
20
+ * @param {QueryBuilder} qb - The query builder instance.
21
+ * @returns {Equals} The built query.
22
+ */
7
23
  export type BuildQuery = (qb: QueryBuilder) => Equals;
24
+ /**
25
+ * Main fields of a Contentlet (Inherited from the Content Type).
26
+ */
8
27
  export interface ContentTypeMainFields {
9
28
  hostName: string;
10
29
  modDate: string;
@@ -38,25 +57,82 @@ export interface ContentTypeMainFields {
38
57
  contentTypeIcon: string;
39
58
  variant: string;
40
59
  }
60
+ /**
61
+ * The contentlet has the main fields and the custom fields of the content type.
62
+ *
63
+ * @template T - The custom fields of the content type.
64
+ */
41
65
  export type Contentlet<T> = T & ContentTypeMainFields;
66
+ /**
67
+ * Callback for a fulfilled promise.
68
+ *
69
+ * @template T - The type of the response.
70
+ * @callback OnFullfilled
71
+ * @param {GetCollectionResponse<T>} value - The response value.
72
+ * @returns {GetCollectionResponse<T> | PromiseLike<GetCollectionResponse<T>> | void} The processed response or a promise.
73
+ */
42
74
  export type OnFullfilled<T> = ((value: GetCollectionResponse<T>) => GetCollectionResponse<T> | PromiseLike<GetCollectionResponse<T>> | void) | undefined | null;
75
+ /**
76
+ * Callback for a rejected promise.
77
+ *
78
+ * @callback OnRejected
79
+ * @param {GetCollectionError} error - The error object.
80
+ * @returns {GetCollectionError | PromiseLike<GetCollectionError> | void} The processed error or a promise.
81
+ */
43
82
  export type OnRejected = ((error: GetCollectionError) => GetCollectionError | PromiseLike<GetCollectionError> | void) | undefined | null;
83
+ /**
84
+ * Response of the get collection method.
85
+ *
86
+ * @template T - The type of the contentlet.
87
+ */
44
88
  export interface GetCollectionResponse<T> {
89
+ /**
90
+ * The list of contentlets.
91
+ */
45
92
  contentlets: Contentlet<T>[];
93
+ /**
94
+ * The current page number.
95
+ */
46
96
  page: number;
97
+ /**
98
+ * The size of the page.
99
+ */
47
100
  size: number;
101
+ /**
102
+ * The total number of contentlets.
103
+ */
48
104
  total: number;
105
+ /**
106
+ * The fields by which the contentlets are sorted.
107
+ */
49
108
  sortedBy?: SortBy[];
50
109
  }
110
+ /**
111
+ * Raw response of the get collection method.
112
+ *
113
+ * @template T - The type of the contentlet.
114
+ */
51
115
  export interface GetCollectionRawResponse<T> {
52
116
  entity: {
53
117
  jsonObjectView: {
118
+ /**
119
+ * The list of contentlets.
120
+ */
54
121
  contentlets: Contentlet<T>[];
55
122
  };
123
+ /**
124
+ * The size of the results.
125
+ */
56
126
  resultsSize: number;
57
127
  };
58
128
  }
129
+ /**
130
+ * Error object for the get collection method.
131
+ */
59
132
  export interface GetCollectionError {
133
+ /**
134
+ * The status code of the error.
135
+ */
60
136
  status: number;
61
137
  [key: string]: unknown;
62
138
  }
@@ -1,12 +1,20 @@
1
1
  /**
2
+ * @description
2
3
  * Sanitizes the query for the given content type.
3
- * It replaces the fields that are not contentType fields with the correct format.
4
+ * It replaces the fields that are not content type fields with the correct format.
4
5
  * Example: +field: -> +contentTypeVar.field:
5
6
  *
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * const query = '+field: value';
11
+ * const contentType = 'contentTypeVar';
12
+ * const sanitizedQuery = sanitizeQueryForContentType(query, contentType); // Output: '+contentTypeVar.field: value'
13
+ * ```
6
14
  *
7
15
  * @export
8
- * @param {string} query
9
- * @param {string} contentType
10
- * @return {*} {string}
16
+ * @param {string} query - The query string to be sanitized.
17
+ * @param {string} contentType - The content type to be used for formatting the fields.
18
+ * @returns {string} The sanitized query string.
11
19
  */
12
20
  export declare function sanitizeQueryForContentType(query: string, contentType: string): string;
@@ -1 +1,12 @@
1
+ /**
2
+ * A record of HTTP status codes and their corresponding error messages.
3
+ *
4
+ * @type {Record<number, string>}
5
+ * @property {string} 401 - Unauthorized. Check the token and try again.
6
+ * @property {string} 403 - Forbidden. Check the permissions and try again.
7
+ * @property {string} 404 - Not Found. Check the URL and try again.
8
+ * @property {string} 500 - Internal Server Error. Try again later.
9
+ * @property {string} 502 - Bad Gateway. Try again later.
10
+ * @property {string} 503 - Service Unavailable. Try again later.
11
+ */
1
12
  export declare const ErrorMessages: Record<number, string>;
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Represents a listener for DotcmsClientListener.
3
+ *
4
+ * @typedef {Object} DotcmsClientListener
5
+ * @property {string} action - The action that triggers the event.
6
+ * @property {string} event - The name of the event.
7
+ * @property {function(...args: any[]): void} callback - The callback function to handle the event.
8
+ */
1
9
  export type DotcmsClientListener = {
2
10
  action: string;
3
11
  event: string;