@sitecore-jss/sitecore-jss 21.0.0-canary.99 → 21.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/cjs/debug.js +4 -3
- package/dist/cjs/graphql-request-client.js +19 -8
- package/dist/cjs/layout/index.js +4 -1
- package/dist/cjs/layout/models.js +16 -1
- package/dist/cjs/native-fetcher.js +32 -3
- package/dist/cjs/personalize/cdp-service.js +76 -30
- package/dist/cjs/personalize/graphql-personalize-service.js +23 -13
- package/dist/cjs/personalize/index.js +2 -1
- package/dist/cjs/personalize/layout-personalizer.js +16 -4
- package/dist/cjs/personalize/utils.js +34 -1
- package/dist/cjs/site/graphql-error-pages-service.js +80 -0
- package/dist/cjs/site/graphql-redirects-service.js +2 -1
- package/dist/cjs/site/graphql-sitemap-service.js +2 -0
- package/dist/cjs/site/index.js +3 -1
- package/dist/cjs/utils/editing.js +13 -13
- package/dist/cjs/utils/index.js +2 -3
- package/dist/cjs/utils/timeout-promise.js +31 -0
- package/dist/cjs/utils/utils.js +15 -1
- package/dist/esm/debug.js +4 -3
- package/dist/esm/graphql-request-client.js +19 -8
- package/dist/esm/layout/index.js +1 -1
- package/dist/esm/layout/models.js +15 -0
- package/dist/esm/native-fetcher.js +32 -3
- package/dist/esm/personalize/cdp-service.js +75 -29
- package/dist/esm/personalize/graphql-personalize-service.js +23 -13
- package/dist/esm/personalize/index.js +1 -1
- package/dist/esm/personalize/layout-personalizer.js +16 -4
- package/dist/esm/personalize/utils.js +32 -0
- package/dist/esm/site/graphql-error-pages-service.js +73 -0
- package/dist/esm/site/graphql-redirects-service.js +2 -1
- package/dist/esm/site/graphql-sitemap-service.js +2 -0
- package/dist/esm/site/index.js +1 -0
- package/dist/esm/utils/editing.js +12 -12
- package/dist/esm/utils/index.js +2 -2
- package/dist/esm/utils/timeout-promise.js +28 -0
- package/dist/esm/utils/utils.js +13 -0
- package/package.json +4 -4
- package/types/debug.d.ts +4 -3
- package/types/graphql-request-client.d.ts +6 -0
- package/types/layout/index.d.ts +1 -1
- package/types/layout/models.d.ts +15 -0
- package/types/native-fetcher.d.ts +13 -0
- package/types/personalize/cdp-service.d.ts +46 -24
- package/types/personalize/graphql-personalize-service.d.ts +4 -0
- package/types/personalize/index.d.ts +2 -2
- package/types/personalize/layout-personalizer.d.ts +1 -1
- package/types/personalize/utils.d.ts +20 -0
- package/types/site/graphql-error-pages-service.d.ts +52 -0
- package/types/site/graphql-redirects-service.d.ts +2 -1
- package/types/site/index.d.ts +1 -0
- package/types/utils/editing.d.ts +12 -12
- package/types/utils/index.d.ts +2 -2
- package/types/utils/timeout-promise.d.ts +18 -0
- package/types/utils/utils.d.ts +6 -0
package/README.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
This module is provided as a part of Sitecore JavaScript Rendering SDK. It contains the core JSS APIs (layout service) and utilities.
|
|
4
4
|
|
|
5
|
-
<!---
|
|
6
|
-
@TODO: Update to version 20.0.0 docs before release
|
|
7
|
-
-->
|
|
8
|
-
[Documentation](https://doc.sitecore.com/xp/en/developers/hd/190/sitecore-headless-development/sitecore-javascript-rendering-sdks--jss-.html)
|
|
9
5
|
|
|
10
|
-
[
|
|
6
|
+
[Documentation (Experience Platform)](https://doc.sitecore.com/xp/en/developers/hd/201/sitecore-headless-development/sitecore-javascript-rendering-sdks--jss-.html)
|
|
7
|
+
|
|
8
|
+
[Documentation (XM Cloud)](https://doc.sitecore.com/xmc/en/developers/xm-cloud/sitecore-javascript-rendering-sdks--jss-.html)
|
|
9
|
+
|
|
10
|
+
[API reference documentation](/ref-docs/sitecore-jss/)
|
package/dist/cjs/debug.js
CHANGED
|
@@ -27,13 +27,14 @@ exports.enableDebug = enableDebug;
|
|
|
27
27
|
* Default Sitecore JSS 'debug' module debuggers. Uses namespace prefix 'sitecore-jss:'.
|
|
28
28
|
* See {@link https://www.npmjs.com/package/debug} for details.
|
|
29
29
|
*/
|
|
30
|
-
exports.default =
|
|
30
|
+
exports.default = {
|
|
31
31
|
http: debug_1.default(`${rootNamespace}:http`),
|
|
32
32
|
layout: debug_1.default(`${rootNamespace}:layout`),
|
|
33
33
|
dictionary: debug_1.default(`${rootNamespace}:dictionary`),
|
|
34
|
-
|
|
34
|
+
editing: debug_1.default(`${rootNamespace}:editing`),
|
|
35
35
|
sitemap: debug_1.default(`${rootNamespace}:sitemap`),
|
|
36
36
|
robots: debug_1.default(`${rootNamespace}:robots`),
|
|
37
37
|
redirects: debug_1.default(`${rootNamespace}:redirects`),
|
|
38
38
|
personalize: debug_1.default(`${rootNamespace}:personalize`),
|
|
39
|
-
})
|
|
39
|
+
errorpages: debug_1.default(`${rootNamespace}:errorpages`),
|
|
40
|
+
};
|
|
@@ -16,6 +16,7 @@ exports.GraphQLRequestClient = void 0;
|
|
|
16
16
|
const graphql_request_1 = require("graphql-request");
|
|
17
17
|
const url_parse_1 = __importDefault(require("url-parse"));
|
|
18
18
|
const debug_1 = __importDefault(require("./debug"));
|
|
19
|
+
const timeout_promise_1 = __importDefault(require("./utils/timeout-promise"));
|
|
19
20
|
/**
|
|
20
21
|
* A GraphQL client for Sitecore APIs that uses the 'graphql-request' library.
|
|
21
22
|
* https://github.com/prisma-labs/graphql-request
|
|
@@ -35,7 +36,11 @@ class GraphQLRequestClient {
|
|
|
35
36
|
if (!endpoint || !url_parse_1.default(endpoint).hostname) {
|
|
36
37
|
throw new Error(`Invalid GraphQL endpoint '${endpoint}'. Verify that 'layoutServiceHost' property in 'scjssconfig.json' file or appropriate environment variable is set`);
|
|
37
38
|
}
|
|
38
|
-
this.
|
|
39
|
+
this.timeout = clientConfig.timeout;
|
|
40
|
+
this.client = new graphql_request_1.GraphQLClient(endpoint, {
|
|
41
|
+
headers: this.headers,
|
|
42
|
+
fetch: clientConfig.fetch,
|
|
43
|
+
});
|
|
39
44
|
this.debug = clientConfig.debugger || debug_1.default.http;
|
|
40
45
|
}
|
|
41
46
|
/**
|
|
@@ -54,15 +59,21 @@ class GraphQLRequestClient {
|
|
|
54
59
|
query,
|
|
55
60
|
variables,
|
|
56
61
|
});
|
|
57
|
-
this.client
|
|
58
|
-
|
|
59
|
-
.
|
|
62
|
+
const fetchWithOptionalTimeout = [this.client.request(query, variables)];
|
|
63
|
+
if (this.timeout) {
|
|
64
|
+
this.abortTimeout = new timeout_promise_1.default(this.timeout);
|
|
65
|
+
fetchWithOptionalTimeout.push(this.abortTimeout.start);
|
|
66
|
+
}
|
|
67
|
+
Promise.race(fetchWithOptionalTimeout).then((data) => {
|
|
68
|
+
var _a;
|
|
69
|
+
(_a = this.abortTimeout) === null || _a === void 0 ? void 0 : _a.clear();
|
|
60
70
|
this.debug('response: %o', data);
|
|
61
71
|
resolve(data);
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
this.
|
|
65
|
-
|
|
72
|
+
}, (error) => {
|
|
73
|
+
var _a;
|
|
74
|
+
(_a = this.abortTimeout) === null || _a === void 0 ? void 0 : _a.clear();
|
|
75
|
+
this.debug('response error: %o', error.response || error.message || error);
|
|
76
|
+
reject(error);
|
|
66
77
|
});
|
|
67
78
|
});
|
|
68
79
|
});
|
package/dist/cjs/layout/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GraphQLLayoutService = exports.RestLayoutService = exports.getChildPlaceholder = exports.getFieldValue = exports.LayoutServicePageState = void 0;
|
|
3
|
+
exports.GraphQLLayoutService = exports.RestLayoutService = exports.getChildPlaceholder = exports.getFieldValue = exports.EDITING_COMPONENT_ID = exports.EDITING_COMPONENT_PLACEHOLDER = exports.RenderingType = exports.LayoutServicePageState = void 0;
|
|
4
4
|
// layout
|
|
5
5
|
var models_1 = require("./models");
|
|
6
6
|
Object.defineProperty(exports, "LayoutServicePageState", { enumerable: true, get: function () { return models_1.LayoutServicePageState; } });
|
|
7
|
+
Object.defineProperty(exports, "RenderingType", { enumerable: true, get: function () { return models_1.RenderingType; } });
|
|
8
|
+
Object.defineProperty(exports, "EDITING_COMPONENT_PLACEHOLDER", { enumerable: true, get: function () { return models_1.EDITING_COMPONENT_PLACEHOLDER; } });
|
|
9
|
+
Object.defineProperty(exports, "EDITING_COMPONENT_ID", { enumerable: true, get: function () { return models_1.EDITING_COMPONENT_ID; } });
|
|
7
10
|
var utils_1 = require("./utils");
|
|
8
11
|
Object.defineProperty(exports, "getFieldValue", { enumerable: true, get: function () { return utils_1.getFieldValue; } });
|
|
9
12
|
Object.defineProperty(exports, "getChildPlaceholder", { enumerable: true, get: function () { return utils_1.getChildPlaceholder; } });
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.LayoutServicePageState = void 0;
|
|
3
|
+
exports.RenderingType = exports.LayoutServicePageState = exports.EDITING_COMPONENT_ID = exports.EDITING_COMPONENT_PLACEHOLDER = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Static placeholder name used for component rendering
|
|
6
|
+
*/
|
|
7
|
+
exports.EDITING_COMPONENT_PLACEHOLDER = 'editing-componentmode-placeholder';
|
|
8
|
+
/**
|
|
9
|
+
* Id of wrapper for component rendering
|
|
10
|
+
*/
|
|
11
|
+
exports.EDITING_COMPONENT_ID = 'editing-component';
|
|
4
12
|
/**
|
|
5
13
|
* Layout Service page state enum
|
|
6
14
|
*/
|
|
@@ -10,3 +18,10 @@ var LayoutServicePageState;
|
|
|
10
18
|
LayoutServicePageState["Edit"] = "edit";
|
|
11
19
|
LayoutServicePageState["Normal"] = "normal";
|
|
12
20
|
})(LayoutServicePageState = exports.LayoutServicePageState || (exports.LayoutServicePageState = {}));
|
|
21
|
+
/**
|
|
22
|
+
* Editing rendering type
|
|
23
|
+
*/
|
|
24
|
+
var RenderingType;
|
|
25
|
+
(function (RenderingType) {
|
|
26
|
+
RenderingType["Component"] = "component";
|
|
27
|
+
})(RenderingType = exports.RenderingType || (exports.RenderingType = {}));
|
|
@@ -25,6 +25,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
26
|
exports.NativeDataFetcher = void 0;
|
|
27
27
|
const debug_1 = __importDefault(require("./debug"));
|
|
28
|
+
const timeout_promise_1 = __importDefault(require("./utils/timeout-promise"));
|
|
28
29
|
class NativeDataFetcher {
|
|
29
30
|
constructor(config = {}) {
|
|
30
31
|
this.config = config;
|
|
@@ -42,10 +43,24 @@ class NativeDataFetcher {
|
|
|
42
43
|
const fetchImpl = fetchOverride || fetch;
|
|
43
44
|
const debug = debugOverride || debug_1.default.http;
|
|
44
45
|
const requestInit = this.getRequestInit(init, data);
|
|
46
|
+
const fetchWithOptionalTimeout = [fetchImpl(url, requestInit)];
|
|
47
|
+
if (init.timeout) {
|
|
48
|
+
this.abortTimeout = new timeout_promise_1.default(init.timeout);
|
|
49
|
+
fetchWithOptionalTimeout.push(this.abortTimeout.start);
|
|
50
|
+
}
|
|
45
51
|
// Note a goal here is to provide consistent debug logging and error handling
|
|
46
52
|
// as we do in AxiosDataFetcher and GraphQLRequestClient
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
const { headers: reqHeaders } = requestInit, rest = __rest(requestInit, ["headers"]);
|
|
54
|
+
debug('request: %o', Object.assign({ url, headers: this.extractDebugHeaders(reqHeaders) }, rest));
|
|
55
|
+
const response = yield Promise.race(fetchWithOptionalTimeout)
|
|
56
|
+
.then((res) => {
|
|
57
|
+
var _a;
|
|
58
|
+
(_a = this.abortTimeout) === null || _a === void 0 ? void 0 : _a.clear();
|
|
59
|
+
return res;
|
|
60
|
+
})
|
|
61
|
+
.catch((error) => {
|
|
62
|
+
var _a;
|
|
63
|
+
(_a = this.abortTimeout) === null || _a === void 0 ? void 0 : _a.clear();
|
|
49
64
|
debug('request error: %o', error);
|
|
50
65
|
throw error;
|
|
51
66
|
});
|
|
@@ -60,7 +75,7 @@ class NativeDataFetcher {
|
|
|
60
75
|
const debugResponse = {
|
|
61
76
|
status: response.status,
|
|
62
77
|
statusText: response.statusText,
|
|
63
|
-
headers: response.headers,
|
|
78
|
+
headers: this.extractDebugHeaders(response.headers),
|
|
64
79
|
url: response.url,
|
|
65
80
|
redirected: response.redirected,
|
|
66
81
|
data: respData,
|
|
@@ -89,5 +104,19 @@ class NativeDataFetcher {
|
|
|
89
104
|
init.headers = headers;
|
|
90
105
|
return init;
|
|
91
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Safely extract all headers for debug logging
|
|
109
|
+
* @param {HeadersInit} incomingHeaders Incoming headers
|
|
110
|
+
* @returns Object with headers as key/value pairs
|
|
111
|
+
*/
|
|
112
|
+
extractDebugHeaders(incomingHeaders = {}) {
|
|
113
|
+
const headers = {};
|
|
114
|
+
if (typeof (incomingHeaders === null || incomingHeaders === void 0 ? void 0 : incomingHeaders.forEach) !== 'string' && incomingHeaders.forEach) {
|
|
115
|
+
incomingHeaders === null || incomingHeaders === void 0 ? void 0 : incomingHeaders.forEach((value, key) => {
|
|
116
|
+
headers[key] = value;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return headers;
|
|
120
|
+
}
|
|
92
121
|
}
|
|
93
122
|
exports.NativeDataFetcher = NativeDataFetcher;
|
|
@@ -12,59 +12,105 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.CdpService = void 0;
|
|
15
|
+
exports.CdpService = exports.DEFAULT_CHANNEL = void 0;
|
|
16
16
|
const debug_1 = __importDefault(require("../debug"));
|
|
17
17
|
const axios_fetcher_1 = require("../axios-fetcher");
|
|
18
|
+
const utils_1 = require("../utils");
|
|
19
|
+
exports.DEFAULT_CHANNEL = 'WEB';
|
|
18
20
|
class CdpService {
|
|
19
|
-
/**
|
|
20
|
-
* @param {CdpServiceConfig} [config] CDP service config
|
|
21
|
-
*/
|
|
22
21
|
constructor(config) {
|
|
23
22
|
this.config = config;
|
|
24
23
|
/**
|
|
25
24
|
* Provides default @see AxiosDataFetcher data fetcher
|
|
25
|
+
* @param {DataFetcherConfig} config
|
|
26
26
|
* @returns default fetcher
|
|
27
27
|
*/
|
|
28
|
-
this.getDefaultFetcher = () => {
|
|
29
|
-
const fetcher = new axios_fetcher_1.AxiosDataFetcher({
|
|
30
|
-
debugger: debug_1.default.personalize,
|
|
31
|
-
});
|
|
28
|
+
this.getDefaultFetcher = (config) => {
|
|
29
|
+
const fetcher = new axios_fetcher_1.AxiosDataFetcher(Object.assign({ debugger: debug_1.default.personalize }, config));
|
|
32
30
|
return (url, data) => fetcher.fetch(url, data);
|
|
33
31
|
};
|
|
32
|
+
this.timeout = config.timeout || 250;
|
|
34
33
|
}
|
|
35
34
|
/**
|
|
36
|
-
* Executes targeted experience for a page and
|
|
35
|
+
* Executes targeted experience for a page and params to determine the variant to render.
|
|
37
36
|
* @param {string} contentId the friendly content id of the page
|
|
38
|
-
* @param {
|
|
39
|
-
* @param {string}
|
|
37
|
+
* @param {string} browserId the browser id
|
|
38
|
+
* @param {string} userAgent the user agent
|
|
39
|
+
* @param {string} pointOfSale current point of sale
|
|
40
|
+
* @param {ExperienceParams} params the experience params for the user
|
|
40
41
|
* @returns {ExecuteExperienceResult} the execute experience result
|
|
41
42
|
*/
|
|
42
|
-
executeExperience(contentId,
|
|
43
|
+
executeExperience(contentId, browserId, userAgent, pointOfSale, params) {
|
|
44
|
+
var _a;
|
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
const endpoint = this.getExecuteExperienceUrl();
|
|
47
|
+
debug_1.default.personalize('executing experience for %s %s %s %o', contentId, browserId, userAgent, params);
|
|
48
|
+
const headers = { 'User-Agent': userAgent };
|
|
49
|
+
const fetcher = this.getFetcher(headers);
|
|
50
|
+
try {
|
|
51
|
+
const response = yield fetcher(endpoint, {
|
|
52
|
+
clientKey: this.config.clientKey,
|
|
53
|
+
pointOfSale: pointOfSale,
|
|
54
|
+
channel: (_a = this.config.channel) !== null && _a !== void 0 ? _a : exports.DEFAULT_CHANNEL,
|
|
55
|
+
browserId,
|
|
56
|
+
friendlyId: contentId,
|
|
57
|
+
params,
|
|
58
|
+
});
|
|
59
|
+
response.data.variantId === '' && (response.data.variantId = undefined);
|
|
60
|
+
return response.data.variantId || undefined;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (utils_1.isTimeoutError(error)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Generates a new browser id
|
|
72
|
+
* @returns {string} browser id
|
|
73
|
+
*/
|
|
74
|
+
generateBrowserId() {
|
|
43
75
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
const endpoint = this.
|
|
45
|
-
debug_1.default.personalize('
|
|
46
|
-
const fetcher = this.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
browserId: response.data.browserId || undefined,
|
|
58
|
-
};
|
|
76
|
+
const endpoint = this.getGenerateBrowserIdUrl();
|
|
77
|
+
debug_1.default.personalize('generating browser id');
|
|
78
|
+
const fetcher = this.getFetcher();
|
|
79
|
+
try {
|
|
80
|
+
const response = yield fetcher(endpoint);
|
|
81
|
+
return response.data.ref;
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
if (utils_1.isTimeoutError(error)) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
59
89
|
});
|
|
60
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Get formatted URL for generateBrowserId call
|
|
93
|
+
* @returns {string} formatted URL
|
|
94
|
+
*/
|
|
95
|
+
getGenerateBrowserIdUrl() {
|
|
96
|
+
return `${this.config.endpoint}/v1.2/browser/create.json?client_key=${this.config.clientKey}&message={}`;
|
|
97
|
+
}
|
|
61
98
|
/**
|
|
62
99
|
* Get formatted URL for executeExperience call
|
|
63
|
-
* @param {string} contentId friendly content id
|
|
64
100
|
* @returns {string} formatted URL
|
|
65
101
|
*/
|
|
66
|
-
getExecuteExperienceUrl(
|
|
67
|
-
return `${this.config.endpoint}/v2/callFlows
|
|
102
|
+
getExecuteExperienceUrl() {
|
|
103
|
+
return `${this.config.endpoint}/v2/callFlows`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Returns provided data fetcher otherwise default one
|
|
107
|
+
* @param {Record<string, string>} [headers] Optional headers
|
|
108
|
+
* @returns {HttpDataFetcher} data fetcher
|
|
109
|
+
*/
|
|
110
|
+
getFetcher(headers) {
|
|
111
|
+
return this.config.dataFetcherResolver
|
|
112
|
+
? this.config.dataFetcherResolver({ timeout: this.timeout, headers })
|
|
113
|
+
: this.getDefaultFetcher({ timeout: this.timeout, headers });
|
|
68
114
|
}
|
|
69
115
|
}
|
|
70
116
|
exports.CdpService = CdpService;
|
|
@@ -15,6 +15,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.GraphQLPersonalizeService = void 0;
|
|
16
16
|
const graphql_request_client_1 = require("../graphql-request-client");
|
|
17
17
|
const debug_1 = __importDefault(require("../debug"));
|
|
18
|
+
const utils_1 = require("../utils");
|
|
19
|
+
const utils_2 = require("./utils");
|
|
18
20
|
class GraphQLPersonalizeService {
|
|
19
21
|
/**
|
|
20
22
|
* Fetch personalize data using the Sitecore GraphQL endpoint.
|
|
@@ -22,6 +24,7 @@ class GraphQLPersonalizeService {
|
|
|
22
24
|
*/
|
|
23
25
|
constructor(config) {
|
|
24
26
|
this.config = config;
|
|
27
|
+
this.config.timeout = config.timeout || 250;
|
|
25
28
|
this.graphQLClient = this.getGraphQLClient();
|
|
26
29
|
}
|
|
27
30
|
get query() {
|
|
@@ -49,20 +52,26 @@ class GraphQLPersonalizeService {
|
|
|
49
52
|
var _a;
|
|
50
53
|
return __awaiter(this, void 0, void 0, function* () {
|
|
51
54
|
debug_1.default.personalize('fetching personalize info for %s %s %s', this.config.siteName, itemPath, language);
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
try {
|
|
56
|
+
const data = yield this.graphQLClient.request(this.query, {
|
|
57
|
+
siteName: this.config.siteName,
|
|
58
|
+
itemPath,
|
|
59
|
+
language,
|
|
60
|
+
});
|
|
61
|
+
return ((_a = data === null || data === void 0 ? void 0 : data.layout) === null || _a === void 0 ? void 0 : _a.item)
|
|
62
|
+
? {
|
|
63
|
+
// CDP expects content id format `embedded_<id>_<lang>` (lowercase)
|
|
64
|
+
contentId: utils_2.CdpHelper.getContentId(data.layout.item.id, language),
|
|
65
|
+
variantIds: data.layout.item.personalization.variantIds,
|
|
66
|
+
}
|
|
67
|
+
: undefined;
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
if (utils_1.isTimeoutError(error)) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
throw error;
|
|
60
74
|
}
|
|
61
|
-
return {
|
|
62
|
-
// CDP expects content id format `<id>_<language>_<version>` (lowercase)
|
|
63
|
-
contentId: `${data.layout.item.id}_${language}_${data.layout.item.version}`.toLowerCase(),
|
|
64
|
-
variantIds: data.layout.item.personalization.variantIds,
|
|
65
|
-
};
|
|
66
75
|
});
|
|
67
76
|
}
|
|
68
77
|
/**
|
|
@@ -76,6 +85,7 @@ class GraphQLPersonalizeService {
|
|
|
76
85
|
apiKey: this.config.apiKey,
|
|
77
86
|
debugger: debug_1.default.personalize,
|
|
78
87
|
fetch: this.config.fetch,
|
|
88
|
+
timeout: this.config.timeout,
|
|
79
89
|
});
|
|
80
90
|
}
|
|
81
91
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.normalizePersonalizedRewrite = exports.getPersonalizedRewriteData = exports.getPersonalizedRewrite = exports.CdpService = exports.GraphQLPersonalizeService = exports.personalizeLayout = void 0;
|
|
3
|
+
exports.CdpHelper = exports.normalizePersonalizedRewrite = exports.getPersonalizedRewriteData = exports.getPersonalizedRewrite = exports.CdpService = exports.GraphQLPersonalizeService = exports.personalizeLayout = void 0;
|
|
4
4
|
var layout_personalizer_1 = require("./layout-personalizer");
|
|
5
5
|
Object.defineProperty(exports, "personalizeLayout", { enumerable: true, get: function () { return layout_personalizer_1.personalizeLayout; } });
|
|
6
6
|
var graphql_personalize_service_1 = require("./graphql-personalize-service");
|
|
@@ -11,3 +11,4 @@ var utils_1 = require("./utils");
|
|
|
11
11
|
Object.defineProperty(exports, "getPersonalizedRewrite", { enumerable: true, get: function () { return utils_1.getPersonalizedRewrite; } });
|
|
12
12
|
Object.defineProperty(exports, "getPersonalizedRewriteData", { enumerable: true, get: function () { return utils_1.getPersonalizedRewriteData; } });
|
|
13
13
|
Object.defineProperty(exports, "normalizePersonalizedRewrite", { enumerable: true, get: function () { return utils_1.normalizePersonalizedRewrite; } });
|
|
14
|
+
Object.defineProperty(exports, "CdpHelper", { enumerable: true, get: function () { return utils_1.CdpHelper; } });
|
|
@@ -8,6 +8,8 @@ exports.personalizeComponent = exports.personalizePlaceholder = exports.personal
|
|
|
8
8
|
*/
|
|
9
9
|
function personalizeLayout(layout, variantId) {
|
|
10
10
|
var _a;
|
|
11
|
+
// Add variantId to Sitecore context so that it is accessible here
|
|
12
|
+
layout.sitecore.context.variantId = variantId;
|
|
11
13
|
const placeholders = (_a = layout.sitecore.route) === null || _a === void 0 ? void 0 : _a.placeholders;
|
|
12
14
|
if (Object.keys(placeholders !== null && placeholders !== void 0 ? placeholders : {}).length === 0) {
|
|
13
15
|
return;
|
|
@@ -27,9 +29,19 @@ exports.personalizeLayout = personalizeLayout;
|
|
|
27
29
|
*/
|
|
28
30
|
function personalizePlaceholder(components, variantId) {
|
|
29
31
|
return components
|
|
30
|
-
.map((component) =>
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
.map((component) => {
|
|
33
|
+
const rendering = component;
|
|
34
|
+
if (rendering.experiences !== undefined) {
|
|
35
|
+
return personalizeComponent(rendering, variantId);
|
|
36
|
+
}
|
|
37
|
+
else if (rendering.placeholders) {
|
|
38
|
+
const placeholders = rendering.placeholders;
|
|
39
|
+
Object.keys(placeholders).forEach((placeholder) => {
|
|
40
|
+
placeholders[placeholder] = personalizePlaceholder(placeholders[placeholder], variantId);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return component;
|
|
44
|
+
})
|
|
33
45
|
.filter(Boolean);
|
|
34
46
|
}
|
|
35
47
|
exports.personalizePlaceholder = personalizePlaceholder;
|
|
@@ -44,7 +56,7 @@ function personalizeComponent(component, variantId) {
|
|
|
44
56
|
// DEFAULT IS HIDDEN
|
|
45
57
|
return null;
|
|
46
58
|
}
|
|
47
|
-
else if (
|
|
59
|
+
else if (variant && variant.componentName === null && variant.dataSource === null) {
|
|
48
60
|
// HIDDEN
|
|
49
61
|
return null;
|
|
50
62
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.normalizePersonalizedRewrite = exports.getPersonalizedRewriteData = exports.getPersonalizedRewrite = exports.VARIANT_PREFIX = exports.DEFAULT_VARIANT = void 0;
|
|
3
|
+
exports.CdpHelper = exports.normalizePersonalizedRewrite = exports.getPersonalizedRewriteData = exports.getPersonalizedRewrite = exports.VARIANT_PREFIX = exports.DEFAULT_VARIANT = void 0;
|
|
4
4
|
exports.DEFAULT_VARIANT = '_default';
|
|
5
5
|
exports.VARIANT_PREFIX = '_variantId_';
|
|
6
6
|
/**
|
|
@@ -44,3 +44,36 @@ function normalizePersonalizedRewrite(pathname) {
|
|
|
44
44
|
return result === null ? pathname : pathname.replace(result[0], '');
|
|
45
45
|
}
|
|
46
46
|
exports.normalizePersonalizedRewrite = normalizePersonalizedRewrite;
|
|
47
|
+
/**
|
|
48
|
+
* Static utility class for Sitecore CDP
|
|
49
|
+
*/
|
|
50
|
+
class CdpHelper {
|
|
51
|
+
/**
|
|
52
|
+
* Gets the page variant id for CDP in the required format
|
|
53
|
+
* @param {string} pageId the page id
|
|
54
|
+
* @param {string} language the language
|
|
55
|
+
* @param {string} variantId the variant id
|
|
56
|
+
* @returns {string} the formatted page variant id
|
|
57
|
+
*/
|
|
58
|
+
static getPageVariantId(pageId, language, variantId) {
|
|
59
|
+
const formattedPageId = pageId.replace(/[{}-]/g, '');
|
|
60
|
+
const formattedLanguage = language.replace('-', '_');
|
|
61
|
+
let formattedVariantId = variantId;
|
|
62
|
+
if (!variantId || variantId === exports.DEFAULT_VARIANT) {
|
|
63
|
+
formattedVariantId = 'default';
|
|
64
|
+
}
|
|
65
|
+
return `${formattedPageId}_${formattedLanguage}_${formattedVariantId}`.toLowerCase();
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets the content id for CDP in the required format `embedded_<id>_<lang>`
|
|
69
|
+
* @param {string} pageId the page id
|
|
70
|
+
* @param {string} language the language
|
|
71
|
+
* @returns {string} the content id
|
|
72
|
+
*/
|
|
73
|
+
static getContentId(pageId, language) {
|
|
74
|
+
const formattedPageId = pageId.replace(/[{}-]/g, '');
|
|
75
|
+
const formattedLanguage = language.replace('-', '_');
|
|
76
|
+
return `embedded_${formattedPageId}_${formattedLanguage}`.toLowerCase();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.CdpHelper = CdpHelper;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.GraphQLErrorPagesService = void 0;
|
|
16
|
+
const graphql_1 = require("../graphql");
|
|
17
|
+
const constants_1 = require("../constants");
|
|
18
|
+
const debug_1 = __importDefault(require("../debug"));
|
|
19
|
+
// The default query for request error handling
|
|
20
|
+
const defaultQuery = /* GraphQL */ `
|
|
21
|
+
query ErrorPagesQuery($siteName: String!, $language: String!) {
|
|
22
|
+
site {
|
|
23
|
+
siteInfo(site: $siteName) {
|
|
24
|
+
errorHandling(language: $language) {
|
|
25
|
+
notFoundPagePath
|
|
26
|
+
serverErrorPagePath
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
`;
|
|
32
|
+
/**
|
|
33
|
+
* Service that fetch the error pages data using Sitecore's GraphQL API.
|
|
34
|
+
*/
|
|
35
|
+
class GraphQLErrorPagesService {
|
|
36
|
+
/**
|
|
37
|
+
* Creates an instance of graphQL error pages service with the provided options
|
|
38
|
+
* @param {GraphQLErrorPagesServiceConfig} options instance
|
|
39
|
+
*/
|
|
40
|
+
constructor(options) {
|
|
41
|
+
this.options = options;
|
|
42
|
+
this.graphQLClient = this.getGraphQLClient();
|
|
43
|
+
}
|
|
44
|
+
get query() {
|
|
45
|
+
return defaultQuery;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Fetch list of error pages for the site
|
|
49
|
+
* @returns {ErrorPages} list of url's error pages
|
|
50
|
+
* @throws {Error} if the siteName is empty.
|
|
51
|
+
*/
|
|
52
|
+
fetchErrorPages() {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
const siteName = this.options.siteName;
|
|
55
|
+
const language = this.options.language;
|
|
56
|
+
if (!siteName) {
|
|
57
|
+
throw new Error(constants_1.siteNameError);
|
|
58
|
+
}
|
|
59
|
+
return this.graphQLClient.request(this.query, {
|
|
60
|
+
siteName,
|
|
61
|
+
language,
|
|
62
|
+
})
|
|
63
|
+
.then((result) => result.site.siteInfo ? result.site.siteInfo.errorHandling : null)
|
|
64
|
+
.catch((e) => Promise.reject(e));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets a GraphQL client that can make requests to the API. Uses graphql-request as the default
|
|
69
|
+
* library for fetching graphql data (@see GraphQLRequestClient). Override this method if you
|
|
70
|
+
* want to use something else.
|
|
71
|
+
* @returns {GraphQLClient} implementation
|
|
72
|
+
*/
|
|
73
|
+
getGraphQLClient() {
|
|
74
|
+
return new graphql_1.GraphQLRequestClient(this.options.endpoint, {
|
|
75
|
+
apiKey: this.options.apiKey,
|
|
76
|
+
debugger: debug_1.default.errorpages,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.GraphQLErrorPagesService = GraphQLErrorPagesService;
|
|
@@ -29,6 +29,7 @@ const defaultQuery = /* GraphQL */ `
|
|
|
29
29
|
target
|
|
30
30
|
redirectType
|
|
31
31
|
isQueryStringPreserved
|
|
32
|
+
locale
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
}
|
|
@@ -64,7 +65,7 @@ class GraphQLRedirectsService {
|
|
|
64
65
|
siteName,
|
|
65
66
|
});
|
|
66
67
|
return redirectsResult
|
|
67
|
-
.then((result) => result.site.siteInfo.redirects)
|
|
68
|
+
.then((result) => { var _a, _b; return ((_b = (_a = result === null || result === void 0 ? void 0 : result.site) === null || _a === void 0 ? void 0 : _a.siteInfo) === null || _b === void 0 ? void 0 : _b.redirects) || []; })
|
|
68
69
|
.catch((e) => Promise.reject(e));
|
|
69
70
|
});
|
|
70
71
|
}
|
|
@@ -73,6 +73,8 @@ class GraphQLSitemapXmlService {
|
|
|
73
73
|
return __awaiter(this, void 0, void 0, function* () {
|
|
74
74
|
const searchSitemap = `${PREFIX_NAME_SITEMAP}${id}.xml`;
|
|
75
75
|
const sitemaps = yield this.fetchSitemaps();
|
|
76
|
+
// added this item - there is default sitemap when will be available everytime
|
|
77
|
+
sitemaps.push('sitemap.xml');
|
|
76
78
|
return sitemaps.find((sitemap) => sitemap.includes(searchSitemap));
|
|
77
79
|
});
|
|
78
80
|
}
|
package/dist/cjs/site/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GraphQLSitemapXmlService = exports.GraphQLRedirectsService = exports.REDIRECT_TYPE_SERVER_TRANSFER = exports.REDIRECT_TYPE_302 = exports.REDIRECT_TYPE_301 = exports.GraphQLRobotsService = void 0;
|
|
3
|
+
exports.GraphQLErrorPagesService = exports.GraphQLSitemapXmlService = exports.GraphQLRedirectsService = exports.REDIRECT_TYPE_SERVER_TRANSFER = exports.REDIRECT_TYPE_302 = exports.REDIRECT_TYPE_301 = exports.GraphQLRobotsService = void 0;
|
|
4
4
|
var graphql_robots_service_1 = require("./graphql-robots-service");
|
|
5
5
|
Object.defineProperty(exports, "GraphQLRobotsService", { enumerable: true, get: function () { return graphql_robots_service_1.GraphQLRobotsService; } });
|
|
6
6
|
var graphql_redirects_service_1 = require("./graphql-redirects-service");
|
|
@@ -10,3 +10,5 @@ Object.defineProperty(exports, "REDIRECT_TYPE_SERVER_TRANSFER", { enumerable: tr
|
|
|
10
10
|
Object.defineProperty(exports, "GraphQLRedirectsService", { enumerable: true, get: function () { return graphql_redirects_service_1.GraphQLRedirectsService; } });
|
|
11
11
|
var graphql_sitemap_service_1 = require("./graphql-sitemap-service");
|
|
12
12
|
Object.defineProperty(exports, "GraphQLSitemapXmlService", { enumerable: true, get: function () { return graphql_sitemap_service_1.GraphQLSitemapXmlService; } });
|
|
13
|
+
var graphql_error_pages_service_1 = require("./graphql-error-pages-service");
|
|
14
|
+
Object.defineProperty(exports, "GraphQLErrorPagesService", { enumerable: true, get: function () { return graphql_error_pages_service_1.GraphQLErrorPagesService; } });
|