@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,416 @@
|
|
|
1
|
+
import { Equals } from '../../../../query-builder/lucene-syntax';
|
|
2
|
+
import { QueryBuilder } from '../../../../query-builder/sdk-query-builder';
|
|
3
|
+
import { ClientOptions } from '../../../sdk-js-client';
|
|
4
|
+
import { CONTENT_API_URL } from '../../shared/const';
|
|
5
|
+
import {
|
|
6
|
+
GetCollectionResponse,
|
|
7
|
+
BuildQuery,
|
|
8
|
+
SortBy,
|
|
9
|
+
GetCollectionRawResponse,
|
|
10
|
+
GetCollectionError,
|
|
11
|
+
OnFullfilled,
|
|
12
|
+
OnRejected
|
|
13
|
+
} from '../../shared/types';
|
|
14
|
+
import { sanitizeQueryForContentType } from '../../shared/utils';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Creates a Builder to filter and fetch content from the content API for a specific content type.
|
|
18
|
+
*
|
|
19
|
+
* @export
|
|
20
|
+
* @class CollectionBuilder
|
|
21
|
+
* @template T Represents the type of the content type to fetch. Defaults to unknown.
|
|
22
|
+
*/
|
|
23
|
+
export class CollectionBuilder<T = unknown> {
|
|
24
|
+
#page = 1;
|
|
25
|
+
#limit = 10;
|
|
26
|
+
#depth = 0;
|
|
27
|
+
#render = false;
|
|
28
|
+
#sortBy?: SortBy[];
|
|
29
|
+
#contentType: string;
|
|
30
|
+
#defaultQuery: Equals;
|
|
31
|
+
#query?: Equals;
|
|
32
|
+
#rawQuery?: string;
|
|
33
|
+
#languageId: number | string = 1;
|
|
34
|
+
#draft = false;
|
|
35
|
+
|
|
36
|
+
#serverUrl: string;
|
|
37
|
+
#requestOptions: ClientOptions;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Creates an instance of CollectionBuilder.
|
|
41
|
+
* @param {ClientOptions} requestOptions Options for the client request.
|
|
42
|
+
* @param {string} serverUrl The server URL.
|
|
43
|
+
* @param {string} contentType The content type to fetch.
|
|
44
|
+
* @memberof CollectionBuilder
|
|
45
|
+
*/
|
|
46
|
+
constructor(requestOptions: ClientOptions, serverUrl: string, contentType: string) {
|
|
47
|
+
this.#requestOptions = requestOptions;
|
|
48
|
+
this.#serverUrl = serverUrl;
|
|
49
|
+
this.#contentType = contentType;
|
|
50
|
+
|
|
51
|
+
// Build the default query with the contentType field
|
|
52
|
+
this.#defaultQuery = new QueryBuilder().field('contentType').equals(this.#contentType);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Returns the sort query in the format: field order, field order, ...
|
|
57
|
+
*
|
|
58
|
+
* @readonly
|
|
59
|
+
* @private
|
|
60
|
+
* @memberof CollectionBuilder
|
|
61
|
+
*/
|
|
62
|
+
private get sort() {
|
|
63
|
+
return this.#sortBy?.map((sort) => `${sort.field} ${sort.order}`).join(',');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Returns the offset for pagination.
|
|
68
|
+
*
|
|
69
|
+
* @readonly
|
|
70
|
+
* @private
|
|
71
|
+
* @memberof CollectionBuilder
|
|
72
|
+
*/
|
|
73
|
+
private get offset() {
|
|
74
|
+
return this.#limit * (this.#page - 1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Returns the full URL for the content API.
|
|
79
|
+
*
|
|
80
|
+
* @readonly
|
|
81
|
+
* @private
|
|
82
|
+
* @memberof CollectionBuilder
|
|
83
|
+
*/
|
|
84
|
+
private get url() {
|
|
85
|
+
return `${this.#serverUrl}${CONTENT_API_URL}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns the current query built.
|
|
90
|
+
*
|
|
91
|
+
* @readonly
|
|
92
|
+
* @private
|
|
93
|
+
* @memberof CollectionBuilder
|
|
94
|
+
*/
|
|
95
|
+
private get currentQuery() {
|
|
96
|
+
return this.#query ?? this.#defaultQuery;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Filters the content by the specified language ID.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* const client = new DotCMSClient(config);
|
|
105
|
+
* const collectionBuilder = client.content.getCollection("Blog");
|
|
106
|
+
* collectionBuilder.language(1);
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @param {number | string} languageId The language ID to filter the content by.
|
|
110
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
111
|
+
* @memberof CollectionBuilder
|
|
112
|
+
*/
|
|
113
|
+
language(languageId: number | string): this {
|
|
114
|
+
this.#languageId = languageId;
|
|
115
|
+
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Setting this to true will server side render (using velocity) any widgets that are returned by the content query.
|
|
121
|
+
*
|
|
122
|
+
* More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
|
|
123
|
+
*
|
|
124
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
125
|
+
* @memberof CollectionBuilder
|
|
126
|
+
*/
|
|
127
|
+
render(): this {
|
|
128
|
+
this.#render = true;
|
|
129
|
+
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Sorts the content by the specified fields and orders.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const client = new DotCMSClient(config);
|
|
139
|
+
* const collectionBuilder = client.content.getCollection("Blog");
|
|
140
|
+
* const sortBy = [{ field: 'title', order: 'asc' }, { field: 'modDate', order: 'desc' }];
|
|
141
|
+
* collectionBuilder("Blog").sortBy(sortBy);
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
* @param {SortBy[]} sortBy Array of constraints to sort the content by.
|
|
145
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
146
|
+
* @memberof CollectionBuilder
|
|
147
|
+
*/
|
|
148
|
+
sortBy(sortBy: SortBy[]): this {
|
|
149
|
+
this.#sortBy = sortBy;
|
|
150
|
+
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Sets the maximum amount of content to fetch.
|
|
156
|
+
*
|
|
157
|
+
* @param {number} limit The maximum amount of content to fetch.
|
|
158
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
159
|
+
* @memberof CollectionBuilder
|
|
160
|
+
*/
|
|
161
|
+
limit(limit: number): this {
|
|
162
|
+
this.#limit = limit;
|
|
163
|
+
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Sets the page number to fetch.
|
|
169
|
+
*
|
|
170
|
+
* @param {number} page The page number to fetch.
|
|
171
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
172
|
+
* @memberof CollectionBuilder
|
|
173
|
+
*/
|
|
174
|
+
page(page: number): this {
|
|
175
|
+
this.#page = page;
|
|
176
|
+
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Filters the content by a Lucene query string.
|
|
182
|
+
*
|
|
183
|
+
* @param {string} query A Lucene query string.
|
|
184
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
185
|
+
* @memberof CollectionBuilder
|
|
186
|
+
*/
|
|
187
|
+
query(query: string): this;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Filters the content by building a query using a QueryBuilder function.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```typescript
|
|
194
|
+
* const client = new DotCMSClient(config);
|
|
195
|
+
* const collectionBuilder = client.content.getCollection("Blog");
|
|
196
|
+
* collectionBuilder.query((queryBuilder) =>
|
|
197
|
+
* queryBuilder.field('title').equals('Hello World').or().equals('Hello World 2')
|
|
198
|
+
* );
|
|
199
|
+
* ```
|
|
200
|
+
*
|
|
201
|
+
* @param {BuildQuery} buildQuery A function that receives a QueryBuilder instance and returns a valid query.
|
|
202
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
203
|
+
* @memberof CollectionBuilder
|
|
204
|
+
*/
|
|
205
|
+
query(buildQuery: BuildQuery): this;
|
|
206
|
+
query(arg: unknown): this {
|
|
207
|
+
if (typeof arg === 'string') {
|
|
208
|
+
this.#rawQuery = arg;
|
|
209
|
+
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (typeof arg !== 'function') {
|
|
214
|
+
throw new Error(
|
|
215
|
+
`Parameter for query method should be a buildQuery function or a string.\nExample:\nclient.content.getCollection('Activity').query((queryBuilder) => queryBuilder.field('title').equals('Hello World'))\nor\nclient.content.getCollection('Activity').query('+Activity.title:"Hello World"') \nSee documentation for more information.`
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const builtQuery = arg(new QueryBuilder());
|
|
220
|
+
|
|
221
|
+
// This can be use in Javascript so we cannot rely on the type checking
|
|
222
|
+
if (builtQuery instanceof Equals) {
|
|
223
|
+
this.#query = builtQuery.raw(this.currentQuery.build());
|
|
224
|
+
} else {
|
|
225
|
+
throw new Error(
|
|
226
|
+
'Provided query is not valid. A query should end in an equals method call.\nExample:\n(queryBuilder) => queryBuilder.field("title").equals("Hello World")\nSee documentation for more information.'
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Retrieves draft content.
|
|
235
|
+
* @example
|
|
236
|
+
* ```ts
|
|
237
|
+
* const client = new DotCMSClient(config);
|
|
238
|
+
* const collectionBuilder = client.content.getCollection("Blog");
|
|
239
|
+
* collectionBuilder
|
|
240
|
+
* .draft() // This will retrieve draft/working content
|
|
241
|
+
* .then((response) => // Your code here })
|
|
242
|
+
* .catch((error) => // Your code here })
|
|
243
|
+
* ```
|
|
244
|
+
*
|
|
245
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
246
|
+
* @memberof CollectionBuilder
|
|
247
|
+
*/
|
|
248
|
+
draft(): this {
|
|
249
|
+
this.#draft = true;
|
|
250
|
+
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Filters the content by a variant ID for [Experiments](https://www.dotcms.com/docs/latest/experiments-and-a-b-testing)
|
|
256
|
+
*
|
|
257
|
+
* More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* const client = new DotCMSClient(config);
|
|
262
|
+
* const collectionBuilder = client.content.getCollection("Blog");
|
|
263
|
+
* collectionBuilder
|
|
264
|
+
* .variant("YOUR_VARIANT_ID")
|
|
265
|
+
* .then((response) => // Your code here })
|
|
266
|
+
* .catch((error) => // Your code here })
|
|
267
|
+
* ```
|
|
268
|
+
*
|
|
269
|
+
* @param {string} variantId A string that represents a variant ID.
|
|
270
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
271
|
+
* @memberof CollectionBuilder
|
|
272
|
+
*/
|
|
273
|
+
variant(variantId: string): this {
|
|
274
|
+
this.#query = this.currentQuery.field('variant').equals(variantId);
|
|
275
|
+
|
|
276
|
+
return this;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Sets the depth of the relationships of the content.
|
|
281
|
+
* Specifies the depth of related content to return in the results.
|
|
282
|
+
*
|
|
283
|
+
* More information here: {@link https://www.dotcms.com/docs/latest/content-api-retrieval-and-querying#ParamsOptional}
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```ts
|
|
287
|
+
* const client = new DotCMSClient(config);
|
|
288
|
+
* const collectionBuilder = client.content.getCollection("Blog");
|
|
289
|
+
* collectionBuilder
|
|
290
|
+
* .depth(1)
|
|
291
|
+
* .then((response) => // Your code here })
|
|
292
|
+
* .catch((error) => // Your code here })
|
|
293
|
+
* ```
|
|
294
|
+
*
|
|
295
|
+
* @param {number} depth The depth of the relationships of the content.
|
|
296
|
+
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
297
|
+
* @memberof CollectionBuilder
|
|
298
|
+
*/
|
|
299
|
+
depth(depth: number): this {
|
|
300
|
+
if (depth < 0 || depth > 3) {
|
|
301
|
+
throw new Error('Depth value must be between 0 and 3');
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
this.#depth = depth;
|
|
305
|
+
|
|
306
|
+
return this;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Executes the fetch and returns a promise that resolves to the content or rejects with an error.
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* ```ts
|
|
314
|
+
* const client = new DotCMSClient(config);
|
|
315
|
+
* const collectionBuilder = client.content.getCollection("Blog");
|
|
316
|
+
* collectionBuilder
|
|
317
|
+
* .limit(10)
|
|
318
|
+
* .then((response) => // Your code here })
|
|
319
|
+
* .catch((error) => // Your code here })
|
|
320
|
+
* ```
|
|
321
|
+
*
|
|
322
|
+
* @param {OnFullfilled} [onfulfilled] A callback that is called when the fetch is successful.
|
|
323
|
+
* @param {OnRejected} [onrejected] A callback that is called when the fetch fails.
|
|
324
|
+
* @return {Promise<GetCollectionResponse<T> | GetCollectionError>} A promise that resolves to the content or rejects with an error.
|
|
325
|
+
* @memberof CollectionBuilder
|
|
326
|
+
*/
|
|
327
|
+
then(
|
|
328
|
+
onfulfilled?: OnFullfilled<T>,
|
|
329
|
+
onrejected?: OnRejected
|
|
330
|
+
): Promise<GetCollectionResponse<T> | GetCollectionError> {
|
|
331
|
+
return this.fetch().then(async (response) => {
|
|
332
|
+
const data = await response.json();
|
|
333
|
+
if (response.ok) {
|
|
334
|
+
const formattedResponse = this.formatResponse<T>(data);
|
|
335
|
+
|
|
336
|
+
const finalResponse =
|
|
337
|
+
typeof onfulfilled === 'function'
|
|
338
|
+
? onfulfilled(formattedResponse)
|
|
339
|
+
: formattedResponse;
|
|
340
|
+
|
|
341
|
+
return finalResponse;
|
|
342
|
+
} else {
|
|
343
|
+
return {
|
|
344
|
+
status: response.status,
|
|
345
|
+
...data
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
}, onrejected);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Formats the response to the desired format.
|
|
353
|
+
*
|
|
354
|
+
* @private
|
|
355
|
+
* @param {GetCollectionRawResponse<T>} data The raw response data.
|
|
356
|
+
* @return {GetCollectionResponse<T>} The formatted response.
|
|
357
|
+
* @memberof CollectionBuilder
|
|
358
|
+
*/
|
|
359
|
+
private formatResponse<T>(data: GetCollectionRawResponse<T>): GetCollectionResponse<T> {
|
|
360
|
+
const contentlets = data.entity.jsonObjectView.contentlets;
|
|
361
|
+
const total = data.entity.resultsSize;
|
|
362
|
+
|
|
363
|
+
const mappedResponse: GetCollectionResponse<T> = {
|
|
364
|
+
contentlets,
|
|
365
|
+
total,
|
|
366
|
+
page: this.#page,
|
|
367
|
+
size: contentlets.length
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
return this.#sortBy
|
|
371
|
+
? {
|
|
372
|
+
...mappedResponse,
|
|
373
|
+
sortedBy: this.#sortBy
|
|
374
|
+
}
|
|
375
|
+
: mappedResponse;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Calls the content API to fetch the content.
|
|
380
|
+
*
|
|
381
|
+
* @private
|
|
382
|
+
* @return {Promise<Response>} The fetch response.
|
|
383
|
+
* @memberof CollectionBuilder
|
|
384
|
+
*/
|
|
385
|
+
private fetch(): Promise<Response> {
|
|
386
|
+
const finalQuery = this.currentQuery
|
|
387
|
+
.field('languageId')
|
|
388
|
+
.equals(this.#languageId.toString())
|
|
389
|
+
.field('live')
|
|
390
|
+
.equals((!this.#draft).toString())
|
|
391
|
+
.build();
|
|
392
|
+
|
|
393
|
+
const sanitizedQuery = sanitizeQueryForContentType(finalQuery, this.#contentType);
|
|
394
|
+
|
|
395
|
+
const query = this.#rawQuery ? `${sanitizedQuery} ${this.#rawQuery}` : sanitizedQuery;
|
|
396
|
+
|
|
397
|
+
return fetch(this.url, {
|
|
398
|
+
...this.#requestOptions,
|
|
399
|
+
method: 'POST',
|
|
400
|
+
headers: {
|
|
401
|
+
...this.#requestOptions.headers,
|
|
402
|
+
'Content-Type': 'application/json'
|
|
403
|
+
},
|
|
404
|
+
body: JSON.stringify({
|
|
405
|
+
query,
|
|
406
|
+
render: this.#render,
|
|
407
|
+
sort: this.sort,
|
|
408
|
+
limit: this.#limit,
|
|
409
|
+
offset: this.offset,
|
|
410
|
+
depth: this.#depth
|
|
411
|
+
//userId: This exist but we currently don't use it
|
|
412
|
+
//allCategoriesInfo: This exist but we currently don't use it
|
|
413
|
+
})
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { CollectionBuilder } from './builders/collection/collection';
|
|
2
|
+
|
|
3
|
+
import { ClientOptions } from '../sdk-js-client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a builder to filter and fetch a collection of content items.
|
|
7
|
+
* @param contentType - The content type to retrieve.
|
|
8
|
+
* @returns A CollectionBuilder instance for chaining filters and executing the query.
|
|
9
|
+
* @template T - The type of the content items (defaults to unknown).
|
|
10
|
+
*
|
|
11
|
+
* @example Fetch blog posts with async/await
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const response = await client.content
|
|
14
|
+
* .getCollection<BlogPost>('Blog')
|
|
15
|
+
* .limit(10)
|
|
16
|
+
* .page(2)
|
|
17
|
+
* .sortBy([{ field: 'title', order: 'asc' }])
|
|
18
|
+
* .query(q => q.field('author').equals('John Doe'))
|
|
19
|
+
* .depth(1)
|
|
20
|
+
* .fetch();
|
|
21
|
+
*
|
|
22
|
+
* console.log(response.contentlets);
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example Fetch blog posts with Promise chain
|
|
26
|
+
* ```typescript
|
|
27
|
+
* client.content
|
|
28
|
+
* .getCollection<BlogPost>('Blog')
|
|
29
|
+
* .limit(10)
|
|
30
|
+
* .page(2)
|
|
31
|
+
* .sortBy([{ field: 'title', order: 'asc' }])
|
|
32
|
+
* .query(q => q.field('author').equals('John Doe'))
|
|
33
|
+
* .depth(1)
|
|
34
|
+
* .fetch()
|
|
35
|
+
* .then(response => console.log(response.contentlets))
|
|
36
|
+
* .catch(error => console.error(error));
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @example Using a custom type
|
|
40
|
+
* ```typescript
|
|
41
|
+
* interface BlogPost {
|
|
42
|
+
* summary: string;
|
|
43
|
+
* author: string;
|
|
44
|
+
* title: string;
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* const posts = await client.content
|
|
48
|
+
* .getCollection<BlogPost>('Blog')
|
|
49
|
+
* .limit(10)
|
|
50
|
+
* .fetch();
|
|
51
|
+
*
|
|
52
|
+
* posts.contentlets.forEach(post => {
|
|
53
|
+
* console.log(post.title, post.author, post.summary);
|
|
54
|
+
* });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export class Content {
|
|
58
|
+
#requestOptions: ClientOptions;
|
|
59
|
+
#serverUrl: string;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Creates an instance of Content.
|
|
63
|
+
* @param {ClientOptions} requestOptions - The options for the client request.
|
|
64
|
+
* @param {string} serverUrl - The server URL.
|
|
65
|
+
*/
|
|
66
|
+
constructor(requestOptions: ClientOptions, serverUrl: string) {
|
|
67
|
+
this.#requestOptions = requestOptions;
|
|
68
|
+
this.#serverUrl = serverUrl;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Takes a content type and returns a builder to filter and fetch the collection.
|
|
73
|
+
* @param {string} contentType - The content type to get the collection.
|
|
74
|
+
* @return {CollectionBuilder<T>} CollectionBuilder to filter and fetch the collection.
|
|
75
|
+
* @template T - Represents the type of the content type to fetch. Defaults to unknown.
|
|
76
|
+
* @memberof Content
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```javascript
|
|
80
|
+
* // Using await and async
|
|
81
|
+
* const collectionResponse = await client.content
|
|
82
|
+
* .getCollection('Blog')
|
|
83
|
+
* .limit(10)
|
|
84
|
+
* .page(2)
|
|
85
|
+
* .sortBy([{ field: 'title', order: 'asc' }])
|
|
86
|
+
* .query((queryBuilder) => queryBuilder.field('author').equals('John Doe'))
|
|
87
|
+
* .depth(1);
|
|
88
|
+
* ```
|
|
89
|
+
* @example
|
|
90
|
+
* ```javascript
|
|
91
|
+
* // Using then and catch
|
|
92
|
+
* client.content
|
|
93
|
+
* .getCollection('Blog')
|
|
94
|
+
* .limit(10)
|
|
95
|
+
* .page(2)
|
|
96
|
+
* .sortBy([{ field: 'title', order: 'asc' }])
|
|
97
|
+
* .query((queryBuilder) => queryBuilder.field('author').equals('John Doe'))
|
|
98
|
+
* .depth(1)
|
|
99
|
+
* .then((response) => {
|
|
100
|
+
* console.log(response.contentlets);
|
|
101
|
+
* })
|
|
102
|
+
* .catch((error) => {
|
|
103
|
+
* console.error(error);
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* // Using a specific type for your content
|
|
109
|
+
*
|
|
110
|
+
* type Blog = {
|
|
111
|
+
* summary: string;
|
|
112
|
+
* author: string;
|
|
113
|
+
* title: string;
|
|
114
|
+
* };
|
|
115
|
+
*
|
|
116
|
+
* client.content
|
|
117
|
+
* .getCollection<Blog>('Blog')
|
|
118
|
+
* .limit(10)
|
|
119
|
+
* .page(2)
|
|
120
|
+
* .sortBy([{ field: 'title', order: 'asc' }])
|
|
121
|
+
* .query((queryBuilder) => queryBuilder.field('author').equals('John Doe'))
|
|
122
|
+
* .depth(1)
|
|
123
|
+
* .then((response) => {
|
|
124
|
+
* response.contentlets.forEach((blog) => {
|
|
125
|
+
* console.log(blog.title);
|
|
126
|
+
* console.log(blog.author);
|
|
127
|
+
* console.log(blog.summary);
|
|
128
|
+
* });
|
|
129
|
+
* })
|
|
130
|
+
* .catch((error) => {
|
|
131
|
+
* console.error(error);
|
|
132
|
+
* });
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
*/
|
|
136
|
+
getCollection<T = unknown>(contentType: string): CollectionBuilder<T> {
|
|
137
|
+
return new CollectionBuilder<T>(this.#requestOptions, this.#serverUrl, contentType);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default variant identifier used in the application.
|
|
3
|
+
*/
|
|
4
|
+
export const DEFAULT_VARIANT_ID = 'DEFAULT';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Fields that should not be formatted when sanitizing the query.
|
|
8
|
+
* These fields are essential for maintaining the integrity of the content type.
|
|
9
|
+
*/
|
|
10
|
+
export const CONTENT_TYPE_MAIN_FIELDS: string[] = ['live', 'variant', 'contentType', 'languageId'];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* URL endpoint for the content API search functionality.
|
|
14
|
+
*/
|
|
15
|
+
export const CONTENT_API_URL = '/api/content/_search';
|