@sitecore-content-sdk/content 1.5.0-canary.5
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/LICENSE.txt +202 -0
- package/README.md +7 -0
- package/client.d.ts +1 -0
- package/codegen.d.ts +1 -0
- package/config-cli.d.ts +1 -0
- package/config.d.ts +1 -0
- package/dist/cjs/client/edge-proxy.js +24 -0
- package/dist/cjs/client/index.js +14 -0
- package/dist/cjs/client/models.js +2 -0
- package/dist/cjs/client/sitecore-client.js +420 -0
- package/dist/cjs/client/utils.js +53 -0
- package/dist/cjs/config/define-config.js +195 -0
- package/dist/cjs/config/index.js +7 -0
- package/dist/cjs/config/models.js +12 -0
- package/dist/cjs/config-cli/define-cli-config.js +23 -0
- package/dist/cjs/config-cli/index.js +7 -0
- package/dist/cjs/config-cli/models.js +8 -0
- package/dist/cjs/constants.js +12 -0
- package/dist/cjs/debug.js +21 -0
- package/dist/cjs/editing/codegen/index.js +14 -0
- package/dist/cjs/editing/codegen/preview.js +277 -0
- package/dist/cjs/editing/component-layout-service.js +62 -0
- package/dist/cjs/editing/design-library.js +184 -0
- package/dist/cjs/editing/editing-service.js +81 -0
- package/dist/cjs/editing/index.js +33 -0
- package/dist/cjs/editing/models.js +44 -0
- package/dist/cjs/editing/utils.js +105 -0
- package/dist/cjs/form/form.js +81 -0
- package/dist/cjs/form/index.js +7 -0
- package/dist/cjs/i18n/dictionary-service.js +144 -0
- package/dist/cjs/i18n/index.js +7 -0
- package/dist/cjs/i18n/utils.js +16 -0
- package/dist/cjs/index.js +47 -0
- package/dist/cjs/layout/content-styles.js +73 -0
- package/dist/cjs/layout/index.js +24 -0
- package/dist/cjs/layout/layout-service.js +68 -0
- package/dist/cjs/layout/models.js +39 -0
- package/dist/cjs/layout/themes.js +77 -0
- package/dist/cjs/layout/utils.js +117 -0
- package/dist/cjs/media/index.js +38 -0
- package/dist/cjs/media/media-api.js +100 -0
- package/dist/cjs/models.js +2 -0
- package/dist/cjs/personalize/index.js +15 -0
- package/dist/cjs/personalize/layout-personalizer.js +98 -0
- package/dist/cjs/personalize/personalize-service.js +109 -0
- package/dist/cjs/personalize/utils.js +143 -0
- package/dist/cjs/site/error-pages-service.js +82 -0
- package/dist/cjs/site/index.js +26 -0
- package/dist/cjs/site/models.js +2 -0
- package/dist/cjs/site/redirects-service.js +109 -0
- package/dist/cjs/site/robots-service.js +74 -0
- package/dist/cjs/site/site-resolver.js +73 -0
- package/dist/cjs/site/siteinfo-service.js +94 -0
- package/dist/cjs/site/sitemap-xml-service.js +92 -0
- package/dist/cjs/site/sitepath-service.js +201 -0
- package/dist/cjs/site/utils.js +55 -0
- package/dist/cjs/sitecore-service-base.js +33 -0
- package/dist/cjs/tools/codegen/component-generation.js +49 -0
- package/dist/cjs/tools/codegen/extract-files.js +105 -0
- package/dist/cjs/tools/codegen/import-map.js +411 -0
- package/dist/cjs/tools/codegen/utils.js +418 -0
- package/dist/cjs/tools/generate-map.js +2 -0
- package/dist/cjs/tools/generateSites.js +59 -0
- package/dist/cjs/tools/index.js +30 -0
- package/dist/cjs/tools/scaffold.js +62 -0
- package/dist/cjs/tools/templating/components.js +96 -0
- package/dist/cjs/tools/templating/index.js +6 -0
- package/dist/esm/client/edge-proxy.js +19 -0
- package/dist/esm/client/index.js +4 -0
- package/dist/esm/client/models.js +1 -0
- package/dist/esm/client/sitecore-client.js +416 -0
- package/dist/esm/client/utils.js +49 -0
- package/dist/esm/config/define-config.js +189 -0
- package/dist/esm/config/index.js +2 -0
- package/dist/esm/config/models.js +9 -0
- package/dist/esm/config-cli/define-cli-config.js +19 -0
- package/dist/esm/config-cli/index.js +2 -0
- package/dist/esm/config-cli/models.js +5 -0
- package/dist/esm/constants.js +9 -0
- package/dist/esm/debug.js +19 -0
- package/dist/esm/editing/codegen/index.js +1 -0
- package/dist/esm/editing/codegen/preview.js +263 -0
- package/dist/esm/editing/component-layout-service.js +55 -0
- package/dist/esm/editing/design-library.js +172 -0
- package/dist/esm/editing/editing-service.js +74 -0
- package/dist/esm/editing/index.js +6 -0
- package/dist/esm/editing/models.js +41 -0
- package/dist/esm/editing/utils.js +98 -0
- package/dist/esm/form/form.js +72 -0
- package/dist/esm/form/index.js +1 -0
- package/dist/esm/i18n/dictionary-service.js +137 -0
- package/dist/esm/i18n/index.js +2 -0
- package/dist/esm/i18n/utils.js +13 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/layout/content-styles.js +65 -0
- package/dist/esm/layout/index.js +6 -0
- package/dist/esm/layout/layout-service.js +61 -0
- package/dist/esm/layout/models.js +36 -0
- package/dist/esm/layout/themes.js +72 -0
- package/dist/esm/layout/utils.js +109 -0
- package/dist/esm/media/index.js +2 -0
- package/dist/esm/media/media-api.js +90 -0
- package/dist/esm/models.js +1 -0
- package/dist/esm/personalize/index.js +3 -0
- package/dist/esm/personalize/layout-personalizer.js +93 -0
- package/dist/esm/personalize/personalize-service.js +102 -0
- package/dist/esm/personalize/utils.js +135 -0
- package/dist/esm/site/error-pages-service.js +75 -0
- package/dist/esm/site/index.js +8 -0
- package/dist/esm/site/models.js +1 -0
- package/dist/esm/site/redirects-service.js +102 -0
- package/dist/esm/site/robots-service.js +67 -0
- package/dist/esm/site/site-resolver.js +69 -0
- package/dist/esm/site/siteinfo-service.js +87 -0
- package/dist/esm/site/sitemap-xml-service.js +85 -0
- package/dist/esm/site/sitepath-service.js +193 -0
- package/dist/esm/site/utils.js +49 -0
- package/dist/esm/sitecore-service-base.js +29 -0
- package/dist/esm/tools/codegen/component-generation.js +44 -0
- package/dist/esm/tools/codegen/extract-files.js +99 -0
- package/dist/esm/tools/codegen/import-map.js +368 -0
- package/dist/esm/tools/codegen/utils.js +373 -0
- package/dist/esm/tools/generate-map.js +1 -0
- package/dist/esm/tools/generateSites.js +52 -0
- package/dist/esm/tools/index.js +6 -0
- package/dist/esm/tools/scaffold.js +54 -0
- package/dist/esm/tools/templating/components.js +59 -0
- package/dist/esm/tools/templating/index.js +1 -0
- package/editing.d.ts +1 -0
- package/i18n.d.ts +1 -0
- package/layout.d.ts +1 -0
- package/media.d.ts +1 -0
- package/package.json +157 -0
- package/personalize.d.ts +1 -0
- package/site.d.ts +1 -0
- package/tools.d.ts +1 -0
- package/types/client/edge-proxy.d.ts +17 -0
- package/types/client/edge-proxy.d.ts.map +1 -0
- package/types/client/index.d.ts +7 -0
- package/types/client/index.d.ts.map +1 -0
- package/types/client/models.d.ts +21 -0
- package/types/client/models.d.ts.map +1 -0
- package/types/client/sitecore-client.d.ts +338 -0
- package/types/client/sitecore-client.d.ts.map +1 -0
- package/types/client/utils.d.ts +15 -0
- package/types/client/utils.d.ts.map +1 -0
- package/types/config/define-config.d.ts +20 -0
- package/types/config/define-config.d.ts.map +1 -0
- package/types/config/index.d.ts +3 -0
- package/types/config/index.d.ts.map +1 -0
- package/types/config/models.d.ts +287 -0
- package/types/config/models.d.ts.map +1 -0
- package/types/config-cli/define-cli-config.d.ts +9 -0
- package/types/config-cli/define-cli-config.d.ts.map +1 -0
- package/types/config-cli/index.d.ts +3 -0
- package/types/config-cli/index.d.ts.map +1 -0
- package/types/config-cli/models.d.ts +6 -0
- package/types/config-cli/models.d.ts.map +1 -0
- package/types/constants.d.ts +10 -0
- package/types/constants.d.ts.map +1 -0
- package/types/debug.d.ts +19 -0
- package/types/debug.d.ts.map +1 -0
- package/types/editing/codegen/index.d.ts +2 -0
- package/types/editing/codegen/index.d.ts.map +1 -0
- package/types/editing/codegen/preview.d.ts +256 -0
- package/types/editing/codegen/preview.d.ts.map +1 -0
- package/types/editing/component-layout-service.d.ts +84 -0
- package/types/editing/component-layout-service.d.ts.map +1 -0
- package/types/editing/design-library.d.ts +111 -0
- package/types/editing/design-library.d.ts.map +1 -0
- package/types/editing/editing-service.d.ts +71 -0
- package/types/editing/editing-service.d.ts.map +1 -0
- package/types/editing/index.d.ts +7 -0
- package/types/editing/index.d.ts.map +1 -0
- package/types/editing/models.d.ts +103 -0
- package/types/editing/models.d.ts.map +1 -0
- package/types/editing/utils.d.ts +82 -0
- package/types/editing/utils.d.ts.map +1 -0
- package/types/form/form.d.ts +25 -0
- package/types/form/form.d.ts.map +1 -0
- package/types/form/index.d.ts +2 -0
- package/types/form/index.d.ts.map +1 -0
- package/types/i18n/dictionary-service.d.ts +133 -0
- package/types/i18n/dictionary-service.d.ts.map +1 -0
- package/types/i18n/index.d.ts +3 -0
- package/types/i18n/index.d.ts.map +1 -0
- package/types/i18n/utils.d.ts +9 -0
- package/types/i18n/utils.d.ts.map +1 -0
- package/types/index.d.ts +7 -0
- package/types/index.d.ts.map +1 -0
- package/types/layout/content-styles.d.ts +20 -0
- package/types/layout/content-styles.d.ts.map +1 -0
- package/types/layout/index.d.ts +6 -0
- package/types/layout/index.d.ts.map +1 -0
- package/types/layout/layout-service.d.ts +45 -0
- package/types/layout/layout-service.d.ts.map +1 -0
- package/types/layout/models.d.ts +174 -0
- package/types/layout/models.d.ts.map +1 -0
- package/types/layout/themes.d.ts +13 -0
- package/types/layout/themes.d.ts.map +1 -0
- package/types/layout/utils.d.ts +56 -0
- package/types/layout/utils.d.ts.map +1 -0
- package/types/media/index.d.ts +3 -0
- package/types/media/index.d.ts.map +1 -0
- package/types/media/media-api.d.ts +60 -0
- package/types/media/media-api.d.ts.map +1 -0
- package/types/models.d.ts +32 -0
- package/types/models.d.ts.map +1 -0
- package/types/personalize/index.d.ts +4 -0
- package/types/personalize/index.d.ts.map +1 -0
- package/types/personalize/layout-personalizer.d.ts +29 -0
- package/types/personalize/layout-personalizer.d.ts.map +1 -0
- package/types/personalize/personalize-service.d.ts +89 -0
- package/types/personalize/personalize-service.d.ts.map +1 -0
- package/types/personalize/utils.d.ts +78 -0
- package/types/personalize/utils.d.ts.map +1 -0
- package/types/site/error-pages-service.d.ts +64 -0
- package/types/site/error-pages-service.d.ts.map +1 -0
- package/types/site/index.d.ts +10 -0
- package/types/site/index.d.ts.map +1 -0
- package/types/site/models.d.ts +23 -0
- package/types/site/models.d.ts.map +1 -0
- package/types/site/redirects-service.d.ts +91 -0
- package/types/site/redirects-service.d.ts.map +1 -0
- package/types/site/robots-service.d.ts +57 -0
- package/types/site/robots-service.d.ts.map +1 -0
- package/types/site/site-resolver.d.ts +28 -0
- package/types/site/site-resolver.d.ts.map +1 -0
- package/types/site/siteinfo-service.d.ts +64 -0
- package/types/site/siteinfo-service.d.ts.map +1 -0
- package/types/site/sitemap-xml-service.d.ts +63 -0
- package/types/site/sitemap-xml-service.d.ts.map +1 -0
- package/types/site/sitepath-service.d.ts +137 -0
- package/types/site/sitepath-service.d.ts.map +1 -0
- package/types/site/utils.d.ts +41 -0
- package/types/site/utils.d.ts.map +1 -0
- package/types/sitecore-service-base.d.ts +31 -0
- package/types/sitecore-service-base.d.ts.map +1 -0
- package/types/tools/codegen/component-generation.d.ts +50 -0
- package/types/tools/codegen/component-generation.d.ts.map +1 -0
- package/types/tools/codegen/extract-files.d.ts +24 -0
- package/types/tools/codegen/extract-files.d.ts.map +1 -0
- package/types/tools/codegen/import-map.d.ts +103 -0
- package/types/tools/codegen/import-map.d.ts.map +1 -0
- package/types/tools/codegen/utils.d.ts +76 -0
- package/types/tools/codegen/utils.d.ts.map +1 -0
- package/types/tools/generate-map.d.ts +36 -0
- package/types/tools/generate-map.d.ts.map +1 -0
- package/types/tools/generateSites.d.ts +25 -0
- package/types/tools/generateSites.d.ts.map +1 -0
- package/types/tools/index.d.ts +8 -0
- package/types/tools/index.d.ts.map +1 -0
- package/types/tools/scaffold.d.ts +27 -0
- package/types/tools/scaffold.d.ts.map +1 -0
- package/types/tools/templating/components.d.ts +104 -0
- package/types/tools/templating/components.d.ts.map +1 -0
- package/types/tools/templating/index.d.ts +2 -0
- package/types/tools/templating/index.d.ts.map +1 -0
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SitecoreClient = exports.ErrorPage = void 0;
|
|
4
|
+
const core_1 = require("@sitecore-content-sdk/core");
|
|
5
|
+
const i18n_1 = require("../i18n");
|
|
6
|
+
const layout_1 = require("../layout");
|
|
7
|
+
const utils_1 = require("../personalize/utils");
|
|
8
|
+
const layout_personalizer_1 = require("../personalize/layout-personalizer");
|
|
9
|
+
const site_1 = require("../site");
|
|
10
|
+
const utils_2 = require("./utils");
|
|
11
|
+
const robots_service_1 = require("../site/robots-service");
|
|
12
|
+
const models_1 = require("../editing/models");
|
|
13
|
+
const editing_1 = require("../editing");
|
|
14
|
+
/**
|
|
15
|
+
* Error page codes
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
var ErrorPage;
|
|
19
|
+
(function (ErrorPage) {
|
|
20
|
+
ErrorPage["NotFound"] = "404";
|
|
21
|
+
ErrorPage["InternalServerError"] = "500";
|
|
22
|
+
})(ErrorPage || (exports.ErrorPage = ErrorPage = {}));
|
|
23
|
+
/**
|
|
24
|
+
* This is a generic content client that can be used by any framework.
|
|
25
|
+
* Use it to retrieve pages, preview data, dictionary and other data
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
class SitecoreClient {
|
|
29
|
+
/**
|
|
30
|
+
* Init SitecoreClient
|
|
31
|
+
* @param {SitecoreClientInit} initOptions initOptions for the client, containing site and Sitecore connection details
|
|
32
|
+
*/
|
|
33
|
+
constructor(initOptions) {
|
|
34
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
35
|
+
this.initOptions = initOptions;
|
|
36
|
+
this.clientFactory = this.getClientFactory();
|
|
37
|
+
this.graphQLClient = this.clientFactory({
|
|
38
|
+
debugger: core_1.debug.http,
|
|
39
|
+
});
|
|
40
|
+
const baseServiceOptions = this.getBaseServiceOptions();
|
|
41
|
+
this.layoutService =
|
|
42
|
+
(_b = (_a = initOptions.custom) === null || _a === void 0 ? void 0 : _a.layoutService) !== null && _b !== void 0 ? _b : this.getLayoutService(baseServiceOptions);
|
|
43
|
+
this.dictionaryService =
|
|
44
|
+
(_d = (_c = initOptions.custom) === null || _c === void 0 ? void 0 : _c.dictionaryService) !== null && _d !== void 0 ? _d : this.getDictionaryService(baseServiceOptions);
|
|
45
|
+
this.editingService = (_f = (_e = initOptions.custom) === null || _e === void 0 ? void 0 : _e.editingService) !== null && _f !== void 0 ? _f : this.getEditingService();
|
|
46
|
+
this.errorPagesService = (_h = (_g = initOptions.custom) === null || _g === void 0 ? void 0 : _g.errorPagesService) !== null && _h !== void 0 ? _h : this.getErrorPagesService();
|
|
47
|
+
this.sitePathService = (_k = (_j = initOptions.custom) === null || _j === void 0 ? void 0 : _j.sitePathService) !== null && _k !== void 0 ? _k : this.getSitePathService();
|
|
48
|
+
this.componentService = this.getComponentService();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Normalize path regardless of type
|
|
52
|
+
* @param {string | string[]} path string or string array path
|
|
53
|
+
* @returns {string} string path
|
|
54
|
+
*/
|
|
55
|
+
parsePath(path) {
|
|
56
|
+
// join array path, while clearing extra slashes and ensure first slash
|
|
57
|
+
return typeof path === 'string'
|
|
58
|
+
? path.startsWith('/')
|
|
59
|
+
? path
|
|
60
|
+
: `/${path}`
|
|
61
|
+
: `/${path
|
|
62
|
+
.filter((part) => part !== '/')
|
|
63
|
+
.map((part) => part.replace(/^\/+/, '').replace(/\/+$/, ''))
|
|
64
|
+
.join('/')}`;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Execute a raw GraphQL request using the client's configured GraphQL Edge endpoint.
|
|
68
|
+
* This is a thin pass-through to the underlying `GraphQLClient.request` method,
|
|
69
|
+
* @param {string | DocumentNode} query GraphQL query
|
|
70
|
+
* @param {Record<string, unknown>} [variables] Optional variables bag
|
|
71
|
+
* @param {FetchOptions} [fetchOptions] Optional fetch overrides (e.g. fetch, headers)
|
|
72
|
+
*/
|
|
73
|
+
getData(query, variables, fetchOptions) {
|
|
74
|
+
return this.graphQLClient.request(query, variables, fetchOptions);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get page details for a route, with layout and other details
|
|
78
|
+
* @param {string} path route path
|
|
79
|
+
* @param {PageOptions} [pageOptions] site, language and personalization variant details for route
|
|
80
|
+
* @param {FetchOptions} [fetchOptions] Additional fetch fetch options to override GraphQL requests
|
|
81
|
+
* @returns {Page | null} page details
|
|
82
|
+
*/
|
|
83
|
+
async getPage(path, pageOptions, fetchOptions) {
|
|
84
|
+
var _a, _b, _c, _d;
|
|
85
|
+
const computedPath = this.parsePath(path);
|
|
86
|
+
const locale = (_a = pageOptions === null || pageOptions === void 0 ? void 0 : pageOptions.locale) !== null && _a !== void 0 ? _a : this.initOptions.defaultLanguage;
|
|
87
|
+
const site = (_b = pageOptions === null || pageOptions === void 0 ? void 0 : pageOptions.site) !== null && _b !== void 0 ? _b : this.initOptions.defaultSite;
|
|
88
|
+
// Fetch layout data, passing on req/res for SSR
|
|
89
|
+
const layout = await this.layoutService.fetchLayoutData(computedPath, {
|
|
90
|
+
locale,
|
|
91
|
+
site,
|
|
92
|
+
}, fetchOptions);
|
|
93
|
+
if (!layout.sitecore.route) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
// Initialize links to be inserted on the page
|
|
98
|
+
if ((_c = pageOptions === null || pageOptions === void 0 ? void 0 : pageOptions.personalize) === null || _c === void 0 ? void 0 : _c.variantId) {
|
|
99
|
+
// Modify layoutData to use specific variant(s) instead of default
|
|
100
|
+
// This will also set the variantId on the Sitecore context so that it is accessible here
|
|
101
|
+
(0, layout_personalizer_1.personalizeLayout)(layout, pageOptions.personalize.variantId, pageOptions.personalize.componentVariantIds);
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
layout,
|
|
105
|
+
siteName: ((_d = layout.sitecore.context.site) === null || _d === void 0 ? void 0 : _d.name) || site,
|
|
106
|
+
locale,
|
|
107
|
+
mode: this.getPageMode(layout_1.LayoutServicePageState.Normal),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Retrieves the head `<link>` elements for Sitecore styles and themes.
|
|
113
|
+
* @param {LayoutServiceData} layoutData - The layout data containing styles and themes.
|
|
114
|
+
* @param {object} [options] - Optional configuration for enabling styles and themes.
|
|
115
|
+
* @param {boolean} [options.enableStyles] - Whether to include content styles.
|
|
116
|
+
* @param {boolean} [options.enableThemes] - Whether to include theme styles.
|
|
117
|
+
* @returns {HTMLLink[]} An array of `<link>` elements for stylesheets.
|
|
118
|
+
*/
|
|
119
|
+
getHeadLinks(layoutData, options = {}) {
|
|
120
|
+
const { enableStyles = true, enableThemes = true } = options;
|
|
121
|
+
const { contextId: serverContextId, clientContextId, edgeUrl } = this.initOptions.api.edge;
|
|
122
|
+
const headLinks = [];
|
|
123
|
+
const contextId = serverContextId || clientContextId;
|
|
124
|
+
if (enableStyles) {
|
|
125
|
+
const contentStyles = (0, layout_1.getContentStylesheetLink)(layoutData, contextId, edgeUrl);
|
|
126
|
+
if (contentStyles)
|
|
127
|
+
headLinks.push(contentStyles);
|
|
128
|
+
}
|
|
129
|
+
if (enableThemes) {
|
|
130
|
+
headLinks.push(...(0, layout_1.getDesignLibraryStylesheetLinks)(layoutData, contextId, edgeUrl));
|
|
131
|
+
}
|
|
132
|
+
return headLinks;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Retrieves dictionary phrases for a given site and locale.
|
|
136
|
+
* @param {RouteOptions} routeOptions - Route options containing language and site name to load dictionary for
|
|
137
|
+
* @param {FetchOptions} [fetchOptions] Additional fetch fetch options to override GraphQL requests
|
|
138
|
+
* @returns {DictionaryPhrases} A promise that resolves to the dictionary phrases.
|
|
139
|
+
*/
|
|
140
|
+
async getDictionary(routeOptions, fetchOptions) {
|
|
141
|
+
const locale = (routeOptions === null || routeOptions === void 0 ? void 0 : routeOptions.locale) || this.initOptions.defaultLanguage;
|
|
142
|
+
const site = (routeOptions === null || routeOptions === void 0 ? void 0 : routeOptions.site) || this.initOptions.defaultSite;
|
|
143
|
+
return await this.dictionaryService.fetchDictionaryData(locale, site, fetchOptions);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Retrieves error pages for a given site and locale.
|
|
147
|
+
* @param {RouteOptions} routeOptions - Route options containing language and site name to load error pages
|
|
148
|
+
* @param {FetchOptions} [fetchOptions] Additional fetch fetch options to override GraphQL requests
|
|
149
|
+
* @returns {ErrorPages | null} A promise that resolves to the error pages or null if not found.
|
|
150
|
+
*/
|
|
151
|
+
async getErrorPages(routeOptions, fetchOptions) {
|
|
152
|
+
const locale = (routeOptions === null || routeOptions === void 0 ? void 0 : routeOptions.locale) || this.initOptions.defaultLanguage;
|
|
153
|
+
const site = (routeOptions === null || routeOptions === void 0 ? void 0 : routeOptions.site) || this.initOptions.defaultSite;
|
|
154
|
+
return await this.errorPagesService.fetchErrorPages(site, locale, fetchOptions);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Retrieves preview page and layout details
|
|
158
|
+
* @param {EditingPreviewData | undefined} previewData - The editing preview data for metadata mode.
|
|
159
|
+
* @param {FetchOptions} [fetchOptions] Additional fetch fetch options to override GraphQL requests
|
|
160
|
+
* @returns {Page} preview page details
|
|
161
|
+
*/
|
|
162
|
+
async getPreview(previewData, fetchOptions) {
|
|
163
|
+
var _a;
|
|
164
|
+
if (!previewData) {
|
|
165
|
+
console.error('Preview data missing');
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
// If we're in Pages preview (editing) mode, prefetch the editing data
|
|
169
|
+
const { site, itemId, language, version, layoutKind, mode } = previewData;
|
|
170
|
+
const variantIds = Array.isArray(previewData.variantIds)
|
|
171
|
+
? previewData.variantIds
|
|
172
|
+
: previewData.variantIds.split(',');
|
|
173
|
+
const data = await this.editingService.fetchEditingData({
|
|
174
|
+
itemId,
|
|
175
|
+
language,
|
|
176
|
+
version,
|
|
177
|
+
layoutKind,
|
|
178
|
+
mode,
|
|
179
|
+
}, fetchOptions);
|
|
180
|
+
if (!data) {
|
|
181
|
+
throw new Error(`Unable to fetch editing data for preview ${JSON.stringify(previewData)}`);
|
|
182
|
+
}
|
|
183
|
+
const page = {
|
|
184
|
+
locale: language,
|
|
185
|
+
layout: data.layoutData,
|
|
186
|
+
siteName: ((_a = data.layoutData.sitecore.context.site) === null || _a === void 0 ? void 0 : _a.name) || site,
|
|
187
|
+
mode: this.getPageMode(mode),
|
|
188
|
+
};
|
|
189
|
+
const personalizeData = (0, utils_1.getGroomedVariantIds)(variantIds);
|
|
190
|
+
(0, layout_personalizer_1.personalizeLayout)(page.layout, personalizeData.variantId, personalizeData.componentVariantIds);
|
|
191
|
+
return page;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Get design library page details for Design Library mode of your app
|
|
195
|
+
* @param {DesignLibraryRenderPreviewData} designLibData preview data set in 'library' mode of the app
|
|
196
|
+
* @param {FetchOptions} [fetchOptions] Additional fetch fetch options to override GraphQL requests
|
|
197
|
+
* @returns {Page} preview page for Design Library
|
|
198
|
+
*/
|
|
199
|
+
async getDesignLibraryData(designLibData, fetchOptions) {
|
|
200
|
+
var _a;
|
|
201
|
+
if (!this.initOptions.api.local) {
|
|
202
|
+
throw new Error('Component Library requires Sitecore apiHost and apiKey to be provided');
|
|
203
|
+
}
|
|
204
|
+
const { itemId, componentUid, site, language, renderingId, dataSourceId, version, mode, generation, } = designLibData;
|
|
205
|
+
const componentData = await this.componentService.fetchComponentData(Object.assign({ siteName: site, itemId,
|
|
206
|
+
language,
|
|
207
|
+
componentUid,
|
|
208
|
+
renderingId,
|
|
209
|
+
dataSourceId,
|
|
210
|
+
version,
|
|
211
|
+
mode }, (generation ? { generation } : {})), fetchOptions);
|
|
212
|
+
if (!componentData) {
|
|
213
|
+
throw new Error(`Unable to fetch editing data for preview ${JSON.stringify(designLibData)}`);
|
|
214
|
+
}
|
|
215
|
+
const page = {
|
|
216
|
+
locale: designLibData.language,
|
|
217
|
+
layout: componentData,
|
|
218
|
+
siteName: ((_a = componentData.sitecore.context.site) === null || _a === void 0 ? void 0 : _a.name) || site,
|
|
219
|
+
mode: this.getPageMode(mode, generation),
|
|
220
|
+
};
|
|
221
|
+
return page;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Get error page details for a given error code
|
|
225
|
+
* @param {ErrorPage} code - The error code to get the error page for
|
|
226
|
+
* @param {Partial<RouteOptions>} pageOptions - The page options to get the error page for
|
|
227
|
+
* @param {FetchOptions} [fetchOptions] - Additional fetch fetch options to override GraphQL requests
|
|
228
|
+
* @returns {Promise<Page | null>} A promise that resolves to the error page details or null if not found
|
|
229
|
+
*/
|
|
230
|
+
async getErrorPage(code, pageOptions, fetchOptions) {
|
|
231
|
+
var _a, _b;
|
|
232
|
+
const locale = (pageOptions === null || pageOptions === void 0 ? void 0 : pageOptions.locale) || this.initOptions.defaultLanguage;
|
|
233
|
+
const site = (pageOptions === null || pageOptions === void 0 ? void 0 : pageOptions.site) || this.initOptions.defaultSite;
|
|
234
|
+
const result = await this.getErrorPages({
|
|
235
|
+
site,
|
|
236
|
+
locale,
|
|
237
|
+
}, fetchOptions);
|
|
238
|
+
let layout = null;
|
|
239
|
+
switch (code) {
|
|
240
|
+
case ErrorPage.NotFound:
|
|
241
|
+
layout = ((_a = result === null || result === void 0 ? void 0 : result.notFoundPage) === null || _a === void 0 ? void 0 : _a.rendered) || null;
|
|
242
|
+
break;
|
|
243
|
+
case ErrorPage.InternalServerError:
|
|
244
|
+
layout = ((_b = result === null || result === void 0 ? void 0 : result.serverErrorPage) === null || _b === void 0 ? void 0 : _b.rendered) || null;
|
|
245
|
+
break;
|
|
246
|
+
default:
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
if (!layout) {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
layout,
|
|
254
|
+
locale,
|
|
255
|
+
mode: this.getPageMode(layout_1.LayoutServicePageState.Normal),
|
|
256
|
+
siteName: site,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Retrieves the static paths for pages based on the given languages.
|
|
261
|
+
* @param {string[]} sites - An array of site names to fetch routes for.
|
|
262
|
+
* @param {string[]} [languages] - An optional array of language codes to generate paths for.
|
|
263
|
+
* @param {FetchOptions} [fetchOptions] - Additional fetch options.
|
|
264
|
+
* @returns {Promise<StaticPath[]>} A promise that resolves to an array of static paths.
|
|
265
|
+
*/
|
|
266
|
+
async getPagePaths(sites, languages, fetchOptions) {
|
|
267
|
+
return this.sitePathService.fetchSiteRoutes(sites, languages || [], fetchOptions);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Retrieves sitemap XML content - either a specific sitemap or the index of all sitemaps.
|
|
271
|
+
* @param {SitemapXmlOptions} reqOptions - Options for sitemap retrieval
|
|
272
|
+
* @param {FetchOptions} [fetchOptions] - Additional fetch options.
|
|
273
|
+
* @returns {Promise<string>} Promise resolving to the sitemap XML content as string
|
|
274
|
+
* @throws {Error} Throws 'REDIRECT_404' if requested sitemap is not found
|
|
275
|
+
*/
|
|
276
|
+
async getSiteMap(reqOptions, fetchOptions) {
|
|
277
|
+
const { reqHost, reqProtocol, id, siteName } = reqOptions;
|
|
278
|
+
// create sitemap graphql service
|
|
279
|
+
const sitemapXmlService = this.getGraphqlSitemapXMLService(siteName || this.initOptions.defaultSite);
|
|
280
|
+
// The id is present if url has sitemap-{n}.xml type.
|
|
281
|
+
// The id can be null if it's index sitemap.xml request
|
|
282
|
+
const sitemapPath = await sitemapXmlService.getSitemap(id);
|
|
283
|
+
// regular sitemap
|
|
284
|
+
if (sitemapPath) {
|
|
285
|
+
try {
|
|
286
|
+
const fetcher = new core_1.NativeDataFetcher();
|
|
287
|
+
const xmlResponse = await fetcher.fetch(sitemapPath);
|
|
288
|
+
if (!xmlResponse.data) {
|
|
289
|
+
throw new Error('REDIRECT_404');
|
|
290
|
+
}
|
|
291
|
+
return xmlResponse.data;
|
|
292
|
+
// eslint-disable-next-line no-unused-vars
|
|
293
|
+
}
|
|
294
|
+
catch (error) {
|
|
295
|
+
throw new Error('REDIRECT_404');
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
// index /sitemap.xml that includes links to all sitemaps
|
|
299
|
+
const sitemaps = await sitemapXmlService.fetchSitemaps(fetchOptions);
|
|
300
|
+
if (!sitemaps.length) {
|
|
301
|
+
throw new Error('REDIRECT_404');
|
|
302
|
+
}
|
|
303
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
304
|
+
<sitemapindex xmlns="http://sitemaps.org/schemas/sitemap/0.9">
|
|
305
|
+
${sitemaps
|
|
306
|
+
.map((item) => {
|
|
307
|
+
const parseUrl = item.split('/');
|
|
308
|
+
const lastSegment = parseUrl[parseUrl.length - 1];
|
|
309
|
+
const escapedUrl = `${reqProtocol}://${reqHost}/${lastSegment}`.replace(/&/g, '&');
|
|
310
|
+
return `<sitemap><loc>${escapedUrl}</loc></sitemap>`;
|
|
311
|
+
})
|
|
312
|
+
.join('')}
|
|
313
|
+
</sitemapindex>`;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Retrieves the robots.txt content for a given site name.
|
|
317
|
+
* @param {string} siteName - The name of the site to retrieve the robots.txt for.
|
|
318
|
+
* @param {FetchOptions} [fetchOptions] - Optional fetch options.
|
|
319
|
+
* @returns {Promise<string | null>} A promise that resolves to the robots.txt content,
|
|
320
|
+
* or null if no content is found.
|
|
321
|
+
*/
|
|
322
|
+
async getRobots(siteName, fetchOptions) {
|
|
323
|
+
const robotsService = this.getRobotsService(siteName || this.initOptions.defaultSite);
|
|
324
|
+
const content = await robotsService.fetchRobots(fetchOptions);
|
|
325
|
+
return content || null;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Factory methods for creating dependencies
|
|
329
|
+
* Subclasses can override these to provide custom implementations.
|
|
330
|
+
*/
|
|
331
|
+
getGraphqlSitemapXMLService(siteName) {
|
|
332
|
+
return new site_1.SitemapXmlService({
|
|
333
|
+
clientFactory: this.clientFactory,
|
|
334
|
+
siteName,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
getRobotsService(siteName) {
|
|
338
|
+
return new robots_service_1.RobotsService({
|
|
339
|
+
clientFactory: this.clientFactory,
|
|
340
|
+
siteName,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
getBaseServiceOptions() {
|
|
344
|
+
return {
|
|
345
|
+
defaultSite: this.initOptions.defaultSite,
|
|
346
|
+
clientFactory: this.clientFactory,
|
|
347
|
+
retries: this.initOptions.retries,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Get page mode based on mode name
|
|
352
|
+
* @param {PageModeName} mode - The mode name to get the page mode for
|
|
353
|
+
* @param { DesignLibraryVariantGeneration} generation - The variant generation mode, if applicable
|
|
354
|
+
* @returns {PageMode} The page mode
|
|
355
|
+
*/
|
|
356
|
+
getPageMode(mode, generation) {
|
|
357
|
+
const pageMode = {
|
|
358
|
+
name: mode,
|
|
359
|
+
isNormal: false,
|
|
360
|
+
isPreview: false,
|
|
361
|
+
isEditing: false,
|
|
362
|
+
isDesignLibrary: false,
|
|
363
|
+
designLibrary: { isVariantGeneration: false },
|
|
364
|
+
};
|
|
365
|
+
switch (mode) {
|
|
366
|
+
case layout_1.LayoutServicePageState.Normal:
|
|
367
|
+
pageMode.isNormal = true;
|
|
368
|
+
break;
|
|
369
|
+
case layout_1.LayoutServicePageState.Preview:
|
|
370
|
+
pageMode.isPreview = true;
|
|
371
|
+
break;
|
|
372
|
+
case layout_1.LayoutServicePageState.Edit:
|
|
373
|
+
pageMode.isEditing = true;
|
|
374
|
+
break;
|
|
375
|
+
case editing_1.DesignLibraryMode.Normal:
|
|
376
|
+
pageMode.isDesignLibrary = true;
|
|
377
|
+
break;
|
|
378
|
+
case editing_1.DesignLibraryMode.Metadata:
|
|
379
|
+
pageMode.isDesignLibrary = true;
|
|
380
|
+
pageMode.isEditing = true;
|
|
381
|
+
break;
|
|
382
|
+
default:
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
if (pageMode.isDesignLibrary && generation === models_1.DesignLibraryVariantGeneration.Variant) {
|
|
386
|
+
pageMode.designLibrary.isVariantGeneration = true;
|
|
387
|
+
pageMode.isEditing = true;
|
|
388
|
+
}
|
|
389
|
+
return pageMode;
|
|
390
|
+
}
|
|
391
|
+
getClientFactory() {
|
|
392
|
+
const graphQLOptions = {
|
|
393
|
+
api: this.initOptions.api,
|
|
394
|
+
retries: this.initOptions.retries.count,
|
|
395
|
+
retryStrategy: this.initOptions.retries.retryStrategy,
|
|
396
|
+
};
|
|
397
|
+
return (0, utils_2.createGraphQLClientFactory)(graphQLOptions);
|
|
398
|
+
}
|
|
399
|
+
getLayoutService(baseOptions) {
|
|
400
|
+
return new layout_1.LayoutService(Object.assign(Object.assign({}, baseOptions), { formatLayoutQuery: this.initOptions.layout.formatLayoutQuery }));
|
|
401
|
+
}
|
|
402
|
+
getDictionaryService(baseOptions) {
|
|
403
|
+
return new i18n_1.DictionaryService(Object.assign(Object.assign({}, baseOptions), { cacheEnabled: this.initOptions.dictionary.caching.enabled, cacheTimeout: this.initOptions.dictionary.caching.timeout }));
|
|
404
|
+
}
|
|
405
|
+
getEditingService() {
|
|
406
|
+
return new editing_1.EditingService({ clientFactory: this.clientFactory });
|
|
407
|
+
}
|
|
408
|
+
getErrorPagesService() {
|
|
409
|
+
return new site_1.ErrorPagesService(Object.assign(Object.assign({}, this.initOptions), { language: this.initOptions.defaultLanguage, clientFactory: this.clientFactory }));
|
|
410
|
+
}
|
|
411
|
+
getComponentService() {
|
|
412
|
+
return new editing_1.ComponentLayoutService(this.initOptions.api.edge);
|
|
413
|
+
}
|
|
414
|
+
getSitePathService() {
|
|
415
|
+
return new site_1.SitePathService({
|
|
416
|
+
clientFactory: this.clientFactory,
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
exports.SitecoreClient = SitecoreClient;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createGraphQLClientFactory = void 0;
|
|
4
|
+
const core_1 = require("@sitecore-content-sdk/core");
|
|
5
|
+
const edge_proxy_1 = require("./edge-proxy");
|
|
6
|
+
/**
|
|
7
|
+
* Creates a new GraphQLRequestClientFactory instance
|
|
8
|
+
* @param {GraphQLClientOptions} options content sdk config
|
|
9
|
+
* @returns GraphQLRequestClientFactory instance
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
const createGraphQLClientFactory = (options) => {
|
|
13
|
+
let clientConfig;
|
|
14
|
+
const { api } = options;
|
|
15
|
+
const { edge, local } = api !== null && api !== void 0 ? api : {};
|
|
16
|
+
const isBrowser = typeof window !== 'undefined';
|
|
17
|
+
if (edge === null || edge === void 0 ? void 0 : edge.contextId) {
|
|
18
|
+
// Real client for server-side rendering / API routes
|
|
19
|
+
clientConfig = {
|
|
20
|
+
endpoint: (0, edge_proxy_1.getEdgeProxyContentUrl)(edge.edgeUrl),
|
|
21
|
+
contextId: edge.contextId,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
else if (isBrowser && (edge === null || edge === void 0 ? void 0 : edge.clientContextId)) {
|
|
25
|
+
// Real client for client-side requests
|
|
26
|
+
clientConfig = {
|
|
27
|
+
endpoint: (0, edge_proxy_1.getEdgeProxyContentUrl)(edge.edgeUrl),
|
|
28
|
+
contextId: edge.clientContextId,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else if ((local === null || local === void 0 ? void 0 : local.apiKey) && (local === null || local === void 0 ? void 0 : local.apiHost)) {
|
|
32
|
+
// Fallback to local API settings
|
|
33
|
+
clientConfig = {
|
|
34
|
+
endpoint: `${local.apiHost}${local.path}`,
|
|
35
|
+
apiKey: local.apiKey,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
else if (isBrowser) {
|
|
39
|
+
// Browser bundle has no IDs – initialise a dummy client and warn
|
|
40
|
+
/* eslint-disable no-console */
|
|
41
|
+
console.warn('GraphQL client initialised in the browser without Edge or local API configuration; client-side requests may fail.');
|
|
42
|
+
clientConfig = { endpoint: '/api/graphql' };
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
throw new Error(`GraphQL client misconfigured.
|
|
46
|
+
Configure one of the following in sitecore.config or your .env file:
|
|
47
|
+
Edge mode: set both sitecore.edge.contextId (server-side) and sitecore.edge.clientContextId (browser).
|
|
48
|
+
Local API mode: set api.local.apiHost and api.local.apiKey.
|
|
49
|
+
Supplying only api.edge.clientContextId will cause the application to fail at runtime.`);
|
|
50
|
+
}
|
|
51
|
+
return core_1.GraphQLRequestClient.createClientFactory(Object.assign(Object.assign({}, clientConfig), options));
|
|
52
|
+
};
|
|
53
|
+
exports.createGraphQLClientFactory = createGraphQLClientFactory;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineConfig = exports.getFallbackConfig = void 0;
|
|
4
|
+
exports.deepMerge = deepMerge;
|
|
5
|
+
const core_1 = require("@sitecore-content-sdk/core");
|
|
6
|
+
const config_cli_1 = require("../config-cli");
|
|
7
|
+
const { SITECORE_EDGE_URL_DEFAULT } = core_1.constants;
|
|
8
|
+
/**
|
|
9
|
+
* Provides default initial values for SitecoreConfig
|
|
10
|
+
* @returns default config
|
|
11
|
+
*/
|
|
12
|
+
const getFallbackConfig = () => ({
|
|
13
|
+
api: {
|
|
14
|
+
edge: {
|
|
15
|
+
contextId: process.env.SITECORE_EDGE_CONTEXT_ID || '',
|
|
16
|
+
clientContextId: '',
|
|
17
|
+
edgeUrl: process.env.SITECORE_EDGE_URL || SITECORE_EDGE_URL_DEFAULT,
|
|
18
|
+
},
|
|
19
|
+
local: {
|
|
20
|
+
apiKey: process.env.SITECORE_API_KEY || process.env.NEXT_PUBLIC_SITECORE_API_KEY || '',
|
|
21
|
+
apiHost: process.env.SITECORE_API_HOST || process.env.NEXT_PUBLIC_SITECORE_API_HOST || '',
|
|
22
|
+
path: '/sitecore/api/graph/edge',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
editingSecret: process.env.SITECORE_EDITING_SECRET || 'editing-secret-missing',
|
|
26
|
+
retries: {
|
|
27
|
+
count: 3,
|
|
28
|
+
retryStrategy: new core_1.DefaultRetryStrategy({
|
|
29
|
+
statusCodes: [429, 502, 503, 504, 520, 521, 522, 523, 524],
|
|
30
|
+
}),
|
|
31
|
+
},
|
|
32
|
+
redirects: {
|
|
33
|
+
enabled: process.env.NODE_ENV !== 'development',
|
|
34
|
+
locales: ['en'],
|
|
35
|
+
},
|
|
36
|
+
multisite: {
|
|
37
|
+
enabled: true,
|
|
38
|
+
useCookieResolution: () => false,
|
|
39
|
+
},
|
|
40
|
+
personalize: {
|
|
41
|
+
enabled: process.env.NODE_ENV !== 'development',
|
|
42
|
+
edgeTimeout: parseInt(process.env.PERSONALIZE_MIDDLEWARE_EDGE_TIMEOUT, 10) || 400,
|
|
43
|
+
cdpTimeout: parseInt(process.env.PERSONALIZE_MIDDLEWARE_CDP_TIMEOUT, 10) || 400,
|
|
44
|
+
scope: '',
|
|
45
|
+
channel: 'WEB',
|
|
46
|
+
currency: 'USD',
|
|
47
|
+
},
|
|
48
|
+
defaultSite: '',
|
|
49
|
+
defaultLanguage: 'en',
|
|
50
|
+
layout: {
|
|
51
|
+
formatLayoutQuery: null,
|
|
52
|
+
},
|
|
53
|
+
dictionary: {
|
|
54
|
+
caching: {
|
|
55
|
+
enabled: true,
|
|
56
|
+
timeout: 60,
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
disableCodeGeneration: false,
|
|
60
|
+
});
|
|
61
|
+
exports.getFallbackConfig = getFallbackConfig;
|
|
62
|
+
/**
|
|
63
|
+
* Deep merge utility that skips undefined or empty string values in the override.
|
|
64
|
+
* @param {T} base base value
|
|
65
|
+
* @param {DeepPartial<T>} [override] override value
|
|
66
|
+
*/
|
|
67
|
+
function deepMerge(base, override) {
|
|
68
|
+
if (!override)
|
|
69
|
+
return base;
|
|
70
|
+
const result = Object.assign({}, base);
|
|
71
|
+
for (const key in override) {
|
|
72
|
+
if (!Object.prototype.hasOwnProperty.call(override, key))
|
|
73
|
+
continue;
|
|
74
|
+
const typedKey = key;
|
|
75
|
+
const baseValue = base[typedKey];
|
|
76
|
+
const overrideValue = override[typedKey];
|
|
77
|
+
// Skip undefined and empty string overrides
|
|
78
|
+
if (overrideValue === undefined || overrideValue === '') {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (typeof overrideValue === 'object' &&
|
|
82
|
+
overrideValue !== null &&
|
|
83
|
+
!Array.isArray(overrideValue) &&
|
|
84
|
+
Object.getPrototypeOf(overrideValue) === Object.prototype) {
|
|
85
|
+
result[typedKey] = deepMerge(baseValue, overrideValue);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
result[typedKey] = overrideValue;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return result;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Resolves sitecore config based on base config and overrides
|
|
95
|
+
* @param {SitecoreConfig} base base sitecore config object
|
|
96
|
+
* @param {SitecoreConfig} override override sitecore config object
|
|
97
|
+
* @returns resolved SitecoreConfig object
|
|
98
|
+
*/
|
|
99
|
+
const resolveConfig = (base, override) => {
|
|
100
|
+
const result = deepMerge(base, override);
|
|
101
|
+
if (Number.isNaN(result.personalize.cdpTimeout) || !result.personalize.cdpTimeout) {
|
|
102
|
+
result.personalize.cdpTimeout = base.personalize.cdpTimeout;
|
|
103
|
+
}
|
|
104
|
+
if (Number.isNaN(result.personalize.edgeTimeout) || !result.personalize.edgeTimeout) {
|
|
105
|
+
result.personalize.edgeTimeout = base.personalize.edgeTimeout;
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
};
|
|
109
|
+
const validateApiConfiguration = (config) => {
|
|
110
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
111
|
+
const isBrowser = typeof window !== 'undefined';
|
|
112
|
+
const hasEdgeContextId = !!((_b = (_a = config.api) === null || _a === void 0 ? void 0 : _a.edge) === null || _b === void 0 ? void 0 : _b.contextId);
|
|
113
|
+
const hasClientContextId = !!((_d = (_c = config.api) === null || _c === void 0 ? void 0 : _c.edge) === null || _d === void 0 ? void 0 : _d.clientContextId);
|
|
114
|
+
const hasLocalCreds = !!((_f = (_e = config.api) === null || _e === void 0 ? void 0 : _e.local) === null || _f === void 0 ? void 0 : _f.apiHost) && !!((_h = (_g = config.api) === null || _g === void 0 ? void 0 : _g.local) === null || _h === void 0 ? void 0 : _h.apiKey);
|
|
115
|
+
// Server-side: allow Edge OR Local; clientContextId alone is NOT sufficient
|
|
116
|
+
if (!isBrowser) {
|
|
117
|
+
if (!hasEdgeContextId && !hasLocalCreds) {
|
|
118
|
+
throw new Error('Configuration error: provide either Edge contextId (api.edge.contextId) or local credentials (api.local.apiHost + api.local.apiKey).');
|
|
119
|
+
}
|
|
120
|
+
if (hasEdgeContextId && !hasClientContextId) {
|
|
121
|
+
// eslint-disable-next-line no-console
|
|
122
|
+
if (process.env.NODE_ENV === 'development') {
|
|
123
|
+
console.warn('Warning: only a server-side edge contextId is provided. Client-side requests will require api.edge.clientContextId or a proxy.');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return; // validation complete on the server
|
|
127
|
+
}
|
|
128
|
+
// Browser-side warning (runs only if contextId exists but clientContextId is missing)
|
|
129
|
+
if (isBrowser && !hasClientContextId) {
|
|
130
|
+
// eslint-disable-next-line no-console
|
|
131
|
+
if (process.env.NODE_ENV === 'development') {
|
|
132
|
+
console.warn(`Warning: clientContextId is missing. The browser will use contextId instead.
|
|
133
|
+
Client Side functionalities (like Tracking and Personalization) may be limited.`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* The validators for the config object during build time.
|
|
139
|
+
* Validators are called when the literal path of the config object is accessed.
|
|
140
|
+
*/
|
|
141
|
+
const PROPERTY_VALIDATORS = {
|
|
142
|
+
'api.edge.contextId': validateApiConfiguration,
|
|
143
|
+
'api.local.apiKey': validateApiConfiguration,
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Creates a proxy for the config object to validate the config object during build time.
|
|
147
|
+
* @param {SitecoreConfig} config - The config object to create a proxy for.
|
|
148
|
+
* @returns {SitecoreConfig} The proxy for the config object.
|
|
149
|
+
*/
|
|
150
|
+
const createConfigProxy = (config) => {
|
|
151
|
+
const validated = new Set();
|
|
152
|
+
const createProxy = (target, propPath = '') => {
|
|
153
|
+
return new Proxy(target, {
|
|
154
|
+
get(obj, prop, receiver) {
|
|
155
|
+
// Skip symbol properties, do not attempt to stringify them
|
|
156
|
+
// Type safety check
|
|
157
|
+
if (typeof prop === 'symbol') {
|
|
158
|
+
return Reflect.get(obj, prop, receiver);
|
|
159
|
+
}
|
|
160
|
+
const fullPath = propPath ? `${propPath}.${prop}` : prop;
|
|
161
|
+
const value = Reflect.get(obj, prop, receiver);
|
|
162
|
+
if (fullPath in PROPERTY_VALIDATORS &&
|
|
163
|
+
!validated.has(fullPath)) {
|
|
164
|
+
const validator = PROPERTY_VALIDATORS[fullPath];
|
|
165
|
+
validator(config);
|
|
166
|
+
validated.add(fullPath);
|
|
167
|
+
}
|
|
168
|
+
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
169
|
+
return createProxy(value, fullPath);
|
|
170
|
+
}
|
|
171
|
+
return value;
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
return createProxy(config);
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Accepts a SitecoreConfigInput object and returns full sitecore configuration
|
|
179
|
+
* @param {SitecoreConfigInput} config override values to be written over default config settings
|
|
180
|
+
* @returns {SitecoreConfig} full sitecore configuration to use in application
|
|
181
|
+
* @public
|
|
182
|
+
*/
|
|
183
|
+
const defineConfig = (config) => {
|
|
184
|
+
const resolvedConfig = resolveConfig((0, exports.getFallbackConfig)(), config);
|
|
185
|
+
const isCLI = process.env[config_cli_1.SITECORE_CLI_MODE_ENV_VAR] === 'true';
|
|
186
|
+
// At `build time`, we create a proxy for the config object to validate the config by
|
|
187
|
+
// accessing the literal paths instead of validating the whole object at once.
|
|
188
|
+
// At `runtime` all the config should be validated to fail fast in case of invalid configuration.
|
|
189
|
+
if (isCLI) {
|
|
190
|
+
return createConfigProxy(resolvedConfig);
|
|
191
|
+
}
|
|
192
|
+
validateApiConfiguration(resolvedConfig);
|
|
193
|
+
return resolvedConfig;
|
|
194
|
+
};
|
|
195
|
+
exports.defineConfig = defineConfig;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineConfig = exports.ComponentTemplateType = void 0;
|
|
4
|
+
var models_1 = require("./models");
|
|
5
|
+
Object.defineProperty(exports, "ComponentTemplateType", { enumerable: true, get: function () { return models_1.ComponentTemplateType; } });
|
|
6
|
+
var define_config_1 = require("./define-config");
|
|
7
|
+
Object.defineProperty(exports, "defineConfig", { enumerable: true, get: function () { return define_config_1.defineConfig; } });
|