@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.
Files changed (52) hide show
  1. package/dotcms-client-1.0.1.tgz +0 -0
  2. package/index.cjs.d.ts +1 -0
  3. package/index.cjs.default.js +1 -0
  4. package/index.cjs.js +1592 -0
  5. package/index.cjs.mjs +2 -0
  6. package/index.esm.d.ts +1 -0
  7. package/index.esm.js +1590 -0
  8. package/internal.cjs.d.ts +1 -0
  9. package/internal.cjs.default.js +1 -0
  10. package/internal.cjs.js +85 -0
  11. package/internal.cjs.mjs +2 -0
  12. package/internal.esm.d.ts +1 -0
  13. package/internal.esm.js +83 -0
  14. package/package.json +24 -7
  15. package/src/lib/client/client.d.ts +56 -0
  16. package/src/lib/client/content/builders/collection/{collection.ts → collection.d.ts} +19 -210
  17. package/src/lib/client/content/builders/query/lucene-syntax/{Equals.ts → Equals.d.ts} +11 -45
  18. package/src/lib/client/content/builders/query/lucene-syntax/{Field.ts → Field.d.ts} +5 -13
  19. package/src/lib/client/content/builders/query/lucene-syntax/{NotOperand.ts → NotOperand.d.ts} +5 -13
  20. package/src/lib/client/content/builders/query/lucene-syntax/{Operand.ts → Operand.d.ts} +7 -21
  21. package/src/lib/client/content/builders/query/{query.ts → query.d.ts} +5 -16
  22. package/src/lib/client/content/builders/query/utils/{index.ts → index.d.ts} +12 -49
  23. package/src/lib/client/content/{content-api.ts → content-api.d.ts} +4 -14
  24. package/src/lib/client/content/shared/{const.ts → const.d.ts} +3 -5
  25. package/src/lib/client/content/shared/{types.ts → types.d.ts} +2 -18
  26. package/src/lib/client/content/shared/{utils.ts → utils.d.ts} +1 -9
  27. package/src/lib/client/models/{index.ts → index.d.ts} +1 -8
  28. package/src/lib/client/navigation/navigation-api.d.ts +14 -0
  29. package/src/lib/client/page/page-api.d.ts +95 -0
  30. package/src/lib/client/page/utils.d.ts +41 -0
  31. package/src/lib/utils/graphql/transforms.d.ts +13 -0
  32. package/.eslintrc.json +0 -18
  33. package/jest.config.ts +0 -15
  34. package/project.json +0 -73
  35. package/src/lib/client/client.spec.ts +0 -147
  36. package/src/lib/client/client.ts +0 -125
  37. package/src/lib/client/content/builders/collection/collection.spec.ts +0 -514
  38. package/src/lib/client/content/builders/query/query.spec.ts +0 -159
  39. package/src/lib/client/navigation/navigation-api.spec.ts +0 -167
  40. package/src/lib/client/navigation/navigation-api.ts +0 -62
  41. package/src/lib/client/page/page-api.spec.ts +0 -359
  42. package/src/lib/client/page/page-api.ts +0 -197
  43. package/src/lib/client/page/utils.ts +0 -291
  44. package/src/lib/utils/graphql/transforms.spec.ts +0 -250
  45. package/src/lib/utils/graphql/transforms.ts +0 -128
  46. package/tsconfig.json +0 -22
  47. package/tsconfig.lib.json +0 -13
  48. package/tsconfig.spec.json +0 -9
  49. /package/src/{index.ts → index.d.ts} +0 -0
  50. /package/src/{internal.ts → internal.d.ts} +0 -0
  51. /package/src/lib/client/content/builders/query/lucene-syntax/{index.ts → index.d.ts} +0 -0
  52. /package/src/lib/utils/{index.ts → index.d.ts} +0 -0
@@ -1,197 +0,0 @@
1
- import consola from 'consola';
2
-
3
- import {
4
- DotCMSClientConfig,
5
- DotCMSComposedPageResponse,
6
- DotCMSExtendedPageResponse,
7
- DotCMSPageResponse,
8
- DotCMSPageRequestParams,
9
- RequestOptions
10
- } from '@dotcms/types';
11
-
12
- import { buildPageQuery, buildQuery, fetchGraphQL, mapResponseData } from './utils';
13
-
14
- import { graphqlToPageEntity } from '../../utils';
15
-
16
- /**
17
- * Client for interacting with the DotCMS Page API.
18
- * Provides methods to retrieve and manipulate pages.
19
- */
20
- export class PageClient {
21
- /**
22
- * Request options including authorization headers.
23
- * @private
24
- */
25
- private requestOptions: RequestOptions;
26
-
27
- /**
28
- * Site ID for page requests.
29
- * @private
30
- */
31
- private siteId: string;
32
-
33
- /**
34
- * DotCMS URL for page requests.
35
- * @private
36
- */
37
- private dotcmsUrl: string;
38
-
39
- /**
40
- * Creates a new PageClient instance.
41
- *
42
- * @param {DotCMSClientConfig} config - Configuration options for the DotCMS client
43
- * @param {RequestOptions} requestOptions - Options for fetch requests including authorization headers
44
- * @example
45
- * ```typescript
46
- * const pageClient = new PageClient(
47
- * {
48
- * dotcmsUrl: 'https://demo.dotcms.com',
49
- * authToken: 'your-auth-token',
50
- * siteId: 'demo.dotcms.com'
51
- * },
52
- * {
53
- * headers: {
54
- * Authorization: 'Bearer your-auth-token'
55
- * }
56
- * }
57
- * );
58
- * ```
59
- */
60
- constructor(config: DotCMSClientConfig, requestOptions: RequestOptions) {
61
- this.requestOptions = requestOptions;
62
- this.siteId = config.siteId || '';
63
- this.dotcmsUrl = config.dotcmsUrl;
64
- }
65
-
66
- /**
67
- * Retrieves a page from DotCMS using GraphQL.
68
- *
69
- * @param {string} url - The URL of the page to retrieve
70
- * @param {DotCMSPageRequestParams} [options] - Options for the request
71
- * @template T - The type of the page and content, defaults to DotCMSBasicPage and Record<string, unknown> | unknown
72
- * @returns {Promise<DotCMSComposedPageResponse<T>>} A Promise that resolves to the page data
73
- *
74
- * @example Using GraphQL
75
- * ```typescript
76
- * const page = await pageClient.get<{ page: MyPageWithBanners; content: { blogPosts: { blogTitle: string } } }>(
77
- * '/index',
78
- * {
79
- * languageId: '1',
80
- * mode: 'LIVE',
81
- * graphql: {
82
- * page: `
83
- * containers {
84
- * containerContentlets {
85
- * contentlets {
86
- * ... on Banner {
87
- * ...bannerFragment
88
- * }
89
- * }
90
- * }
91
- * `,
92
- * content: {
93
- * blogPosts: `
94
- * BlogCollection(limit: 3) {
95
- * ...blogFragment
96
- * }
97
- * `,
98
- * },
99
- * fragments: [
100
- * `
101
- * fragment bannerFragment on Banner {
102
- * caption
103
- * }
104
- * `,
105
- * `
106
- * fragment blogFragment on Blog {
107
- * title
108
- * urlTitle
109
- * }
110
- * `
111
- * ]
112
- * }
113
- * });
114
- * ```
115
- */
116
- async get<T extends DotCMSExtendedPageResponse = DotCMSPageResponse>(
117
- url: string,
118
- options?: DotCMSPageRequestParams
119
- ): Promise<DotCMSComposedPageResponse<T>> {
120
- const {
121
- languageId = '1',
122
- mode = 'LIVE',
123
- siteId = this.siteId,
124
- fireRules = false,
125
- personaId,
126
- publishDate,
127
- variantName,
128
- graphql = {}
129
- } = options || {};
130
- const { page, content = {}, variables, fragments } = graphql;
131
-
132
- const contentQuery = buildQuery(content);
133
- const completeQuery = buildPageQuery({
134
- page,
135
- fragments,
136
- additionalQueries: contentQuery
137
- });
138
-
139
- const requestVariables: Record<string, unknown> = {
140
- // The url is expected to have a leading slash to comply on VanityURL Matching, some frameworks like Angular will not add the leading slash
141
- url: url.startsWith('/') ? url : `/${url}`,
142
- mode,
143
- languageId,
144
- personaId,
145
- fireRules,
146
- publishDate,
147
- siteId,
148
- variantName,
149
- ...variables
150
- };
151
-
152
- const requestHeaders = this.requestOptions.headers as Record<string, string>;
153
- const requestBody = JSON.stringify({ query: completeQuery, variables: requestVariables });
154
-
155
- try {
156
- const { data, errors } = await fetchGraphQL({
157
- baseURL: this.dotcmsUrl,
158
- body: requestBody,
159
- headers: requestHeaders
160
- });
161
-
162
- if (errors) {
163
- errors.forEach((error: { message: string }) => {
164
- consola.error('[DotCMS GraphQL Error]: ', error.message);
165
- });
166
- }
167
-
168
- const pageResponse = graphqlToPageEntity(data);
169
-
170
- if (!pageResponse) {
171
- throw new Error('No page data found');
172
- }
173
-
174
- const contentResponse = mapResponseData(data, Object.keys(content));
175
-
176
- return {
177
- pageAsset: pageResponse,
178
- content: contentResponse,
179
- graphql: {
180
- query: completeQuery,
181
- variables: requestVariables
182
- }
183
- };
184
- } catch (error) {
185
- const errorMessage = {
186
- error,
187
- message: 'Failed to retrieve page data',
188
- graphql: {
189
- query: completeQuery,
190
- variables: requestVariables
191
- }
192
- };
193
-
194
- throw errorMessage;
195
- }
196
- }
197
- }
@@ -1,291 +0,0 @@
1
- import { consola } from 'consola';
2
-
3
- import { ErrorMessages } from '../models';
4
-
5
- const DEFAULT_PAGE_CONTENTLETS_CONTENT = `
6
- publishDate
7
- inode
8
- identifier
9
- archived
10
- urlMap
11
- urlMap
12
- locked
13
- contentType
14
- creationDate
15
- modDate
16
- title
17
- baseType
18
- working
19
- live
20
- publishUser {
21
- firstName
22
- lastName
23
- }
24
- owner {
25
- lastName
26
- }
27
- conLanguage {
28
- language
29
- languageCode
30
- }
31
- modUser {
32
- firstName
33
- lastName
34
- }
35
- `;
36
-
37
- /**
38
- * Builds a GraphQL query for retrieving page content from DotCMS.
39
- *
40
- * @param {string} pageQuery - Custom fragment fields to include in the ClientPage fragment
41
- * @param {string} additionalQueries - Additional GraphQL queries to include in the main query
42
- * @returns {string} Complete GraphQL query string for page content
43
- */
44
- export const buildPageQuery = ({
45
- page,
46
- fragments,
47
- additionalQueries
48
- }: {
49
- page?: string;
50
- fragments?: string[];
51
- additionalQueries?: string;
52
- }) => {
53
- if (!page) {
54
- consola.warn(
55
- "[DotCMS Client]: No page query was found, so we're loading all content using _map. This might slow things down. For better performance, we recommend adding a specific query in the page attribute."
56
- );
57
- }
58
-
59
- return `
60
- fragment DotCMSPage on DotPage {
61
- publishDate
62
- type
63
- httpsRequired
64
- inode
65
- path
66
- identifier
67
- hasTitleImage
68
- sortOrder
69
- extension
70
- canRead
71
- pageURI
72
- canEdit
73
- archived
74
- friendlyName
75
- workingInode
76
- url
77
- pageURI
78
- hasLiveVersion
79
- deleted
80
- pageUrl
81
- shortyWorking
82
- mimeType
83
- locked
84
- stInode
85
- contentType
86
- creationDate
87
- liveInode
88
- name
89
- shortyLive
90
- modDate
91
- title
92
- baseType
93
- working
94
- canLock
95
- live
96
- isContentlet
97
- statusIcons
98
- canEdit
99
- canLock
100
- canRead
101
- canEdit
102
- canLock
103
- canRead
104
- runningExperimentId
105
- urlContentMap {
106
- _map
107
- }
108
- host {
109
- identifier
110
- hostName
111
- googleMap
112
- archived
113
- contentType
114
- }
115
- vanityUrl {
116
- action
117
- forwardTo
118
- uri
119
- }
120
- conLanguage {
121
- id
122
- language
123
- languageCode
124
- }
125
- template {
126
- drawed
127
- anonymous
128
- theme
129
- identifier
130
- }
131
- containers {
132
- path
133
- identifier
134
- maxContentlets
135
- containerStructures {
136
- id
137
- code
138
- structureId
139
- containerId
140
- contentTypeVar
141
- containerInode
142
- }
143
- containerContentlets {
144
- uuid
145
- contentlets {
146
- ${page ? DEFAULT_PAGE_CONTENTLETS_CONTENT : '_map'}
147
- }
148
- }
149
- }
150
- layout {
151
- header
152
- footer
153
- body {
154
- rows {
155
- styleClass
156
- columns {
157
- leftOffset
158
- styleClass
159
- width
160
- left
161
- containers {
162
- identifier
163
- uuid
164
- }
165
- }
166
- }
167
- }
168
- }
169
- viewAs {
170
- visitor {
171
- persona {
172
- modDate
173
- inode
174
- name
175
- identifier
176
- keyTag
177
- photo {
178
- versionPath
179
- }
180
- }
181
- }
182
- persona {
183
- modDate
184
- inode
185
- name
186
- identifier
187
- keyTag
188
- photo {
189
- versionPath
190
- }
191
- }
192
- language {
193
- id
194
- languageCode
195
- countryCode
196
- language
197
- country
198
- }
199
- }
200
- }
201
-
202
- ${page ? ` fragment ClientPage on DotPage { ${page} } ` : ''}
203
-
204
- ${fragments ? fragments.join('\n\n') : ''}
205
-
206
- query PageContent($url: String!, $languageId: String, $mode: String, $personaId: String, $fireRules: Boolean, $publishDate: String, $siteId: String, $variantName: String) {
207
- page: page(url: $url, languageId: $languageId, pageMode: $mode, persona: $personaId, fireRules: $fireRules, publishDate: $publishDate, site: $siteId, variantName: $variantName) {
208
- ...DotCMSPage
209
- ${page ? '...ClientPage' : ''}
210
- }
211
-
212
- ${additionalQueries}
213
- }
214
- `;
215
- };
216
-
217
- /**
218
- * Converts a record of query strings into a single GraphQL query string.
219
- *
220
- * @param {Record<string, string>} queryData - Object containing named query strings
221
- * @returns {string} Combined query string or empty string if no queryData provided
222
- */
223
- export function buildQuery(queryData: Record<string, string>): string {
224
- if (!queryData) return '';
225
-
226
- return Object.entries(queryData)
227
- .map(([key, query]) => `${key}: ${query}`)
228
- .join(' ');
229
- }
230
-
231
- /**
232
- * Filters response data to include only specified keys.
233
- *
234
- * @param {Record<string, string>} responseData - Original response data object
235
- * @param {string[]} keys - Array of keys to extract from the response data
236
- * @returns {Record<string, string>} New object containing only the specified keys
237
- */
238
- export function mapResponseData(
239
- responseData: Record<string, string>,
240
- keys: string[]
241
- ): Record<string, string> {
242
- return keys.reduce(
243
- (accumulator, key) => {
244
- if (responseData[key] !== undefined) {
245
- accumulator[key] = responseData[key];
246
- }
247
-
248
- return accumulator;
249
- },
250
- {} as Record<string, string>
251
- );
252
- }
253
-
254
- /**
255
- * Executes a GraphQL query against the DotCMS API.
256
- *
257
- * @param {Object} options - Options for the fetch request
258
- * @param {string} options.body - GraphQL query string
259
- * @param {Record<string, string>} options.headers - HTTP headers for the request
260
- * @returns {Promise<any>} Parsed JSON response from the GraphQL API
261
- * @throws {Error} If the HTTP response is not successful
262
- */
263
- export async function fetchGraphQL({
264
- baseURL,
265
- body,
266
- headers
267
- }: {
268
- baseURL: string;
269
- body: string;
270
- headers: Record<string, string>;
271
- }) {
272
- const url = new URL(baseURL);
273
- url.pathname = '/api/v1/graphql';
274
-
275
- const response = await fetch(url.toString(), {
276
- method: 'POST',
277
- body,
278
- headers
279
- });
280
-
281
- if (!response.ok) {
282
- const error = {
283
- status: response.status,
284
- message: ErrorMessages[response.status] || response.statusText
285
- };
286
-
287
- throw error;
288
- }
289
-
290
- return await response.json();
291
- }