@dotcms/client 0.0.1-alpha.5 → 0.0.1-alpha.50

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 (43) hide show
  1. package/README.md +158 -14
  2. package/index.cjs.d.ts +1 -0
  3. package/index.cjs.default.js +1 -0
  4. package/index.cjs.js +2019 -0
  5. package/index.cjs.mjs +2 -0
  6. package/index.esm.d.ts +1 -0
  7. package/index.esm.js +2008 -0
  8. package/package.json +19 -7
  9. package/src/index.d.ts +7 -2
  10. package/src/lib/client/content/builders/collection/collection.d.ts +226 -0
  11. package/src/lib/client/content/content-api.d.ts +129 -0
  12. package/src/lib/client/content/shared/const.d.ts +13 -0
  13. package/src/lib/client/content/shared/types.d.ts +138 -0
  14. package/src/lib/client/content/shared/utils.d.ts +20 -0
  15. package/src/lib/client/models/index.d.ts +12 -0
  16. package/src/lib/client/models/types.d.ts +13 -0
  17. package/src/lib/client/sdk-js-client.d.ts +276 -0
  18. package/src/lib/editor/listeners/listeners.d.ts +45 -0
  19. package/src/lib/editor/models/client.model.d.ts +101 -0
  20. package/src/lib/editor/models/editor.model.d.ts +62 -0
  21. package/src/lib/editor/models/listeners.model.d.ts +55 -0
  22. package/src/lib/editor/sdk-editor-vtl.d.ts +1 -0
  23. package/src/lib/editor/sdk-editor.d.ts +77 -0
  24. package/src/lib/editor/utils/editor.utils.d.ts +159 -0
  25. package/src/lib/editor/utils/traditional-vtl.utils.d.ts +5 -0
  26. package/src/lib/query-builder/lucene-syntax/Equals.d.ts +114 -0
  27. package/src/lib/query-builder/lucene-syntax/Field.d.ts +32 -0
  28. package/src/lib/query-builder/lucene-syntax/NotOperand.d.ts +26 -0
  29. package/src/lib/query-builder/lucene-syntax/Operand.d.ts +44 -0
  30. package/src/lib/query-builder/lucene-syntax/index.d.ts +4 -0
  31. package/src/lib/query-builder/sdk-query-builder.d.ts +76 -0
  32. package/src/lib/query-builder/utils/index.d.ts +142 -0
  33. package/src/lib/utils/graphql/transforms.d.ts +24 -0
  34. package/src/lib/utils/index.d.ts +2 -0
  35. package/src/lib/utils/page/common-utils.d.ts +33 -0
  36. package/src/index.js +0 -3
  37. package/src/index.js.map +0 -1
  38. package/src/lib/postMessageToEditor.d.ts +0 -50
  39. package/src/lib/postMessageToEditor.js +0 -42
  40. package/src/lib/postMessageToEditor.js.map +0 -1
  41. package/src/lib/sdk-js-client.d.ts +0 -183
  42. package/src/lib/sdk-js-client.js +0 -145
  43. package/src/lib/sdk-js-client.js.map +0 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # @dotcms/client
1
+ # DotCMS API Client - `@dotcms/client`
2
2
 
3
- `@dotcms/client` is the official dotCMS JavaScript library designed to simplify interactions with the DotCMS REST APIs.
3
+ The `@dotcms/client` is a JavaScript/TypeScript library for interacting with a DotCMS instance. It allows you to easily fetch pages, content, and navigation information in JSON format, as well as to make complex queries on content collections.
4
4
 
5
5
  This client library provides a streamlined, promise-based interface to fetch pages and navigation API.
6
6
 
@@ -10,9 +10,11 @@ This client library provides a streamlined, promise-based interface to fetch pag
10
10
  - Support for custom actions to communicate with the DotCMS page editor.
11
11
  - Comprehensive TypeScript typings for better development experience.
12
12
 
13
+ # DotCMS API Client
14
+
13
15
  ## Installation
14
16
 
15
- Install the package via npm:
17
+ To get started, install the client via npm or yarn:
16
18
 
17
19
  ```bash
18
20
  npm install @dotcms/client
@@ -26,12 +28,24 @@ yarn add @dotcms/client
26
28
 
27
29
  ## Usage
28
30
 
29
- First, initialize the client with your DotCMS instance details.
31
+ `@dotcms/client` supports both ES modules and CommonJS. You can import it using either syntax:
32
+
33
+ ### ES Modules
34
+
35
+ ```javascript
36
+ import { DotCmsClient } from '@dotcms/client';
37
+ ```
38
+
39
+ ### CommonJS
30
40
 
31
41
  ```javascript
32
- import { dotcmsClient } from '@dotcms/client';
42
+ const { DotCmsClient } = require('@dotcms/client');
43
+ ```
33
44
 
34
- const client = dotcmsClient.init({
45
+ First, initialize the client with your DotCMS instance details.
46
+
47
+ ```javascript
48
+ const client = DotCmsClient.init({
35
49
  dotcmsUrl: 'https://your-dotcms-instance.com',
36
50
  authToken: 'your-auth-token',
37
51
  siteId: 'your-site-id'
@@ -40,7 +54,7 @@ const client = dotcmsClient.init({
40
54
 
41
55
  ### Fetching a Page
42
56
 
43
- Retrieve the elements of any page in your DotCMS system in JSON format.
57
+ You can retrieve the elements of any page in your DotCMS system in JSON format using the client.page.get() method.
44
58
 
45
59
  ```javascript
46
60
  const pageData = await client.page.get({
@@ -48,13 +62,11 @@ const pageData = await client.page.get({
48
62
  language_id: 1,
49
63
  personaId: 'optional-persona-id'
50
64
  });
51
-
52
- console.log(pageData);
53
65
  ```
54
66
 
55
67
  ### Fetching Navigation
56
68
 
57
- Retrieve information about the DotCMS file and folder tree.
69
+ Retrieve the DotCMS file and folder tree to get information about the navigation structure.
58
70
 
59
71
  ```javascript
60
72
  const navData = await client.nav.get({
@@ -62,15 +74,137 @@ const navData = await client.nav.get({
62
74
  depth: 2,
63
75
  languageId: 1
64
76
  });
77
+ ```
78
+
79
+ ### Fetching a Collection of Content
80
+
81
+ The `getCollection` method allows you to fetch a collection of content items using a builder pattern for complex queries.
82
+
83
+ #### Basic Usage
84
+
85
+ Here’s a simple example to fetch content from a collection:
86
+
87
+ ```typescript
88
+ import { DotCmsClient } from '@dotcms/client';
89
+
90
+ const client = DotCmsClient.init({
91
+ dotcmsUrl: 'https://your-dotcms-instance.com',
92
+ authToken: 'your-auth-token'
93
+ });
94
+
95
+ const collectionResponse = await client.content
96
+ .getCollection('Blog') // Collection name
97
+ .limit(10) // Limit results to 10 items
98
+ .page(1) // Fetch the first page
99
+ .fetch(); // Execute the query
100
+
101
+ console.log(collectionResponse.contentlets);
102
+ ```
103
+
104
+ #### Sorting Content
105
+
106
+ You can sort the content by any field in ascending or descending order:
107
+
108
+ ```typescript
109
+ const sortedResponse = await client.content
110
+ .getCollection('Blog')
111
+ .sortBy([{ field: 'title', order: 'asc' }]) // Sort by title in ascending order
112
+ .fetch();
113
+ ```
114
+
115
+ #### Filtering by Language
116
+
117
+ If you need to filter content by language, you can specify the `language` parameter:
65
118
 
66
- console.log(navData);
119
+ ```typescript
120
+ const languageFilteredResponse = await client.content
121
+ .getCollection('Blog')
122
+ .language(2) // Filter by language ID (e.g., 2)
123
+ .fetch();
124
+ ```
125
+
126
+ #### Using Complex Queries
127
+
128
+ You can build more complex queries using the query builder. For example, filter by author and `title`:
129
+
130
+ ```typescript
131
+ const complexQueryResponse = await client.content
132
+ .getCollection('Blog')
133
+ .query((qb) => qb.field('author').equals('John Doe').and().field('title').equals('Hello World'))
134
+ .fetch();
135
+ ```
136
+
137
+ #### Fetching Draft Content
138
+
139
+ To only fetch draft content, use the `draft()` method:
140
+
141
+ ```typescript
142
+ const draftContentResponse = await client.content
143
+ .getCollection('Blog')
144
+ .draft() // Fetch only drafts content
145
+ .fetch();
146
+ ```
147
+
148
+ #### Setting Depth for Relationships
149
+
150
+ To fetch content with a specific relationship depth, use the `depth()` method:
151
+
152
+ ```typescript
153
+ const depthResponse = await client.content
154
+ .getCollection('Blog')
155
+ .depth(2) // Fetch related content up to depth 2
156
+ .fetch();
157
+ ```
158
+
159
+ #### Combining Multiple Methods
160
+
161
+ You can combine multiple methods to build more complex queries. For example, limit results, sort them, and filter by author:
162
+
163
+ ```typescript
164
+ const combinedResponse = await client.content
165
+ .getCollection('Blog')
166
+ .limit(5)
167
+ .page(2)
168
+ .sortBy([{ field: 'title', order: 'asc' }])
169
+ .query((qb) => qb.field('author').equals('John Doe'))
170
+ .depth(1)
171
+ .fetch();
172
+ ```
173
+
174
+ ## Error Handling Example
175
+
176
+ To handle errors gracefully, you can use a `try-catch` block around your API calls. Here’s an example:
177
+
178
+ ```typescript
179
+ try {
180
+ const pageData = await client.page.get({
181
+ path: '/your-page-path',
182
+ languageId: 1
183
+ });
184
+ } catch (error) {
185
+ console.error('Failed to fetch page data:', error);
186
+ }
187
+ ```
188
+
189
+ This ensures that any errors that occur during the fetch (e.g., network issues, invalid paths, etc.) are caught and logged properly.
190
+
191
+ ## Pagination
192
+
193
+ When fetching large collections of content, pagination is key to managing the number of results returned:
194
+
195
+ ```typescript
196
+ const paginatedResponse = await client.content
197
+ .getCollection('Blog')
198
+ .limit(10) // Limit to 10 items per page
199
+ .page(2) // Get the second page of results
200
+ .fetch();
67
201
  ```
68
202
 
69
203
  ## API Reference
70
204
 
71
205
  Detailed documentation of the `@dotcms/client` methods, parameters, and types can be found below:
72
206
 
73
- ### `dotcmsClient.init(config: ClientConfig): DotCmsClient`
207
+ ### `DotCmsClient.init(config: ClientConfig): DotCmsClient`
74
208
 
75
209
  Initializes the DotCMS client with the specified configuration.
76
210
 
@@ -82,6 +216,17 @@ Retrieves the specified page's elements from your DotCMS system in JSON format.
82
216
 
83
217
  Retrieves information about the DotCMS file and folder tree.
84
218
 
219
+ ### `DotCmsClient.content.getCollection(contentType: string): CollectionBuilder<T>`
220
+ Creates a builder to filter and fetch a collection of content items for a specific content type.
221
+
222
+ #### Parameters
223
+
224
+ contentType (string): The content type to retrieve.
225
+
226
+ #### Returns
227
+
228
+ CollectionBuilder<T>: A builder instance for chaining filters and executing the query.
229
+
85
230
  ## Contributing
86
231
 
87
232
  GitHub pull requests are the preferred method to contribute code to dotCMS. Before any pull requests can be accepted, an automated tool will ask you to agree to the [dotCMS Contributor's Agreement](https://gist.github.com/wezell/85ef45298c48494b90d92755b583acb3).
@@ -105,7 +250,6 @@ Always refer to the official [DotCMS documentation](https://www.dotcms.com/docs/
105
250
  | Installation | [Installation](https://dotcms.com/docs/latest/installation) |
106
251
  | Documentation | [Documentation](https://dotcms.com/docs/latest/table-of-contents) |
107
252
  | Videos | [Helpful Videos](http://dotcms.com/videos/) |
108
- | Code Examples | [Codeshare](https://dotcms.com/codeshare/) |
109
253
  | Forums/Listserv | [via Google Groups](https://groups.google.com/forum/#!forum/dotCMS) |
110
254
  | Twitter | @dotCMS |
111
- | Main Site | [dotCMS.com](https://dotcms.com/) |
255
+ | Main Site | [dotCMS.com](https://dotcms.com/) |
package/index.cjs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
@@ -0,0 +1 @@
1
+ exports._default = require('./index.cjs.js').default;