@dotcms/client 0.0.1-alpha.0
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 +113 -0
- package/package.json +28 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +3 -0
- package/src/index.js.map +1 -0
- package/src/lib/postMessageToEditor.d.ts +45 -0
- package/src/lib/postMessageToEditor.js +37 -0
- package/src/lib/postMessageToEditor.js.map +1 -0
- package/src/lib/sdk-js-client.d.ts +171 -0
- package/src/lib/sdk-js-client.js +152 -0
- package/src/lib/sdk-js-client.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# @dotcms/client
|
|
2
|
+
|
|
3
|
+
`@dotcms/client` is the official dotCMS JavaScript library designed to simplify interactions with the DotCMS REST APIs.
|
|
4
|
+
|
|
5
|
+
This client library provides a streamlined, promise-based interface to fetch pages and navigation API.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
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.
|
|
11
|
+
- Comprehensive TypeScript typings for better development experience.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Install the package via npm:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @dotcms/client
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or using Yarn:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
yarn add @dotcms/client
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
First, initialize the client with your DotCMS instance details.
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import { dotcmsClient } from '@dotcms/client';
|
|
33
|
+
|
|
34
|
+
const client = dotcmsClient.init({
|
|
35
|
+
dotcmsUrl: 'https://your-dotcms-instance.com',
|
|
36
|
+
authToken: 'your-auth-token',
|
|
37
|
+
siteId: 'your-site-id'
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Fetching a Page
|
|
42
|
+
|
|
43
|
+
Retrieve the elements of any page in your DotCMS system in JSON format.
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
const pageData = await client.getPage({
|
|
47
|
+
path: '/your-page-path',
|
|
48
|
+
language_id: 1,
|
|
49
|
+
personaId: 'optional-persona-id'
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
console.log(pageData);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Fetching Navigation
|
|
56
|
+
|
|
57
|
+
Retrieve information about the DotCMS file and folder tree.
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
const navData = await client.getNav({
|
|
61
|
+
path: '/',
|
|
62
|
+
depth: 2,
|
|
63
|
+
languageId: 1
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
console.log(navData);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## API Reference
|
|
70
|
+
|
|
71
|
+
Detailed documentation of the `@dotcms/client` methods, parameters, and types can be found below:
|
|
72
|
+
|
|
73
|
+
### `dotcmsClient.init(config: ClientConfig): DotCmsClient`
|
|
74
|
+
|
|
75
|
+
Initializes the DotCMS client with the specified configuration.
|
|
76
|
+
|
|
77
|
+
### `DotCmsClient.getPage(options: PageApiOptions): Promise<unknown>`
|
|
78
|
+
|
|
79
|
+
Retrieves the specified page's elements from your DotCMS system in JSON format.
|
|
80
|
+
|
|
81
|
+
### `DotCmsClient.getNav(options: NavApiOptions): Promise<unknown>`
|
|
82
|
+
|
|
83
|
+
Retrieves information about the DotCMS file and folder tree.
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
## Contributing
|
|
87
|
+
|
|
88
|
+
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).
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
## Licensing
|
|
92
|
+
|
|
93
|
+
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 a number of enterprise features and is available via a supported, indemnified commercial license from dotCMS. For the differences between the editions, see [the feature page](http://dotcms.com/cms-platform/features).
|
|
94
|
+
|
|
95
|
+
## Support
|
|
96
|
+
|
|
97
|
+
If you need help or have any questions, please [open an issue](https://github.com/dotCMS/core/issues/new/choose) in the GitHub repository.
|
|
98
|
+
|
|
99
|
+
## Documentation
|
|
100
|
+
|
|
101
|
+
Always refer to the official [DotCMS documentation](https://www.dotcms.com/docs/latest/) for comprehensive guides and API references.
|
|
102
|
+
|
|
103
|
+
## Getting Help
|
|
104
|
+
|
|
105
|
+
| Source | Location |
|
|
106
|
+
| --------------- | ------------------------------------------------------------------- |
|
|
107
|
+
| Installation | [Installation](https://dotcms.com/docs/latest/installation) |
|
|
108
|
+
| Documentation | [Documentation](https://dotcms.com/docs/latest/table-of-contents) |
|
|
109
|
+
| Videos | [Helpful Videos](http://dotcms.com/videos/) |
|
|
110
|
+
| Code Examples | [Codeshare](https://dotcms.com/codeshare/) |
|
|
111
|
+
| Forums/Listserv | [via Google Groups](https://groups.google.com/forum/#!forum/dotCMS) |
|
|
112
|
+
| Twitter | @dotCMS |
|
|
113
|
+
| Main Site | [dotCMS.com](https://dotcms.com/) |
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dotcms/client",
|
|
3
|
+
"version": "0.0.1-alpha.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Official JavaScript library for interacting with DotCMS REST APIs.",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/dotCMS/core/tree/master/core-web/libs/sdk/client"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"dotCMS",
|
|
12
|
+
"CMS",
|
|
13
|
+
"Content Management",
|
|
14
|
+
"API Client",
|
|
15
|
+
"REST API"
|
|
16
|
+
],
|
|
17
|
+
"author": "dotcms <dev@dotcms.com>",
|
|
18
|
+
"license": "GPL-3.0",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/dotCMS/core/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/dotCMS/core/tree/master/core-web/libs/sdk/client/README.md",
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"tslib": "^2.3.0"
|
|
25
|
+
},
|
|
26
|
+
"main": "./src/index.js",
|
|
27
|
+
"types": "./src/index.d.ts"
|
|
28
|
+
}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../libs/sdk/client/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Actions send to the dotcms editor
|
|
3
|
+
*
|
|
4
|
+
* @export
|
|
5
|
+
* @enum {number}
|
|
6
|
+
*/
|
|
7
|
+
export declare enum CUSTOMER_ACTIONS {
|
|
8
|
+
/**
|
|
9
|
+
* Tell the dotcms editor that page change
|
|
10
|
+
*/
|
|
11
|
+
SET_URL = "set-url",
|
|
12
|
+
/**
|
|
13
|
+
* Send the element position of the rows, columnsm containers and contentlets
|
|
14
|
+
*/
|
|
15
|
+
SET_BOUNDS = "set-bounds",
|
|
16
|
+
/**
|
|
17
|
+
* Send the information of the hovered contentlet
|
|
18
|
+
*/
|
|
19
|
+
SET_CONTENTLET = "set-contentlet",
|
|
20
|
+
/**
|
|
21
|
+
* Tell the editor that the page is being scrolled
|
|
22
|
+
*/
|
|
23
|
+
IFRAME_SCROLL = "scroll",
|
|
24
|
+
NOOP = "noop"
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Post message props
|
|
28
|
+
*
|
|
29
|
+
* @export
|
|
30
|
+
* @template T
|
|
31
|
+
* @interface PostMessageProps
|
|
32
|
+
*/
|
|
33
|
+
type PostMessageProps<T> = {
|
|
34
|
+
action: CUSTOMER_ACTIONS;
|
|
35
|
+
payload?: T;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Post message to dotcms page editor
|
|
39
|
+
*
|
|
40
|
+
* @export
|
|
41
|
+
* @template T
|
|
42
|
+
* @param {PostMessageProps<T>} message
|
|
43
|
+
*/
|
|
44
|
+
export declare function postMessageToEditor<T = unknown>(message: PostMessageProps<T>): void;
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Actions send to the dotcms editor
|
|
3
|
+
*
|
|
4
|
+
* @export
|
|
5
|
+
* @enum {number}
|
|
6
|
+
*/
|
|
7
|
+
export var CUSTOMER_ACTIONS;
|
|
8
|
+
(function (CUSTOMER_ACTIONS) {
|
|
9
|
+
/**
|
|
10
|
+
* Tell the dotcms editor that page change
|
|
11
|
+
*/
|
|
12
|
+
CUSTOMER_ACTIONS["SET_URL"] = "set-url";
|
|
13
|
+
/**
|
|
14
|
+
* Send the element position of the rows, columnsm containers and contentlets
|
|
15
|
+
*/
|
|
16
|
+
CUSTOMER_ACTIONS["SET_BOUNDS"] = "set-bounds";
|
|
17
|
+
/**
|
|
18
|
+
* Send the information of the hovered contentlet
|
|
19
|
+
*/
|
|
20
|
+
CUSTOMER_ACTIONS["SET_CONTENTLET"] = "set-contentlet";
|
|
21
|
+
/**
|
|
22
|
+
* Tell the editor that the page is being scrolled
|
|
23
|
+
*/
|
|
24
|
+
CUSTOMER_ACTIONS["IFRAME_SCROLL"] = "scroll";
|
|
25
|
+
CUSTOMER_ACTIONS["NOOP"] = "noop";
|
|
26
|
+
})(CUSTOMER_ACTIONS || (CUSTOMER_ACTIONS = {}));
|
|
27
|
+
/**
|
|
28
|
+
* Post message to dotcms page editor
|
|
29
|
+
*
|
|
30
|
+
* @export
|
|
31
|
+
* @template T
|
|
32
|
+
* @param {PostMessageProps<T>} message
|
|
33
|
+
*/
|
|
34
|
+
export function postMessageToEditor(message) {
|
|
35
|
+
window.parent.postMessage(message, '*');
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=postMessageToEditor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postMessageToEditor.js","sourceRoot":"","sources":["../../../../../../libs/sdk/client/src/lib/postMessageToEditor.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAN,IAAY,gBAkBX;AAlBD,WAAY,gBAAgB;IACxB;;OAEG;IACH,uCAAmB,CAAA;IACnB;;OAEG;IACH,6CAAyB,CAAA;IACzB;;OAEG;IACH,qDAAiC,CAAA;IACjC;;OAEG;IACH,4CAAwB,CAAA;IACxB,iCAAa,CAAA;AACjB,CAAC,EAlBW,gBAAgB,KAAhB,gBAAgB,QAkB3B;AAcD;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAc,OAA4B;IACzE,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
export interface ClientConfig {
|
|
2
|
+
/**
|
|
3
|
+
* The URL of the dotCMS instance.
|
|
4
|
+
*
|
|
5
|
+
* @description This is the URL of the dotCMS instance you want to interact with. Ensure to include the protocol (http or https).
|
|
6
|
+
* @example `https://demo.dotcms.com`
|
|
7
|
+
* @type {string}
|
|
8
|
+
* @required
|
|
9
|
+
*/
|
|
10
|
+
dotcmsUrl: string;
|
|
11
|
+
/**
|
|
12
|
+
* The id of the site you want to interact with.
|
|
13
|
+
*
|
|
14
|
+
* @description to get the site id, go to the site you want to interact with and copy the id from the History tab
|
|
15
|
+
*
|
|
16
|
+
* @type {string}
|
|
17
|
+
* @required
|
|
18
|
+
*/
|
|
19
|
+
siteId?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The authentication token to use for the requests. If not provided, it will fallback to default site.
|
|
22
|
+
*
|
|
23
|
+
* @description you can get the auth token from our UI {@link https://www.dotcms.com/docs/latest/rest-api-authentication#creating-an-api-token-in-the-ui}
|
|
24
|
+
*
|
|
25
|
+
* @type {string}
|
|
26
|
+
* @required
|
|
27
|
+
*/
|
|
28
|
+
authToken: string;
|
|
29
|
+
}
|
|
30
|
+
type PageApiOptions = {
|
|
31
|
+
/**
|
|
32
|
+
* The path of the page you want to retrieve.
|
|
33
|
+
*
|
|
34
|
+
* @type {string}
|
|
35
|
+
*/
|
|
36
|
+
path: string;
|
|
37
|
+
/**
|
|
38
|
+
* The id of the site you want to interact with. If not provided, the one from the config will be used.
|
|
39
|
+
*
|
|
40
|
+
* @type {number}
|
|
41
|
+
*/
|
|
42
|
+
siteId?: string;
|
|
43
|
+
/**
|
|
44
|
+
* The language id of the page you want to retrieve. If not provided will use the default language of the site.
|
|
45
|
+
*
|
|
46
|
+
* @type {number}
|
|
47
|
+
*/
|
|
48
|
+
language_id?: number;
|
|
49
|
+
/**
|
|
50
|
+
* The id of the persona you want to retrieve the page for.
|
|
51
|
+
*
|
|
52
|
+
* @type {string}
|
|
53
|
+
*/
|
|
54
|
+
personaId?: string;
|
|
55
|
+
/**
|
|
56
|
+
* If you want to fire the rules set on the page
|
|
57
|
+
*
|
|
58
|
+
* @type {boolean}
|
|
59
|
+
*/
|
|
60
|
+
fireRules?: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Allows access to related content via the Relationship fields of contentlets on a Page; 0 (default)
|
|
63
|
+
*
|
|
64
|
+
* @type {number}
|
|
65
|
+
*/
|
|
66
|
+
depth?: number;
|
|
67
|
+
};
|
|
68
|
+
type NavApiOptions = {
|
|
69
|
+
/**
|
|
70
|
+
* The root path to begin traversing the folder tree.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* `/api/v1/nav/` starts from the root of the site
|
|
74
|
+
* @example
|
|
75
|
+
* `/about-us` starts from the "About Us" folder
|
|
76
|
+
*
|
|
77
|
+
* @type {string}
|
|
78
|
+
*/
|
|
79
|
+
path: string;
|
|
80
|
+
/**
|
|
81
|
+
* The depth of the folder tree to return.
|
|
82
|
+
* @example
|
|
83
|
+
* `1` returns only the element specified in the path.
|
|
84
|
+
* @example
|
|
85
|
+
* `2` returns the element specified in the path, and if that element is a folder, returns all direct children of that folder.
|
|
86
|
+
* @example
|
|
87
|
+
* `3` returns all children and grandchildren of the element specified in the path.
|
|
88
|
+
*
|
|
89
|
+
* @type {number}
|
|
90
|
+
*/
|
|
91
|
+
depth?: number;
|
|
92
|
+
/**
|
|
93
|
+
* The language ID of content to return.
|
|
94
|
+
* @example
|
|
95
|
+
* `1` (or unspecified) returns content in the default language of the site.
|
|
96
|
+
*
|
|
97
|
+
* @link https://www.dotcms.com/docs/latest/system-language-properties#DefaultLanguage
|
|
98
|
+
* @link https://www.dotcms.com/docs/latest/adding-and-editing-languages#LanguageID
|
|
99
|
+
* @type {number}
|
|
100
|
+
*/
|
|
101
|
+
languageId?: number;
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* `DotCmsClient` is a TypeScript class that provides methods to interact with the DotCMS REST API.
|
|
105
|
+
* It requires a configuration object on instantiation, which includes the DotCMS URL, site ID, and authentication token.
|
|
106
|
+
*
|
|
107
|
+
* @class DotCmsClient
|
|
108
|
+
*
|
|
109
|
+
* @property {ClientConfig} config - The configuration object for the DotCMS client.
|
|
110
|
+
*
|
|
111
|
+
* @method constructor(config: ClientConfig) - Constructs a new instance of the DotCmsClient class.
|
|
112
|
+
*
|
|
113
|
+
* @method getPage(options: PageApiOptions): Promise<unknown> - Retrieves all the elements of any Page in your dotCMS system in JSON format.
|
|
114
|
+
*
|
|
115
|
+
* @method getNav(options: NavApiOptions = { depth: 0, path: '/', languageId: 1 }): Promise<unknown> - Retrieves information about the dotCMS file and folder tree.
|
|
116
|
+
*
|
|
117
|
+
*/
|
|
118
|
+
export declare class DotCmsClient {
|
|
119
|
+
private config;
|
|
120
|
+
constructor(config: ClientConfig);
|
|
121
|
+
/**
|
|
122
|
+
* `getPage` is an asynchronous method of the `DotCmsClient` class that retrieves all the elements of any Page in your dotCMS system in JSON format.
|
|
123
|
+
* It takes a `PageApiOptions` object as a parameter and returns a Promise that resolves to the response from the DotCMS API.
|
|
124
|
+
*
|
|
125
|
+
* The Page API enables you to retrieve all the elements of any Page in your dotCMS system.
|
|
126
|
+
* The elements may be retrieved in JSON format.
|
|
127
|
+
*
|
|
128
|
+
* @link https://www.dotcms.com/docs/latest/page-rest-api-layout-as-a-service-laas
|
|
129
|
+
* @method getPage
|
|
130
|
+
* @async
|
|
131
|
+
* @param {PageApiOptions} options - The options for the Page API call.
|
|
132
|
+
* @returns {Promise<unknown>} - A Promise that resolves to the response from the DotCMS API.
|
|
133
|
+
* @throws {Error} - Throws an error if the options are not valid.
|
|
134
|
+
*/
|
|
135
|
+
getPage(options: PageApiOptions): Promise<unknown>;
|
|
136
|
+
/**
|
|
137
|
+
* `getNav` is an asynchronous method of the `DotCmsClient` class that retrieves information about the dotCMS file and folder tree.
|
|
138
|
+
* It takes a `NavApiOptions` object as a parameter (with default values) and returns a Promise that resolves to the response from the DotCMS API.
|
|
139
|
+
*
|
|
140
|
+
* The navigation REST API enables you to retrieve information about the dotCMS file and folder tree through REST API calls.
|
|
141
|
+
* @link https://www.dotcms.com/docs/latest/navigation-rest-api
|
|
142
|
+
* @method getNav
|
|
143
|
+
* @async
|
|
144
|
+
* @param {NavApiOptions} options - The options for the Nav API call. Defaults to `{ depth: 0, path: '/', languageId: 1 }`.
|
|
145
|
+
* @returns {Promise<unknown>} - A Promise that resolves to the response from the DotCMS API.
|
|
146
|
+
* @throws {Error} - Throws an error if the options are not valid.
|
|
147
|
+
*/
|
|
148
|
+
getNav(options?: NavApiOptions): Promise<unknown>;
|
|
149
|
+
private validatePageOptions;
|
|
150
|
+
private validateNavOptions;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* `dotcmsClient` is an object that provides a method to initialize the DotCMS SDK client.
|
|
154
|
+
* It has a single method `init` which takes a configuration object and returns an instance of the `DotCmsClient` class.
|
|
155
|
+
*
|
|
156
|
+
* @namespace dotcmsClient
|
|
157
|
+
*
|
|
158
|
+
* @method init(config: ClientConfig): DotCmsClient - Initializes the SDK client.
|
|
159
|
+
*/
|
|
160
|
+
export declare const dotcmsClient: {
|
|
161
|
+
/**
|
|
162
|
+
* `init` is a method of the `dotcmsClient` object that initializes the SDK client.
|
|
163
|
+
* It takes a configuration object as a parameter and returns an instance of the `DotCmsClient` class.
|
|
164
|
+
*
|
|
165
|
+
* @method init
|
|
166
|
+
* @param {ClientConfig} config - The configuration object for the DotCMS client.
|
|
167
|
+
* @returns {DotCmsClient} - An instance of the {@link DotCmsClient} class.
|
|
168
|
+
*/
|
|
169
|
+
init: (config: ClientConfig) => DotCmsClient;
|
|
170
|
+
};
|
|
171
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { __awaiter, __rest } from "tslib";
|
|
2
|
+
function isValidUrl(url) {
|
|
3
|
+
try {
|
|
4
|
+
new URL(url);
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
catch (error) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* `DotCmsClient` is a TypeScript class that provides methods to interact with the DotCMS REST API.
|
|
13
|
+
* It requires a configuration object on instantiation, which includes the DotCMS URL, site ID, and authentication token.
|
|
14
|
+
*
|
|
15
|
+
* @class DotCmsClient
|
|
16
|
+
*
|
|
17
|
+
* @property {ClientConfig} config - The configuration object for the DotCMS client.
|
|
18
|
+
*
|
|
19
|
+
* @method constructor(config: ClientConfig) - Constructs a new instance of the DotCmsClient class.
|
|
20
|
+
*
|
|
21
|
+
* @method getPage(options: PageApiOptions): Promise<unknown> - Retrieves all the elements of any Page in your dotCMS system in JSON format.
|
|
22
|
+
*
|
|
23
|
+
* @method getNav(options: NavApiOptions = { depth: 0, path: '/', languageId: 1 }): Promise<unknown> - Retrieves information about the dotCMS file and folder tree.
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
export class DotCmsClient {
|
|
27
|
+
constructor(config) {
|
|
28
|
+
if (!config.dotcmsUrl) {
|
|
29
|
+
throw new Error("Invalid configuration - 'dotcmsUrl' is required");
|
|
30
|
+
}
|
|
31
|
+
if (!isValidUrl(config.dotcmsUrl)) {
|
|
32
|
+
throw new Error("Invalid configuration - 'dotcmsUrl' must be a valid URL");
|
|
33
|
+
}
|
|
34
|
+
if (!config.authToken) {
|
|
35
|
+
throw new Error("Invalid configuration - 'authToken' is required");
|
|
36
|
+
}
|
|
37
|
+
this.config = config;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* `getPage` is an asynchronous method of the `DotCmsClient` class that retrieves all the elements of any Page in your dotCMS system in JSON format.
|
|
41
|
+
* It takes a `PageApiOptions` object as a parameter and returns a Promise that resolves to the response from the DotCMS API.
|
|
42
|
+
*
|
|
43
|
+
* The Page API enables you to retrieve all the elements of any Page in your dotCMS system.
|
|
44
|
+
* The elements may be retrieved in JSON format.
|
|
45
|
+
*
|
|
46
|
+
* @link https://www.dotcms.com/docs/latest/page-rest-api-layout-as-a-service-laas
|
|
47
|
+
* @method getPage
|
|
48
|
+
* @async
|
|
49
|
+
* @param {PageApiOptions} options - The options for the Page API call.
|
|
50
|
+
* @returns {Promise<unknown>} - A Promise that resolves to the response from the DotCMS API.
|
|
51
|
+
* @throws {Error} - Throws an error if the options are not valid.
|
|
52
|
+
*/
|
|
53
|
+
getPage(options) {
|
|
54
|
+
var _a, _b;
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
this.validatePageOptions(options);
|
|
57
|
+
const queryParamsObj = {};
|
|
58
|
+
for (const [key, value] of Object.entries(options)) {
|
|
59
|
+
if (value !== undefined && key !== 'path') {
|
|
60
|
+
if (key !== 'siteId') {
|
|
61
|
+
if (key === 'personaId') {
|
|
62
|
+
queryParamsObj['com.dotmarketing.persona.id'] = String(value);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
queryParamsObj[key] = String(value);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const queryHostId = (_b = (_a = options.siteId) !== null && _a !== void 0 ? _a : this.config.siteId) !== null && _b !== void 0 ? _b : '';
|
|
71
|
+
if (queryHostId) {
|
|
72
|
+
queryParamsObj['host_id'] = queryHostId;
|
|
73
|
+
}
|
|
74
|
+
const queryParams = new URLSearchParams(queryParamsObj).toString();
|
|
75
|
+
const formattedPath = options.path.startsWith('/') ? options.path : `/${options.path}`;
|
|
76
|
+
const url = `${this.config.dotcmsUrl}/api/v1/page/json${formattedPath}${queryParams ? `?${queryParams}` : ''}`;
|
|
77
|
+
const response = yield fetch(url, {
|
|
78
|
+
headers: {
|
|
79
|
+
Authorization: `Bearer ${this.config.authToken}`
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
return response.json();
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* `getNav` is an asynchronous method of the `DotCmsClient` class that retrieves information about the dotCMS file and folder tree.
|
|
87
|
+
* It takes a `NavApiOptions` object as a parameter (with default values) and returns a Promise that resolves to the response from the DotCMS API.
|
|
88
|
+
*
|
|
89
|
+
* The navigation REST API enables you to retrieve information about the dotCMS file and folder tree through REST API calls.
|
|
90
|
+
* @link https://www.dotcms.com/docs/latest/navigation-rest-api
|
|
91
|
+
* @method getNav
|
|
92
|
+
* @async
|
|
93
|
+
* @param {NavApiOptions} options - The options for the Nav API call. Defaults to `{ depth: 0, path: '/', languageId: 1 }`.
|
|
94
|
+
* @returns {Promise<unknown>} - A Promise that resolves to the response from the DotCMS API.
|
|
95
|
+
* @throws {Error} - Throws an error if the options are not valid.
|
|
96
|
+
*/
|
|
97
|
+
getNav(options = { depth: 0, path: '/', languageId: 1 }) {
|
|
98
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
this.validateNavOptions(options);
|
|
100
|
+
// Extract the 'path' from the options and prepare the rest as query parameters
|
|
101
|
+
const { path } = options, queryParamsOptions = __rest(options, ["path"]);
|
|
102
|
+
const queryParamsObj = {};
|
|
103
|
+
Object.entries(queryParamsOptions).forEach(([key, value]) => {
|
|
104
|
+
if (value !== undefined) {
|
|
105
|
+
queryParamsObj[key] = String(value);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
const queryParams = new URLSearchParams(queryParamsObj).toString();
|
|
109
|
+
// Format the URL correctly depending on the 'path' value
|
|
110
|
+
const formattedPath = path === '/' ? '/' : `/${path}`;
|
|
111
|
+
const url = `${this.config.dotcmsUrl}/api/v1/nav${formattedPath}${queryParams ? `?${queryParams}` : ''}`;
|
|
112
|
+
const response = yield fetch(url, {
|
|
113
|
+
headers: {
|
|
114
|
+
Authorization: `Bearer ${this.config.authToken}`
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
return response.json();
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
validatePageOptions(options) {
|
|
121
|
+
if (!options.path) {
|
|
122
|
+
throw new Error("The 'path' parameter is required for the Page API");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
validateNavOptions(options) {
|
|
126
|
+
if (!options.path) {
|
|
127
|
+
throw new Error("The 'path' parameter is required for the Nav API");
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* `dotcmsClient` is an object that provides a method to initialize the DotCMS SDK client.
|
|
133
|
+
* It has a single method `init` which takes a configuration object and returns an instance of the `DotCmsClient` class.
|
|
134
|
+
*
|
|
135
|
+
* @namespace dotcmsClient
|
|
136
|
+
*
|
|
137
|
+
* @method init(config: ClientConfig): DotCmsClient - Initializes the SDK client.
|
|
138
|
+
*/
|
|
139
|
+
export const dotcmsClient = {
|
|
140
|
+
/**
|
|
141
|
+
* `init` is a method of the `dotcmsClient` object that initializes the SDK client.
|
|
142
|
+
* It takes a configuration object as a parameter and returns an instance of the `DotCmsClient` class.
|
|
143
|
+
*
|
|
144
|
+
* @method init
|
|
145
|
+
* @param {ClientConfig} config - The configuration object for the DotCMS client.
|
|
146
|
+
* @returns {DotCmsClient} - An instance of the {@link DotCmsClient} class.
|
|
147
|
+
*/
|
|
148
|
+
init: (config) => {
|
|
149
|
+
return new DotCmsClient(config);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
//# sourceMappingURL=sdk-js-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sdk-js-client.js","sourceRoot":"","sources":["../../../../../../libs/sdk/client/src/lib/sdk-js-client.ts"],"names":[],"mappings":";AAyGA,SAAS,UAAU,CAAC,GAAW;IAC3B,IAAI;QACA,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QAEb,OAAO,IAAI,CAAC;KACf;IAAC,OAAO,KAAK,EAAE;QACZ,OAAO,KAAK,CAAC;KAChB;AACL,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,YAAY;IAGrB,YAAY,MAAoB;QAC5B,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;SACtE;QAED,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE;YAC/B,MAAM,IAAI,KAAK,CAAC,yDAAyD,CAAC,CAAC;SAC9E;QAED,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;SACtE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