@contentstack/studio-client 1.3.0 → 1.5.0
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/dist/breakpoints/BreakpointManager.js +0 -1
- package/dist/breakpoints/BreakpointManager.js.map +1 -1
- package/dist/composable-studio/initializer-sdk.js +1 -10
- package/dist/composable-studio/initializer-sdk.js.map +1 -1
- package/dist/composable-studio/sdk.js +448 -127
- package/dist/composable-studio/sdk.js.map +1 -1
- package/dist/config/config.schema.js +11 -0
- package/dist/config/config.schema.js.map +1 -1
- package/dist/config/default-config.js +1 -0
- package/dist/config/default-config.js.map +1 -1
- package/dist/config/process-user-config.js +10 -1
- package/dist/config/process-user-config.js.map +1 -1
- package/dist/config/stack-sdk-wrapper.js +52 -12
- package/dist/config/stack-sdk-wrapper.js.map +1 -1
- package/dist/context-menu/context-menu.js +0 -1
- package/dist/context-menu/context-menu.js.map +1 -1
- package/dist/design/handle-active-style-change.js +0 -1
- package/dist/design/handle-active-style-change.js.map +1 -1
- package/dist/dnd/dnd-manager.js +2 -3
- package/dist/dnd/dnd-manager.js.map +1 -1
- package/dist/highlight-node/highlight-node-manager.js +0 -1
- package/dist/highlight-node/highlight-node-manager.js.map +1 -1
- package/dist/highlight-node/highlight-node.js +30 -14
- package/dist/highlight-node/highlight-node.js.map +1 -1
- package/dist/index.d.ts +249 -25
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/node_modules/fflate/esm/browser.js +384 -0
- package/dist/node_modules/fflate/esm/browser.js.map +1 -0
- package/dist/packages/studio-internal/dist/utils/spec-compression.js +43 -0
- package/dist/packages/studio-internal/dist/utils/spec-compression.js.map +1 -0
- package/dist/query/expression-resolver.js +124 -0
- package/dist/query/expression-resolver.js.map +1 -0
- package/dist/query/query-executor.js +327 -0
- package/dist/query/query-executor.js.map +1 -0
- package/dist/url-change/url-change.js +65 -0
- package/dist/url-change/url-change.js.map +1 -0
- package/dist/utils/composable-studio-error.js +9 -7
- package/dist/utils/composable-studio-error.js.map +1 -1
- package/dist/utils/conditionBlock.js +16 -4
- package/dist/utils/conditionBlock.js.map +1 -1
- package/dist/utils/error-messages.default.js +141 -0
- package/dist/utils/error-messages.default.js.map +1 -0
- package/dist/utils/error-messages.js +47 -0
- package/dist/utils/error-messages.js.map +1 -0
- package/dist/utils/http-error.constants.js +96 -0
- package/dist/utils/http-error.constants.js.map +1 -0
- package/dist/utils/is-inside-iframe.js +16 -0
- package/dist/utils/is-inside-iframe.js.map +1 -0
- package/dist/utils/reference_fetcher.js +104 -8
- package/dist/utils/reference_fetcher.js.map +1 -1
- package/dist/utils/sdk.js +3 -0
- package/dist/utils/sdk.js.map +1 -1
- package/package.json +9 -8
|
@@ -5,14 +5,115 @@ import { processUserConfig } from '../config/process-user-config.js';
|
|
|
5
5
|
import { getCompositionFromSlug } from '../get-composition-from-slug/get-composition-from-slug.js';
|
|
6
6
|
import { INCLUDE_DEPTH_COUNT } from '../utils/constants.js';
|
|
7
7
|
import { isCompositionBeingEdited } from '../utils/is-composition-being-edited.js';
|
|
8
|
-
import {
|
|
8
|
+
import { setTemplateEntryInDS, entriesFromResponse, setStaticEntryInDS } from '../utils/sdk.js';
|
|
9
9
|
import { ComposableStudioError, ComposableStudioErrors } from '../utils/composable-studio-error.js';
|
|
10
10
|
import { clientRendererModeUtil, RendererModeUtil } from '../utils/canvas.js';
|
|
11
11
|
import { getEntriesQueries } from '../utils/reference_fetcher.js';
|
|
12
12
|
import { preProcessStaticValues } from './data/pre-process-static-values.js';
|
|
13
13
|
import { convertCompositionQueryInputToCompositionQuery } from '../utils/convertCompositionQueryInputToCompositionQuery.js';
|
|
14
14
|
import { getUrlPatterns } from '../utils/get-url-patterns.js';
|
|
15
|
+
import { getStatusCode, isContentstackAuthError, hasAuthErrorShape, isAuthError, isNetworkError, HttpStatus, ContentstackErrorCode, isServerError } from '../utils/http-error.constants.js';
|
|
16
|
+
import { getErrorMessage, getTroubleshootingInfo } from '../utils/error-messages.js';
|
|
17
|
+
import { decompressSpec } from '../packages/studio-internal/dist/utils/spec-compression.js';
|
|
18
|
+
import { ErrorMessageKey, TroubleshootingInfoKey } from '../utils/error-messages.default.js';
|
|
15
19
|
|
|
20
|
+
function getAuthErrorMessage(err) {
|
|
21
|
+
const detail = err?.error_message ||
|
|
22
|
+
err?.errors?.api_key?.[0] ||
|
|
23
|
+
err?.errors?.authorization?.[0] ||
|
|
24
|
+
err?.message ||
|
|
25
|
+
getErrorMessage(ErrorMessageKey.INVALID_CREDENTIALS_DEFAULT);
|
|
26
|
+
return getErrorMessage(ErrorMessageKey.AUTHENTICATION_ERROR, { detail });
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Helper function to detect and classify API errors
|
|
30
|
+
* @internal - Exported for testing purposes
|
|
31
|
+
*/
|
|
32
|
+
function classifyApiError(err) {
|
|
33
|
+
const statusCode = getStatusCode(err);
|
|
34
|
+
// Check for authentication errors: Contentstack error_code 104/105 or errors.api_key/authorization (no HTTP status)
|
|
35
|
+
if (isContentstackAuthError(err) || (!statusCode && hasAuthErrorShape(err))) {
|
|
36
|
+
return {
|
|
37
|
+
errorType: ComposableStudioErrors.AUTHENTICATION_ERROR,
|
|
38
|
+
message: getAuthErrorMessage(err),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
// Check for authentication errors (401, 403)
|
|
42
|
+
if (statusCode !== undefined && isAuthError(statusCode)) {
|
|
43
|
+
return {
|
|
44
|
+
errorType: ComposableStudioErrors.AUTHENTICATION_ERROR,
|
|
45
|
+
message: getAuthErrorMessage(err),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
// Check for network errors (no response, timeout, connection refused)
|
|
49
|
+
// Empty response object often indicates network failure
|
|
50
|
+
const isEmptyResponse = err && typeof err === "object" && Object.keys(err).length === 0;
|
|
51
|
+
if ((!statusCode && isNetworkError(err)) || isEmptyResponse) {
|
|
52
|
+
return {
|
|
53
|
+
errorType: ComposableStudioErrors.NETWORK_ERROR,
|
|
54
|
+
message: isEmptyResponse
|
|
55
|
+
? getErrorMessage(ErrorMessageKey.NETWORK_ERROR_EMPTY)
|
|
56
|
+
: getErrorMessage(ErrorMessageKey.NETWORK_ERROR_GENERIC, {
|
|
57
|
+
detail: err?.message ||
|
|
58
|
+
getErrorMessage(ErrorMessageKey.NETWORK_ERROR_DETAIL_DEFAULT),
|
|
59
|
+
}),
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
// Check for 404 - composition not found
|
|
63
|
+
if (statusCode === HttpStatus.NOT_FOUND ||
|
|
64
|
+
err?.error_code === ContentstackErrorCode.ENTRY_NOT_FOUND) {
|
|
65
|
+
return {
|
|
66
|
+
errorType: ComposableStudioErrors.COMPOSITION_NOT_FOUND,
|
|
67
|
+
message: err?.error_message ||
|
|
68
|
+
err?.message ||
|
|
69
|
+
getErrorMessage(ErrorMessageKey.COMPOSITION_NOT_FOUND_404),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
// Check for server errors (5xx)
|
|
73
|
+
if (statusCode !== undefined && isServerError(statusCode)) {
|
|
74
|
+
return {
|
|
75
|
+
errorType: ComposableStudioErrors.SERVER_ERROR,
|
|
76
|
+
message: getErrorMessage(ErrorMessageKey.SERVER_ERROR, {
|
|
77
|
+
statusCode,
|
|
78
|
+
detail: err?.error_message ||
|
|
79
|
+
err?.message ||
|
|
80
|
+
getErrorMessage(ErrorMessageKey.SERVER_ERROR_DETAIL_DEFAULT),
|
|
81
|
+
}),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// Other HTTP errors or default: single COMPOSITION_NOT_FOUND return with shared message builder
|
|
85
|
+
const message = statusCode !== undefined
|
|
86
|
+
? getErrorMessage(ErrorMessageKey.COMPOSITION_NOT_FOUND_API, {
|
|
87
|
+
statusCode,
|
|
88
|
+
detail: err?.error_message ||
|
|
89
|
+
err?.message ||
|
|
90
|
+
getErrorMessage(ErrorMessageKey.COMPOSITION_NOT_FOUND_API_DETAIL_DEFAULT),
|
|
91
|
+
})
|
|
92
|
+
: err?.error_message ||
|
|
93
|
+
err?.message ||
|
|
94
|
+
getErrorMessage(ErrorMessageKey.COMPOSITION_NOT_FOUND_DEFAULT);
|
|
95
|
+
return {
|
|
96
|
+
errorType: ComposableStudioErrors.COMPOSITION_NOT_FOUND,
|
|
97
|
+
message,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Helper function to get troubleshooting information for composition fetch errors.
|
|
102
|
+
* Uses getTroubleshootingInfo from error-messages (single source of truth).
|
|
103
|
+
* @internal
|
|
104
|
+
*/
|
|
105
|
+
function getTroubleshootingInfoForCompositionFetchError(errorType, isUrlBased = false) {
|
|
106
|
+
if (errorType === ComposableStudioErrors.NETWORK_ERROR) {
|
|
107
|
+
return getTroubleshootingInfo(TroubleshootingInfoKey.NETWORK_ERROR);
|
|
108
|
+
}
|
|
109
|
+
if (errorType === ComposableStudioErrors.AUTHENTICATION_ERROR) {
|
|
110
|
+
return getTroubleshootingInfo(TroubleshootingInfoKey.AUTHENTICATION_ERROR);
|
|
111
|
+
}
|
|
112
|
+
if (isUrlBased) {
|
|
113
|
+
return getTroubleshootingInfo(TroubleshootingInfoKey.COMPOSITION_NOT_FOUND_URL);
|
|
114
|
+
}
|
|
115
|
+
return getTroubleshootingInfo(TroubleshootingInfoKey.COMPOSITION_NOT_FOUND_UID);
|
|
116
|
+
}
|
|
16
117
|
function addEditableTags(entry, contentTypeUid, tagsAsObject, locale = "en-us") {
|
|
17
118
|
if (entry) {
|
|
18
119
|
const appliedVariants = entry._applied_variants || entry?.system?.applied_variants || null;
|
|
@@ -199,21 +300,28 @@ function generateSdk(userConfig) {
|
|
|
199
300
|
config.stackSdk.livePreviewQuery?.(hash);
|
|
200
301
|
}
|
|
201
302
|
async function resolveStaticData(staticBindingQueries, staticBindingOnlyQueries, data, options = {}) {
|
|
202
|
-
const { variantAlias } = options;
|
|
303
|
+
const { variantAlias, locale } = options;
|
|
304
|
+
const isInsideVB = clientRendererModeUtil.isInsideVisualBuilderFrame();
|
|
203
305
|
try {
|
|
204
306
|
const promises = [];
|
|
205
307
|
for (const _content_type_uid in staticBindingQueries) {
|
|
206
308
|
const ct_promises = [];
|
|
207
309
|
for (const entry_uid in staticBindingQueries[_content_type_uid]) {
|
|
208
310
|
const references = staticBindingQueries[_content_type_uid][entry_uid];
|
|
209
|
-
|
|
210
|
-
//
|
|
211
|
-
if (
|
|
212
|
-
onlyQuery
|
|
311
|
+
let onlyQuery = [];
|
|
312
|
+
// When inside Visual Builder, fetch all fields (don't use onlyQueries)
|
|
313
|
+
if (!isInsideVB) {
|
|
314
|
+
onlyQuery =
|
|
315
|
+
staticBindingOnlyQueries[_content_type_uid][entry_uid] || [];
|
|
316
|
+
// We need locale for redirect to the correct locale.
|
|
317
|
+
if (config.cslp.appendTags) {
|
|
318
|
+
onlyQuery.push(["BASE", "locale"]);
|
|
319
|
+
}
|
|
213
320
|
}
|
|
214
321
|
const query = config.stackSdk.entryQuery(_content_type_uid, entry_uid, {
|
|
215
322
|
only: onlyQuery,
|
|
216
323
|
variantAlias,
|
|
324
|
+
locale: locale || config.locale, // Use override locale or default
|
|
217
325
|
});
|
|
218
326
|
if (references.length) {
|
|
219
327
|
query.includeReference(references);
|
|
@@ -252,64 +360,112 @@ function generateSdk(userConfig) {
|
|
|
252
360
|
}
|
|
253
361
|
/**
|
|
254
362
|
* When composition is binded to a content type, we need to fetch the entry for that content type. Called template entry.
|
|
363
|
+
* Supports custom fetcher overrides (per-query > global > SDK default).
|
|
255
364
|
*/
|
|
256
|
-
async function fetchTemplateEntry(
|
|
365
|
+
async function fetchTemplateEntry(fetchOptions, data, compositionEntry, compositionQueryOptions) {
|
|
257
366
|
if (!config.stackSdk) {
|
|
258
367
|
console.error("Fetching template entry not possible without stackSdk");
|
|
259
368
|
return;
|
|
260
369
|
}
|
|
261
|
-
const { connected_content_type, compositionQuery, includeQueries, onlyQueries, } =
|
|
370
|
+
const { connected_content_type, compositionQuery, includeQueries, onlyQueries, } = fetchOptions;
|
|
262
371
|
if (!connected_content_type) {
|
|
263
372
|
return;
|
|
264
373
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
374
|
+
const { fetchComposition: _, fetchTemplateEntry: perQueryFetchTemplateEntry, ...resolvedOptions } = compositionQueryOptions || {};
|
|
375
|
+
const isInsideVB = clientRendererModeUtil.isInsideVisualBuilderFrame();
|
|
376
|
+
const defaultImpl = async () => {
|
|
377
|
+
const queryOptions = {
|
|
378
|
+
references: includeQueries,
|
|
379
|
+
variantAlias: resolvedOptions.variantAlias,
|
|
380
|
+
locale: resolvedOptions.locale || config.locale,
|
|
381
|
+
};
|
|
382
|
+
if (!isInsideVB) {
|
|
383
|
+
queryOptions.only = onlyQueries;
|
|
384
|
+
}
|
|
385
|
+
let dataQuery = config.stackSdk
|
|
386
|
+
.ctQuery(connected_content_type, queryOptions)
|
|
387
|
+
.limit(1);
|
|
388
|
+
// Resolution priority: templateEntryUid > url > first entry
|
|
389
|
+
if (resolvedOptions.templateEntryUid) {
|
|
390
|
+
dataQuery = config.stackSdk.where(dataQuery, "uid", resolvedOptions.templateEntryUid);
|
|
391
|
+
}
|
|
392
|
+
else if ("url" in compositionQuery) {
|
|
393
|
+
dataQuery = config.stackSdk.where(dataQuery, "url", compositionQuery.url);
|
|
394
|
+
}
|
|
395
|
+
const response = await dataQuery.find().catch((err) => {
|
|
396
|
+
const { errorType, message } = classifyApiError(err);
|
|
397
|
+
console.error("SDK Error (connected data):", {
|
|
398
|
+
errorType,
|
|
399
|
+
message,
|
|
400
|
+
originalError: err,
|
|
401
|
+
});
|
|
402
|
+
const finalErrorType = errorType === ComposableStudioErrors.COMPOSITION_NOT_FOUND
|
|
403
|
+
? ComposableStudioErrors.FETCHING_CONNECTED_DATA_FAILED
|
|
404
|
+
: errorType;
|
|
405
|
+
throw new ComposableStudioError(finalErrorType, finalErrorType ===
|
|
406
|
+
ComposableStudioErrors.FETCHING_CONNECTED_DATA_FAILED
|
|
407
|
+
? getErrorMessage(ErrorMessageKey.FETCHING_CONNECTED_DATA_FAILED)
|
|
408
|
+
: message, {
|
|
409
|
+
options: fetchOptions,
|
|
410
|
+
response: err,
|
|
411
|
+
statusCode: err?.status || err?.statusCode,
|
|
412
|
+
errorCode: err?.error_code,
|
|
413
|
+
});
|
|
282
414
|
});
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
415
|
+
const entries = entriesFromResponse(response);
|
|
416
|
+
if (!entries?.[0]) {
|
|
417
|
+
console.error("Preview/Template entry not found", {
|
|
418
|
+
contentType: connected_content_type,
|
|
419
|
+
compositionQuery,
|
|
420
|
+
});
|
|
421
|
+
throw new ComposableStudioError(ComposableStudioErrors.PREVIEW_ENTRY_NOT_FOUND, getErrorMessage(ErrorMessageKey.PREVIEW_ENTRY_NOT_FOUND, {
|
|
422
|
+
contentType: connected_content_type,
|
|
423
|
+
}), {
|
|
424
|
+
contentType: connected_content_type,
|
|
425
|
+
hasStackSdk: !!config.stackSdk,
|
|
426
|
+
compositionQuery,
|
|
427
|
+
options: fetchOptions,
|
|
428
|
+
troubleshootingInfo: getTroubleshootingInfo(TroubleshootingInfoKey.PREVIEW_ENTRY_NOT_FOUND),
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
return entries[0];
|
|
432
|
+
};
|
|
433
|
+
const customFetcher = perQueryFetchTemplateEntry ?? config.fetchTemplateEntry;
|
|
434
|
+
let entry;
|
|
435
|
+
if (customFetcher) {
|
|
436
|
+
entry = await customFetcher({
|
|
437
|
+
compositionEntry,
|
|
438
|
+
compositionQuery,
|
|
439
|
+
connectedContentType: connected_content_type,
|
|
440
|
+
options: resolvedOptions,
|
|
441
|
+
includeReferences: includeQueries,
|
|
442
|
+
only: isInsideVB ? [] : onlyQueries,
|
|
443
|
+
stackSdk: config.stackSdk.sdk,
|
|
444
|
+
defaultFetch: defaultImpl,
|
|
445
|
+
});
|
|
446
|
+
if (!entry) {
|
|
447
|
+
throw new ComposableStudioError(ComposableStudioErrors.PREVIEW_ENTRY_NOT_FOUND, getErrorMessage(ErrorMessageKey.PREVIEW_ENTRY_NOT_FOUND, {
|
|
448
|
+
contentType: connected_content_type,
|
|
449
|
+
}));
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
else {
|
|
453
|
+
entry = await defaultImpl();
|
|
287
454
|
}
|
|
288
|
-
const entry = entries[0];
|
|
289
455
|
if (config.cslp.appendTags) {
|
|
290
456
|
addEditableTags(entry, connected_content_type, true, entry.locale);
|
|
291
457
|
}
|
|
292
458
|
setTemplateEntryInDS(data.dataSources, entry);
|
|
293
|
-
if (!isCompositionBeingEdited(
|
|
459
|
+
if (!isCompositionBeingEdited(fetchOptions?.composableUid || ""))
|
|
294
460
|
return;
|
|
295
461
|
}
|
|
296
|
-
async function
|
|
297
|
-
const { variantAlias } = options || {};
|
|
298
|
-
if (!config.stackSdk) {
|
|
299
|
-
throw new ComposableStudioError(ComposableStudioErrors.STACK_SDK_NOT_AVAILABLE, "Stack SDK not available to fetch composition.");
|
|
300
|
-
}
|
|
301
|
-
const { compositionEntry, symbolEntries } = await fetchComposition(config, queryOptions);
|
|
302
|
-
// This will iterate over ui and collect all data to fetch ( entries and depth of refrerences for both template and static contentstack)
|
|
462
|
+
async function fetchData(compositionEntry, queryOptions, options) {
|
|
463
|
+
const { variantAlias, locale } = options || {};
|
|
303
464
|
const { templateReferencesPath, templateOnlyKeys, contentstackEntriesReferencesPath, contentstackEntriesOnlyKeys, } = getEntriesQueries(compositionEntry, compositionEntry.connected_content_type || "", options);
|
|
304
|
-
const page = typeof compositionEntry.ui === "string"
|
|
305
|
-
? JSON.parse(compositionEntry.ui)
|
|
306
|
-
: compositionEntry.ui;
|
|
307
|
-
const ui = page;
|
|
308
465
|
const data = {
|
|
309
466
|
[DATA_SOURCES_PREFIX]: {},
|
|
310
467
|
[DATA_SOURCES.STATIC_VALUE]: preProcessStaticValues(compositionEntry.static_value || {}),
|
|
311
468
|
};
|
|
312
|
-
// TODO: Add a variantAlias to the fetchTemplateEntry and resolveStaticData.
|
|
313
469
|
await Promise.all([
|
|
314
470
|
fetchTemplateEntry({
|
|
315
471
|
connected_content_type: compositionEntry.connected_content_type || "",
|
|
@@ -317,21 +473,126 @@ function generateSdk(userConfig) {
|
|
|
317
473
|
includeQueries: templateReferencesPath,
|
|
318
474
|
onlyQueries: templateOnlyKeys,
|
|
319
475
|
composableUid: compositionEntry.composable_uid,
|
|
320
|
-
|
|
321
|
-
}, data),
|
|
476
|
+
}, data, compositionEntry, options),
|
|
322
477
|
resolveStaticData(contentstackEntriesReferencesPath, contentstackEntriesOnlyKeys, data, {
|
|
323
478
|
variantAlias: variantAlias,
|
|
479
|
+
locale: locale,
|
|
480
|
+
}),
|
|
481
|
+
resolveQueryData(compositionEntry, data, {
|
|
482
|
+
variantAlias,
|
|
483
|
+
locale: locale || config.locale,
|
|
324
484
|
}),
|
|
325
485
|
]);
|
|
486
|
+
return data;
|
|
487
|
+
}
|
|
488
|
+
async function fetchSpec(queryOptions, options) {
|
|
489
|
+
if (!config.stackSdk) {
|
|
490
|
+
throw new ComposableStudioError(ComposableStudioErrors.STACK_SDK_NOT_AVAILABLE, getErrorMessage(ErrorMessageKey.STACK_SDK_NOT_AVAILABLE));
|
|
491
|
+
}
|
|
492
|
+
const { compositionEntry, symbolEntries } = await fetchComposition(config, queryOptions, options);
|
|
493
|
+
const data = await fetchData(compositionEntry, queryOptions, options);
|
|
494
|
+
const ui = typeof compositionEntry.ui === "string"
|
|
495
|
+
? decompressSpec(compositionEntry.ui)
|
|
496
|
+
: compositionEntry.ui;
|
|
326
497
|
return {
|
|
327
498
|
data,
|
|
328
499
|
ui,
|
|
329
500
|
compositionEntry,
|
|
330
|
-
//TODO: Need to fetch symbols from the server
|
|
331
501
|
symbols: symbolEntries,
|
|
332
502
|
config: getLightConfig(),
|
|
333
503
|
};
|
|
334
504
|
}
|
|
505
|
+
/**
|
|
506
|
+
* Reusable helper to fetch data from Contentstack
|
|
507
|
+
* Now uses QueryExecutor internally to support full QuerySpec with filters, sorting, etc.
|
|
508
|
+
*
|
|
509
|
+
* @param querySpec - Full QuerySpec with filters, sorting, pagination, etc.
|
|
510
|
+
* @param context - RuntimeContext for expression resolution (URL params, etc.)
|
|
511
|
+
* @returns Promise with data array and count
|
|
512
|
+
*/
|
|
513
|
+
async function fetchContentstackData(querySpec, context = {}, options) {
|
|
514
|
+
if (!config.stackSdk) {
|
|
515
|
+
console.warn("Stack SDK not available for fetchContentstackData");
|
|
516
|
+
return { entries: [], count: 0 };
|
|
517
|
+
}
|
|
518
|
+
try {
|
|
519
|
+
// Use QueryExecutor to execute the full QuerySpec
|
|
520
|
+
// This supports filters, sorting, pagination, includes, only, and expression resolution
|
|
521
|
+
const { QueryExecutor } = await import('../query/query-executor.js');
|
|
522
|
+
const executor = new QueryExecutor(config.stackSdk.sdk);
|
|
523
|
+
// Execute query using QueryExecutor
|
|
524
|
+
// ExpressionResolver inside QueryExecutor will resolve expressions at execution time
|
|
525
|
+
const result = await executor.execute(querySpec, 1, context, options);
|
|
526
|
+
const entries = result.entries || [];
|
|
527
|
+
const count = result.count || 0;
|
|
528
|
+
// Add editable tags (CSLP)
|
|
529
|
+
if (config.cslp.appendTags && Array.isArray(entries)) {
|
|
530
|
+
entries.forEach((entry) => {
|
|
531
|
+
addEditableTags(entry, querySpec.contentType, true, entry.locale || "en-us");
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
return {
|
|
535
|
+
entries: entries,
|
|
536
|
+
count: count,
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
catch (err) {
|
|
540
|
+
console.error(`[SDK] Failed to fetch data for ${querySpec.contentType}`, err);
|
|
541
|
+
return { entries: [], count: 0 };
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
async function resolveQueryData(compositionEntry, data, options) {
|
|
545
|
+
if (!compositionEntry.data_sources) {
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
let dataSources = [];
|
|
549
|
+
try {
|
|
550
|
+
const parsed = JSON.parse(compositionEntry.data_sources);
|
|
551
|
+
dataSources = Array.isArray(parsed) ? parsed : Object.values(parsed);
|
|
552
|
+
}
|
|
553
|
+
catch (e) {
|
|
554
|
+
console.warn("[SDK] Failed to parse data_sources from composition", e);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
const queriesDS = dataSources.find((ds) => ds.uid === "contentstack_queries" || ds.id === "contentstack_queries");
|
|
558
|
+
if (!queriesDS || !queriesDS.data) {
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
const queries = queriesDS.data || queriesDS.queries || [];
|
|
562
|
+
if (!Array.isArray(queries))
|
|
563
|
+
return;
|
|
564
|
+
const queryResults = {};
|
|
565
|
+
// Build RuntimeContext for expression resolution
|
|
566
|
+
// In SDK, use window.location.href for URL params
|
|
567
|
+
const urlParams = {};
|
|
568
|
+
if (typeof window !== "undefined" && window.location) {
|
|
569
|
+
const searchParams = new URLSearchParams(window.location.search);
|
|
570
|
+
searchParams.forEach((value, key) => {
|
|
571
|
+
urlParams[key] = value;
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
const context = {
|
|
575
|
+
url: {
|
|
576
|
+
params: urlParams,
|
|
577
|
+
path: {}, // Can be extended if path params are needed
|
|
578
|
+
hash: typeof window !== "undefined" ? window.location.hash : undefined,
|
|
579
|
+
},
|
|
580
|
+
// NO componentProps or user context - expressions only support URL params and time
|
|
581
|
+
};
|
|
582
|
+
await Promise.all(queries.map(async (queryConfig) => {
|
|
583
|
+
const { uid, spec } = queryConfig;
|
|
584
|
+
if (!uid || !spec?.contentType)
|
|
585
|
+
return;
|
|
586
|
+
// Use fetchContentstackData with full QuerySpec
|
|
587
|
+
// This now uses QueryExecutor internally to support filters, sorting, etc.
|
|
588
|
+
const result = await fetchContentstackData(spec, context, options);
|
|
589
|
+
queryResults[uid] = result;
|
|
590
|
+
}));
|
|
591
|
+
if (!data.dataSources[DATA_SOURCES.QUERY]) {
|
|
592
|
+
data.dataSources[DATA_SOURCES.QUERY] = {};
|
|
593
|
+
}
|
|
594
|
+
Object.assign(data.dataSources[DATA_SOURCES.QUERY], queryResults);
|
|
595
|
+
}
|
|
335
596
|
function getLightConfig() {
|
|
336
597
|
return pick(config, ["openInStudioButton", "contentTypeUid", "cslp"]);
|
|
337
598
|
}
|
|
@@ -347,6 +608,7 @@ function generateSdk(userConfig) {
|
|
|
347
608
|
// Once we fetch the data, we need to perform certain operation to store the UI and data.
|
|
348
609
|
// Hence, we moved this to the internal method.
|
|
349
610
|
fetchSpec,
|
|
611
|
+
fetchData,
|
|
350
612
|
fetchTemplateEntry,
|
|
351
613
|
getSeoMetadata,
|
|
352
614
|
},
|
|
@@ -361,7 +623,7 @@ function generateSdk(userConfig) {
|
|
|
361
623
|
*/
|
|
362
624
|
fetchCompositionData: async (queryOptions, options) => {
|
|
363
625
|
if (!config.stackSdk) {
|
|
364
|
-
throw new ComposableStudioError(ComposableStudioErrors.STACK_SDK_NOT_AVAILABLE,
|
|
626
|
+
throw new ComposableStudioError(ComposableStudioErrors.STACK_SDK_NOT_AVAILABLE, getErrorMessage(ErrorMessageKey.STACK_SDK_NOT_AVAILABLE));
|
|
365
627
|
}
|
|
366
628
|
const { searchQuery, ...compositionQueryInput } = queryOptions;
|
|
367
629
|
const compositionQuery = convertCompositionQueryInputToCompositionQuery(compositionQueryInput);
|
|
@@ -431,11 +693,11 @@ function generateSdk(userConfig) {
|
|
|
431
693
|
}
|
|
432
694
|
const parseUI = (ui) => {
|
|
433
695
|
try {
|
|
434
|
-
return
|
|
696
|
+
return decompressSpec(ui);
|
|
435
697
|
}
|
|
436
698
|
catch (error) {
|
|
437
|
-
|
|
438
|
-
|
|
699
|
+
console.error("UI parsing error", error);
|
|
700
|
+
throw new ComposableStudioError(ComposableStudioErrors.UI_PARSING_ERROR, getErrorMessage(ErrorMessageKey.UI_PARSING_ERROR), { ui, error });
|
|
439
701
|
}
|
|
440
702
|
};
|
|
441
703
|
function getAllSymbols(compositionEntry) {
|
|
@@ -454,90 +716,149 @@ function getAllSymbols(compositionEntry) {
|
|
|
454
716
|
}
|
|
455
717
|
/**
|
|
456
718
|
* This function fetches UI Composition from defined options.
|
|
719
|
+
* Supports custom fetcher overrides (per-query > global > SDK default).
|
|
457
720
|
**/
|
|
458
|
-
async function fetchComposition(config,
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
.
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
721
|
+
async function fetchComposition(config, queryOptions, compositionQueryOptions) {
|
|
722
|
+
const { fetchComposition: perQueryFetchComposition, fetchTemplateEntry: _, ...resolvedOptions } = compositionQueryOptions || {};
|
|
723
|
+
const locale = resolvedOptions.locale;
|
|
724
|
+
const defaultImpl = async () => {
|
|
725
|
+
if ("composableUid" in queryOptions) {
|
|
726
|
+
const uiQuery = config.stackSdk.where(config.stackSdk.ctQuery(config.contentTypeUid, {
|
|
727
|
+
locale: locale || config.locale,
|
|
728
|
+
}), "composable_uid", queryOptions.composableUid);
|
|
729
|
+
return await uiQuery
|
|
730
|
+
.find()
|
|
731
|
+
.then((res) => {
|
|
732
|
+
let response;
|
|
733
|
+
if (Array.isArray(res)) {
|
|
734
|
+
response = res[0][0];
|
|
735
|
+
}
|
|
736
|
+
else {
|
|
737
|
+
response = res.entries[0];
|
|
738
|
+
}
|
|
739
|
+
if (!response) {
|
|
740
|
+
throw new ComposableStudioError(ComposableStudioErrors.COMPOSITION_NOT_FOUND, getErrorMessage(ErrorMessageKey.COMPOSITION_NOT_FOUND_404), {
|
|
741
|
+
compositionUid: queryOptions.composableUid,
|
|
742
|
+
hasStackSdk: !!config.stackSdk,
|
|
743
|
+
options: queryOptions,
|
|
744
|
+
troubleshootingInfo: getTroubleshootingInfoForCompositionFetchError(ComposableStudioErrors.COMPOSITION_NOT_FOUND, false),
|
|
745
|
+
});
|
|
746
|
+
}
|
|
747
|
+
return response;
|
|
748
|
+
})
|
|
749
|
+
.catch((err) => {
|
|
750
|
+
if (err instanceof ComposableStudioError)
|
|
751
|
+
throw err;
|
|
752
|
+
const { errorType, message } = classifyApiError(err);
|
|
753
|
+
console.error("SDK Error:", {
|
|
754
|
+
errorType,
|
|
755
|
+
message,
|
|
756
|
+
originalError: err,
|
|
757
|
+
});
|
|
758
|
+
throw new ComposableStudioError(errorType, message, {
|
|
759
|
+
compositionUid: queryOptions.composableUid,
|
|
760
|
+
hasStackSdk: !!config.stackSdk,
|
|
761
|
+
statusCode: err?.status || err?.statusCode || "none",
|
|
762
|
+
errorCode: err?.error_code || "none",
|
|
763
|
+
errorMessage: err?.error_message || err?.message || "none",
|
|
764
|
+
troubleshootingInfo: getTroubleshootingInfoForCompositionFetchError(errorType, false),
|
|
476
765
|
});
|
|
477
|
-
}
|
|
478
|
-
return response;
|
|
479
|
-
})
|
|
480
|
-
.catch((err) => {
|
|
481
|
-
console.error("Could not fetch Composition 2", err);
|
|
482
|
-
throw new ComposableStudioError(ComposableStudioErrors.COMPOSITION_NOT_FOUND, "Composition not found", // TODO: Add more details - Add potential solution to check if the user changed the content type.
|
|
483
|
-
{
|
|
484
|
-
options,
|
|
485
|
-
response: err,
|
|
486
766
|
});
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
767
|
+
}
|
|
768
|
+
else if ("url" in queryOptions) {
|
|
769
|
+
const possibleEntryUrlPatterns = getUrlPatterns(queryOptions.url);
|
|
770
|
+
return await config.stackSdk
|
|
771
|
+
.ctQuery(config.contentTypeUid, {
|
|
772
|
+
includeAll: true,
|
|
773
|
+
includeAllDepth: INCLUDE_DEPTH_COUNT,
|
|
774
|
+
locale: locale || config.locale,
|
|
775
|
+
query: {
|
|
776
|
+
url: {
|
|
777
|
+
$in: possibleEntryUrlPatterns,
|
|
778
|
+
},
|
|
779
|
+
$or: [
|
|
780
|
+
{ place_composition_as: "page" },
|
|
781
|
+
{ place_composition_as: "" },
|
|
782
|
+
{ place_composition_as: null },
|
|
783
|
+
{ place_composition_as: undefined },
|
|
784
|
+
{ place_composition_as: { $exists: false } },
|
|
785
|
+
{ connected_content_type: { $exists: true } },
|
|
786
|
+
],
|
|
502
787
|
},
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
788
|
+
})
|
|
789
|
+
.find()
|
|
790
|
+
.then((res) => {
|
|
791
|
+
const entries = entriesFromResponse(res);
|
|
792
|
+
const entry = getCompositionFromSlug(queryOptions.url, entries);
|
|
793
|
+
if (!entry) {
|
|
794
|
+
throw new ComposableStudioError(ComposableStudioErrors.COMPOSITION_NOT_FOUND_BY_URL, getErrorMessage(ErrorMessageKey.COMPOSITION_NOT_FOUND_BY_URL, {
|
|
795
|
+
url: queryOptions.url,
|
|
796
|
+
}), {
|
|
797
|
+
url: queryOptions.url,
|
|
798
|
+
hasStackSdk: !!config.stackSdk,
|
|
799
|
+
searchedPatterns: possibleEntryUrlPatterns,
|
|
800
|
+
entriesFound: entries.length,
|
|
801
|
+
options: queryOptions,
|
|
802
|
+
troubleshootingInfo: getTroubleshootingInfoForCompositionFetchError(ComposableStudioErrors.COMPOSITION_NOT_FOUND_BY_URL, true),
|
|
803
|
+
});
|
|
804
|
+
}
|
|
805
|
+
return entry;
|
|
806
|
+
})
|
|
807
|
+
.catch((err) => {
|
|
808
|
+
if (err instanceof ComposableStudioError)
|
|
809
|
+
throw err;
|
|
810
|
+
const { errorType, message } = classifyApiError(err);
|
|
811
|
+
console.error("SDK Error (URL-based fetch):", {
|
|
812
|
+
errorType,
|
|
813
|
+
message,
|
|
814
|
+
originalError: err,
|
|
522
815
|
});
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
816
|
+
throw new ComposableStudioError(errorType, message, {
|
|
817
|
+
url: queryOptions.url,
|
|
818
|
+
hasStackSdk: !!config.stackSdk,
|
|
819
|
+
statusCode: err?.status || err?.statusCode || "none",
|
|
820
|
+
errorCode: err?.error_code || "none",
|
|
821
|
+
errorMessage: err?.error_message || err?.message || "none",
|
|
822
|
+
troubleshootingInfo: getTroubleshootingInfoForCompositionFetchError(errorType, true),
|
|
823
|
+
});
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
else {
|
|
827
|
+
throw new ComposableStudioError(ComposableStudioErrors.MISSING_COMPOSITION_IDENTIFIER, getErrorMessage(ErrorMessageKey.MISSING_COMPOSITION_IDENTIFIER), {
|
|
828
|
+
options: queryOptions,
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
const customFetcher = perQueryFetchComposition ?? config.fetchComposition;
|
|
833
|
+
if (customFetcher) {
|
|
834
|
+
const entry = await customFetcher({
|
|
835
|
+
compositionQuery: queryOptions,
|
|
836
|
+
contentTypeUid: config.contentTypeUid,
|
|
837
|
+
options: resolvedOptions,
|
|
838
|
+
stackSdk: config.stackSdk.sdk,
|
|
839
|
+
defaultFetch: defaultImpl,
|
|
538
840
|
});
|
|
841
|
+
if (!entry) {
|
|
842
|
+
const isUrlBased = "url" in queryOptions;
|
|
843
|
+
throw new ComposableStudioError(isUrlBased
|
|
844
|
+
? ComposableStudioErrors.COMPOSITION_NOT_FOUND_BY_URL
|
|
845
|
+
: ComposableStudioErrors.COMPOSITION_NOT_FOUND, isUrlBased
|
|
846
|
+
? getErrorMessage(ErrorMessageKey.COMPOSITION_NOT_FOUND_BY_URL, {
|
|
847
|
+
url: queryOptions.url,
|
|
848
|
+
})
|
|
849
|
+
: getErrorMessage(ErrorMessageKey.COMPOSITION_NOT_FOUND_404));
|
|
850
|
+
}
|
|
851
|
+
return {
|
|
852
|
+
compositionEntry: entry,
|
|
853
|
+
symbolEntries: getAllSymbols(entry),
|
|
854
|
+
};
|
|
539
855
|
}
|
|
856
|
+
const entry = await defaultImpl();
|
|
857
|
+
return {
|
|
858
|
+
compositionEntry: entry,
|
|
859
|
+
symbolEntries: getAllSymbols(entry),
|
|
860
|
+
};
|
|
540
861
|
}
|
|
541
862
|
|
|
542
|
-
export { generateSdk, parseUI };
|
|
863
|
+
export { classifyApiError, generateSdk, parseUI };
|
|
543
864
|
//# sourceMappingURL=sdk.js.map
|