@dotcms/client 1.0.1 → 1.0.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/dotcms-client-1.0.1.tgz +0 -0
- 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,95 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
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>;
|
|
@@ -0,0 +1,13 @@
|
|
|
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;
|
package/.eslintrc.json
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": ["../../../.eslintrc.base.json"],
|
|
3
|
-
"ignorePatterns": ["!**/*"],
|
|
4
|
-
"overrides": [
|
|
5
|
-
{
|
|
6
|
-
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
|
|
7
|
-
"rules": {}
|
|
8
|
-
},
|
|
9
|
-
{
|
|
10
|
-
"files": ["*.ts", "*.tsx"],
|
|
11
|
-
"rules": {}
|
|
12
|
-
},
|
|
13
|
-
{
|
|
14
|
-
"files": ["*.js", "*.jsx"],
|
|
15
|
-
"rules": {}
|
|
16
|
-
}
|
|
17
|
-
]
|
|
18
|
-
}
|
package/jest.config.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
export default {
|
|
3
|
-
displayName: 'sdk-client',
|
|
4
|
-
preset: '../../../jest.preset.js',
|
|
5
|
-
transform: {
|
|
6
|
-
'^.+\\.[tj]s$': [
|
|
7
|
-
'ts-jest',
|
|
8
|
-
{
|
|
9
|
-
tsconfig: '<rootDir>/tsconfig.spec.json'
|
|
10
|
-
}
|
|
11
|
-
]
|
|
12
|
-
},
|
|
13
|
-
moduleFileExtensions: ['ts', 'js', 'html'],
|
|
14
|
-
coverageDirectory: '../../../coverage/libs/sdk/client'
|
|
15
|
-
};
|
package/project.json
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "sdk-client",
|
|
3
|
-
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
-
"sourceRoot": "libs/sdk/client/src",
|
|
5
|
-
"projectType": "library",
|
|
6
|
-
"targets": {
|
|
7
|
-
"build": {
|
|
8
|
-
"executor": "@nx/rollup:rollup",
|
|
9
|
-
"outputs": ["{options.outputPath}"],
|
|
10
|
-
"options": {
|
|
11
|
-
"format": ["esm", "cjs"],
|
|
12
|
-
"compiler": "tsc",
|
|
13
|
-
"additionalEntryPoints": ["libs/sdk/client/src/internal.ts"],
|
|
14
|
-
"generateExportsField": true,
|
|
15
|
-
"outputPath": "dist/libs/sdk/client",
|
|
16
|
-
"assets": [{ "input": "libs/sdk/client", "output": ".", "glob": "*.md" }],
|
|
17
|
-
"main": "libs/sdk/client/src/index.ts",
|
|
18
|
-
"tsConfig": "libs/sdk/client/tsconfig.lib.json"
|
|
19
|
-
}
|
|
20
|
-
},
|
|
21
|
-
"build:js": {
|
|
22
|
-
"executor": "@nx/esbuild:esbuild",
|
|
23
|
-
"outputs": ["{options.outputPath}"],
|
|
24
|
-
"options": {
|
|
25
|
-
"outputPath": "../dotCMS/src/main/webapp/html/js/editor-js",
|
|
26
|
-
"outputFileName": "sdk-editor",
|
|
27
|
-
"format": ["esm"],
|
|
28
|
-
"tsConfig": "libs/sdk/client/tsconfig.lib.json",
|
|
29
|
-
"project": "libs/sdk/client/package.json",
|
|
30
|
-
"entryFile": "libs/sdk/client/src/lib/deprecated/editor/sdk-editor-vtl.ts",
|
|
31
|
-
"external": ["react/jsx-runtime"],
|
|
32
|
-
"rollupConfig": "@nrwl/react/plugins/bundle-rollup",
|
|
33
|
-
"compiler": "tsc",
|
|
34
|
-
"extractCss": false,
|
|
35
|
-
"minify": true
|
|
36
|
-
}
|
|
37
|
-
},
|
|
38
|
-
"publish": {
|
|
39
|
-
"executor": "nx:run-commands",
|
|
40
|
-
"options": {
|
|
41
|
-
"command": "node tools/scripts/publish.mjs sdk-client {args.ver} {args.tag}"
|
|
42
|
-
},
|
|
43
|
-
"dependsOn": ["build"]
|
|
44
|
-
},
|
|
45
|
-
"nx-release-publish": {
|
|
46
|
-
"options": {
|
|
47
|
-
"packageRoot": "dist/libs/sdk/client"
|
|
48
|
-
}
|
|
49
|
-
},
|
|
50
|
-
"lint": {
|
|
51
|
-
"executor": "@nx/eslint:lint",
|
|
52
|
-
"outputs": ["{options.outputFile}"],
|
|
53
|
-
"options": {
|
|
54
|
-
"lintFilePatterns": ["libs/sdk/client/**/*.ts"]
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
"test": {
|
|
58
|
-
"executor": "@nrwl/jest:jest",
|
|
59
|
-
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
|
60
|
-
"options": {
|
|
61
|
-
"jestConfig": "libs/sdk/client/jest.config.ts",
|
|
62
|
-
"passWithNoTests": true
|
|
63
|
-
},
|
|
64
|
-
"configurations": {
|
|
65
|
-
"ci": {
|
|
66
|
-
"ci": true,
|
|
67
|
-
"codeCoverage": true
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
"tags": []
|
|
73
|
-
}
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { DotCMSClientConfig, RequestOptions } from '@dotcms/types';
|
|
2
|
-
|
|
3
|
-
import { createDotCMSClient } from './client';
|
|
4
|
-
import { Content } from './content/content-api';
|
|
5
|
-
import { NavigationClient } from './navigation/navigation-api';
|
|
6
|
-
import { PageClient } from './page/page-api';
|
|
7
|
-
|
|
8
|
-
// Mock the dependencies
|
|
9
|
-
jest.mock('./content/content-api');
|
|
10
|
-
jest.mock('./navigation/navigation-api');
|
|
11
|
-
jest.mock('./page/page-api');
|
|
12
|
-
|
|
13
|
-
describe('DotCMSClient', () => {
|
|
14
|
-
const originalTypeError = global.TypeError;
|
|
15
|
-
const mockTypeError = jest.fn().mockImplementation((...args) => new originalTypeError(...args));
|
|
16
|
-
const validConfig: DotCMSClientConfig = {
|
|
17
|
-
dotcmsUrl: 'https://demo.dotcms.com',
|
|
18
|
-
authToken: 'test-token',
|
|
19
|
-
siteId: 'test-site',
|
|
20
|
-
requestOptions: {
|
|
21
|
-
headers: {
|
|
22
|
-
'Content-Type': 'application/json'
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
beforeEach(() => {
|
|
28
|
-
jest.clearAllMocks();
|
|
29
|
-
global.TypeError = mockTypeError as unknown as ErrorConstructor;
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
afterAll(() => {
|
|
33
|
-
global.TypeError = originalTypeError;
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should initialize sub-clients with correct parameters', () => {
|
|
37
|
-
createDotCMSClient(validConfig);
|
|
38
|
-
|
|
39
|
-
const expectedRequestOptions: RequestOptions = {
|
|
40
|
-
headers: {
|
|
41
|
-
'Content-Type': 'application/json',
|
|
42
|
-
Authorization: 'Bearer test-token'
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
expect(PageClient).toHaveBeenCalledWith(
|
|
47
|
-
expect.objectContaining({
|
|
48
|
-
dotcmsUrl: 'https://demo.dotcms.com',
|
|
49
|
-
authToken: 'test-token',
|
|
50
|
-
siteId: 'test-site'
|
|
51
|
-
}),
|
|
52
|
-
expectedRequestOptions
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
expect(Content).toHaveBeenCalledWith(expectedRequestOptions, 'https://demo.dotcms.com');
|
|
56
|
-
|
|
57
|
-
expect(NavigationClient).toHaveBeenCalledWith(
|
|
58
|
-
expect.objectContaining({
|
|
59
|
-
dotcmsUrl: 'https://demo.dotcms.com',
|
|
60
|
-
authToken: 'test-token',
|
|
61
|
-
siteId: 'test-site'
|
|
62
|
-
}),
|
|
63
|
-
expectedRequestOptions
|
|
64
|
-
);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should add authorization header to request options', () => {
|
|
68
|
-
const configWithoutHeaders: DotCMSClientConfig = {
|
|
69
|
-
dotcmsUrl: 'https://demo.dotcms.com',
|
|
70
|
-
authToken: 'test-token'
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
createDotCMSClient(configWithoutHeaders);
|
|
74
|
-
|
|
75
|
-
expect(PageClient).toHaveBeenCalledWith(
|
|
76
|
-
expect.anything(),
|
|
77
|
-
expect.objectContaining({
|
|
78
|
-
headers: {
|
|
79
|
-
Authorization: 'Bearer test-token'
|
|
80
|
-
}
|
|
81
|
-
})
|
|
82
|
-
);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('should preserve existing headers when adding authorization', () => {
|
|
86
|
-
createDotCMSClient(validConfig);
|
|
87
|
-
|
|
88
|
-
expect(PageClient).toHaveBeenCalledWith(
|
|
89
|
-
expect.anything(),
|
|
90
|
-
expect.objectContaining({
|
|
91
|
-
headers: {
|
|
92
|
-
'Content-Type': 'application/json',
|
|
93
|
-
Authorization: 'Bearer test-token'
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe('validation and normalization', () => {
|
|
100
|
-
it('should throw TypeError when dotcmsUrl is invalid', () => {
|
|
101
|
-
const invalidConfig = {
|
|
102
|
-
...validConfig,
|
|
103
|
-
dotcmsUrl: 'invalid-url'
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
try {
|
|
107
|
-
createDotCMSClient(invalidConfig);
|
|
108
|
-
fail('Expected TypeError to be thrown');
|
|
109
|
-
} catch (error) {
|
|
110
|
-
// This is expected, verify the error
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
expect(mockTypeError).toHaveBeenCalledWith(
|
|
114
|
-
"Invalid configuration - 'dotcmsUrl' must be a valid URL"
|
|
115
|
-
);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it('should throw TypeError when authToken is missing', () => {
|
|
119
|
-
const invalidConfig = {
|
|
120
|
-
...validConfig,
|
|
121
|
-
authToken: ''
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
try {
|
|
125
|
-
createDotCMSClient(invalidConfig);
|
|
126
|
-
fail('Expected TypeError to be thrown');
|
|
127
|
-
} catch (error) {
|
|
128
|
-
// This is expected, verify the error
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
expect(mockTypeError).toHaveBeenCalledWith(
|
|
132
|
-
"Invalid configuration - 'authToken' is required"
|
|
133
|
-
);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('should extract origin from dotcmsUrl', () => {
|
|
137
|
-
const configWithPath = {
|
|
138
|
-
...validConfig,
|
|
139
|
-
dotcmsUrl: 'https://demo.dotcms.com/some/path'
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
createDotCMSClient(configWithPath);
|
|
143
|
-
|
|
144
|
-
expect(Content).toHaveBeenCalledWith(expect.anything(), 'https://demo.dotcms.com');
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
});
|
package/src/lib/client/client.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import { consola } from 'consola';
|
|
2
|
-
|
|
3
|
-
import { DotCMSClientConfig, RequestOptions } from '@dotcms/types';
|
|
4
|
-
|
|
5
|
-
import { Content } from './content/content-api';
|
|
6
|
-
import { NavigationClient } from './navigation/navigation-api';
|
|
7
|
-
import { PageClient } from './page/page-api';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Parses a string into a URL object.
|
|
11
|
-
*
|
|
12
|
-
* @param url - The URL string to parse
|
|
13
|
-
* @returns A URL object if parsing is successful, undefined otherwise
|
|
14
|
-
*/
|
|
15
|
-
function parseURL(url: string): URL | undefined {
|
|
16
|
-
try {
|
|
17
|
-
return new URL(url);
|
|
18
|
-
} catch {
|
|
19
|
-
consola.error('[DotCMS Client]: Invalid URL:', url);
|
|
20
|
-
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Default configuration for the DotCMS client.
|
|
27
|
-
*/
|
|
28
|
-
const defaultConfig: DotCMSClientConfig = {
|
|
29
|
-
dotcmsUrl: '',
|
|
30
|
-
authToken: '',
|
|
31
|
-
requestOptions: {}
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Client for interacting with the DotCMS REST API.
|
|
36
|
-
* Provides access to content, page, and navigation functionality.
|
|
37
|
-
*/
|
|
38
|
-
class DotCMSClient {
|
|
39
|
-
private config: DotCMSClientConfig;
|
|
40
|
-
private requestOptions!: RequestOptions;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Client for content-related operations.
|
|
44
|
-
*/
|
|
45
|
-
content: Content;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Client for page-related operations.
|
|
49
|
-
*/
|
|
50
|
-
page: PageClient;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Client for navigation-related operations.
|
|
54
|
-
*/
|
|
55
|
-
nav: NavigationClient;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Creates a new DotCMS client instance.
|
|
59
|
-
*
|
|
60
|
-
* @param config - Configuration options for the client
|
|
61
|
-
* @throws Warning if dotcmsUrl is invalid or authToken is missing
|
|
62
|
-
*/
|
|
63
|
-
constructor(config: DotCMSClientConfig = defaultConfig) {
|
|
64
|
-
this.config = config;
|
|
65
|
-
this.requestOptions = this.createAuthenticatedRequestOptions(this.config);
|
|
66
|
-
|
|
67
|
-
// Initialize clients
|
|
68
|
-
this.page = new PageClient(this.config, this.requestOptions);
|
|
69
|
-
this.nav = new NavigationClient(this.config, this.requestOptions);
|
|
70
|
-
this.content = new Content(this.requestOptions, this.config.dotcmsUrl);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Creates request options with authentication headers.
|
|
75
|
-
*
|
|
76
|
-
* @param config - The client configuration
|
|
77
|
-
* @returns Request options with authorization headers
|
|
78
|
-
*/
|
|
79
|
-
private createAuthenticatedRequestOptions(config: DotCMSClientConfig): RequestOptions {
|
|
80
|
-
return {
|
|
81
|
-
...config.requestOptions,
|
|
82
|
-
headers: {
|
|
83
|
-
...config.requestOptions?.headers,
|
|
84
|
-
Authorization: `Bearer ${config.authToken}`
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Creates and returns a new DotCMS client instance.
|
|
92
|
-
*
|
|
93
|
-
* @param config - Configuration options for the client
|
|
94
|
-
* @returns A configured DotCMS client instance
|
|
95
|
-
* @example
|
|
96
|
-
* ```typescript
|
|
97
|
-
* const client = dotCMSCreateClient({
|
|
98
|
-
* dotcmsUrl: 'https://demo.dotcms.com',
|
|
99
|
-
* authToken: 'your-auth-token'
|
|
100
|
-
* });
|
|
101
|
-
*
|
|
102
|
-
* // Use the client to fetch content
|
|
103
|
-
* const pages = await client.page.get('/about-us');
|
|
104
|
-
* ```
|
|
105
|
-
*/
|
|
106
|
-
export const createDotCMSClient = (clientConfig: DotCMSClientConfig): DotCMSClient => {
|
|
107
|
-
const { dotcmsUrl, authToken } = clientConfig || {};
|
|
108
|
-
const instanceUrl = parseURL(dotcmsUrl)?.origin;
|
|
109
|
-
|
|
110
|
-
if (!instanceUrl) {
|
|
111
|
-
throw new TypeError("Invalid configuration - 'dotcmsUrl' must be a valid URL");
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (!authToken) {
|
|
115
|
-
throw new TypeError("Invalid configuration - 'authToken' is required");
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const config = {
|
|
119
|
-
...clientConfig,
|
|
120
|
-
authToken,
|
|
121
|
-
dotcmsUrl: instanceUrl
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
return new DotCMSClient(config);
|
|
125
|
-
};
|