@dotcms/client 1.0.0 → 1.0.2-next.1

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/CLAUDE.md ADDED
@@ -0,0 +1,253 @@
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Project Overview
6
+
7
+ This is the **DotCMS Client SDK** (`@dotcms/client`) - a JavaScript/TypeScript library for interacting with DotCMS REST APIs. The SDK is part of a larger DotCMS monorepo and serves as the foundation for framework-specific integrations (React, Angular, etc.).
8
+
9
+ ## Essential Commands
10
+
11
+ ### Development Commands
12
+ ```bash
13
+ # Install dependencies (from monorepo root)
14
+ yarn install
15
+
16
+ # Build library for distribution
17
+ nx run sdk-client:build # ESM/CJS dual package build
18
+ nx run sdk-client:build:js # Specialized esbuild for editor integration
19
+
20
+ # Run tests
21
+ nx run sdk-client:test # Run Jest tests
22
+ nx run sdk-client:test:ci # Run tests with CI configuration and coverage
23
+
24
+ # Code quality
25
+ nx run sdk-client:lint # ESLint code linting
26
+
27
+ # Publishing
28
+ nx run sdk-client:publish --args.ver=1.0.0 --args.tag=latest
29
+ ```
30
+
31
+ ### Testing Commands
32
+ ```bash
33
+ # Run specific test file
34
+ nx test sdk-client --testNamePattern="specific test name"
35
+
36
+ # Run tests in watch mode
37
+ nx test sdk-client --watch
38
+
39
+ # Run tests with coverage
40
+ nx test sdk-client --coverage
41
+ ```
42
+
43
+ ## Architecture Overview
44
+
45
+ ### Project Structure
46
+ ```
47
+ libs/sdk/client/
48
+ ├── src/
49
+ │ ├── index.ts # Main export (createDotCMSClient)
50
+ │ ├── internal.ts # Internal utilities for other SDKs
51
+ │ └── lib/
52
+ │ ├── client/
53
+ │ │ ├── client.ts # Main DotCMSClient class
54
+ │ │ ├── content/ # Content API with query builders
55
+ │ │ ├── navigation/ # Navigation API
56
+ │ │ ├── page/ # Page API with GraphQL support
57
+ │ │ └── models/ # TypeScript interfaces and types
58
+ │ └── utils/
59
+ │ └── graphql/ # GraphQL utilities
60
+ ├── project.json # Nx project configuration
61
+ ├── package.json # NPM package configuration
62
+ ├── tsconfig.json # TypeScript configuration
63
+ └── jest.config.ts # Jest testing configuration
64
+ ```
65
+
66
+ ### Key Components
67
+
68
+ **Core API Pattern**: The SDK follows a client-builder pattern with three main APIs:
69
+ - `client.page.get()` - Fetches complete page content with layout and containers
70
+ - `client.content.getCollection()` - Builder pattern for querying content collections
71
+ - `client.navigation.get()` - Fetches site navigation structure
72
+
73
+ **Build Targets**:
74
+ - `build` - Standard library build producing ESM/CJS dual packages
75
+ - `build:js` - Specialized esbuild for editor integration (outputs to `dotCMS/src/main/webapp/html/js/editor-js`)
76
+
77
+ ## Development Standards
78
+
79
+ ### TypeScript Configuration
80
+ - **Strict mode enabled**: Full TypeScript strict checking
81
+ - **Target**: ES2020 with lib support for ES2020, DOM, DOM.Iterable
82
+ - **Dual package support**: ESM and CJS through Rollup build
83
+ - **Type definitions**: `@dotcms/types` for comprehensive type safety
84
+
85
+ ### Code Patterns
86
+
87
+ #### Client Builder Pattern
88
+ ```typescript
89
+ // Main client initialization
90
+ const client = createDotCMSClient({
91
+ dotcmsUrl: 'https://instance.com',
92
+ authToken: 'token',
93
+ siteId: 'site-id'
94
+ });
95
+
96
+ // Page API - single request for complete page
97
+ const { pageAsset } = await client.page.get('/about-us');
98
+
99
+ // Content API - fluent builder pattern
100
+ const blogs = await client.content
101
+ .getCollection('Blog')
102
+ .query((qb) => qb.field('title').equals('dotCMS*'))
103
+ .limit(10)
104
+ .sortBy([{ field: 'publishDate', direction: 'desc' }]);
105
+ ```
106
+
107
+ #### GraphQL Integration
108
+ ```typescript
109
+ // Extend page requests with GraphQL for additional content
110
+ const { pageAsset, content } = await client.page.get('/about-us', {
111
+ graphql: {
112
+ page: `title vanityUrl { url }`,
113
+ content: {
114
+ blogs: `BlogCollection(limit: 3) { title urlTitle }`,
115
+ navigation: `DotNavigation(uri: "/", depth: 2) { href title }`
116
+ }
117
+ }
118
+ });
119
+ ```
120
+
121
+ #### Query Builder Pattern
122
+ ```typescript
123
+ // Fluent query building for content collections
124
+ const query = await client.content
125
+ .getCollection('Product')
126
+ .query((qb) => qb
127
+ .field('category').equals('electronics')
128
+ .and()
129
+ .field('price').raw(':[100 TO 500]')
130
+ .not()
131
+ .field('discontinued').equals('true')
132
+ )
133
+ .limit(10)
134
+ .page(1);
135
+ ```
136
+
137
+ ### Testing Standards
138
+
139
+ #### Jest Configuration
140
+ - **Framework**: Jest with TypeScript support
141
+ - **Coverage**: Outputs to `../../../coverage/libs/sdk/client`
142
+ - **File patterns**: `**/*.spec.ts` and `**/*.test.ts`
143
+ - **CI mode**: Supports CI configuration with coverage reporting
144
+
145
+ #### Test Patterns
146
+ ```typescript
147
+ // Use descriptive test names and group related tests
148
+ describe('DotCMSClient', () => {
149
+ describe('page.get()', () => {
150
+ it('should fetch page content with default options', async () => {
151
+ // Test implementation
152
+ });
153
+
154
+ it('should apply GraphQL extensions correctly', async () => {
155
+ // Test implementation
156
+ });
157
+ });
158
+ });
159
+
160
+ // Mock external dependencies
161
+ jest.mock('../utils/fetch', () => ({
162
+ dotFetch: jest.fn()
163
+ }));
164
+ ```
165
+
166
+ ### Build System Integration
167
+
168
+ #### Nx Monorepo Integration
169
+ - **Executor**: `@nx/rollup:rollup` for main build, `@nx/esbuild:esbuild` for editor build
170
+ - **Outputs**: Dual package (ESM/CJS) with proper export maps
171
+ - **Dependencies**: Automatically managed through Nx dependency graph
172
+
173
+ #### Export Configuration
174
+ ```typescript
175
+ // Package exports support both named and default imports
176
+ export { createDotCMSClient } from './lib/client/client';
177
+ export type { DotCMSClient } from './lib/client/client';
178
+
179
+ // Internal exports for framework SDKs
180
+ export { DotCMSClientImpl } from './lib/client/client-impl';
181
+ ```
182
+
183
+ ## Key Configuration Files
184
+
185
+ - **project.json**: Nx project configuration with build targets
186
+ - **package.json**: NPM package metadata with proper exports field
187
+ - **tsconfig.json**: TypeScript configuration with strict mode
188
+ - **tsconfig.lib.json**: Library-specific TypeScript settings
189
+ - **jest.config.ts**: Jest testing configuration
190
+
191
+ ## Development Workflow
192
+
193
+ ### Standard Development Flow
194
+ 1. **Make changes** → Test locally with `nx test sdk-client`
195
+ 2. **Lint code** → `nx lint sdk-client`
196
+ 3. **Build library** → `nx build sdk-client`
197
+ 4. **Integration testing** → Test with dependent SDKs or examples
198
+
199
+ ### Editor Integration Build
200
+ For changes affecting the editor integration:
201
+ 1. **Build editor version** → `nx run sdk-client:build:js`
202
+ 2. **Test in dotCMS editor** → Verify functionality in dotCMS admin
203
+ 3. **Clean build artifacts** → Script automatically removes temporary files
204
+
205
+ ### Release Process
206
+ 1. **Version bump** → Update version in `package.json`
207
+ 2. **Build all targets** → `nx build sdk-client`
208
+ 3. **Publish** → `nx run sdk-client:publish --args.ver=X.X.X --args.tag=latest`
209
+
210
+ ## Integration Context
211
+
212
+ ### Framework SDKs
213
+ This client SDK serves as the foundation for:
214
+ - `@dotcms/react` - React integration with UVE support
215
+ - `@dotcms/angular` - Angular integration with UVE support
216
+ - `@dotcms/uve` - Universal Visual Editor low-level integration
217
+
218
+ ### Dependencies
219
+ - **Runtime**: `consola` for logging
220
+ - **Development**: `@dotcms/types` for TypeScript definitions
221
+ - **Peer Dependencies**: Framework-specific SDKs extend this client
222
+
223
+ ## Common Development Tasks
224
+
225
+ ### Adding New API Methods
226
+ 1. **Define interfaces** in `lib/client/models/`
227
+ 2. **Implement method** in appropriate API class (`page/`, `content/`, `navigation/`)
228
+ 3. **Export** from main `index.ts`
229
+ 4. **Add tests** following existing patterns
230
+ 5. **Update TypeScript types** if needed
231
+
232
+ ### Extending Query Builders
233
+ 1. **Add method** to appropriate builder class
234
+ 2. **Update builder interface** with new method signature
235
+ 3. **Add tests** for new query capabilities
236
+ 4. **Document** in README with examples
237
+
238
+ ### GraphQL Integration
239
+ 1. **Define GraphQL fragments** in `utils/graphql/`
240
+ 2. **Add to page API** GraphQL parameter types
241
+ 3. **Test** with various GraphQL queries
242
+ 4. **Update documentation** with new capabilities
243
+
244
+ ## Summary Checklist
245
+ - ✅ Use Nx commands for all build/test operations
246
+ - ✅ Follow client-builder pattern for new APIs
247
+ - ✅ Maintain TypeScript strict mode compliance
248
+ - ✅ Write comprehensive Jest tests for new features
249
+ - ✅ Use proper export patterns for dual package support
250
+ - ✅ Test both ESM and CJS builds
251
+ - ✅ Verify editor integration when making core changes
252
+ - ❌ Avoid breaking changes to public API without migration plan
253
+ - ❌ Don't add runtime dependencies without careful consideration
package/MIGRATION.md ADDED
@@ -0,0 +1,329 @@
1
+ # Migration Guide: Alpha to 1.0.X
2
+
3
+ If you're upgrading from the `alpha.xx` version of `@dotcms/client`, this guide will help you migrate to the 1.0.X release. The 1.0.X version introduces several breaking changes and improvements for better developer experience, type safety, and performance.
4
+
5
+ ### Breaking Changes Summary
6
+
7
+ | Change | Alpha Version | 1.0.X Version |
8
+ |--------|---------------|----------------|
9
+ | Client Initialization | `DotCmsClient.init()` | `createDotCMSClient()` |
10
+ | Import Statement | `import { DotCmsClient }` | `import { createDotCMSClient }` |
11
+ | Navigation API | `client.nav.get()` | `client.navigation.get()` |
12
+ | Content Collection | `.fetch()` method required | Direct await on collection |
13
+ | Parameter Names | `language_id` | `languageId` |
14
+ | Response Structure | Different format | Standardized response format |
15
+
16
+ ## Step-by-Step Migration Guide
17
+
18
+ ### 1. Update Dependencies
19
+
20
+ First, update your package.json to use the latest 1.0.X version:
21
+
22
+ ```bash
23
+ # Remove the alpha version
24
+ npm uninstall @dotcms/client
25
+
26
+ # Install the 1.0.X version with types
27
+ npm install @dotcms/client@latest @dotcms/types@latest
28
+ ```
29
+
30
+ ### 2. Update Import Statements
31
+
32
+ **Before (Alpha):**
33
+ ```javascript
34
+ import { DotCmsClient } from '@dotcms/client';
35
+ ```
36
+
37
+ **After (1.0.X):**
38
+ ```javascript
39
+ import { createDotCMSClient } from '@dotcms/client';
40
+ ```
41
+
42
+ ### 3. Update Client Initialization
43
+
44
+ **Before (Alpha):**
45
+ ```javascript
46
+ const client = DotCmsClient.init({
47
+ dotcmsUrl: 'https://your-dotcms-instance.com',
48
+ authToken: 'your-auth-token',
49
+ siteId: 'your-site-id'
50
+ });
51
+ ```
52
+
53
+ **After (1.0.X):**
54
+ ```javascript
55
+ const client = createDotCMSClient({
56
+ dotcmsUrl: 'https://your-dotcms-instance.com',
57
+ authToken: 'your-auth-token',
58
+ siteId: 'your-site-id'
59
+ });
60
+ ```
61
+
62
+ ### 4. Update Navigation API Calls
63
+
64
+ **Before (Alpha):**
65
+ ```javascript
66
+ const navData = await client.nav.get({
67
+ path: '/',
68
+ depth: 2,
69
+ languageId: 1
70
+ });
71
+ ```
72
+
73
+ **After (1.0.X):**
74
+ ```javascript
75
+ const navData = await client.navigation.get('/', {
76
+ depth: 2,
77
+ languageId: 1
78
+ });
79
+ ```
80
+
81
+ ### 5. Update Content Collection Queries
82
+
83
+ **Before (Alpha):**
84
+ ```javascript
85
+ const collectionResponse = await client.content
86
+ .getCollection('Blog')
87
+ .limit(10)
88
+ .page(1)
89
+ .fetch(); // .fetch() was required
90
+
91
+ console.log(collectionResponse.contentlets);
92
+ ```
93
+
94
+ **After (1.0.X):**
95
+ ```javascript
96
+ const blogs = await client.content
97
+ .getCollection('Blog')
98
+ .limit(10)
99
+ .page(1); // Direct await, no .fetch() needed
100
+
101
+ console.log(blogs.contentlets);
102
+ ```
103
+
104
+ ### 6. Update Parameter Names
105
+
106
+ **Before (Alpha):**
107
+ ```javascript
108
+ const pageData = await client.page.get({
109
+ path: '/your-page-path',
110
+ language_id: 1, // underscore naming
111
+ personaId: 'optional-persona-id'
112
+ });
113
+ ```
114
+
115
+ **After (1.0.X):**
116
+ ```javascript
117
+ const { pageAsset } = await client.page.get('/your-page-path', {
118
+ languageId: 1, // camelCase naming
119
+ personaId: 'optional-persona-id'
120
+ });
121
+ ```
122
+
123
+ 🚨 Also notice that the url path is now the first param of the `get` method and is not longer in the object.
124
+
125
+ ### 7. Update Response Handling
126
+
127
+ **Before (Alpha):**
128
+ ```javascript
129
+ const pageData = await client.page.get({ path: '/about-us' });
130
+ // Direct access to page data
131
+ console.log(pageData.page.title);
132
+ ```
133
+
134
+ **After (1.0.X):**
135
+ ```javascript
136
+ const { pageAsset } = await client.page.get('/about-us');
137
+ // Destructured response
138
+ console.log(pageAsset.page.title);
139
+ ```
140
+
141
+ ### 8. Update Query Builder Syntax
142
+
143
+ **Before (Alpha):**
144
+ ```javascript
145
+ const complexQueryResponse = await client.content
146
+ .getCollection('Blog')
147
+ .query((qb) => qb.field('author').equals('John Doe').and().field('title').equals('Hello World'))
148
+ .fetch();
149
+ ```
150
+
151
+ **After (1.0.X):**
152
+ ```javascript
153
+ const blogs = await client.content
154
+ .getCollection('Blog')
155
+ .query((qb) => qb.field('author').equals('John Doe').and().field('title').equals('Hello World'));
156
+ ```
157
+
158
+ ### 9. Update Sorting Syntax
159
+
160
+ **Before (Alpha):**
161
+ ```javascript
162
+ const sortedResponse = await client.content
163
+ .getCollection('Blog')
164
+ .sortBy([{ field: 'title', order: 'asc' }])
165
+ .fetch();
166
+ ```
167
+
168
+ **After (1.0.X):**
169
+ ```javascript
170
+ const blogs = await client.content
171
+ .getCollection('Blog')
172
+ .sortBy([{ field: 'title', order: 'asc' }]);
173
+ ```
174
+
175
+ ## Troubleshooting
176
+
177
+ ### Common Migration Issues
178
+
179
+ **Issue 1: `DotCmsClient is not a function` Error**
180
+
181
+ **Problem:** Using the old initialization method.
182
+
183
+ **Solution:**
184
+ ```javascript
185
+ // ❌ This will fail
186
+ const client = DotCmsClient.init({...});
187
+
188
+ // ✅ Use this instead
189
+ const client = createDotCMSClient({...});
190
+ ```
191
+
192
+ **Issue 2: `client.nav is not a function` Error**
193
+
194
+ **Problem:** The navigation API method name changed.
195
+
196
+ **Solution:**
197
+ ```javascript
198
+ // ❌ This will fail
199
+ const navData = await client.nav.get({...});
200
+
201
+ // ✅ Use this instead
202
+ const navData = await client.navigation.get('/', {...});
203
+ ```
204
+
205
+ **Issue 3: `Cannot read property 'contentlets' of undefined` Error**
206
+
207
+ **Problem:** Response structure changed, especially for page requests.
208
+
209
+ **Solution:**
210
+ ```javascript
211
+ // ❌ This might fail
212
+ const pageData = await client.page.get('/path');
213
+ console.log(pageData.page.title);
214
+
215
+ // ✅ Use destructuring
216
+ const { pageAsset } = await client.page.get('/path');
217
+ console.log(pageAsset.page.title);
218
+ ```
219
+
220
+ **Issue 4: Content Collection `.fetch()` Method Not Found**
221
+
222
+ **Problem:** The `.fetch()` method is no longer required.
223
+
224
+ **Solution:**
225
+ ```javascript
226
+ // ❌ This will fail
227
+ const result = await client.content.getCollection('Blog').fetch();
228
+
229
+ // ✅ Direct await
230
+ const result = await client.content.getCollection('Blog');
231
+ ```
232
+
233
+ **Issue 5: TypeScript Errors After Migration**
234
+
235
+ **Problem:** Missing type definitions or changed interfaces.
236
+
237
+ **Solution:**
238
+ ```bash
239
+ # Install the types package
240
+ npm install @dotcms/types@latest
241
+
242
+ # Update your TypeScript imports
243
+ import type { DotCMSClient } from '@dotcms/client';
244
+ import type { DotCMSPageAsset } from '@dotcms/types';
245
+ ```
246
+
247
+ **Issue 6: Build or Runtime Errors with Module Resolution**
248
+
249
+ **Problem:** Module resolution issues after updating packages.
250
+
251
+ **Solution:**
252
+ ```bash
253
+ # Clear node_modules and reinstall
254
+ rm -rf node_modules package-lock.json
255
+ npm install
256
+
257
+ # Or if using Yarn
258
+ rm -rf node_modules yarn.lock
259
+ yarn install
260
+ ```
261
+
262
+ **Issue 7: Unexpected Response Format**
263
+
264
+ **Problem:** Trying to access properties that don't exist in the new response format.
265
+
266
+ **Solution:**
267
+ ```javascript
268
+ // ❌ This might fail
269
+ const response = await client.page.get('/path');
270
+ console.log(response.containers); // This property structure changed
271
+
272
+ // ✅ Use the new structure
273
+ const { pageAsset } = await client.page.get('/path');
274
+ console.log(pageAsset.containers);
275
+ ```
276
+
277
+ ### Migration Checklist
278
+
279
+ Use this checklist to ensure you've completed all necessary migration steps:
280
+
281
+ - [ ] **Dependencies**: Update package.json dependencies
282
+ - [ ] **Imports**: Change import statements from `DotCmsClient` to `createDotCMSClient`
283
+ - [ ] **Initialization**: Update client initialization method
284
+ - [ ] **Navigation API**: Replace `client.nav.get()` with `client.navigation.get()`
285
+ - [ ] **Content Collections**: Remove `.fetch()` calls from content collection queries
286
+ - [ ] **Parameter Names**: Update parameter names from snake_case to camelCase
287
+ - [ ] **Response Handling**: Update response destructuring (especially for page requests)
288
+ - [ ] **TypeScript**: Install and configure TypeScript types if using TypeScript
289
+ - [ ] **Testing**: Test all API calls to ensure they work correctly
290
+ - [ ] **Documentation**: Update any internal documentation or comments
291
+
292
+ ### Testing Your Migration
293
+
294
+ After completing the migration, test these key areas:
295
+
296
+ 1. **Client Initialization**: Verify the client initializes without errors
297
+ 2. **Page Fetching**: Test fetching different pages and accessing their properties
298
+ 3. **Content Collections**: Test querying different content types with various filters
299
+ 4. **Navigation**: Test navigation API calls with different parameters
300
+ 5. **Error Handling**: Verify error handling works as expected
301
+ 6. **TypeScript**: If using TypeScript, ensure there are no type errors
302
+
303
+ ### Performance Considerations
304
+
305
+ The 1.0.X version includes several performance improvements:
306
+
307
+ - **Optimized Requests**: Reduced request overhead and improved caching
308
+ - **Better Error Handling**: More efficient error processing
309
+ - **Type Safety**: Compile-time checks prevent runtime errors
310
+ - **GraphQL Integration**: More efficient data fetching with GraphQL
311
+
312
+ ### Need Help?
313
+
314
+ If you encounter issues during migration that aren't covered here:
315
+
316
+ 1. **Check the Full Documentation**: Review the [README.md](./README.md) for updated API documentation
317
+ 2. **GitHub Issues**: [Open an issue](https://github.com/dotCMS/core/issues/new/choose) on GitHub with your specific problem
318
+ 3. **Community Support**: Visit our [community forum](https://community.dotcms.com/) for community support
319
+
320
+ ### Additional Resources
321
+
322
+ - [dotCMS Client SDK Documentation](./README.md)
323
+ - [dotCMS API Documentation](https://dev.dotcms.com/docs/rest-api)
324
+ - [dotCMS GraphQL Documentation](https://dev.dotcms.com/docs/graphql)
325
+ - [dotCMS Community Forum](https://community.dotcms.com/)
326
+
327
+ ---
328
+
329
+ **Note**: This migration guide is specific to upgrading from the alpha version to the 1.0.X release. For other version migrations, please refer to the appropriate documentation or release notes.