@dotcms/client 0.0.1-alpha.9 → 0.0.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +166 -19
  2. package/index.cjs.d.ts +1 -0
  3. package/index.cjs.default.js +1 -0
  4. package/index.cjs.js +2050 -0
  5. package/index.cjs.mjs +2 -0
  6. package/index.esm.d.ts +1 -0
  7. package/index.esm.js +2038 -0
  8. package/package.json +36 -25
  9. package/src/index.d.ts +8 -0
  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 +98 -0
  20. package/src/lib/editor/models/editor.model.d.ts +62 -0
  21. package/src/lib/editor/models/inline-event.model.d.ts +9 -0
  22. package/src/lib/editor/models/{listeners.model.ts → listeners.model.d.ts} +17 -8
  23. package/src/lib/editor/sdk-editor-vtl.d.ts +1 -0
  24. package/src/lib/editor/sdk-editor.d.ts +92 -0
  25. package/src/lib/editor/utils/editor.utils.d.ts +159 -0
  26. package/src/lib/editor/utils/traditional-vtl.utils.d.ts +4 -0
  27. package/src/lib/query-builder/lucene-syntax/Equals.d.ts +114 -0
  28. package/src/lib/query-builder/lucene-syntax/Field.d.ts +32 -0
  29. package/src/lib/query-builder/lucene-syntax/NotOperand.d.ts +26 -0
  30. package/src/lib/query-builder/lucene-syntax/Operand.d.ts +44 -0
  31. package/src/lib/query-builder/lucene-syntax/index.d.ts +4 -0
  32. package/src/lib/query-builder/sdk-query-builder.d.ts +76 -0
  33. package/src/lib/query-builder/utils/index.d.ts +142 -0
  34. package/src/lib/utils/graphql/transforms.d.ts +24 -0
  35. package/src/lib/utils/index.d.ts +2 -0
  36. package/src/lib/utils/page/common-utils.d.ts +33 -0
  37. package/.eslintrc.json +0 -18
  38. package/jest.config.ts +0 -15
  39. package/project.json +0 -63
  40. package/src/index.ts +0 -4
  41. package/src/lib/client/sdk-js-client.spec.ts +0 -258
  42. package/src/lib/client/sdk-js-client.ts +0 -297
  43. package/src/lib/editor/listeners/listeners.spec.ts +0 -55
  44. package/src/lib/editor/listeners/listeners.ts +0 -200
  45. package/src/lib/editor/models/client.model.ts +0 -55
  46. package/src/lib/editor/models/editor.model.ts +0 -17
  47. package/src/lib/editor/sdk-editor-vtl.ts +0 -24
  48. package/src/lib/editor/sdk-editor.spec.ts +0 -95
  49. package/src/lib/editor/sdk-editor.ts +0 -70
  50. package/src/lib/editor/utils/editor.utils.spec.ts +0 -164
  51. package/src/lib/editor/utils/editor.utils.ts +0 -151
  52. package/tsconfig.json +0 -22
  53. package/tsconfig.lib.json +0 -10
  54. package/tsconfig.spec.json +0 -9
package/README.md CHANGED
@@ -1,18 +1,20 @@
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
 
7
7
  ## Features
8
8
 
9
- - Easy-to-use methods to interact with the [DotCMS Page](https://www.dotcms.com/docs/latest/page-rest-api-layout-as-a-service-laas) and [Navigation APIs](https://www.dotcms.com/docs/latest/navigation-rest-api).
10
- - Support for custom actions to communicate with the DotCMS page editor.
9
+ - Easy-to-use methods to interact with [dotCMS pages](https://www.dotcms.com/docs/latest/page-rest-api-layout-as-a-service-laas) and the [Navigation API](https://www.dotcms.com/docs/latest/navigation-rest-api).
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,26 @@ 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
+ ```
44
+
45
+ ### Initialization
33
46
 
34
- const client = dotcmsClient.init({
47
+ First, initialize the client with your dotCMS instance details.
48
+
49
+ ```javascript
50
+ const client = DotCmsClient.init({
35
51
  dotcmsUrl: 'https://your-dotcms-instance.com',
36
52
  authToken: 'your-auth-token',
37
53
  siteId: 'your-site-id'
@@ -40,7 +56,7 @@ const client = dotcmsClient.init({
40
56
 
41
57
  ### Fetching a Page
42
58
 
43
- Retrieve the elements of any page in your DotCMS system in JSON format.
59
+ You can retrieve the elements of any page in your dotCMS system in JSON format using the `client.page.get()` method.
44
60
 
45
61
  ```javascript
46
62
  const pageData = await client.page.get({
@@ -48,13 +64,11 @@ const pageData = await client.page.get({
48
64
  language_id: 1,
49
65
  personaId: 'optional-persona-id'
50
66
  });
51
-
52
- console.log(pageData);
53
67
  ```
54
68
 
55
69
  ### Fetching Navigation
56
70
 
57
- Retrieve information about the DotCMS file and folder tree.
71
+ Retrieve the dotCMS file and folder tree to get information about the navigation structure.
58
72
 
59
73
  ```javascript
60
74
  const navData = await client.nav.get({
@@ -62,25 +76,159 @@ const navData = await client.nav.get({
62
76
  depth: 2,
63
77
  languageId: 1
64
78
  });
79
+ ```
80
+
81
+ ### Fetching a Collection of Content
82
+
83
+ The `getCollection` method allows you to fetch a collection of content items (sometimes called "contentlets") using a builder pattern for complex queries.
84
+
85
+ #### Basic Usage
86
+
87
+ Here’s a simple example to fetch content from a collection:
88
+
89
+ ```typescript
90
+ import { DotCmsClient } from '@dotcms/client';
91
+
92
+ const client = DotCmsClient.init({
93
+ dotcmsUrl: 'https://your-dotcms-instance.com',
94
+ authToken: 'your-auth-token'
95
+ });
96
+
97
+ const collectionResponse = await client.content
98
+ .getCollection('Blog') // Collection name
99
+ .limit(10) // Limit results to 10 items
100
+ .page(1) // Fetch the first page
101
+ .fetch(); // Execute the query
102
+
103
+ console.log(collectionResponse.contentlets);
104
+ ```
105
+
106
+ #### Sorting Content
107
+
108
+ You can sort the content by any field in ascending or descending order:
109
+
110
+ ```typescript
111
+ const sortedResponse = await client.content
112
+ .getCollection('Blog')
113
+ .sortBy([{ field: 'title', order: 'asc' }]) // Sort by title in ascending order
114
+ .fetch();
115
+ ```
116
+
117
+ #### Filtering by Language
118
+
119
+ If you need to filter content by language, you can specify the `language` parameter:
120
+
121
+ ```typescript
122
+ const languageFilteredResponse = await client.content
123
+ .getCollection('Blog')
124
+ .language(2) // Filter by language ID (e.g., 2)
125
+ .fetch();
126
+ ```
127
+
128
+ #### Using Complex Queries
129
+
130
+ You can build more complex queries using the query builder. For example, filter by `author` and `title`:
131
+
132
+ ```typescript
133
+ const complexQueryResponse = await client.content
134
+ .getCollection('Blog')
135
+ .query((qb) => qb.field('author').equals('John Doe').and().field('title').equals('Hello World'))
136
+ .fetch();
137
+ ```
138
+
139
+ #### Fetching Draft Content
140
+
141
+ To only fetch draft content, use the `draft()` method:
142
+
143
+ ```typescript
144
+ const draftContentResponse = await client.content
145
+ .getCollection('Blog')
146
+ .draft() // Fetch only drafts content
147
+ .fetch();
148
+ ```
149
+
150
+ #### Setting Depth for Relationships
151
+
152
+ To fetch content with a specific relationship depth, use the `depth()` method:
153
+
154
+ ```typescript
155
+ const depthResponse = await client.content
156
+ .getCollection('Blog')
157
+ .depth(2) // Fetch related content up to depth 2
158
+ .fetch();
159
+ ```
160
+
161
+ #### Combining Multiple Methods
162
+
163
+ You can combine multiple methods to build more complex queries. For example, limit results, sort them, and filter by author:
65
164
 
66
- console.log(navData);
165
+ ```typescript
166
+ const combinedResponse = await client.content
167
+ .getCollection('Blog')
168
+ .limit(5)
169
+ .page(2)
170
+ .sortBy([{ field: 'title', order: 'asc' }])
171
+ .query((qb) => qb.field('author').equals('John Doe'))
172
+ .depth(1)
173
+ .fetch();
174
+ ```
175
+
176
+ ## Error Handling Example
177
+
178
+ To handle errors gracefully, you can use a `try-catch` block around your API calls. Here’s an example:
179
+
180
+ ```typescript
181
+ try {
182
+ const pageData = await client.page.get({
183
+ path: '/your-page-path',
184
+ languageId: 1
185
+ });
186
+ } catch (error) {
187
+ console.error('Failed to fetch page data:', error);
188
+ }
189
+ ```
190
+
191
+ This ensures that any errors that occur during the fetch (e.g., network issues, invalid paths, etc.) are caught and logged properly.
192
+
193
+ ## Pagination
194
+
195
+ When fetching large collections of content, pagination is key to managing the number of results returned:
196
+
197
+ ```typescript
198
+ const paginatedResponse = await client.content
199
+ .getCollection('Blog')
200
+ .limit(10) // Limit to 10 items per page
201
+ .page(2) // Get the second page of results
202
+ .fetch();
67
203
  ```
68
204
 
69
205
  ## API Reference
70
206
 
71
207
  Detailed documentation of the `@dotcms/client` methods, parameters, and types can be found below:
72
208
 
73
- ### `dotcmsClient.init(config: ClientConfig): DotCmsClient`
209
+ ### `DotCmsClient.init(config: ClientConfig): DotCmsClient`
74
210
 
75
- Initializes the DotCMS client with the specified configuration.
211
+ Initializes the dotCMS client with the specified configuration.
76
212
 
77
213
  ### `DotCmsClient.page.get(options: PageApiOptions): Promise<unknown>`
78
214
 
79
- Retrieves the specified page's elements from your DotCMS system in JSON format.
215
+ Retrieves the specified page's elements from your dotCMS system in JSON format.
80
216
 
81
217
  ### `DotCmsClient.nav.get(options: NavApiOptions): Promise<unknown>`
82
218
 
83
- Retrieves information about the DotCMS file and folder tree.
219
+ Retrieves information about the dotCMS file and folder tree.
220
+
221
+ ### `DotCmsClient.content.getCollection(contentType: string): CollectionBuilder<T>`
222
+
223
+ Creates a builder to filter and fetches a collection of content items for a specific content type.
224
+
225
+ #### Parameters
226
+
227
+ `contentType` (string): The content type to retrieve.
228
+
229
+ #### Returns
230
+
231
+ `CollectionBuilder<T>`: A builder instance for chaining filters and executing the query.
84
232
 
85
233
  ## Contributing
86
234
 
@@ -96,7 +244,7 @@ If you need help or have any questions, please [open an issue](https://github.co
96
244
 
97
245
  ## Documentation
98
246
 
99
- Always refer to the official [DotCMS documentation](https://www.dotcms.com/docs/latest/) for comprehensive guides and API references.
247
+ Always refer to the official [dotCMS documentation](https://www.dotcms.com/docs/latest/) for comprehensive guides and API references.
100
248
 
101
249
  ## Getting Help
102
250
 
@@ -105,7 +253,6 @@ Always refer to the official [DotCMS documentation](https://www.dotcms.com/docs/
105
253
  | Installation | [Installation](https://dotcms.com/docs/latest/installation) |
106
254
  | Documentation | [Documentation](https://dotcms.com/docs/latest/table-of-contents) |
107
255
  | Videos | [Helpful Videos](http://dotcms.com/videos/) |
108
- | Code Examples | [Codeshare](https://dotcms.com/codeshare/) |
109
256
  | Forums/Listserv | [via Google Groups](https://groups.google.com/forum/#!forum/dotCMS) |
110
257
  | Twitter | @dotCMS |
111
258
  | 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;