@dotcms/client 0.0.1-alpha.38 → 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.
Files changed (53) hide show
  1. package/.eslintrc.json +18 -0
  2. package/jest.config.ts +15 -0
  3. package/package.json +3 -15
  4. package/project.json +72 -0
  5. package/src/index.ts +30 -0
  6. package/src/lib/client/content/builders/collection/collection.spec.ts +515 -0
  7. package/src/lib/client/content/builders/collection/{collection.d.ts → collection.ts} +209 -19
  8. package/src/lib/client/content/{content-api.d.ts → content-api.ts} +14 -4
  9. package/src/lib/client/content/shared/{const.d.ts → const.ts} +5 -3
  10. package/src/lib/client/content/shared/{types.d.ts → types.ts} +19 -2
  11. package/src/lib/client/content/shared/{utils.d.ts → utils.ts} +9 -1
  12. package/src/lib/client/models/{index.d.ts → index.ts} +8 -1
  13. package/src/lib/client/models/{types.d.ts → types.ts} +1 -0
  14. package/src/lib/client/sdk-js-client.spec.ts +483 -0
  15. package/src/lib/client/{sdk-js-client.d.ts → sdk-js-client.ts} +181 -15
  16. package/src/lib/editor/listeners/listeners.spec.ts +119 -0
  17. package/src/lib/editor/listeners/listeners.ts +223 -0
  18. package/src/lib/editor/models/{client.model.d.ts → client.model.ts} +19 -16
  19. package/src/lib/editor/models/{editor.model.d.ts → editor.model.ts} +9 -5
  20. package/src/lib/editor/models/{listeners.model.d.ts → listeners.model.ts} +9 -6
  21. package/src/lib/editor/sdk-editor-vtl.ts +31 -0
  22. package/src/lib/editor/sdk-editor.spec.ts +116 -0
  23. package/src/lib/editor/sdk-editor.ts +105 -0
  24. package/src/lib/editor/utils/editor.utils.spec.ts +206 -0
  25. package/src/lib/editor/utils/{editor.utils.d.ts → editor.utils.ts} +121 -22
  26. package/src/lib/query-builder/lucene-syntax/{Equals.d.ts → Equals.ts} +45 -11
  27. package/src/lib/query-builder/lucene-syntax/{Field.d.ts → Field.ts} +13 -5
  28. package/src/lib/query-builder/lucene-syntax/{NotOperand.d.ts → NotOperand.ts} +13 -5
  29. package/src/lib/query-builder/lucene-syntax/{Operand.d.ts → Operand.ts} +21 -7
  30. package/src/lib/query-builder/sdk-query-builder.spec.ts +159 -0
  31. package/src/lib/query-builder/{sdk-query-builder.d.ts → sdk-query-builder.ts} +16 -5
  32. package/src/lib/query-builder/utils/{index.d.ts → index.ts} +49 -12
  33. package/src/lib/utils/graphql/transforms.spec.ts +150 -0
  34. package/src/lib/utils/graphql/transforms.ts +99 -0
  35. package/src/lib/utils/page/common-utils.spec.ts +37 -0
  36. package/src/lib/utils/page/common-utils.ts +64 -0
  37. package/tsconfig.json +22 -0
  38. package/tsconfig.lib.json +13 -0
  39. package/tsconfig.spec.json +9 -0
  40. package/index.cjs.d.ts +0 -1
  41. package/index.cjs.default.js +0 -1
  42. package/index.cjs.js +0 -1953
  43. package/index.cjs.mjs +0 -2
  44. package/index.esm.d.ts +0 -1
  45. package/index.esm.js +0 -1944
  46. package/src/index.d.ts +0 -6
  47. package/src/lib/editor/listeners/listeners.d.ts +0 -50
  48. package/src/lib/editor/sdk-editor-vtl.d.ts +0 -6
  49. package/src/lib/editor/sdk-editor.d.ts +0 -54
  50. package/src/lib/utils/graphql/transforms.d.ts +0 -24
  51. package/src/lib/utils/page/common-utils.d.ts +0 -33
  52. /package/src/lib/query-builder/lucene-syntax/{index.d.ts → index.ts} +0 -0
  53. /package/src/lib/utils/{index.d.ts → index.ts} +0 -0
@@ -2,17 +2,19 @@ import { Equals } from '../lucene-syntax/Equals';
2
2
  import { Field } from '../lucene-syntax/Field';
3
3
  import { NotOperand } from '../lucene-syntax/NotOperand';
4
4
  import { Operand } from '../lucene-syntax/Operand';
5
+
5
6
  /**
6
7
  * Enum for common Operands
7
8
  *
8
9
  * @export
9
10
  * @enum {number}
10
11
  */
11
- export declare enum OPERAND {
12
- OR = "OR",
13
- AND = "AND",
14
- NOT = "NOT"
12
+ export enum OPERAND {
13
+ OR = 'OR',
14
+ AND = 'AND',
15
+ NOT = 'NOT'
15
16
  }
17
+
16
18
  /**
17
19
  * This function removes extra spaces from a string.
18
20
  *
@@ -25,7 +27,10 @@ export declare enum OPERAND {
25
27
  * @param {string} str
26
28
  * @return {*} {string}
27
29
  */
28
- export declare function sanitizeQuery(str: string): string;
30
+ export function sanitizeQuery(str: string): string {
31
+ return str.replace(/\s{2,}/g, ' ').trim();
32
+ }
33
+
29
34
  /**
30
35
  * This function sanitizes a term by adding quotes if it contains spaces.
31
36
  * In lucene, a term with spaces should be enclosed in quotes.
@@ -40,7 +45,10 @@ export declare function sanitizeQuery(str: string): string;
40
45
  * @param {string} term
41
46
  * @return {*} {string}
42
47
  */
43
- export declare function sanitizePhrases(term: string): string;
48
+ export function sanitizePhrases(term: string): string {
49
+ return term.includes(' ') ? `"${term}"` : term;
50
+ }
51
+
44
52
  /**
45
53
  * This function builds a term to be used in a lucene query.
46
54
  * We need to sanitize the term before adding it to the query.
@@ -55,7 +63,12 @@ export declare function sanitizePhrases(term: string): string;
55
63
  * @param {string} term
56
64
  * @return {*} {Equals}
57
65
  */
58
- export declare function buildEquals(query: string, term: string): Equals;
66
+ export function buildEquals(query: string, term: string): Equals {
67
+ const newQuery = query + sanitizePhrases(term);
68
+
69
+ return new Equals(newQuery);
70
+ }
71
+
59
72
  /**
60
73
  * This function builds a term to be used in a lucene query.
61
74
  * We need to sanitize the raw query before adding it to the query.
@@ -71,7 +84,12 @@ export declare function buildEquals(query: string, term: string): Equals;
71
84
  * @param {string} raw
72
85
  * @return {*} {Equals}
73
86
  */
74
- export declare function buildRawEquals(query: string, raw: string): Equals;
87
+ export function buildRawEquals(query: string, raw: string): Equals {
88
+ const newQuery = query + ` ${raw}`;
89
+
90
+ return new Equals(sanitizeQuery(newQuery));
91
+ }
92
+
75
93
  /**
76
94
  * This function builds a field to be used in a lucene query.
77
95
  * We need to format the field before adding it to the query.
@@ -86,7 +104,12 @@ export declare function buildRawEquals(query: string, raw: string): Equals;
86
104
  * @param {string} field
87
105
  * @return {*} {Field}
88
106
  */
89
- export declare function buildField(query: string, field: string): Field;
107
+ export function buildField(query: string, field: string): Field {
108
+ const newQuery = query + ` +${field}:`;
109
+
110
+ return new Field(newQuery);
111
+ }
112
+
90
113
  /**
91
114
  * This function builds an exclude field to be used in a lucene query.
92
115
  * We need to format the field before adding it to the query.
@@ -102,7 +125,12 @@ export declare function buildField(query: string, field: string): Field;
102
125
  * @param {string} field
103
126
  * @return {*} {Field}
104
127
  */
105
- export declare function buildExcludeField(query: string, field: string): Field;
128
+ export function buildExcludeField(query: string, field: string): Field {
129
+ const newQuery = query + ` -${field}:`;
130
+
131
+ return new Field(newQuery);
132
+ }
133
+
106
134
  /**
107
135
  * This function builds an operand to be used in a lucene query.
108
136
  * We need to format the operand before adding it to the query.
@@ -124,7 +152,12 @@ export declare function buildExcludeField(query: string, field: string): Field;
124
152
  * @param {OPERAND} operand
125
153
  * @return {*} {Operand}
126
154
  */
127
- export declare function buildOperand(query: string, operand: OPERAND): Operand;
155
+ export function buildOperand(query: string, operand: OPERAND): Operand {
156
+ const newQuery = query + ` ${operand} `;
157
+
158
+ return new Operand(newQuery);
159
+ }
160
+
128
161
  /**
129
162
  * This function builds a NOT operand to be used in a lucene query.
130
163
  * We need to format the operand before adding it to the query.
@@ -139,4 +172,8 @@ export declare function buildOperand(query: string, operand: OPERAND): Operand;
139
172
  * @param {string} query
140
173
  * @return {*} {NotOperand}
141
174
  */
142
- export declare function buildNotOperand(query: string): NotOperand;
175
+ export function buildNotOperand(query: string): NotOperand {
176
+ const newQuery = query + ` ${OPERAND.NOT} `;
177
+
178
+ return new NotOperand(newQuery);
179
+ }
@@ -0,0 +1,150 @@
1
+ import { graphqlToPageEntity } from './transforms';
2
+
3
+ const GRAPHQL_RESPONSE_MOCK = {
4
+ page: {
5
+ title: 'test2',
6
+ url: '/test2',
7
+ seodescription: null,
8
+ containers: [
9
+ {
10
+ path: '//demo.dotcms.com/application/containers/default/',
11
+ identifier: '69b3d24d-7e80-4be6-b04a-d352d16493ee',
12
+ containerStructures: [
13
+ {
14
+ id: '77ec7720-bad8-431c-a0a3-6443fe87af73'
15
+ },
16
+ {
17
+ id: '1e5372c4-9fff-4793-ad8a-a52558d0d395'
18
+ },
19
+ {
20
+ id: '461b6fad-04a8-49ba-b75b-6634799bc289'
21
+ },
22
+ {
23
+ id: '57b4a9de-4765-4c57-8a05-2a0a317ec546'
24
+ },
25
+ {
26
+ id: '342a83bf-040a-45a2-a4bc-defb0f1bf4eb'
27
+ },
28
+ {
29
+ id: '91a955a0-5c97-4fc3-91dd-f894e2f3dc54'
30
+ },
31
+ {
32
+ id: '71ded7c9deb5b05452824bb42b9aa9d5'
33
+ },
34
+ {
35
+ id: '86a06a2e-ba40-43d4-a3ad-3a870d43bfc1'
36
+ },
37
+ {
38
+ id: '7673e601-26d4-4dbc-b6ad-7028599df656'
39
+ },
40
+ {
41
+ id: '313498a4-5b57-4d9e-8d32-ef2d30f9e867'
42
+ }
43
+ ],
44
+ containerContentlets: [
45
+ {
46
+ uuid: 'uuid-1562770692396',
47
+ contentlets: [
48
+ {
49
+ identifier: 'd9444fa005a10d555318d8b014e474d4'
50
+ }
51
+ ]
52
+ }
53
+ ]
54
+ }
55
+ ],
56
+ host: {
57
+ hostName: 'demo.dotcms.com'
58
+ },
59
+ layout: {
60
+ title: 'default-template'
61
+ },
62
+ template: {
63
+ identifier: '31f4c794-c769-4929-9d5d-7c383408c65c'
64
+ },
65
+ urlContentMap: {
66
+ identifier: '31f4c794-c769-4929-9d5d-7c383408c74d'
67
+ },
68
+ viewAs: {
69
+ mode: 'LIVE'
70
+ }
71
+ }
72
+ };
73
+
74
+ const MOCK_PAGE_ENTITY = {
75
+ containers: {
76
+ '//demo.dotcms.com/application/containers/default/': {
77
+ container: {
78
+ identifier: '69b3d24d-7e80-4be6-b04a-d352d16493ee',
79
+ path: '//demo.dotcms.com/application/containers/default/'
80
+ },
81
+ containerStructures: [
82
+ {
83
+ id: '77ec7720-bad8-431c-a0a3-6443fe87af73'
84
+ },
85
+ {
86
+ id: '1e5372c4-9fff-4793-ad8a-a52558d0d395'
87
+ },
88
+ {
89
+ id: '461b6fad-04a8-49ba-b75b-6634799bc289'
90
+ },
91
+ {
92
+ id: '57b4a9de-4765-4c57-8a05-2a0a317ec546'
93
+ },
94
+ {
95
+ id: '342a83bf-040a-45a2-a4bc-defb0f1bf4eb'
96
+ },
97
+ {
98
+ id: '91a955a0-5c97-4fc3-91dd-f894e2f3dc54'
99
+ },
100
+ {
101
+ id: '71ded7c9deb5b05452824bb42b9aa9d5'
102
+ },
103
+ {
104
+ id: '86a06a2e-ba40-43d4-a3ad-3a870d43bfc1'
105
+ },
106
+ {
107
+ id: '7673e601-26d4-4dbc-b6ad-7028599df656'
108
+ },
109
+ {
110
+ id: '313498a4-5b57-4d9e-8d32-ef2d30f9e867'
111
+ }
112
+ ],
113
+ contentlets: {
114
+ 'uuid-1562770692396': [
115
+ {
116
+ identifier: 'd9444fa005a10d555318d8b014e474d4'
117
+ }
118
+ ]
119
+ }
120
+ }
121
+ },
122
+ layout: {
123
+ title: 'default-template'
124
+ },
125
+ page: {
126
+ host: {
127
+ hostName: 'demo.dotcms.com'
128
+ },
129
+ seodescription: null,
130
+ title: 'test2',
131
+ url: '/test2'
132
+ },
133
+ urlContentMap: {
134
+ identifier: '31f4c794-c769-4929-9d5d-7c383408c74d'
135
+ },
136
+ template: {
137
+ identifier: '31f4c794-c769-4929-9d5d-7c383408c65c'
138
+ },
139
+ viewAs: {
140
+ mode: 'LIVE'
141
+ }
142
+ };
143
+
144
+ describe('GraphQL Parser', () => {
145
+ it('should return the correct page entity', () => {
146
+ const pageEntity = graphqlToPageEntity(GRAPHQL_RESPONSE_MOCK);
147
+
148
+ expect(pageEntity).toEqual(MOCK_PAGE_ENTITY);
149
+ });
150
+ });
@@ -0,0 +1,99 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /**
3
+ * Represents the response from a GraphQL query for a page.
4
+ *
5
+ * @interface GraphQLPageResponse
6
+ * @property {Record<string, unknown>} page - The main page data.
7
+ * @property {unknown} [key: string] - Additional properties that may be included in the response.
8
+ */
9
+ interface GraphQLPageResponse {
10
+ page: Record<string, unknown>;
11
+ [key: string]: unknown;
12
+ }
13
+
14
+ /**
15
+ * Transforms a GraphQL Page response to a Page Entity.
16
+ *
17
+ * @param {GraphQLPageResponse} graphQLPageResponse - The GraphQL Page response object.
18
+ * @returns {object|null} The transformed Page Entity or null if the page is not present.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const pageEntity = graphqlToPageEntity(graphQLPageResponse);
23
+ * ```
24
+ */
25
+ export const graphqlToPageEntity = (graphQLPageResponse: GraphQLPageResponse) => {
26
+ const { page } = graphQLPageResponse;
27
+
28
+ // If there is no page, return null
29
+ if (!page) {
30
+ return null;
31
+ }
32
+
33
+ const { layout, template, containers, urlContentMap, viewAs, site, _map, ...pageAsset } = page;
34
+ const data = (_map || {}) as Record<string, unknown>;
35
+
36
+ return {
37
+ layout,
38
+ template,
39
+ viewAs,
40
+ urlContentMap,
41
+ site,
42
+ page: {
43
+ ...data,
44
+ ...pageAsset
45
+ },
46
+ containers: parseContainers(containers as [])
47
+ } as any;
48
+ };
49
+
50
+ /**
51
+ * Parses the containers from the GraphQL response.
52
+ *
53
+ * @param {Array<Record<string, unknown>>} [containers=[]] - The containers array from the GraphQL response.
54
+ * @returns {Record<string, unknown>} The parsed containers.
55
+ */
56
+ const parseContainers = (containers: Record<string, unknown>[] = []) => {
57
+ return containers.reduce((acc: Record<string, unknown>, container: Record<string, unknown>) => {
58
+ const { path, identifier, containerStructures, containerContentlets, ...rest } = container;
59
+
60
+ const key = (path || identifier) as string;
61
+
62
+ acc[key] = {
63
+ containerStructures,
64
+ container: {
65
+ path,
66
+ identifier,
67
+ ...rest
68
+ },
69
+ contentlets: parseContentletsToUuidMap(containerContentlets as [])
70
+ };
71
+
72
+ return acc;
73
+ }, {});
74
+ };
75
+
76
+ /**
77
+ * Parses the contentlets from the GraphQL response.
78
+ *
79
+ * @param {Array<Record<string, unknown>>} containerContentlets - The contentlets array from the GraphQL response.
80
+ * @returns {Record<string, Array<Record<string, unknown>>>} The parsed contentlets mapped by UUID.
81
+ */
82
+ const parseContentletsToUuidMap = (containerContentlets: Record<string, unknown>[] = []) => {
83
+ return containerContentlets.reduce((acc, containerContentlet) => {
84
+ const { uuid, contentlets } = containerContentlet as {
85
+ uuid: string;
86
+ contentlets: Record<string, unknown>[];
87
+ };
88
+
89
+ // TODO: This is a temporary solution, we need to find a better way to handle this.
90
+ acc[uuid] = contentlets.map(({ _map = {}, ...rest }) => {
91
+ return {
92
+ ...(_map as Record<string, unknown>),
93
+ ...rest
94
+ };
95
+ });
96
+
97
+ return acc;
98
+ }, {});
99
+ };
@@ -0,0 +1,37 @@
1
+ import { getPageRequestParams } from './common-utils';
2
+
3
+ describe('Common Utils', () => {
4
+ it('should return the correct Request Params', () => {
5
+ const pageRequestParams = getPageRequestParams({
6
+ path: 'test',
7
+ params: {
8
+ personaId: '123',
9
+ language_id: '1',
10
+ mode: 'LIVE',
11
+ variantName: 'default'
12
+ }
13
+ });
14
+
15
+ expect(pageRequestParams).toEqual({
16
+ path: 'test',
17
+ mode: 'LIVE',
18
+ language_id: '1',
19
+ variantName: 'default',
20
+ personaId: '123'
21
+ });
22
+ });
23
+
24
+ it('should return the correct Request Params with empty params', () => {
25
+ const pageRequestParams = getPageRequestParams({
26
+ path: 'test',
27
+ params: {
28
+ personaId: '',
29
+ language_id: '',
30
+ mode: '',
31
+ variantName: ''
32
+ }
33
+ });
34
+
35
+ expect(pageRequestParams).toEqual({ path: 'test' });
36
+ });
37
+ });
@@ -0,0 +1,64 @@
1
+ import { PageApiOptions } from '../../client/sdk-js-client';
2
+
3
+ /**
4
+ * Interface representing the properties for page request parameters.
5
+ *
6
+ * @export
7
+ * @interface PageRequestParamsProps
8
+ */
9
+ export interface PageRequestParamsProps {
10
+ /**
11
+ * The API endpoint path.
12
+ * @type {string}
13
+ */
14
+ path: string;
15
+
16
+ /**
17
+ * The query parameters for the API request.
18
+ * Can be an object with key-value pairs or a URLSearchParams instance.
19
+ * @type {{ [key: string]: unknown } | URLSearchParams}
20
+ */
21
+ params: { [key: string]: unknown } | URLSearchParams;
22
+ }
23
+
24
+ /**
25
+ * Generates the page request parameters to be used in the API call.
26
+ *
27
+ * @param {PageRequestParamsProps} PageRequestParamsProps - The properties for the page request.
28
+ * @returns {PageApiOptions} The options for the page API.
29
+ * @example
30
+ * ```ts
31
+ * const pageApiOptions = getPageRequestParams({ path: '/api/v1/page', params: queryParams });
32
+ * ```
33
+ */
34
+ export const getPageRequestParams = ({
35
+ path = '',
36
+ params = {}
37
+ }: PageRequestParamsProps): PageApiOptions => {
38
+ const copiedParams: PageRequestParamsProps['params'] =
39
+ params instanceof URLSearchParams ? Object.fromEntries(params.entries()) : { ...params };
40
+
41
+ const finalParams: Record<string, unknown> = {};
42
+ const dotMarketingPersonaId = copiedParams['com.dotmarketing.persona.id'] || '';
43
+
44
+ if (copiedParams['mode']) {
45
+ finalParams['mode'] = copiedParams['mode'];
46
+ }
47
+
48
+ if (copiedParams['language_id']) {
49
+ finalParams['language_id'] = copiedParams['language_id'];
50
+ }
51
+
52
+ if (copiedParams['variantName']) {
53
+ finalParams['variantName'] = copiedParams['variantName'];
54
+ }
55
+
56
+ if (copiedParams['personaId'] || dotMarketingPersonaId) {
57
+ finalParams['personaId'] = copiedParams['personaId'] || dotMarketingPersonaId;
58
+ }
59
+
60
+ return {
61
+ path,
62
+ ...finalParams
63
+ };
64
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "extends": "../../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "ESNext",
5
+ "forceConsistentCasingInFileNames": true,
6
+ "strict": true,
7
+ "noImplicitOverride": true,
8
+ "noPropertyAccessFromIndexSignature": true,
9
+ "noImplicitReturns": true,
10
+ "noFallthroughCasesInSwitch": true
11
+ },
12
+ "files": [],
13
+ "include": [],
14
+ "references": [
15
+ {
16
+ "path": "./tsconfig.lib.json"
17
+ },
18
+ {
19
+ "path": "./tsconfig.spec.json"
20
+ }
21
+ ]
22
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../../dist/out-tsc",
5
+ "declaration": true,
6
+ "types": [""],
7
+ "target": "es2020",
8
+ "module": "es2020",
9
+ "moduleResolution": "node"
10
+ },
11
+ "include": ["src/**/*.ts"],
12
+ "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
13
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../../dist/out-tsc",
5
+ "module": "commonjs",
6
+ "types": ["jest", "node"]
7
+ },
8
+ "include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
9
+ }
package/index.cjs.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/index";
@@ -1 +0,0 @@
1
- exports._default = require('./index.cjs.js').default;