@dotcms/client 0.0.1-alpha.37 → 0.0.1-alpha.39
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 +18 -0
- package/README.md +4 -4
- package/jest.config.ts +15 -0
- package/package.json +3 -15
- package/project.json +72 -0
- package/src/index.ts +30 -0
- package/src/lib/client/content/builders/collection/collection.spec.ts +515 -0
- package/src/lib/client/content/builders/collection/collection.ts +416 -0
- package/src/lib/client/content/content-api.ts +139 -0
- package/src/lib/client/content/shared/const.ts +15 -0
- package/src/lib/client/content/shared/types.ts +155 -0
- package/src/lib/client/content/shared/utils.ts +28 -0
- package/src/lib/client/models/index.ts +19 -0
- package/src/lib/client/models/types.ts +14 -0
- package/src/lib/client/sdk-js-client.spec.ts +483 -0
- package/src/lib/client/sdk-js-client.ts +442 -0
- package/src/lib/editor/listeners/listeners.spec.ts +119 -0
- package/src/lib/editor/listeners/listeners.ts +223 -0
- package/src/lib/editor/models/{client.model.d.ts → client.model.ts} +19 -16
- package/src/lib/editor/models/{editor.model.d.ts → editor.model.ts} +9 -5
- package/src/lib/editor/models/{listeners.model.d.ts → listeners.model.ts} +9 -6
- package/src/lib/editor/sdk-editor-vtl.ts +31 -0
- package/src/lib/editor/sdk-editor.spec.ts +116 -0
- package/src/lib/editor/sdk-editor.ts +105 -0
- package/src/lib/editor/utils/editor.utils.spec.ts +206 -0
- package/src/lib/editor/utils/editor.utils.ts +258 -0
- package/src/lib/query-builder/lucene-syntax/{Equals.d.ts → Equals.ts} +83 -18
- package/src/lib/query-builder/lucene-syntax/Field.ts +40 -0
- package/src/lib/query-builder/lucene-syntax/{NotOperand.d.ts → NotOperand.ts} +18 -6
- package/src/lib/query-builder/lucene-syntax/{Operand.d.ts → Operand.ts} +21 -7
- package/src/lib/query-builder/sdk-query-builder.spec.ts +159 -0
- package/src/lib/query-builder/sdk-query-builder.ts +87 -0
- package/src/lib/query-builder/utils/index.ts +179 -0
- package/src/lib/utils/graphql/transforms.spec.ts +150 -0
- package/src/lib/utils/graphql/transforms.ts +99 -0
- package/src/lib/utils/page/common-utils.spec.ts +37 -0
- package/src/lib/utils/page/common-utils.ts +64 -0
- package/tsconfig.json +22 -0
- package/tsconfig.lib.json +13 -0
- package/tsconfig.spec.json +9 -0
- package/index.cjs.d.ts +0 -1
- package/index.cjs.default.js +0 -1
- package/index.cjs.js +0 -1490
- package/index.cjs.mjs +0 -2
- package/index.esm.d.ts +0 -1
- package/index.esm.js +0 -1481
- package/src/index.d.ts +0 -6
- package/src/lib/client/content/builders/collection/collection.d.ts +0 -148
- package/src/lib/client/content/content-api.d.ts +0 -78
- package/src/lib/client/content/shared/const.d.ts +0 -3
- package/src/lib/client/content/shared/types.d.ts +0 -62
- package/src/lib/client/content/shared/utils.d.ts +0 -12
- package/src/lib/client/models/index.d.ts +0 -1
- package/src/lib/client/models/types.d.ts +0 -5
- package/src/lib/client/sdk-js-client.d.ts +0 -193
- package/src/lib/editor/listeners/listeners.d.ts +0 -46
- package/src/lib/editor/sdk-editor-vtl.d.ts +0 -6
- package/src/lib/editor/sdk-editor.d.ts +0 -29
- package/src/lib/editor/utils/editor.utils.d.ts +0 -83
- package/src/lib/query-builder/lucene-syntax/Field.d.ts +0 -23
- package/src/lib/query-builder/sdk-query-builder.d.ts +0 -42
- package/src/lib/query-builder/utils/index.d.ts +0 -91
- package/src/lib/utils/graphql/transforms.d.ts +0 -11
- package/src/lib/utils/page/common-utils.d.ts +0 -11
- /package/src/lib/query-builder/lucene-syntax/{index.d.ts → index.ts} +0 -0
- /package/src/lib/utils/{index.d.ts → index.ts} +0 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import { Content } from './content/content-api';
|
|
2
|
+
import { ErrorMessages } from './models';
|
|
3
|
+
import { DotcmsClientListener } from './models/types';
|
|
4
|
+
|
|
5
|
+
import { isInsideEditor } from '../editor/sdk-editor';
|
|
6
|
+
|
|
7
|
+
export type ClientOptions = Omit<RequestInit, 'body' | 'method'>;
|
|
8
|
+
|
|
9
|
+
export interface ClientConfig {
|
|
10
|
+
/**
|
|
11
|
+
* The URL of the dotCMS instance.
|
|
12
|
+
*
|
|
13
|
+
* @description This is the URL of the dotCMS instance you want to interact with. Ensure to include the protocol (http or https).
|
|
14
|
+
* @example `https://demo.dotcms.com`
|
|
15
|
+
* @type {string}
|
|
16
|
+
* @required
|
|
17
|
+
*/
|
|
18
|
+
dotcmsUrl: string;
|
|
19
|
+
/**
|
|
20
|
+
* The id of the site you want to interact with. If not provided, it will use the default site.
|
|
21
|
+
*
|
|
22
|
+
* More information here: {@link https://www.dotcms.com/docs/latest/multi-site-management}
|
|
23
|
+
*
|
|
24
|
+
* @description To get the site id, go to the site you want to interact with and copy the id from the History tab.
|
|
25
|
+
* @type {string}
|
|
26
|
+
* @optional
|
|
27
|
+
*/
|
|
28
|
+
siteId?: string;
|
|
29
|
+
/**
|
|
30
|
+
* The authentication token to use for the requests.
|
|
31
|
+
*
|
|
32
|
+
* @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}
|
|
33
|
+
* @type {string}
|
|
34
|
+
* @required
|
|
35
|
+
*/
|
|
36
|
+
authToken: string;
|
|
37
|
+
/**
|
|
38
|
+
* Additional options to pass to the fetch request.
|
|
39
|
+
*
|
|
40
|
+
* @description These options will be used in the fetch request. Any option can be specified except for 'body' and 'method' which are omitted.
|
|
41
|
+
* @example `{ headers: { 'Content-Type': 'application/json' } }`
|
|
42
|
+
* @type {ClientOptions}
|
|
43
|
+
* @optional
|
|
44
|
+
*/
|
|
45
|
+
requestOptions?: ClientOptions;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type PageApiOptions = {
|
|
49
|
+
/**
|
|
50
|
+
* The path of the page you want to retrieve.
|
|
51
|
+
* @type {string}
|
|
52
|
+
*/
|
|
53
|
+
path: string;
|
|
54
|
+
/**
|
|
55
|
+
* The id of the site you want to interact with. If not provided, the one from the config will be used.
|
|
56
|
+
*
|
|
57
|
+
* More information here: {@link https://www.dotcms.com/docs/latest/multi-site-management}
|
|
58
|
+
* @type {string}
|
|
59
|
+
* @optional
|
|
60
|
+
*/
|
|
61
|
+
siteId?: string;
|
|
62
|
+
/**
|
|
63
|
+
* The mode of the page you want to retrieve. If not provided, will use the default mode of the site.
|
|
64
|
+
*
|
|
65
|
+
* More information here: {@link https://www.dotcms.com/docs/latest/page-viewing-modes}
|
|
66
|
+
* @type {string}
|
|
67
|
+
* @optional
|
|
68
|
+
*/
|
|
69
|
+
mode?: 'EDIT_MODE' | 'PREVIEW_MODE' | 'LIVE_MODE';
|
|
70
|
+
/**
|
|
71
|
+
* The language id of the page you want to retrieve. If not provided, will use the default language of the site.
|
|
72
|
+
* @type {number}
|
|
73
|
+
* @optional
|
|
74
|
+
*/
|
|
75
|
+
language_id?: number;
|
|
76
|
+
/**
|
|
77
|
+
* The id of the persona you want to retrieve the page for.
|
|
78
|
+
*
|
|
79
|
+
* More information here: {@link https://www.dotcms.com/docs/latest/personas}
|
|
80
|
+
* @type {string}
|
|
81
|
+
* @optional
|
|
82
|
+
*/
|
|
83
|
+
personaId?: string;
|
|
84
|
+
/**
|
|
85
|
+
* If you want to fire the rules set on the page.
|
|
86
|
+
*
|
|
87
|
+
* More information here: {@link https://www.dotcms.com/docs/latest/adding-rules-to-pages}
|
|
88
|
+
*
|
|
89
|
+
* @type {boolean}
|
|
90
|
+
* @optional
|
|
91
|
+
*/
|
|
92
|
+
fireRules?: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Allows access to related content via the Relationship fields of contentlets on a Page; 0 (default).
|
|
95
|
+
* @type {number}
|
|
96
|
+
* @optional
|
|
97
|
+
*/
|
|
98
|
+
depth?: number;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
type NavApiOptions = {
|
|
102
|
+
/**
|
|
103
|
+
* The root path to begin traversing the folder tree.
|
|
104
|
+
* @example
|
|
105
|
+
* `/api/v1/nav/` starts from the root of the site
|
|
106
|
+
* @example
|
|
107
|
+
* `/about-us` starts from the "About Us" folder
|
|
108
|
+
* @type {string}
|
|
109
|
+
*/
|
|
110
|
+
path: string;
|
|
111
|
+
/**
|
|
112
|
+
* The depth of the folder tree to return.
|
|
113
|
+
* @example
|
|
114
|
+
* `1` returns only the element specified in the path.
|
|
115
|
+
* @example
|
|
116
|
+
* `2` returns the element specified in the path, and if that element is a folder, returns all direct children of that folder.
|
|
117
|
+
* @example
|
|
118
|
+
* `3` returns all children and grandchildren of the element specified in the path.
|
|
119
|
+
* @type {number}
|
|
120
|
+
* @optional
|
|
121
|
+
*/
|
|
122
|
+
depth?: number;
|
|
123
|
+
/**
|
|
124
|
+
* The language ID of content to return.
|
|
125
|
+
* @example
|
|
126
|
+
* `1` (or unspecified) returns content in the default language of the site.
|
|
127
|
+
* @link https://www.dotcms.com/docs/latest/system-language-properties#DefaultLanguage
|
|
128
|
+
* @link https://www.dotcms.com/docs/latest/adding-and-editing-languages#LanguageID
|
|
129
|
+
* @type {number}
|
|
130
|
+
* @optional
|
|
131
|
+
*/
|
|
132
|
+
languageId?: number;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
function getHostURL(url: string): URL | undefined {
|
|
136
|
+
try {
|
|
137
|
+
return new URL(url);
|
|
138
|
+
} catch (error) {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* `DotCmsClient` is a TypeScript class that provides methods to interact with the DotCMS REST API.
|
|
145
|
+
* DotCMS is a hybrid-headless CMS and digital experience platform.
|
|
146
|
+
*
|
|
147
|
+
* @class DotCmsClient
|
|
148
|
+
* @property {ClientConfig} config - The configuration object for the DotCMS client.
|
|
149
|
+
* @property {Content} content - Provides methods to interact with content in DotCMS.
|
|
150
|
+
*
|
|
151
|
+
* @method constructor(config: ClientConfig) - Constructs a new instance of the DotCmsClient class.
|
|
152
|
+
*
|
|
153
|
+
* @method page.get(options: PageApiOptions): Promise<PageApiResponse> - Retrieves all the elements of any Page in your dotCMS system in JSON format.
|
|
154
|
+
* The Page API enables you to retrieve page information, layout, template, content blocks, and more.
|
|
155
|
+
* @see {@link https://www.dotcms.com/docs/latest/page-rest-api-layout-as-a-service-laas}
|
|
156
|
+
*
|
|
157
|
+
* @method nav.get(options: NavApiOptions = { depth: 0, path: '/', languageId: 1 }): Promise<NavApiResponse> - Retrieves information about the dotCMS file and folder tree.
|
|
158
|
+
* The Navigation API allows you to fetch the site structure and menu items.
|
|
159
|
+
* @see {@link https://www.dotcms.com/docs/latest/navigation-rest-api}
|
|
160
|
+
*
|
|
161
|
+
* @method content.get(options: ContentApiOptions): Promise<ContentApiResponse> - Retrieves content items based on specified criteria.
|
|
162
|
+
* The Content API allows you to query and retrieve content by ID, inode, or using Lucene queries.
|
|
163
|
+
* @see {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying}
|
|
164
|
+
*
|
|
165
|
+
* @method editor.on(action: string, callbackFn: (payload: unknown) => void) - Allows you to react to actions issued by the Universal Visual Editor (UVE).
|
|
166
|
+
* @method editor.off(action: string) - Stops listening to an action issued by UVE.
|
|
167
|
+
*
|
|
168
|
+
* @static
|
|
169
|
+
* @method init(config: ClientConfig): DotCmsClient - Initializes and returns a DotCmsClient instance.
|
|
170
|
+
* @method dotcmsUrl: string - Retrieves the DotCMS URL from the instance configuration.
|
|
171
|
+
*
|
|
172
|
+
* @example <caption>Basic usage</caption>
|
|
173
|
+
* ```javascript
|
|
174
|
+
* const client = DotCmsClient.init({ dotcmsUrl: 'https://demo.dotcms.com', authToken: 'your-auth-token' });
|
|
175
|
+
*
|
|
176
|
+
* // Get a page
|
|
177
|
+
* client.page.get({ path: '/about-us' }).then(response => console.log(response));
|
|
178
|
+
*
|
|
179
|
+
* // Get navigation
|
|
180
|
+
* client.nav.get({ path: '/about-us', depth: 2 }).then(response => console.log(response));
|
|
181
|
+
*
|
|
182
|
+
* // Get content
|
|
183
|
+
* client.content.get({ query: '+contentType:Blog +languageId:1', limit: 10 }).then(response => console.log(response));
|
|
184
|
+
*
|
|
185
|
+
* // Listen to editor changes
|
|
186
|
+
* client.editor.on('changes', (payload) => console.log('Changes detected:', payload));
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export class DotCmsClient {
|
|
190
|
+
static instance: DotCmsClient;
|
|
191
|
+
#config: ClientConfig;
|
|
192
|
+
#requestOptions!: ClientOptions;
|
|
193
|
+
#listeners: DotcmsClientListener[] = [];
|
|
194
|
+
|
|
195
|
+
dotcmsUrl?: string;
|
|
196
|
+
content: Content;
|
|
197
|
+
|
|
198
|
+
constructor(
|
|
199
|
+
config: ClientConfig = { dotcmsUrl: '', authToken: '', requestOptions: {}, siteId: '' }
|
|
200
|
+
) {
|
|
201
|
+
if (!config.dotcmsUrl) {
|
|
202
|
+
throw new Error("Invalid configuration - 'dotcmsUrl' is required");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
this.dotcmsUrl = getHostURL(config.dotcmsUrl)?.origin;
|
|
206
|
+
|
|
207
|
+
if (!this.dotcmsUrl) {
|
|
208
|
+
throw new Error("Invalid configuration - 'dotcmsUrl' must be a valid URL");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (!config.authToken) {
|
|
212
|
+
throw new Error("Invalid configuration - 'authToken' is required");
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
this.#config = {
|
|
216
|
+
...config,
|
|
217
|
+
dotcmsUrl: this.dotcmsUrl
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
this.#requestOptions = {
|
|
221
|
+
...this.#config.requestOptions,
|
|
222
|
+
headers: {
|
|
223
|
+
Authorization: `Bearer ${this.#config.authToken}`,
|
|
224
|
+
...this.#config.requestOptions?.headers
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
this.content = new Content(this.#requestOptions, this.#config.dotcmsUrl);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
page = {
|
|
232
|
+
/**
|
|
233
|
+
* `page.get` is an asynchronous method of the `DotCmsClient` class that retrieves all the elements of any Page in your dotCMS system in JSON format.
|
|
234
|
+
* It takes a `PageApiOptions` object as a parameter and returns a Promise that resolves to the response from the DotCMS API.
|
|
235
|
+
*
|
|
236
|
+
* The Page API enables you to retrieve all the elements of any Page in your dotCMS system.
|
|
237
|
+
* The elements may be retrieved in JSON format.
|
|
238
|
+
*
|
|
239
|
+
* @link https://www.dotcms.com/docs/latest/page-rest-api-layout-as-a-service-laas
|
|
240
|
+
* @async
|
|
241
|
+
* @param {PageApiOptions} options - The options for the Page API call.
|
|
242
|
+
* @returns {Promise<unknown>} - A Promise that resolves to the response from the DotCMS API.
|
|
243
|
+
* @throws {Error} - Throws an error if the options are not valid.
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* const client = new DotCmsClient({ dotcmsUrl: 'https://your.dotcms.com', authToken: 'your-auth-token', siteId: 'your-site-id' });
|
|
247
|
+
* client.page.get({ path: '/about-us' }).then(response => console.log(response));
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
get: async (options: PageApiOptions): Promise<unknown> => {
|
|
251
|
+
this.validatePageOptions(options);
|
|
252
|
+
|
|
253
|
+
const queryParamsObj: Record<string, string> = {};
|
|
254
|
+
for (const [key, value] of Object.entries(options)) {
|
|
255
|
+
if (value === undefined || key === 'path' || key === 'siteId') continue;
|
|
256
|
+
|
|
257
|
+
if (key === 'personaId') {
|
|
258
|
+
queryParamsObj['com.dotmarketing.persona.id'] = String(value);
|
|
259
|
+
} else if (key === 'mode' && value) {
|
|
260
|
+
queryParamsObj['mode'] = String(value);
|
|
261
|
+
} else {
|
|
262
|
+
queryParamsObj[key] = String(value);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const queryHostId = options.siteId ?? this.#config.siteId ?? '';
|
|
267
|
+
|
|
268
|
+
if (queryHostId) {
|
|
269
|
+
queryParamsObj['host_id'] = queryHostId;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const queryParams = new URLSearchParams(queryParamsObj).toString();
|
|
273
|
+
|
|
274
|
+
const formattedPath = options.path.startsWith('/') ? options.path : `/${options.path}`;
|
|
275
|
+
const url = `${this.#config.dotcmsUrl}/api/v1/page/json${formattedPath}${
|
|
276
|
+
queryParams ? `?${queryParams}` : ''
|
|
277
|
+
}`;
|
|
278
|
+
|
|
279
|
+
const response = await fetch(url, this.#requestOptions);
|
|
280
|
+
if (!response.ok) {
|
|
281
|
+
const error = {
|
|
282
|
+
status: response.status,
|
|
283
|
+
message: ErrorMessages[response.status] || response.statusText
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
console.error(error);
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return response.json().then((data) => data.entity);
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
editor = {
|
|
295
|
+
/**
|
|
296
|
+
* `editor.on` is an asynchronous method of the `DotCmsClient` class that allows you to react to actions issued by the UVE.
|
|
297
|
+
*
|
|
298
|
+
* NOTE: This is being used by the development team - This logic is probably varied or moved to another function/object.
|
|
299
|
+
* @param {string} action - The name of the action emitted by UVE.
|
|
300
|
+
* @param {function} callbackFn - The function to execute when the UVE emits the action.
|
|
301
|
+
* @example
|
|
302
|
+
* ```ts
|
|
303
|
+
* client.editor.on('changes', (payload) => {
|
|
304
|
+
* console.log('Changes detected:', payload);
|
|
305
|
+
* });
|
|
306
|
+
* ```
|
|
307
|
+
*/
|
|
308
|
+
on: (action: string, callbackFn: (payload: unknown) => void) => {
|
|
309
|
+
if (!isInsideEditor()) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (action === 'changes') {
|
|
314
|
+
const messageCallback = (event: MessageEvent) => {
|
|
315
|
+
if (event.data.name === 'SET_PAGE_DATA') {
|
|
316
|
+
callbackFn(event.data.payload);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
window.addEventListener('message', messageCallback);
|
|
321
|
+
this.#listeners.push({ event: 'message', callback: messageCallback, action });
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
/**
|
|
325
|
+
* `editor.off` is a synchronous method of the `DotCmsClient` class that allows you to stop listening and reacting to an action issued by UVE.
|
|
326
|
+
*
|
|
327
|
+
* NOTE: This is being used by the development team - This logic is probably varied or moved to another function/object.
|
|
328
|
+
* @param {string} action - The name of the action to stop listening to.
|
|
329
|
+
* @example
|
|
330
|
+
* ```ts
|
|
331
|
+
* client.editor.off('changes');
|
|
332
|
+
* ```
|
|
333
|
+
*/
|
|
334
|
+
off: (action: string) => {
|
|
335
|
+
const listenerIndex = this.#listeners.findIndex(
|
|
336
|
+
(listener) => listener.action === action
|
|
337
|
+
);
|
|
338
|
+
if (listenerIndex !== -1) {
|
|
339
|
+
const listener = this.#listeners[listenerIndex];
|
|
340
|
+
window.removeEventListener(listener.event, listener.callback);
|
|
341
|
+
this.#listeners.splice(listenerIndex, 1);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
nav = {
|
|
347
|
+
/**
|
|
348
|
+
* `nav.get` is an asynchronous method of the `DotCmsClient` class that retrieves information about the dotCMS file and folder tree.
|
|
349
|
+
* It takes a `NavApiOptions` object as a parameter (with default values) and returns a Promise that resolves to the response from the DotCMS API.
|
|
350
|
+
*
|
|
351
|
+
* The navigation REST API enables you to retrieve information about the dotCMS file and folder tree through REST API calls.
|
|
352
|
+
* @link https://www.dotcms.com/docs/latest/navigation-rest-api
|
|
353
|
+
* @async
|
|
354
|
+
* @param {NavApiOptions} options - The options for the Nav API call. Defaults to `{ depth: 0, path: '/', languageId: 1 }`.
|
|
355
|
+
* @returns {Promise<unknown>} - A Promise that resolves to the response from the DotCMS API.
|
|
356
|
+
* @throws {Error} - Throws an error if the options are not valid.
|
|
357
|
+
* @example
|
|
358
|
+
* ```ts
|
|
359
|
+
* const client = new DotCmsClient({ dotcmsUrl: 'https://your.dotcms.com', authToken: 'your-auth-token', siteId: 'your-site-id' }});
|
|
360
|
+
* client.nav.get({ path: '/about-us', depth: 2 }).then(response => console.log(response));
|
|
361
|
+
* ```
|
|
362
|
+
*/
|
|
363
|
+
get: async (
|
|
364
|
+
options: NavApiOptions = { depth: 0, path: '/', languageId: 1 }
|
|
365
|
+
): Promise<unknown> => {
|
|
366
|
+
this.validateNavOptions(options);
|
|
367
|
+
|
|
368
|
+
// Extract the 'path' from the options and prepare the rest as query parameters
|
|
369
|
+
const { path, ...queryParamsOptions } = options;
|
|
370
|
+
const queryParamsObj: Record<string, string> = {};
|
|
371
|
+
Object.entries(queryParamsOptions).forEach(([key, value]) => {
|
|
372
|
+
if (value !== undefined) {
|
|
373
|
+
queryParamsObj[key] = String(value);
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
const queryParams = new URLSearchParams(queryParamsObj).toString();
|
|
378
|
+
|
|
379
|
+
// Format the URL correctly depending on the 'path' value
|
|
380
|
+
const formattedPath = path === '/' ? '/' : `/${path}`;
|
|
381
|
+
const url = `${this.#config.dotcmsUrl}/api/v1/nav${formattedPath}${
|
|
382
|
+
queryParams ? `?${queryParams}` : ''
|
|
383
|
+
}`;
|
|
384
|
+
|
|
385
|
+
const response = await fetch(url, this.#requestOptions);
|
|
386
|
+
|
|
387
|
+
return response.json();
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Initializes the DotCmsClient instance with the provided configuration.
|
|
393
|
+
* If an instance already exists, it returns the existing instance.
|
|
394
|
+
*
|
|
395
|
+
* @param {ClientConfig} config - The configuration object for the DotCMS client.
|
|
396
|
+
* @returns {DotCmsClient} - The initialized DotCmsClient instance.
|
|
397
|
+
* @example
|
|
398
|
+
* ```ts
|
|
399
|
+
* const client = DotCmsClient.init({ dotcmsUrl: 'https://demo.dotcms.com', authToken: 'your-auth-token' });
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
static init(config: ClientConfig): DotCmsClient {
|
|
403
|
+
if (this.instance) {
|
|
404
|
+
console.warn(
|
|
405
|
+
'DotCmsClient has already been initialized. Please use the instance to interact with the DotCMS API.'
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return this.instance ?? (this.instance = new DotCmsClient(config));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Retrieves the DotCMS URL from the instance configuration.
|
|
414
|
+
*
|
|
415
|
+
* @returns {string} - The DotCMS URL.
|
|
416
|
+
*/
|
|
417
|
+
static get dotcmsUrl(): string {
|
|
418
|
+
return (this.instance && this.instance.#config.dotcmsUrl) || '';
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Throws an error if the path is not valid.
|
|
423
|
+
*
|
|
424
|
+
* @returns {string} - The authentication token.
|
|
425
|
+
*/
|
|
426
|
+
private validatePageOptions(options: PageApiOptions): void {
|
|
427
|
+
if (!options.path) {
|
|
428
|
+
throw new Error("The 'path' parameter is required for the Page API");
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Throws an error if the path is not valid.
|
|
434
|
+
*
|
|
435
|
+
* @returns {string} - The authentication token.
|
|
436
|
+
*/
|
|
437
|
+
private validateNavOptions(options: NavApiOptions): void {
|
|
438
|
+
if (!options.path) {
|
|
439
|
+
throw new Error("The 'path' parameter is required for the Nav API");
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fetchPageDataFromInsideUVE,
|
|
3
|
+
listenEditorMessages,
|
|
4
|
+
listenHoveredContentlet,
|
|
5
|
+
preserveScrollOnIframe,
|
|
6
|
+
scrollHandler
|
|
7
|
+
} from './listeners';
|
|
8
|
+
|
|
9
|
+
import { CUSTOMER_ACTIONS, postMessageToEditor } from '../models/client.model';
|
|
10
|
+
|
|
11
|
+
jest.mock('../models/client.model', () => ({
|
|
12
|
+
postMessageToEditor: jest.fn(),
|
|
13
|
+
CUSTOMER_ACTIONS: {
|
|
14
|
+
NAVIGATION_UPDATE: 'set-url',
|
|
15
|
+
SET_BOUNDS: 'set-bounds',
|
|
16
|
+
SET_CONTENTLET: 'set-contentlet',
|
|
17
|
+
IFRAME_SCROLL: 'scroll',
|
|
18
|
+
PING_EDITOR: 'ping-editor',
|
|
19
|
+
CONTENT_CHANGE: 'content-change',
|
|
20
|
+
GET_PAGE_DATA: 'get-page-data',
|
|
21
|
+
NOOP: 'noop'
|
|
22
|
+
}
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
const CONTAINER_MOCK = {
|
|
26
|
+
acceptTypes: 'text/html',
|
|
27
|
+
identifier: '123',
|
|
28
|
+
maxContentlets: '1',
|
|
29
|
+
uuid: '123-456'
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const getContentletTestElement = () => {
|
|
33
|
+
const div = document.createElement('div');
|
|
34
|
+
|
|
35
|
+
div.setAttribute('data-dot-object', 'contentlet');
|
|
36
|
+
div.setAttribute('data-dot-identifier', '123');
|
|
37
|
+
div.setAttribute('data-dot-title', 'title');
|
|
38
|
+
div.setAttribute('data-dot-inode', '123-456');
|
|
39
|
+
div.setAttribute('data-dot-type', 'CONTENT');
|
|
40
|
+
div.setAttribute('data-dot-basetype', 'WIDGET');
|
|
41
|
+
div.setAttribute('data-dot-widget-title', 'widgetTitle');
|
|
42
|
+
div.setAttribute('data-dot-on-number-of-pages', '1');
|
|
43
|
+
div.setAttribute('data-dot-container', JSON.stringify(CONTAINER_MOCK));
|
|
44
|
+
div.innerHTML = 'contentlet';
|
|
45
|
+
|
|
46
|
+
return div;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Observation: We must test the execution of methods as well.
|
|
51
|
+
*/
|
|
52
|
+
describe('listeners', () => {
|
|
53
|
+
it('should listen editor messages', () => {
|
|
54
|
+
const addEventListenerSpy = jest.spyOn(window, 'addEventListener');
|
|
55
|
+
listenEditorMessages();
|
|
56
|
+
expect(addEventListenerSpy).toHaveBeenCalledWith('message', expect.any(Function));
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should listen to hovered contentlet', () => {
|
|
60
|
+
const addEventListenerSpy = jest.spyOn(document, 'addEventListener');
|
|
61
|
+
|
|
62
|
+
listenHoveredContentlet();
|
|
63
|
+
|
|
64
|
+
const target = getContentletTestElement();
|
|
65
|
+
const event = new MouseEvent('pointermove', {
|
|
66
|
+
bubbles: true,
|
|
67
|
+
cancelable: true
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
document.body.appendChild(target);
|
|
71
|
+
target.dispatchEvent(event);
|
|
72
|
+
|
|
73
|
+
expect(addEventListenerSpy).toHaveBeenCalledWith('pointermove', expect.any(Function));
|
|
74
|
+
expect(postMessageToEditor).toHaveBeenCalledWith({
|
|
75
|
+
action: CUSTOMER_ACTIONS.SET_CONTENTLET,
|
|
76
|
+
payload: {
|
|
77
|
+
x: expect.any(Number),
|
|
78
|
+
y: expect.any(Number),
|
|
79
|
+
width: expect.any(Number),
|
|
80
|
+
height: expect.any(Number),
|
|
81
|
+
payload: {
|
|
82
|
+
container: CONTAINER_MOCK,
|
|
83
|
+
contentlet: {
|
|
84
|
+
baseType: 'WIDGET',
|
|
85
|
+
identifier: '123',
|
|
86
|
+
inode: '123-456',
|
|
87
|
+
contentType: 'CONTENT',
|
|
88
|
+
title: 'title',
|
|
89
|
+
widgetTitle: 'widgetTitle',
|
|
90
|
+
onNumberOfPages: '1'
|
|
91
|
+
},
|
|
92
|
+
vtlFiles: null
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should handle scroll', () => {
|
|
99
|
+
const addEventListenerSpy = jest.spyOn(window, 'addEventListener');
|
|
100
|
+
scrollHandler();
|
|
101
|
+
expect(addEventListenerSpy).toHaveBeenCalledWith('scroll', expect.any(Function));
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should preserve scroll on iframe', () => {
|
|
105
|
+
const addEventListenerSpy = jest.spyOn(window, 'addEventListener');
|
|
106
|
+
preserveScrollOnIframe();
|
|
107
|
+
expect(addEventListenerSpy).toHaveBeenCalledWith('load', expect.any(Function));
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should get page data post message to editor', () => {
|
|
111
|
+
fetchPageDataFromInsideUVE('some-url');
|
|
112
|
+
expect(postMessageToEditor).toHaveBeenCalledWith({
|
|
113
|
+
action: CUSTOMER_ACTIONS.GET_PAGE_DATA,
|
|
114
|
+
payload: {
|
|
115
|
+
pathname: 'some-url'
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
});
|