@dotcms/client 0.0.1-alpha.49 → 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.
- package/README.md +141 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# DotCMS API Client - `@dotcms/client`
|
|
2
2
|
|
|
3
|
-
`@dotcms/client` is
|
|
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
|
-
|
|
17
|
+
To get started, install the client via npm or yarn:
|
|
16
18
|
|
|
17
19
|
```bash
|
|
18
20
|
npm install @dotcms/client
|
|
@@ -52,7 +54,7 @@ const client = DotCmsClient.init({
|
|
|
52
54
|
|
|
53
55
|
### Fetching a Page
|
|
54
56
|
|
|
55
|
-
|
|
57
|
+
You can retrieve the elements of any page in your DotCMS system in JSON format using the client.page.get() method.
|
|
56
58
|
|
|
57
59
|
```javascript
|
|
58
60
|
const pageData = await client.page.get({
|
|
@@ -60,13 +62,11 @@ const pageData = await client.page.get({
|
|
|
60
62
|
language_id: 1,
|
|
61
63
|
personaId: 'optional-persona-id'
|
|
62
64
|
});
|
|
63
|
-
|
|
64
|
-
console.log(pageData);
|
|
65
65
|
```
|
|
66
66
|
|
|
67
67
|
### Fetching Navigation
|
|
68
68
|
|
|
69
|
-
Retrieve
|
|
69
|
+
Retrieve the DotCMS file and folder tree to get information about the navigation structure.
|
|
70
70
|
|
|
71
71
|
```javascript
|
|
72
72
|
const navData = await client.nav.get({
|
|
@@ -74,8 +74,130 @@ const navData = await client.nav.get({
|
|
|
74
74
|
depth: 2,
|
|
75
75
|
languageId: 1
|
|
76
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:
|
|
118
|
+
|
|
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`:
|
|
77
129
|
|
|
78
|
-
|
|
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();
|
|
79
201
|
```
|
|
80
202
|
|
|
81
203
|
## API Reference
|
|
@@ -94,6 +216,17 @@ Retrieves the specified page's elements from your DotCMS system in JSON format.
|
|
|
94
216
|
|
|
95
217
|
Retrieves information about the DotCMS file and folder tree.
|
|
96
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
|
+
|
|
97
230
|
## Contributing
|
|
98
231
|
|
|
99
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).
|