@dotcms/client 1.0.6 → 1.1.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/README.md CHANGED
@@ -17,7 +17,9 @@ The `@dotcms/client` is a powerful JavaScript/TypeScript SDK designed to simplif
17
17
  - **Security First**: Handles authentication and requests securely
18
18
  - **Developer Experience**: Rich autocompletion and documentation
19
19
 
20
- > **📋 Migrating from Alpha Version?** If you're upgrading from the alpha version of `@dotcms/client`, please see our [Migration Guide](./MIGRATION.md) for step-by-step instructions and troubleshooting tips.
20
+ > **📋 Migration Guides:**
21
+ > - **From Alpha Version?** If you're upgrading from the alpha version of `@dotcms/client`, please see our [Migration Guide](./MIGRATION.md) for step-by-step instructions.
22
+ > - **From v1.0.x to v1.1.1?** See the [Changelog](#v111) section below for new features and improvements.
21
23
 
22
24
  ## Table of Contents
23
25
 
@@ -34,9 +36,11 @@ The `@dotcms/client` is a powerful JavaScript/TypeScript SDK designed to simplif
34
36
  - [How to Enable Page Editing](#how-to-enable-page-editing)
35
37
  - [API Reference](#api-reference)
36
38
  - [Client Initialization](#client-initialization)
39
+ - [HTTP Client Configuration](#http-client-configuration)
37
40
  - [page.get() Method](#pageget-method)
38
41
  - [content.getCollection() Method](#contentgetcollection-method)
39
42
  - [navigation.get() Method](#navigationget-method)
43
+ - [Error Handling](#error-handling)
40
44
  - [Concepts & Architecture](#concepts--architecture)
41
45
  - [Key Concepts](#key-concepts)
42
46
  - [Choosing the Right Method](#choosing-the-right-method)
@@ -45,6 +49,8 @@ The `@dotcms/client` is a powerful JavaScript/TypeScript SDK designed to simplif
45
49
  - [dotCMS Support](#dotcms-support)
46
50
  - [How To Contribute](#how-to-contribute)
47
51
  - [Licensing Information](#licensing-information)
52
+ - [Changelog](#changelog)
53
+ - [v1.1.1](#v111)
48
54
 
49
55
  ## Getting Started
50
56
 
@@ -289,22 +295,71 @@ createDotCMSClient(config: DotCMSClientConfig): DotCMSClient
289
295
 
290
296
  #### Parameters
291
297
 
292
- | Option | Type | Required | Description |
293
- | ---------------- | -------------- | -------- | ------------------------------------------------------------- |
294
- | `dotcmsUrl` | string | ✅ | Your dotCMS instance URL |
295
- | `authToken` | string | ✅ | Authentication token |
296
- | `siteId` | string | ❌ | Site identifier (falls back to default site if not specified) |
297
- | `requestOptions` | RequestOptions | ❌ | Additional fetch options |
298
+ | Option | Type | Required | Description |
299
+ | ---------------- | ----------------- | -------- | ------------------------------------------------------------- |
300
+ | `dotcmsUrl` | string | ✅ | Your dotCMS instance URL |
301
+ | `authToken` | string | ✅ | Authentication token |
302
+ | `siteId` | string | ❌ | Site identifier (falls back to default site if not specified) |
303
+ | `requestOptions` | DotRequestOptions | ❌ | Additional request options |
304
+ | `httpClient` | DotHttpClient | ❌ | Custom HTTP client implementation |
298
305
 
299
306
  #### Example
300
307
  ```typescript
301
308
  const client = createDotCMSClient({
302
309
  dotcmsUrl: 'https://your-dotcms-instance.com',
303
310
  authToken: 'your-auth-token',
304
- siteId: 'your-site-id'
311
+ siteId: 'your-site-id',
312
+ httpClient: customHttpClient // Optional: provide custom HTTP client
305
313
  });
306
314
  ```
307
315
 
316
+ ### HTTP Client Configuration
317
+
318
+ The SDK now supports custom HTTP client implementations for advanced use cases. By default, it uses the built-in `FetchHttpClient` based on the native Fetch API.
319
+
320
+ #### Default HTTP Client
321
+ ```typescript
322
+ // The SDK automatically uses FetchHttpClient if no custom client is provided
323
+ const client = createDotCMSClient({
324
+ dotcmsUrl: 'https://your-dotcms-instance.com',
325
+ authToken: 'your-auth-token'
326
+ // No httpClient specified - uses FetchHttpClient internally
327
+ });
328
+ ```
329
+
330
+ #### Custom HTTP Client
331
+ ```typescript
332
+ import { BaseHttpClient } from '@dotcms/types';
333
+
334
+ // Implement your own HTTP client by extending BaseHttpClient
335
+ class CustomHttpClient extends BaseHttpClient {
336
+ async request<T>(url: string, options?: DotRequestOptions): Promise<T> {
337
+ // Your custom implementation
338
+ // Must handle JSON parsing, error conversion, etc.
339
+ return customRequest(url, options);
340
+ }
341
+ }
342
+
343
+ const client = createDotCMSClient({
344
+ dotcmsUrl: 'https://your-dotcms-instance.com',
345
+ authToken: 'your-auth-token',
346
+ httpClient: new CustomHttpClient()
347
+ });
348
+ ```
349
+
350
+ #### When You Might Need a Custom HTTP Client
351
+
352
+ - **Corporate Proxies**: "All our API calls must go through our corporate proxy with authentication"
353
+ - **Request Monitoring**: "We need to log every dotCMS API call for our compliance audit trail"
354
+ - **Custom Authentication**: "Our enterprise SSO requires adding custom headers to every request"
355
+ - **Performance Optimization**: "We want to reuse HTTP connections and implement our own retry logic"
356
+
357
+ #### HTTP Client Features
358
+ - **Automatic Response Parsing**: JSON responses are automatically parsed
359
+ - **Error Standardization**: All HTTP failures are converted to `DotHttpError` instances
360
+ - **Type Safety**: Full TypeScript support with generic response types
361
+ - **Universal Compatibility**: Works in both browser and Node.js environments
362
+
308
363
  ### page.get() Method
309
364
 
310
365
  ```typescript
@@ -397,6 +452,112 @@ get(
397
452
  const nav = await client.navigation.get('/', { depth: 2 });
398
453
  ```
399
454
 
455
+ ### Error Handling
456
+
457
+ The SDK provides comprehensive error handling with specific error types for different API operations. These domain-specific errors may wrap `DotHttpError` instances when HTTP failures occur and include contextual information to help with debugging.
458
+
459
+ #### Error Types
460
+
461
+ | Error Type | When It's Thrown | Properties |
462
+ | ------------------- | ------------------------------------------ | --------------------------------------------------- |
463
+ | `DotHttpError` | HTTP/network failures (4xx/5xx, timeouts) | `status`, `statusText`, `headers`, `body` |
464
+ | `DotErrorPage` | Page API failures | `httpError?`, `context` (query, variables) |
465
+ | `DotErrorContent` | Content API failures | `contentType`, `operation`, `httpError?`, `query?` |
466
+ | `DotErrorNavigation`| Navigation API failures | `path`, `httpError?` |
467
+
468
+ #### Basic Error Handling
469
+ ```typescript
470
+ try {
471
+ const { pageAsset } = await client.page.get('/about-us');
472
+ console.log(pageAsset.page.title);
473
+ } catch (error) {
474
+ if (error instanceof DotErrorPage) {
475
+ console.error('Page error:', error.message);
476
+ console.error('Context:', error.context);
477
+ } else {
478
+ console.error('Unexpected error:', error);
479
+ }
480
+ }
481
+ ```
482
+
483
+ #### Content Collection Error Handling
484
+ ```typescript
485
+ try {
486
+ const blogs = await client.content
487
+ .getCollection('Blog')
488
+ .limit(10);
489
+ } catch (error) {
490
+ if (error instanceof DotErrorContent) {
491
+ console.error('Content error:', error.message);
492
+ console.error('Content type:', error.contentType);
493
+ console.error('Operation:', error.operation);
494
+ if (error.httpError) {
495
+ console.error('HTTP status:', error.httpError.status);
496
+ }
497
+ }
498
+ }
499
+ ```
500
+
501
+ #### Navigation Error Handling
502
+ ```typescript
503
+ try {
504
+ const nav = await client.navigation.get('/missing-path');
505
+ } catch (error) {
506
+ if (error instanceof DotErrorNavigation) {
507
+ console.error('Navigation error:', error.message);
508
+ console.error('Path:', error.path);
509
+ if (error.httpError) {
510
+ console.error('HTTP status:', error.httpError.status);
511
+ }
512
+ }
513
+ }
514
+ ```
515
+
516
+ #### Promise-Style Error Handling
517
+ ```typescript
518
+ // Content collections support .then() with error handling
519
+ const result = await client.content
520
+ .getCollection('Blog')
521
+ .limit(10)
522
+ .then(
523
+ (response) => {
524
+ console.log('Success:', response.contentlets);
525
+ return response;
526
+ },
527
+ (error) => {
528
+ console.error('Error:', error.message);
529
+ // Return fallback data or re-throw
530
+ return { contentlets: [], total: 0 };
531
+ }
532
+ );
533
+ ```
534
+
535
+ #### Common Error Scenarios
536
+
537
+ **401 Unauthorized**
538
+ ```typescript
539
+ // Missing or invalid authentication token
540
+ DotHttpError: status 401, message: "Authentication required"
541
+ ```
542
+
543
+ **403 Forbidden**
544
+ ```typescript
545
+ // Valid token but insufficient permissions
546
+ DotHttpError: status 403, message: "Access denied"
547
+ ```
548
+
549
+ **404 Not Found**
550
+ ```typescript
551
+ // Page, content, or navigation path not found
552
+ DotErrorPage: "Page /missing-page not found. Check the page URL and permissions."
553
+ ```
554
+
555
+ **Network Errors**
556
+ ```typescript
557
+ // Connection issues, timeouts, etc.
558
+ DotHttpError: "Network request failed"
559
+ ```
560
+
400
561
  ## Concepts & Architecture
401
562
 
402
563
  ### Key Concepts
@@ -477,6 +638,116 @@ GitHub pull requests are the preferred method to contribute code to dotCMS. We w
477
638
 
478
639
  Please ensure your code follows the existing style and includes appropriate tests.
479
640
 
641
+ ## Changelog
642
+
643
+ ### v1.1.1
644
+
645
+ Version 1.1.1 introduces significant improvements to error handling and HTTP client architecture. Most applications will continue to work without changes.
646
+
647
+ #### ✨ Added - Enhanced Error Handling
648
+
649
+ **New Features:**
650
+ - Introduced specific error types: `DotErrorPage`, `DotErrorContent`, `DotErrorNavigation`
651
+ - Domain-specific errors wrap `DotHttpError` instances with contextual information
652
+ - Enhanced error context and debugging information
653
+ - Improved error handling in promise chains
654
+
655
+ **Migration Required If:**
656
+ - You're catching generic `Error` instances from SDK calls
657
+ - You're using `.then()` callbacks on content collections without return values
658
+ - You're parsing raw HTTP error responses manually
659
+
660
+ **Before (v1.0.x):**
661
+ ```typescript
662
+ try {
663
+ const { pageAsset } = await client.page.get('/about');
664
+ } catch (error) {
665
+ // Generic error handling
666
+ console.error('Error:', error.message);
667
+ console.error('Status:', error.status); // May not exist
668
+ }
669
+
670
+ // Collection error handling
671
+ client.content.getCollection('Blog').then(
672
+ (response) => console.log(response),
673
+ (error) => {
674
+ console.error(error.status); // Raw HTTP status
675
+ // No return value required
676
+ }
677
+ );
678
+ ```
679
+
680
+ **After (v1.1.1):**
681
+ ```typescript
682
+ try {
683
+ const { pageAsset } = await client.page.get('/about');
684
+ } catch (error) {
685
+ // Specific error type checking
686
+ if (error instanceof DotErrorPage) {
687
+ console.error('Page Error:', error.message);
688
+ console.error('Context:', error.context);
689
+ if (error.httpError) {
690
+ console.error('HTTP Status:', error.httpError.status);
691
+ }
692
+ }
693
+ }
694
+
695
+ // Collection error handling with required return values
696
+ client.content.getCollection('Blog').then(
697
+ (response) => {
698
+ console.log(response);
699
+ return response; // Return value recommended
700
+ },
701
+ (error) => {
702
+ if (error instanceof DotErrorContent) {
703
+ console.error('Content Error:', error.contentType, error.operation);
704
+ }
705
+ return { contentlets: [], total: 0 }; // Return fallback or re-throw
706
+ }
707
+ );
708
+ ```
709
+
710
+ #### ✨ Added - HTTP Client Architecture
711
+
712
+ **New Features:**
713
+ - New `DotHttpClient` interface for pluggable HTTP implementations
714
+ - Default `FetchHttpClient` replaces direct `fetch()` calls
715
+ - Better TypeScript support for custom HTTP clients
716
+
717
+ **Migration Required If:**
718
+ - You're extending or mocking SDK internals
719
+ - You need custom HTTP behavior (proxies, interceptors, etc.)
720
+
721
+ **New Capabilities:**
722
+ ```typescript
723
+ // Custom HTTP client support
724
+ const client = createDotCMSClient({
725
+ dotcmsUrl: 'https://your-instance.com',
726
+ authToken: 'your-token',
727
+ httpClient: new CustomHttpClient() // Optional: custom implementation
728
+ });
729
+ ```
730
+
731
+ #### 🔄 Changed - Type Updates
732
+
733
+ **Improvements:**
734
+ - `RequestOptions` renamed to `DotRequestOptions`
735
+ - GraphQL response types improved
736
+ - Error response types standardized
737
+
738
+ **Migration Required If:**
739
+ - You're importing `RequestOptions` directly
740
+ - You're using internal type definitions
741
+
742
+ **Update Imports:**
743
+ ```typescript
744
+ // Before
745
+ import { RequestOptions } from '@dotcms/types';
746
+
747
+ // After
748
+ import { DotRequestOptions } from '@dotcms/types';
749
+ ```
750
+
480
751
  ### Licensing Information
481
752
 
482
753
  dotCMS comes in multiple editions and as such is dual-licensed. The dotCMS Community Edition is licensed under the GPL 3.0 and is freely available for download, customization, and deployment for use within organizations of all stripes. dotCMS Enterprise Editions (EE) adds several enterprise features and is available via a supported, indemnified commercial license from dotCMS. For the differences between the editions, see [the feature page](http://www.dotcms.com/cms-platform/features).