@dotcms/client 1.0.0 → 1.0.1

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 (54) hide show
  1. package/.eslintrc.json +18 -0
  2. package/CLAUDE.md +253 -0
  3. package/MIGRATION.md +329 -0
  4. package/README.md +250 -434
  5. package/jest.config.ts +15 -0
  6. package/package.json +8 -25
  7. package/project.json +73 -0
  8. package/src/lib/client/client.spec.ts +147 -0
  9. package/src/lib/client/client.ts +125 -0
  10. package/src/lib/client/content/builders/collection/collection.spec.ts +514 -0
  11. package/src/lib/client/content/builders/collection/{collection.d.ts → collection.ts} +210 -19
  12. package/src/lib/client/content/builders/query/lucene-syntax/{Equals.d.ts → Equals.ts} +45 -11
  13. package/src/lib/client/content/builders/query/lucene-syntax/{Field.d.ts → Field.ts} +13 -5
  14. package/src/lib/client/content/builders/query/lucene-syntax/{NotOperand.d.ts → NotOperand.ts} +13 -5
  15. package/src/lib/client/content/builders/query/lucene-syntax/{Operand.d.ts → Operand.ts} +21 -7
  16. package/src/lib/client/content/builders/query/query.spec.ts +159 -0
  17. package/src/lib/client/content/builders/query/{query.d.ts → query.ts} +16 -5
  18. package/src/lib/client/content/builders/query/utils/{index.d.ts → index.ts} +49 -12
  19. package/src/lib/client/content/{content-api.d.ts → content-api.ts} +14 -4
  20. package/src/lib/client/content/shared/{const.d.ts → const.ts} +5 -3
  21. package/src/lib/client/content/shared/{types.d.ts → types.ts} +18 -2
  22. package/src/lib/client/content/shared/{utils.d.ts → utils.ts} +9 -1
  23. package/src/lib/client/models/{index.d.ts → index.ts} +8 -1
  24. package/src/lib/client/navigation/navigation-api.spec.ts +167 -0
  25. package/src/lib/client/navigation/navigation-api.ts +62 -0
  26. package/src/lib/client/page/page-api.spec.ts +359 -0
  27. package/src/lib/client/page/page-api.ts +197 -0
  28. package/src/lib/client/page/utils.ts +291 -0
  29. package/src/lib/utils/graphql/transforms.spec.ts +250 -0
  30. package/src/lib/utils/graphql/transforms.ts +128 -0
  31. package/tsconfig.json +22 -0
  32. package/tsconfig.lib.json +13 -0
  33. package/tsconfig.spec.json +9 -0
  34. package/index.cjs.d.ts +0 -1
  35. package/index.cjs.default.js +0 -1
  36. package/index.cjs.js +0 -1591
  37. package/index.cjs.mjs +0 -2
  38. package/index.esm.d.ts +0 -1
  39. package/index.esm.js +0 -1589
  40. package/internal.cjs.d.ts +0 -1
  41. package/internal.cjs.default.js +0 -1
  42. package/internal.cjs.js +0 -85
  43. package/internal.cjs.mjs +0 -2
  44. package/internal.esm.d.ts +0 -1
  45. package/internal.esm.js +0 -83
  46. package/src/lib/client/client.d.ts +0 -56
  47. package/src/lib/client/navigation/navigation-api.d.ts +0 -14
  48. package/src/lib/client/page/page-api.d.ts +0 -95
  49. package/src/lib/client/page/utils.d.ts +0 -41
  50. package/src/lib/utils/graphql/transforms.d.ts +0 -13
  51. /package/src/{index.d.ts → index.ts} +0 -0
  52. /package/src/{internal.d.ts → internal.ts} +0 -0
  53. /package/src/lib/client/content/builders/query/lucene-syntax/{index.d.ts → index.ts} +0 -0
  54. /package/src/lib/utils/{index.d.ts → index.ts} +0 -0
package/internal.cjs.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/internal";
@@ -1 +0,0 @@
1
- exports._default = require('./internal.cjs.js').default;
package/internal.cjs.js DELETED
@@ -1,85 +0,0 @@
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 DELETED
@@ -1,2 +0,0 @@
1
- export * from './internal.cjs.js';
2
- export { _default as default } from './internal.cjs.default.js';
package/internal.esm.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./src/internal";
package/internal.esm.js DELETED
@@ -1,83 +0,0 @@
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 };
@@ -1,56 +0,0 @@
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,14 +0,0 @@
1
- import { DotCMSClientConfig, DotCMSNavigationRequestParams, RequestOptions, DotCMSNavigationItem } from '@dotcms/types';
2
- export declare class NavigationClient {
3
- private requestOptions;
4
- private BASE_URL;
5
- constructor(config: DotCMSClientConfig, requestOptions: RequestOptions);
6
- /**
7
- * Retrieves information about the dotCMS file and folder tree.
8
- * @param {NavigationApiOptions} options - The options for the Navigation API call. Defaults to `{ depth: 0, path: '/', languageId: 1 }`.
9
- * @returns {Promise<DotCMSNavigationItem[]>} - A Promise that resolves to the response from the DotCMS API.
10
- * @throws {Error} - Throws an error if the options are not valid.
11
- */
12
- get(path: string, params?: DotCMSNavigationRequestParams): Promise<DotCMSNavigationItem[]>;
13
- private mapToBackendParams;
14
- }
@@ -1,95 +0,0 @@
1
- import { DotCMSClientConfig, DotCMSComposedPageResponse, DotCMSExtendedPageResponse, DotCMSPageResponse, DotCMSPageRequestParams, RequestOptions } from '@dotcms/types';
2
- /**
3
- * Client for interacting with the DotCMS Page API.
4
- * Provides methods to retrieve and manipulate pages.
5
- */
6
- export declare class PageClient {
7
- /**
8
- * Request options including authorization headers.
9
- * @private
10
- */
11
- private requestOptions;
12
- /**
13
- * Site ID for page requests.
14
- * @private
15
- */
16
- private siteId;
17
- /**
18
- * DotCMS URL for page requests.
19
- * @private
20
- */
21
- private dotcmsUrl;
22
- /**
23
- * Creates a new PageClient instance.
24
- *
25
- * @param {DotCMSClientConfig} config - Configuration options for the DotCMS client
26
- * @param {RequestOptions} requestOptions - Options for fetch requests including authorization headers
27
- * @example
28
- * ```typescript
29
- * const pageClient = new PageClient(
30
- * {
31
- * dotcmsUrl: 'https://demo.dotcms.com',
32
- * authToken: 'your-auth-token',
33
- * siteId: 'demo.dotcms.com'
34
- * },
35
- * {
36
- * headers: {
37
- * Authorization: 'Bearer your-auth-token'
38
- * }
39
- * }
40
- * );
41
- * ```
42
- */
43
- constructor(config: DotCMSClientConfig, requestOptions: RequestOptions);
44
- /**
45
- * Retrieves a page from DotCMS using GraphQL.
46
- *
47
- * @param {string} url - The URL of the page to retrieve
48
- * @param {DotCMSPageRequestParams} [options] - Options for the request
49
- * @template T - The type of the page and content, defaults to DotCMSBasicPage and Record<string, unknown> | unknown
50
- * @returns {Promise<DotCMSComposedPageResponse<T>>} A Promise that resolves to the page data
51
- *
52
- * @example Using GraphQL
53
- * ```typescript
54
- * const page = await pageClient.get<{ page: MyPageWithBanners; content: { blogPosts: { blogTitle: string } } }>(
55
- * '/index',
56
- * {
57
- * languageId: '1',
58
- * mode: 'LIVE',
59
- * graphql: {
60
- * page: `
61
- * containers {
62
- * containerContentlets {
63
- * contentlets {
64
- * ... on Banner {
65
- * ...bannerFragment
66
- * }
67
- * }
68
- * }
69
- * `,
70
- * content: {
71
- * blogPosts: `
72
- * BlogCollection(limit: 3) {
73
- * ...blogFragment
74
- * }
75
- * `,
76
- * },
77
- * fragments: [
78
- * `
79
- * fragment bannerFragment on Banner {
80
- * caption
81
- * }
82
- * `,
83
- * `
84
- * fragment blogFragment on Blog {
85
- * title
86
- * urlTitle
87
- * }
88
- * `
89
- * ]
90
- * }
91
- * });
92
- * ```
93
- */
94
- get<T extends DotCMSExtendedPageResponse = DotCMSPageResponse>(url: string, options?: DotCMSPageRequestParams): Promise<DotCMSComposedPageResponse<T>>;
95
- }
@@ -1,41 +0,0 @@
1
- /**
2
- * Builds a GraphQL query for retrieving page content from DotCMS.
3
- *
4
- * @param {string} pageQuery - Custom fragment fields to include in the ClientPage fragment
5
- * @param {string} additionalQueries - Additional GraphQL queries to include in the main query
6
- * @returns {string} Complete GraphQL query string for page content
7
- */
8
- export declare const buildPageQuery: ({ page, fragments, additionalQueries }: {
9
- page?: string;
10
- fragments?: string[];
11
- additionalQueries?: string;
12
- }) => string;
13
- /**
14
- * Converts a record of query strings into a single GraphQL query string.
15
- *
16
- * @param {Record<string, string>} queryData - Object containing named query strings
17
- * @returns {string} Combined query string or empty string if no queryData provided
18
- */
19
- export declare function buildQuery(queryData: Record<string, string>): string;
20
- /**
21
- * Filters response data to include only specified keys.
22
- *
23
- * @param {Record<string, string>} responseData - Original response data object
24
- * @param {string[]} keys - Array of keys to extract from the response data
25
- * @returns {Record<string, string>} New object containing only the specified keys
26
- */
27
- export declare function mapResponseData(responseData: Record<string, string>, keys: string[]): Record<string, string>;
28
- /**
29
- * Executes a GraphQL query against the DotCMS API.
30
- *
31
- * @param {Object} options - Options for the fetch request
32
- * @param {string} options.body - GraphQL query string
33
- * @param {Record<string, string>} options.headers - HTTP headers for the request
34
- * @returns {Promise<any>} Parsed JSON response from the GraphQL API
35
- * @throws {Error} If the HTTP response is not successful
36
- */
37
- export declare function fetchGraphQL({ baseURL, body, headers }: {
38
- baseURL: string;
39
- body: string;
40
- headers: Record<string, string>;
41
- }): Promise<any>;
@@ -1,13 +0,0 @@
1
- import { DotCMSGraphQLPageResponse, DotCMSPageAsset } from '@dotcms/types';
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
- export declare const graphqlToPageEntity: (graphQLPageResponse: DotCMSGraphQLPageResponse) => DotCMSPageAsset | null;
File without changes
File without changes
File without changes