@dotcms/client 1.0.1 → 1.0.2-next.2
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/index.cjs.d.ts +1 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +1592 -0
- package/index.cjs.mjs +2 -0
- package/index.esm.d.ts +1 -0
- package/index.esm.js +1590 -0
- package/internal.cjs.d.ts +1 -0
- package/internal.cjs.default.js +1 -0
- package/internal.cjs.js +85 -0
- package/internal.cjs.mjs +2 -0
- package/internal.esm.d.ts +1 -0
- package/internal.esm.js +83 -0
- package/package.json +24 -7
- package/src/lib/client/client.d.ts +56 -0
- package/src/lib/client/content/builders/collection/{collection.ts → collection.d.ts} +19 -210
- package/src/lib/client/content/builders/query/lucene-syntax/{Equals.ts → Equals.d.ts} +11 -45
- package/src/lib/client/content/builders/query/lucene-syntax/{Field.ts → Field.d.ts} +5 -13
- package/src/lib/client/content/builders/query/lucene-syntax/{NotOperand.ts → NotOperand.d.ts} +5 -13
- package/src/lib/client/content/builders/query/lucene-syntax/{Operand.ts → Operand.d.ts} +7 -21
- package/src/lib/client/content/builders/query/{query.ts → query.d.ts} +5 -16
- package/src/lib/client/content/builders/query/utils/{index.ts → index.d.ts} +12 -49
- package/src/lib/client/content/{content-api.ts → content-api.d.ts} +4 -14
- package/src/lib/client/content/shared/{const.ts → const.d.ts} +3 -5
- package/src/lib/client/content/shared/{types.ts → types.d.ts} +2 -18
- package/src/lib/client/content/shared/{utils.ts → utils.d.ts} +1 -9
- package/src/lib/client/models/{index.ts → index.d.ts} +1 -8
- package/src/lib/client/navigation/navigation-api.d.ts +14 -0
- package/src/lib/client/page/page-api.d.ts +95 -0
- package/src/lib/client/page/utils.d.ts +41 -0
- package/src/lib/utils/graphql/transforms.d.ts +13 -0
- package/.eslintrc.json +0 -18
- package/jest.config.ts +0 -15
- package/project.json +0 -73
- package/src/lib/client/client.spec.ts +0 -147
- package/src/lib/client/client.ts +0 -125
- package/src/lib/client/content/builders/collection/collection.spec.ts +0 -514
- package/src/lib/client/content/builders/query/query.spec.ts +0 -159
- package/src/lib/client/navigation/navigation-api.spec.ts +0 -167
- package/src/lib/client/navigation/navigation-api.ts +0 -62
- package/src/lib/client/page/page-api.spec.ts +0 -359
- package/src/lib/client/page/page-api.ts +0 -197
- package/src/lib/client/page/utils.ts +0 -291
- package/src/lib/utils/graphql/transforms.spec.ts +0 -250
- package/src/lib/utils/graphql/transforms.ts +0 -128
- package/tsconfig.json +0 -22
- package/tsconfig.lib.json +0 -13
- package/tsconfig.spec.json +0 -9
- /package/src/{index.ts → index.d.ts} +0 -0
- /package/src/{internal.ts → internal.d.ts} +0 -0
- /package/src/lib/client/content/builders/query/lucene-syntax/{index.ts → index.d.ts} +0 -0
- /package/src/lib/utils/{index.ts → index.d.ts} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/internal";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./internal.cjs.js').default;
|
package/internal.cjs.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
4
|
+
/**
|
|
5
|
+
* Transforms a GraphQL Page response to a Page Entity.
|
|
6
|
+
*
|
|
7
|
+
* @param {GraphQLPageResponse} graphQLPageResponse - The GraphQL Page response object.
|
|
8
|
+
* @returns {object|null} The transformed Page Entity or null if the page is not present.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const pageEntity = graphqlToPageEntity(graphQLPageResponse);
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
const graphqlToPageEntity = (graphQLPageResponse) => {
|
|
16
|
+
const { page } = graphQLPageResponse;
|
|
17
|
+
// If there is no page, return null
|
|
18
|
+
if (!page) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
const { layout, template, containers, urlContentMap, viewAs, host, vanityUrl, runningExperimentId, _map, ...pageAsset } = page;
|
|
22
|
+
const data = (_map || {});
|
|
23
|
+
const typedPageAsset = pageAsset;
|
|
24
|
+
// To prevent type errors, we cast the urlContentMap to an object
|
|
25
|
+
const urlContentMapObject = urlContentMap;
|
|
26
|
+
// Extract the _map data from the urlContentMap object
|
|
27
|
+
const urlContentMapData = urlContentMapObject?.['_map'];
|
|
28
|
+
return {
|
|
29
|
+
layout,
|
|
30
|
+
template,
|
|
31
|
+
viewAs,
|
|
32
|
+
vanityUrl,
|
|
33
|
+
runningExperimentId,
|
|
34
|
+
site: host,
|
|
35
|
+
urlContentMap: urlContentMapData,
|
|
36
|
+
containers: parseContainers(containers),
|
|
37
|
+
page: {
|
|
38
|
+
...data,
|
|
39
|
+
...typedPageAsset
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Parses the containers from the GraphQL response.
|
|
45
|
+
*
|
|
46
|
+
* @param {DotCMSGraphQLPageContainer[]} [containers=[]] - The containers array from the GraphQL response.
|
|
47
|
+
* @returns {DotCMSPageAssetContainers} The parsed containers.
|
|
48
|
+
*/
|
|
49
|
+
const parseContainers = (containers = []) => {
|
|
50
|
+
return containers.reduce((acc, container) => {
|
|
51
|
+
const { path, identifier, containerStructures, containerContentlets, ...rest } = container;
|
|
52
|
+
const key = (path || identifier);
|
|
53
|
+
acc[key] = {
|
|
54
|
+
containerStructures,
|
|
55
|
+
container: {
|
|
56
|
+
path,
|
|
57
|
+
identifier,
|
|
58
|
+
...rest
|
|
59
|
+
},
|
|
60
|
+
contentlets: parseContentletsToUuidMap(containerContentlets)
|
|
61
|
+
};
|
|
62
|
+
return acc;
|
|
63
|
+
}, {});
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Parses the contentlets from the GraphQL response.
|
|
67
|
+
*
|
|
68
|
+
* @param {Array<Record<string, unknown>>} containerContentlets - The contentlets array from the GraphQL response.
|
|
69
|
+
* @returns {Record<string, Array<Record<string, unknown>>>} The parsed contentlets mapped by UUID.
|
|
70
|
+
*/
|
|
71
|
+
const parseContentletsToUuidMap = (containerContentlets = []) => {
|
|
72
|
+
return containerContentlets.reduce((acc, containerContentlet) => {
|
|
73
|
+
const { uuid, contentlets } = containerContentlet;
|
|
74
|
+
// TODO: This is a temporary solution, we need to find a better way to handle this.
|
|
75
|
+
acc[uuid] = contentlets.map(({ _map = {}, ...rest }) => {
|
|
76
|
+
return {
|
|
77
|
+
..._map,
|
|
78
|
+
...rest
|
|
79
|
+
};
|
|
80
|
+
});
|
|
81
|
+
return acc;
|
|
82
|
+
}, {});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
exports.graphqlToPageEntity = graphqlToPageEntity;
|
package/internal.cjs.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/internal";
|
package/internal.esm.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/**
|
|
3
|
+
* Transforms a GraphQL Page response to a Page Entity.
|
|
4
|
+
*
|
|
5
|
+
* @param {GraphQLPageResponse} graphQLPageResponse - The GraphQL Page response object.
|
|
6
|
+
* @returns {object|null} The transformed Page Entity or null if the page is not present.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const pageEntity = graphqlToPageEntity(graphQLPageResponse);
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
const graphqlToPageEntity = (graphQLPageResponse) => {
|
|
14
|
+
const { page } = graphQLPageResponse;
|
|
15
|
+
// If there is no page, return null
|
|
16
|
+
if (!page) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
const { layout, template, containers, urlContentMap, viewAs, host, vanityUrl, runningExperimentId, _map, ...pageAsset } = page;
|
|
20
|
+
const data = (_map || {});
|
|
21
|
+
const typedPageAsset = pageAsset;
|
|
22
|
+
// To prevent type errors, we cast the urlContentMap to an object
|
|
23
|
+
const urlContentMapObject = urlContentMap;
|
|
24
|
+
// Extract the _map data from the urlContentMap object
|
|
25
|
+
const urlContentMapData = urlContentMapObject?.['_map'];
|
|
26
|
+
return {
|
|
27
|
+
layout,
|
|
28
|
+
template,
|
|
29
|
+
viewAs,
|
|
30
|
+
vanityUrl,
|
|
31
|
+
runningExperimentId,
|
|
32
|
+
site: host,
|
|
33
|
+
urlContentMap: urlContentMapData,
|
|
34
|
+
containers: parseContainers(containers),
|
|
35
|
+
page: {
|
|
36
|
+
...data,
|
|
37
|
+
...typedPageAsset
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Parses the containers from the GraphQL response.
|
|
43
|
+
*
|
|
44
|
+
* @param {DotCMSGraphQLPageContainer[]} [containers=[]] - The containers array from the GraphQL response.
|
|
45
|
+
* @returns {DotCMSPageAssetContainers} The parsed containers.
|
|
46
|
+
*/
|
|
47
|
+
const parseContainers = (containers = []) => {
|
|
48
|
+
return containers.reduce((acc, container) => {
|
|
49
|
+
const { path, identifier, containerStructures, containerContentlets, ...rest } = container;
|
|
50
|
+
const key = (path || identifier);
|
|
51
|
+
acc[key] = {
|
|
52
|
+
containerStructures,
|
|
53
|
+
container: {
|
|
54
|
+
path,
|
|
55
|
+
identifier,
|
|
56
|
+
...rest
|
|
57
|
+
},
|
|
58
|
+
contentlets: parseContentletsToUuidMap(containerContentlets)
|
|
59
|
+
};
|
|
60
|
+
return acc;
|
|
61
|
+
}, {});
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Parses the contentlets from the GraphQL response.
|
|
65
|
+
*
|
|
66
|
+
* @param {Array<Record<string, unknown>>} containerContentlets - The contentlets array from the GraphQL response.
|
|
67
|
+
* @returns {Record<string, Array<Record<string, unknown>>>} The parsed contentlets mapped by UUID.
|
|
68
|
+
*/
|
|
69
|
+
const parseContentletsToUuidMap = (containerContentlets = []) => {
|
|
70
|
+
return containerContentlets.reduce((acc, containerContentlet) => {
|
|
71
|
+
const { uuid, contentlets } = containerContentlet;
|
|
72
|
+
// TODO: This is a temporary solution, we need to find a better way to handle this.
|
|
73
|
+
acc[uuid] = contentlets.map(({ _map = {}, ...rest }) => {
|
|
74
|
+
return {
|
|
75
|
+
..._map,
|
|
76
|
+
...rest
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
return acc;
|
|
80
|
+
}, {});
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export { graphqlToPageEntity };
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotcms/client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2-next.2",
|
|
4
4
|
"description": "Official JavaScript library for interacting with DotCMS REST APIs.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/dotCMS/core.git#main"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"consola": "^3.4.2"
|
|
10
|
+
"consola": "^3.4.2"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"@dotcms/types": "latest"
|
|
@@ -24,13 +24,27 @@
|
|
|
24
24
|
],
|
|
25
25
|
"exports": {
|
|
26
26
|
"./package.json": "./package.json",
|
|
27
|
-
".":
|
|
28
|
-
|
|
27
|
+
".": {
|
|
28
|
+
"module": "./index.esm.js",
|
|
29
|
+
"types": "./index.esm.d.ts",
|
|
30
|
+
"import": "./index.cjs.mjs",
|
|
31
|
+
"default": "./index.cjs.js"
|
|
32
|
+
},
|
|
33
|
+
"./internal": {
|
|
34
|
+
"module": "./internal.esm.js",
|
|
35
|
+
"types": "./internal.esm.d.ts",
|
|
36
|
+
"import": "./internal.cjs.mjs",
|
|
37
|
+
"default": "./internal.cjs.js"
|
|
38
|
+
}
|
|
29
39
|
},
|
|
30
40
|
"typesVersions": {
|
|
31
41
|
"*": {
|
|
32
|
-
".": [
|
|
33
|
-
|
|
42
|
+
".": [
|
|
43
|
+
"./src/index.d.ts"
|
|
44
|
+
],
|
|
45
|
+
"internal": [
|
|
46
|
+
"./src/internal.d.ts"
|
|
47
|
+
]
|
|
34
48
|
}
|
|
35
49
|
},
|
|
36
50
|
"author": "dotcms <dev@dotcms.com>",
|
|
@@ -38,5 +52,8 @@
|
|
|
38
52
|
"bugs": {
|
|
39
53
|
"url": "https://github.com/dotCMS/core/issues"
|
|
40
54
|
},
|
|
41
|
-
"homepage": "https://github.com/dotCMS/core/tree/main/core-web/libs/sdk/client/README.md"
|
|
55
|
+
"homepage": "https://github.com/dotCMS/core/tree/main/core-web/libs/sdk/client/README.md",
|
|
56
|
+
"module": "./index.esm.js",
|
|
57
|
+
"main": "./index.cjs.js",
|
|
58
|
+
"types": "./index.esm.d.ts"
|
|
42
59
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { DotCMSClientConfig } from '@dotcms/types';
|
|
2
|
+
import { Content } from './content/content-api';
|
|
3
|
+
import { NavigationClient } from './navigation/navigation-api';
|
|
4
|
+
import { PageClient } from './page/page-api';
|
|
5
|
+
/**
|
|
6
|
+
* Client for interacting with the DotCMS REST API.
|
|
7
|
+
* Provides access to content, page, and navigation functionality.
|
|
8
|
+
*/
|
|
9
|
+
declare class DotCMSClient {
|
|
10
|
+
private config;
|
|
11
|
+
private requestOptions;
|
|
12
|
+
/**
|
|
13
|
+
* Client for content-related operations.
|
|
14
|
+
*/
|
|
15
|
+
content: Content;
|
|
16
|
+
/**
|
|
17
|
+
* Client for page-related operations.
|
|
18
|
+
*/
|
|
19
|
+
page: PageClient;
|
|
20
|
+
/**
|
|
21
|
+
* Client for navigation-related operations.
|
|
22
|
+
*/
|
|
23
|
+
nav: NavigationClient;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new DotCMS client instance.
|
|
26
|
+
*
|
|
27
|
+
* @param config - Configuration options for the client
|
|
28
|
+
* @throws Warning if dotcmsUrl is invalid or authToken is missing
|
|
29
|
+
*/
|
|
30
|
+
constructor(config?: DotCMSClientConfig);
|
|
31
|
+
/**
|
|
32
|
+
* Creates request options with authentication headers.
|
|
33
|
+
*
|
|
34
|
+
* @param config - The client configuration
|
|
35
|
+
* @returns Request options with authorization headers
|
|
36
|
+
*/
|
|
37
|
+
private createAuthenticatedRequestOptions;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates and returns a new DotCMS client instance.
|
|
41
|
+
*
|
|
42
|
+
* @param config - Configuration options for the client
|
|
43
|
+
* @returns A configured DotCMS client instance
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const client = dotCMSCreateClient({
|
|
47
|
+
* dotcmsUrl: 'https://demo.dotcms.com',
|
|
48
|
+
* authToken: 'your-auth-token'
|
|
49
|
+
* });
|
|
50
|
+
*
|
|
51
|
+
* // Use the client to fetch content
|
|
52
|
+
* const pages = await client.page.get('/about-us');
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare const createDotCMSClient: (clientConfig: DotCMSClientConfig) => DotCMSClient;
|
|
56
|
+
export {};
|
|
@@ -1,19 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
GetCollectionResponse,
|
|
4
|
-
BuildQuery,
|
|
5
|
-
SortBy,
|
|
6
|
-
GetCollectionRawResponse,
|
|
7
|
-
GetCollectionError,
|
|
8
|
-
OnFullfilled,
|
|
9
|
-
OnRejected
|
|
10
|
-
} from '../../shared/types';
|
|
11
|
-
import { sanitizeQueryForContentType } from '../../shared/utils';
|
|
12
|
-
import { Equals } from '../query/lucene-syntax';
|
|
13
|
-
import { QueryBuilder } from '../query/query';
|
|
14
|
-
|
|
1
|
+
import { GetCollectionResponse, BuildQuery, SortBy, GetCollectionError, OnFullfilled, OnRejected } from '../../shared/types';
|
|
15
2
|
export type ClientOptions = Omit<RequestInit, 'body' | 'method'>;
|
|
16
|
-
|
|
17
3
|
/**
|
|
18
4
|
* Creates a Builder to filter and fetch content from the content API for a specific content type.
|
|
19
5
|
*
|
|
@@ -21,22 +7,8 @@ export type ClientOptions = Omit<RequestInit, 'body' | 'method'>;
|
|
|
21
7
|
* @class CollectionBuilder
|
|
22
8
|
* @template T Represents the type of the content type to fetch. Defaults to unknown.
|
|
23
9
|
*/
|
|
24
|
-
export class CollectionBuilder<T = unknown> {
|
|
25
|
-
#
|
|
26
|
-
#limit = 10;
|
|
27
|
-
#depth = 0;
|
|
28
|
-
#render = false;
|
|
29
|
-
#sortBy?: SortBy[];
|
|
30
|
-
#contentType: string;
|
|
31
|
-
#defaultQuery: Equals;
|
|
32
|
-
#query?: Equals;
|
|
33
|
-
#rawQuery?: string;
|
|
34
|
-
#languageId: number | string = 1;
|
|
35
|
-
#draft = false;
|
|
36
|
-
|
|
37
|
-
#serverUrl: string;
|
|
38
|
-
#requestOptions: ClientOptions;
|
|
39
|
-
|
|
10
|
+
export declare class CollectionBuilder<T = unknown> {
|
|
11
|
+
#private;
|
|
40
12
|
/**
|
|
41
13
|
* Creates an instance of CollectionBuilder.
|
|
42
14
|
* @param {ClientOptions} requestOptions Options for the client request.
|
|
@@ -44,15 +16,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
44
16
|
* @param {string} contentType The content type to fetch.
|
|
45
17
|
* @memberof CollectionBuilder
|
|
46
18
|
*/
|
|
47
|
-
constructor(requestOptions: ClientOptions, serverUrl: string, contentType: string)
|
|
48
|
-
this.#requestOptions = requestOptions;
|
|
49
|
-
this.#serverUrl = serverUrl;
|
|
50
|
-
this.#contentType = contentType;
|
|
51
|
-
|
|
52
|
-
// Build the default query with the contentType field
|
|
53
|
-
this.#defaultQuery = new QueryBuilder().field('contentType').equals(this.#contentType);
|
|
54
|
-
}
|
|
55
|
-
|
|
19
|
+
constructor(requestOptions: ClientOptions, serverUrl: string, contentType: string);
|
|
56
20
|
/**
|
|
57
21
|
* Returns the sort query in the format: field order, field order, ...
|
|
58
22
|
*
|
|
@@ -60,10 +24,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
60
24
|
* @private
|
|
61
25
|
* @memberof CollectionBuilder
|
|
62
26
|
*/
|
|
63
|
-
private get sort()
|
|
64
|
-
return this.#sortBy?.map((sort) => `${sort.field} ${sort.order}`).join(',');
|
|
65
|
-
}
|
|
66
|
-
|
|
27
|
+
private get sort();
|
|
67
28
|
/**
|
|
68
29
|
* Returns the offset for pagination.
|
|
69
30
|
*
|
|
@@ -71,10 +32,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
71
32
|
* @private
|
|
72
33
|
* @memberof CollectionBuilder
|
|
73
34
|
*/
|
|
74
|
-
private get offset()
|
|
75
|
-
return this.#limit * (this.#page - 1);
|
|
76
|
-
}
|
|
77
|
-
|
|
35
|
+
private get offset();
|
|
78
36
|
/**
|
|
79
37
|
* Returns the full URL for the content API.
|
|
80
38
|
*
|
|
@@ -82,10 +40,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
82
40
|
* @private
|
|
83
41
|
* @memberof CollectionBuilder
|
|
84
42
|
*/
|
|
85
|
-
private get url()
|
|
86
|
-
return `${this.#serverUrl}${CONTENT_API_URL}`;
|
|
87
|
-
}
|
|
88
|
-
|
|
43
|
+
private get url();
|
|
89
44
|
/**
|
|
90
45
|
* Returns the current query built.
|
|
91
46
|
*
|
|
@@ -93,10 +48,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
93
48
|
* @private
|
|
94
49
|
* @memberof CollectionBuilder
|
|
95
50
|
*/
|
|
96
|
-
private get currentQuery()
|
|
97
|
-
return this.#query ?? this.#defaultQuery;
|
|
98
|
-
}
|
|
99
|
-
|
|
51
|
+
private get currentQuery();
|
|
100
52
|
/**
|
|
101
53
|
* Filters the content by the specified language ID.
|
|
102
54
|
*
|
|
@@ -111,12 +63,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
111
63
|
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
112
64
|
* @memberof CollectionBuilder
|
|
113
65
|
*/
|
|
114
|
-
language(languageId: number | string): this
|
|
115
|
-
this.#languageId = languageId;
|
|
116
|
-
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
|
|
66
|
+
language(languageId: number | string): this;
|
|
120
67
|
/**
|
|
121
68
|
* Setting this to true will server side render (using velocity) any widgets that are returned by the content query.
|
|
122
69
|
*
|
|
@@ -125,12 +72,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
125
72
|
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
126
73
|
* @memberof CollectionBuilder
|
|
127
74
|
*/
|
|
128
|
-
render(): this
|
|
129
|
-
this.#render = true;
|
|
130
|
-
|
|
131
|
-
return this;
|
|
132
|
-
}
|
|
133
|
-
|
|
75
|
+
render(): this;
|
|
134
76
|
/**
|
|
135
77
|
* Sorts the content by the specified fields and orders.
|
|
136
78
|
*
|
|
@@ -146,12 +88,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
146
88
|
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
147
89
|
* @memberof CollectionBuilder
|
|
148
90
|
*/
|
|
149
|
-
sortBy(sortBy: SortBy[]): this
|
|
150
|
-
this.#sortBy = sortBy;
|
|
151
|
-
|
|
152
|
-
return this;
|
|
153
|
-
}
|
|
154
|
-
|
|
91
|
+
sortBy(sortBy: SortBy[]): this;
|
|
155
92
|
/**
|
|
156
93
|
* Sets the maximum amount of content to fetch.
|
|
157
94
|
*
|
|
@@ -159,12 +96,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
159
96
|
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
160
97
|
* @memberof CollectionBuilder
|
|
161
98
|
*/
|
|
162
|
-
limit(limit: number): this
|
|
163
|
-
this.#limit = limit;
|
|
164
|
-
|
|
165
|
-
return this;
|
|
166
|
-
}
|
|
167
|
-
|
|
99
|
+
limit(limit: number): this;
|
|
168
100
|
/**
|
|
169
101
|
* Sets the page number to fetch.
|
|
170
102
|
*
|
|
@@ -172,12 +104,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
172
104
|
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
173
105
|
* @memberof CollectionBuilder
|
|
174
106
|
*/
|
|
175
|
-
page(page: number): this
|
|
176
|
-
this.#page = page;
|
|
177
|
-
|
|
178
|
-
return this;
|
|
179
|
-
}
|
|
180
|
-
|
|
107
|
+
page(page: number): this;
|
|
181
108
|
/**
|
|
182
109
|
* Filters the content by a Lucene query string.
|
|
183
110
|
*
|
|
@@ -186,7 +113,6 @@ export class CollectionBuilder<T = unknown> {
|
|
|
186
113
|
* @memberof CollectionBuilder
|
|
187
114
|
*/
|
|
188
115
|
query(query: string): this;
|
|
189
|
-
|
|
190
116
|
/**
|
|
191
117
|
* Filters the content by building a query using a QueryBuilder function.
|
|
192
118
|
*
|
|
@@ -204,33 +130,6 @@ export class CollectionBuilder<T = unknown> {
|
|
|
204
130
|
* @memberof CollectionBuilder
|
|
205
131
|
*/
|
|
206
132
|
query(buildQuery: BuildQuery): this;
|
|
207
|
-
query(arg: unknown): this {
|
|
208
|
-
if (typeof arg === 'string') {
|
|
209
|
-
this.#rawQuery = arg;
|
|
210
|
-
|
|
211
|
-
return this;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (typeof arg !== 'function') {
|
|
215
|
-
throw new Error(
|
|
216
|
-
`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.`
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const builtQuery = arg(new QueryBuilder());
|
|
221
|
-
|
|
222
|
-
// This can be use in Javascript so we cannot rely on the type checking
|
|
223
|
-
if (builtQuery instanceof Equals) {
|
|
224
|
-
this.#query = builtQuery.raw(this.currentQuery.build());
|
|
225
|
-
} else {
|
|
226
|
-
throw new Error(
|
|
227
|
-
'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.'
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return this;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
133
|
/**
|
|
235
134
|
* Retrieves draft content.
|
|
236
135
|
* @example
|
|
@@ -246,12 +145,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
246
145
|
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
247
146
|
* @memberof CollectionBuilder
|
|
248
147
|
*/
|
|
249
|
-
draft(): this
|
|
250
|
-
this.#draft = true;
|
|
251
|
-
|
|
252
|
-
return this;
|
|
253
|
-
}
|
|
254
|
-
|
|
148
|
+
draft(): this;
|
|
255
149
|
/**
|
|
256
150
|
* Filters the content by a variant ID for [Experiments](https://www.dotcms.com/docs/latest/experiments-and-a-b-testing)
|
|
257
151
|
*
|
|
@@ -271,12 +165,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
271
165
|
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
272
166
|
* @memberof CollectionBuilder
|
|
273
167
|
*/
|
|
274
|
-
variant(variantId: string): this
|
|
275
|
-
this.#query = this.currentQuery.field('variant').equals(variantId);
|
|
276
|
-
|
|
277
|
-
return this;
|
|
278
|
-
}
|
|
279
|
-
|
|
168
|
+
variant(variantId: string): this;
|
|
280
169
|
/**
|
|
281
170
|
* Sets the depth of the relationships of the content.
|
|
282
171
|
* Specifies the depth of related content to return in the results.
|
|
@@ -297,16 +186,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
297
186
|
* @return {CollectionBuilder} A CollectionBuilder instance.
|
|
298
187
|
* @memberof CollectionBuilder
|
|
299
188
|
*/
|
|
300
|
-
depth(depth: number): this
|
|
301
|
-
if (depth < 0 || depth > 3) {
|
|
302
|
-
throw new Error('Depth value must be between 0 and 3');
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
this.#depth = depth;
|
|
306
|
-
|
|
307
|
-
return this;
|
|
308
|
-
}
|
|
309
|
-
|
|
189
|
+
depth(depth: number): this;
|
|
310
190
|
/**
|
|
311
191
|
* Executes the fetch and returns a promise that resolves to the content or rejects with an error.
|
|
312
192
|
*
|
|
@@ -325,30 +205,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
325
205
|
* @return {Promise<GetCollectionResponse<T> | GetCollectionError>} A promise that resolves to the content or rejects with an error.
|
|
326
206
|
* @memberof CollectionBuilder
|
|
327
207
|
*/
|
|
328
|
-
then(
|
|
329
|
-
onfulfilled?: OnFullfilled<T>,
|
|
330
|
-
onrejected?: OnRejected
|
|
331
|
-
): Promise<GetCollectionResponse<T> | GetCollectionError> {
|
|
332
|
-
return this.fetch().then(async (response) => {
|
|
333
|
-
const data = await response.json();
|
|
334
|
-
if (response.ok) {
|
|
335
|
-
const formattedResponse = this.formatResponse<T>(data);
|
|
336
|
-
|
|
337
|
-
const finalResponse =
|
|
338
|
-
typeof onfulfilled === 'function'
|
|
339
|
-
? onfulfilled(formattedResponse)
|
|
340
|
-
: formattedResponse;
|
|
341
|
-
|
|
342
|
-
return finalResponse;
|
|
343
|
-
} else {
|
|
344
|
-
return {
|
|
345
|
-
status: response.status,
|
|
346
|
-
...data
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
}, onrejected);
|
|
350
|
-
}
|
|
351
|
-
|
|
208
|
+
then(onfulfilled?: OnFullfilled<T>, onrejected?: OnRejected): Promise<GetCollectionResponse<T> | GetCollectionError>;
|
|
352
209
|
/**
|
|
353
210
|
* Formats the response to the desired format.
|
|
354
211
|
*
|
|
@@ -357,25 +214,7 @@ export class CollectionBuilder<T = unknown> {
|
|
|
357
214
|
* @return {GetCollectionResponse<T>} The formatted response.
|
|
358
215
|
* @memberof CollectionBuilder
|
|
359
216
|
*/
|
|
360
|
-
private formatResponse
|
|
361
|
-
const contentlets = data.entity.jsonObjectView.contentlets;
|
|
362
|
-
const total = data.entity.resultsSize;
|
|
363
|
-
|
|
364
|
-
const mappedResponse: GetCollectionResponse<T> = {
|
|
365
|
-
contentlets,
|
|
366
|
-
total,
|
|
367
|
-
page: this.#page,
|
|
368
|
-
size: contentlets.length
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
return this.#sortBy
|
|
372
|
-
? {
|
|
373
|
-
...mappedResponse,
|
|
374
|
-
sortedBy: this.#sortBy
|
|
375
|
-
}
|
|
376
|
-
: mappedResponse;
|
|
377
|
-
}
|
|
378
|
-
|
|
217
|
+
private formatResponse;
|
|
379
218
|
/**
|
|
380
219
|
* Calls the content API to fetch the content.
|
|
381
220
|
*
|
|
@@ -383,35 +222,5 @@ export class CollectionBuilder<T = unknown> {
|
|
|
383
222
|
* @return {Promise<Response>} The fetch response.
|
|
384
223
|
* @memberof CollectionBuilder
|
|
385
224
|
*/
|
|
386
|
-
private fetch
|
|
387
|
-
const finalQuery = this.currentQuery
|
|
388
|
-
.field('languageId')
|
|
389
|
-
.equals(this.#languageId.toString())
|
|
390
|
-
.field('live')
|
|
391
|
-
.equals((!this.#draft).toString())
|
|
392
|
-
.build();
|
|
393
|
-
|
|
394
|
-
const sanitizedQuery = sanitizeQueryForContentType(finalQuery, this.#contentType);
|
|
395
|
-
|
|
396
|
-
const query = this.#rawQuery ? `${sanitizedQuery} ${this.#rawQuery}` : sanitizedQuery;
|
|
397
|
-
|
|
398
|
-
return fetch(this.url, {
|
|
399
|
-
...this.#requestOptions,
|
|
400
|
-
method: 'POST',
|
|
401
|
-
headers: {
|
|
402
|
-
...this.#requestOptions.headers,
|
|
403
|
-
'Content-Type': 'application/json'
|
|
404
|
-
},
|
|
405
|
-
body: JSON.stringify({
|
|
406
|
-
query,
|
|
407
|
-
render: this.#render,
|
|
408
|
-
sort: this.sort,
|
|
409
|
-
limit: this.#limit,
|
|
410
|
-
offset: this.offset,
|
|
411
|
-
depth: this.#depth
|
|
412
|
-
//userId: This exist but we currently don't use it
|
|
413
|
-
//allCategoriesInfo: This exist but we currently don't use it
|
|
414
|
-
})
|
|
415
|
-
});
|
|
416
|
-
}
|
|
225
|
+
private fetch;
|
|
417
226
|
}
|