@dotcms/client 0.0.1-alpha.1 → 0.0.1-alpha.11

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/.eslintrc.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "extends": ["../../../.eslintrc.base.json"],
3
+ "ignorePatterns": ["!**/*"],
4
+ "overrides": [
5
+ {
6
+ "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7
+ "rules": {}
8
+ },
9
+ {
10
+ "files": ["*.ts", "*.tsx"],
11
+ "rules": {}
12
+ },
13
+ {
14
+ "files": ["*.js", "*.jsx"],
15
+ "rules": {}
16
+ }
17
+ ]
18
+ }
package/README.md CHANGED
@@ -6,9 +6,9 @@ This client library provides a streamlined, promise-based interface to fetch pag
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.
11
- - Comprehensive TypeScript typings for better development experience.
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
12
 
13
13
  ## Installation
14
14
 
@@ -43,7 +43,7 @@ const client = dotcmsClient.init({
43
43
  Retrieve the elements of any page in your DotCMS system in JSON format.
44
44
 
45
45
  ```javascript
46
- const pageData = await client.getPage({
46
+ const pageData = await client.page.get({
47
47
  path: '/your-page-path',
48
48
  language_id: 1,
49
49
  personaId: 'optional-persona-id'
@@ -57,7 +57,7 @@ console.log(pageData);
57
57
  Retrieve information about the DotCMS file and folder tree.
58
58
 
59
59
  ```javascript
60
- const navData = await client.getNav({
60
+ const navData = await client.nav.get({
61
61
  path: '/',
62
62
  depth: 2,
63
63
  languageId: 1
@@ -74,20 +74,18 @@ Detailed documentation of the `@dotcms/client` methods, parameters, and types ca
74
74
 
75
75
  Initializes the DotCMS client with the specified configuration.
76
76
 
77
- ### `DotCmsClient.getPage(options: PageApiOptions): Promise<unknown>`
77
+ ### `DotCmsClient.page.get(options: PageApiOptions): Promise<unknown>`
78
78
 
79
79
  Retrieves the specified page's elements from your DotCMS system in JSON format.
80
80
 
81
- ### `DotCmsClient.getNav(options: NavApiOptions): Promise<unknown>`
81
+ ### `DotCmsClient.nav.get(options: NavApiOptions): Promise<unknown>`
82
82
 
83
83
  Retrieves information about the DotCMS file and folder tree.
84
84
 
85
-
86
85
  ## Contributing
87
86
 
88
87
  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
88
 
90
-
91
89
  ## Licensing
92
90
 
93
91
  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).
@@ -110,4 +108,4 @@ Always refer to the official [DotCMS documentation](https://www.dotcms.com/docs/
110
108
  | Code Examples | [Codeshare](https://dotcms.com/codeshare/) |
111
109
  | Forums/Listserv | [via Google Groups](https://groups.google.com/forum/#!forum/dotCMS) |
112
110
  | Twitter | @dotCMS |
113
- | Main Site | [dotCMS.com](https://dotcms.com/) |
111
+ | Main Site | [dotCMS.com](https://dotcms.com/) |
package/jest.config.ts ADDED
@@ -0,0 +1,15 @@
1
+ /* eslint-disable */
2
+ export default {
3
+ displayName: 'sdk-client',
4
+ preset: '../../../jest.preset.js',
5
+ globals: {
6
+ 'ts-jest': {
7
+ tsconfig: '<rootDir>/tsconfig.spec.json'
8
+ }
9
+ },
10
+ transform: {
11
+ '^.+\\.[tj]s$': 'ts-jest'
12
+ },
13
+ moduleFileExtensions: ['ts', 'js', 'html'],
14
+ coverageDirectory: '../../../coverage/libs/sdk/client'
15
+ };
package/package.json CHANGED
@@ -1,28 +1,26 @@
1
1
  {
2
- "name": "@dotcms/client",
3
- "version": "0.0.1-alpha.1",
4
- "type": "module",
5
- "description": "Official JavaScript library for interacting with DotCMS REST APIs.",
6
- "repository": {
7
- "type": "git",
8
- "url": "git+https://github.com/dotCMS/core.git#master"
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": "MIT",
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"
2
+ "name": "@dotcms/client",
3
+ "version": "0.0.1-alpha.11",
4
+ "type": "module",
5
+ "description": "Official JavaScript library for interacting with DotCMS REST APIs.",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/dotCMS/core.git#master"
9
+ },
10
+ "scripts": {
11
+ "build": "nx run sdk-client:build:js; cd ../../../../dotCMS/src/main/webapp/html/js/editor-js; rm -rf src package.json *.esm.d.ts"
12
+ },
13
+ "keywords": [
14
+ "dotCMS",
15
+ "CMS",
16
+ "Content Management",
17
+ "API Client",
18
+ "REST API"
19
+ ],
20
+ "author": "dotcms <dev@dotcms.com>",
21
+ "license": "MIT",
22
+ "bugs": {
23
+ "url": "https://github.com/dotCMS/core/issues"
24
+ },
25
+ "homepage": "https://github.com/dotCMS/core/tree/master/core-web/libs/sdk/client/README.md"
28
26
  }
package/project.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "sdk-client",
3
+ "$schema": "../../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "libs/sdk/client/src",
5
+ "projectType": "library",
6
+ "targets": {
7
+ "build": {
8
+ "executor": "@nrwl/js:tsc",
9
+ "outputs": ["{options.outputPath}"],
10
+ "options": {
11
+ "outputPath": "dist/libs/sdk/client",
12
+ "main": "libs/sdk/client/src/index.ts",
13
+ "tsConfig": "libs/sdk/client/tsconfig.lib.json",
14
+ "assets": ["libs/sdk/client/*.md"]
15
+ }
16
+ },
17
+ "build:js": {
18
+ "executor": "@nrwl/rollup:rollup",
19
+ "outputs": ["{options.outputPath}"],
20
+ "options": {
21
+ "outputPath": "../dotCMS/src/main/webapp/html/js/editor-js",
22
+ "outputFileName": "sdk-editor",
23
+ "format": ["esm"],
24
+ "tsConfig": "libs/sdk/client/tsconfig.lib.json",
25
+ "project": "libs/sdk/client/package.json",
26
+ "entryFile": "libs/sdk/client/src/lib/editor/sdk-editor-vtl.ts",
27
+ "external": ["react/jsx-runtime"],
28
+ "rollupConfig": "@nrwl/react/plugins/bundle-rollup",
29
+ "compiler": "swc",
30
+ "extractCss": false
31
+ }
32
+ },
33
+ "publish": {
34
+ "executor": "nx:run-commands",
35
+ "options": {
36
+ "command": "node tools/scripts/publish.mjs sdk-js-client {args.ver} {args.tag}"
37
+ },
38
+ "dependsOn": ["build"]
39
+ },
40
+ "lint": {
41
+ "executor": "@nrwl/linter:eslint",
42
+ "outputs": ["{options.outputFile}"],
43
+ "options": {
44
+ "lintFilePatterns": ["libs/sdk/client/**/*.ts"]
45
+ }
46
+ },
47
+ "test": {
48
+ "executor": "@nrwl/jest:jest",
49
+ "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
50
+ "options": {
51
+ "jestConfig": "libs/sdk/client/jest.config.ts",
52
+ "passWithNoTests": true
53
+ },
54
+ "configurations": {
55
+ "ci": {
56
+ "ci": true,
57
+ "codeCoverage": true
58
+ }
59
+ }
60
+ }
61
+ },
62
+ "tags": []
63
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './lib/client/sdk-js-client';
2
+ export * from './lib/editor/sdk-editor';
3
+ export * from './lib/editor/models/editor.model';
4
+ export * from './lib/editor/models/client.model';
@@ -0,0 +1,258 @@
1
+ /// <reference types="jest" />
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ import { DotCmsClient, dotcmsClient } from './sdk-js-client';
4
+ global.fetch = jest.fn();
5
+
6
+ // Utility function to mock fetch responses
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ const mockFetchResponse = (body: any, ok = true, status = 200) => {
9
+ (fetch as jest.Mock).mockImplementationOnce(() =>
10
+ Promise.resolve({
11
+ ok,
12
+ status,
13
+ json: () => Promise.resolve(body)
14
+ })
15
+ );
16
+ };
17
+
18
+ describe('DotCmsClient', () => {
19
+ describe('with full configuration', () => {
20
+ let client: DotCmsClient;
21
+
22
+ beforeEach(() => {
23
+ (fetch as jest.Mock).mockClear();
24
+
25
+ client = dotcmsClient.init({
26
+ dotcmsUrl: 'http://localhost',
27
+ siteId: '123456',
28
+ authToken: 'ABC'
29
+ });
30
+ });
31
+
32
+ describe('init', () => {
33
+ it('should initialize with valid configuration', () => {
34
+ const config = {
35
+ dotcmsUrl: 'https://example.com',
36
+ siteId: '123456',
37
+ authToken: 'ABC'
38
+ };
39
+
40
+ const client = dotcmsClient.init(config);
41
+ expect(client).toBeDefined();
42
+ });
43
+
44
+ it('should throw error on missing dotcmsUrl', () => {
45
+ const config = {
46
+ dotcmsUrl: '',
47
+ siteId: '123456',
48
+ authToken: 'ABC'
49
+ };
50
+
51
+ expect(() => {
52
+ dotcmsClient.init(config);
53
+ }).toThrow("Invalid configuration - 'dotcmsUrl' is required");
54
+ });
55
+
56
+ it('should throw error if dotcmsUrl is not a valid URL', () => {
57
+ const config = {
58
+ dotcmsUrl: '//example.com',
59
+ siteId: '123456',
60
+ authToken: 'ABC'
61
+ };
62
+
63
+ expect(() => {
64
+ dotcmsClient.init(config);
65
+ }).toThrow("Invalid configuration - 'dotcmsUrl' must be a valid URL");
66
+ });
67
+
68
+ it('should throw error on missing authToken', () => {
69
+ const config = {
70
+ dotcmsUrl: 'https://example.com',
71
+ siteId: '123456',
72
+ authToken: ''
73
+ };
74
+
75
+ expect(() => {
76
+ dotcmsClient.init(config);
77
+ }).toThrow("Invalid configuration - 'authToken' is required");
78
+ });
79
+ });
80
+
81
+ describe('page.get', () => {
82
+ it('should fetch page data successfully', async () => {
83
+ const mockResponse = { content: 'Page data' };
84
+ mockFetchResponse(mockResponse);
85
+
86
+ const data = await client.page.get({ path: '/home' });
87
+
88
+ expect(fetch).toHaveBeenCalledTimes(1);
89
+ expect(fetch).toHaveBeenCalledWith(
90
+ 'http://localhost/api/v1/page/json/home?host_id=123456',
91
+ {
92
+ headers: { Authorization: 'Bearer ABC' }
93
+ }
94
+ );
95
+ expect(data).toEqual(mockResponse);
96
+ });
97
+
98
+ it('should throw an error if the path is not provided', async () => {
99
+ await expect(client.page.get({} as any)).rejects.toThrowError(
100
+ `The 'path' parameter is required for the Page API`
101
+ );
102
+ });
103
+
104
+ it('should get the page for specified persona', () => {
105
+ const mockResponse = { content: 'Page data' };
106
+ mockFetchResponse(mockResponse);
107
+
108
+ client.page.get({ path: '/home', personaId: 'doe123' });
109
+
110
+ expect(fetch).toHaveBeenCalledWith(
111
+ 'http://localhost/api/v1/page/json/home?com.dotmarketing.persona.id=doe123&host_id=123456',
112
+ {
113
+ headers: { Authorization: 'Bearer ABC' }
114
+ }
115
+ );
116
+ });
117
+
118
+ it('should get the page for specified siteId', () => {
119
+ const mockResponse = { content: 'Page data' };
120
+ mockFetchResponse(mockResponse);
121
+
122
+ client.page.get({ path: '/home', siteId: 'host-123' });
123
+
124
+ expect(fetch).toHaveBeenCalledWith(
125
+ 'http://localhost/api/v1/page/json/home?host_id=host-123',
126
+ {
127
+ headers: { Authorization: 'Bearer ABC' }
128
+ }
129
+ );
130
+ });
131
+
132
+ it('should get the page for specified language', () => {
133
+ const mockResponse = { content: 'Page data' };
134
+ mockFetchResponse(mockResponse);
135
+
136
+ client.page.get({ path: '/home', language_id: 99 });
137
+
138
+ expect(fetch).toHaveBeenCalledWith(
139
+ 'http://localhost/api/v1/page/json/home?language_id=99&host_id=123456',
140
+ {
141
+ headers: { Authorization: 'Bearer ABC' }
142
+ }
143
+ );
144
+ });
145
+ });
146
+
147
+ describe('nav.get', () => {
148
+ it('should fetch navigation data successfully', async () => {
149
+ const mockResponse = { nav: 'Navigation data' };
150
+ mockFetchResponse(mockResponse);
151
+
152
+ const data = await client.nav.get({ path: '/', depth: 1 });
153
+
154
+ expect(fetch).toHaveBeenCalledTimes(1);
155
+ expect(fetch).toHaveBeenCalledWith('http://localhost/api/v1/nav/?depth=1', {
156
+ headers: { Authorization: 'Bearer ABC' }
157
+ });
158
+ expect(data).toEqual(mockResponse);
159
+ });
160
+
161
+ it('should correctly handle the root path', async () => {
162
+ const mockResponse = { nav: 'Root nav data' };
163
+ mockFetchResponse(mockResponse);
164
+
165
+ const data = await client.nav.get({ path: '/' });
166
+
167
+ expect(fetch).toHaveBeenCalledWith('http://localhost/api/v1/nav/', {
168
+ headers: { Authorization: 'Bearer ABC' }
169
+ });
170
+ expect(data).toEqual(mockResponse);
171
+ });
172
+
173
+ it('should throw an error if the path is not provided', async () => {
174
+ await expect(client.nav.get({} as any)).rejects.toThrowError(
175
+ `The 'path' parameter is required for the Nav API`
176
+ );
177
+ });
178
+ });
179
+ });
180
+
181
+ describe('with minimal configuration', () => {
182
+ let client: DotCmsClient;
183
+
184
+ beforeEach(() => {
185
+ (fetch as jest.Mock).mockClear();
186
+
187
+ client = dotcmsClient.init({
188
+ dotcmsUrl: 'http://localhost',
189
+ authToken: 'ABC'
190
+ });
191
+ });
192
+
193
+ it('should get the page without siteId', () => {
194
+ const mockResponse = { content: 'Page data' };
195
+ mockFetchResponse(mockResponse);
196
+
197
+ client.page.get({ path: '/home' });
198
+
199
+ expect(fetch).toHaveBeenCalledWith('http://localhost/api/v1/page/json/home', {
200
+ headers: { Authorization: 'Bearer ABC' }
201
+ });
202
+ });
203
+ });
204
+
205
+ describe('with requestOptions', () => {
206
+ let client: DotCmsClient;
207
+
208
+ beforeEach(() => {
209
+ (fetch as jest.Mock).mockClear();
210
+
211
+ client = dotcmsClient.init({
212
+ dotcmsUrl: 'http://localhost',
213
+ siteId: '123456',
214
+ authToken: 'ABC',
215
+ requestOptions: {
216
+ headers: {
217
+ 'X-My-Header': 'my-value'
218
+ },
219
+ cache: 'no-cache'
220
+ }
221
+ });
222
+ });
223
+
224
+ it('should fetch page data with extra headers and cache', async () => {
225
+ const mockResponse = { content: 'Page data' };
226
+ mockFetchResponse(mockResponse);
227
+
228
+ const data = await client.page.get({ path: '/home' });
229
+
230
+ expect(fetch).toHaveBeenCalledTimes(1);
231
+ expect(fetch).toHaveBeenCalledWith(
232
+ 'http://localhost/api/v1/page/json/home?host_id=123456',
233
+ {
234
+ headers: { Authorization: 'Bearer ABC', 'X-My-Header': 'my-value' },
235
+ cache: 'no-cache'
236
+ }
237
+ );
238
+ expect(data).toEqual(mockResponse);
239
+ });
240
+
241
+ it('should fetch bav data with extra headers and cache', async () => {
242
+ const mockResponse = { content: 'Page data' };
243
+ mockFetchResponse(mockResponse);
244
+
245
+ const data = await client.nav.get();
246
+
247
+ expect(fetch).toHaveBeenCalledTimes(1);
248
+ expect(fetch).toHaveBeenCalledWith(
249
+ 'http://localhost/api/v1/nav/?depth=0&languageId=1',
250
+ {
251
+ headers: { Authorization: 'Bearer ABC', 'X-My-Header': 'my-value' },
252
+ cache: 'no-cache'
253
+ }
254
+ );
255
+ expect(data).toEqual(mockResponse);
256
+ });
257
+ });
258
+ });