@sitecore-cloudsdk/personalize 0.1.0-rc.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.
Files changed (50) hide show
  1. package/README.md +11 -0
  2. package/dist/cjs/package.json +31 -0
  3. package/dist/cjs/src/client.d.ts +2 -0
  4. package/dist/cjs/src/client.js +8 -0
  5. package/dist/cjs/src/index.d.ts +4 -0
  6. package/dist/cjs/src/index.js +22 -0
  7. package/dist/cjs/src/lib/consts.d.ts +4 -0
  8. package/dist/cjs/src/lib/consts.js +13 -0
  9. package/dist/cjs/src/lib/initializer/client/initializer.d.ts +31 -0
  10. package/dist/cjs/src/lib/initializer/client/initializer.js +63 -0
  11. package/dist/cjs/src/lib/initializer/server/initializer.d.ts +23 -0
  12. package/dist/cjs/src/lib/initializer/server/initializer.js +44 -0
  13. package/dist/cjs/src/lib/personalization/callflow-edge-proxy-client.d.ts +61 -0
  14. package/dist/cjs/src/lib/personalization/callflow-edge-proxy-client.js +45 -0
  15. package/dist/cjs/src/lib/personalization/personalize.d.ts +10 -0
  16. package/dist/cjs/src/lib/personalization/personalize.js +18 -0
  17. package/dist/cjs/src/lib/personalization/personalizeServer.d.ts +12 -0
  18. package/dist/cjs/src/lib/personalization/personalizeServer.js +21 -0
  19. package/dist/cjs/src/lib/personalization/personalizer.d.ts +55 -0
  20. package/dist/cjs/src/lib/personalization/personalizer.js +78 -0
  21. package/dist/cjs/src/lib/utils/appendScriptWithAttributes.d.ts +6 -0
  22. package/dist/cjs/src/lib/utils/appendScriptWithAttributes.js +12 -0
  23. package/dist/cjs/src/server.d.ts +2 -0
  24. package/dist/cjs/src/server.js +8 -0
  25. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -0
  26. package/dist/esm/package.json +31 -0
  27. package/dist/esm/src/client.d.ts +2 -0
  28. package/dist/esm/src/client.js +3 -0
  29. package/dist/esm/src/index.d.ts +4 -0
  30. package/dist/esm/src/index.js +4 -0
  31. package/dist/esm/src/lib/consts.d.ts +4 -0
  32. package/dist/esm/src/lib/consts.js +7 -0
  33. package/dist/esm/src/lib/initializer/client/initializer.d.ts +31 -0
  34. package/dist/esm/src/lib/initializer/client/initializer.js +57 -0
  35. package/dist/esm/src/lib/initializer/server/initializer.d.ts +23 -0
  36. package/dist/esm/src/lib/initializer/server/initializer.js +38 -0
  37. package/dist/esm/src/lib/personalization/callflow-edge-proxy-client.d.ts +61 -0
  38. package/dist/esm/src/lib/personalization/callflow-edge-proxy-client.js +41 -0
  39. package/dist/esm/src/lib/personalization/personalize.d.ts +10 -0
  40. package/dist/esm/src/lib/personalization/personalize.js +14 -0
  41. package/dist/esm/src/lib/personalization/personalizeServer.d.ts +12 -0
  42. package/dist/esm/src/lib/personalization/personalizeServer.js +17 -0
  43. package/dist/esm/src/lib/personalization/personalizer.d.ts +55 -0
  44. package/dist/esm/src/lib/personalization/personalizer.js +74 -0
  45. package/dist/esm/src/lib/utils/appendScriptWithAttributes.d.ts +6 -0
  46. package/dist/esm/src/lib/utils/appendScriptWithAttributes.js +8 -0
  47. package/dist/esm/src/server.d.ts +2 -0
  48. package/dist/esm/src/server.js +3 -0
  49. package/dist/esm/tsconfig.tsbuildinfo +1 -0
  50. package/package.json +31 -0
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # personalize
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build personalize` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test personalize` to execute the unit tests via [Jest](https://jestjs.io).
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@sitecore-cloudsdk/personalize",
3
+ "version": "0.1.0-rc.0",
4
+ "license": "Apache-2.0",
5
+ "main": "dist/cjs/src/index.js",
6
+ "module": "dist/esm/src/index.js",
7
+ "types": "dist/esm/src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": {
11
+ "require": "./dist/cjs/src/index.d.ts",
12
+ "default": "./dist/esm/src/index.d.ts"
13
+ },
14
+ "import": "./dist/esm/src/index.js",
15
+ "require": "./dist/cjs/src/index.js"
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "@sitecore-cloudsdk/core": "0.x",
20
+ "@sitecore-cloudsdk/utils": "0.x"
21
+ },
22
+ "scripts": {
23
+ "build": "npm run build:cjs && npm run build:es",
24
+ "build:cjs": "tsc -b tsconfig.cjs.json",
25
+ "build:es": "tsc -b ."
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "README.md"
30
+ ]
31
+ }
@@ -0,0 +1,2 @@
1
+ export { init } from './lib/initializer/client/initializer';
2
+ export { personalize } from './lib/personalization/personalize';
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.personalize = exports.init = void 0;
5
+ var initializer_1 = require("./lib/initializer/client/initializer");
6
+ Object.defineProperty(exports, "init", { enumerable: true, get: function () { return initializer_1.init; } });
7
+ var personalize_1 = require("./lib/personalization/personalize");
8
+ Object.defineProperty(exports, "personalize", { enumerable: true, get: function () { return personalize_1.personalize; } });
@@ -0,0 +1,4 @@
1
+ export * from './client';
2
+ export * from './server';
3
+ export { LIBRARY_VERSION } from './lib/consts';
4
+ export type { IPersonalizerInput } from './lib/personalization/personalizer';
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.LIBRARY_VERSION = void 0;
19
+ __exportStar(require("./client"), exports);
20
+ __exportStar(require("./server"), exports);
21
+ var consts_1 = require("./lib/consts");
22
+ Object.defineProperty(exports, "LIBRARY_VERSION", { enumerable: true, get: function () { return consts_1.LIBRARY_VERSION; } });
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Returns the version of the library.
3
+ */
4
+ export declare const LIBRARY_VERSION: string;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.LIBRARY_VERSION = void 0;
8
+ /* eslint-disable @typescript-eslint/naming-convention */
9
+ const package_json_1 = __importDefault(require("../../package.json"));
10
+ /**
11
+ * Returns the version of the library.
12
+ */
13
+ exports.LIBRARY_VERSION = package_json_1.default.version;
@@ -0,0 +1,31 @@
1
+ import { ISettings, ISettingsParamsBrowser, IWebPersonalizationConfig } from '@sitecore-cloudsdk/core';
2
+ import { CallFlowEdgeProxyClient } from '../../personalization/callflow-edge-proxy-client';
3
+ /**
4
+ * Sets the personalize settings to be used by the application.
5
+ *
6
+ * @param settings - The personalize settings to be set, or `null` to clear the settings.
7
+ */
8
+ export declare function setDependencies(settings: IBrowserPersonalizeSettings | null): void;
9
+ /**
10
+ * Retrieves the browser personalize settings object.
11
+ *
12
+ * This function ensures that the browser personalize settings have been initialized and contain essential properties like `settings` and `callFlowCDPClient`.
13
+ *
14
+ * @returns The browser personalize settings object.
15
+ * @throws Error if the personalize settings haven't been initialized with the required properties.
16
+ */
17
+ export declare function getDependencies(): IBrowserPersonalizeSettings;
18
+ /**
19
+ * Initiates the Engage library using the global settings added by the developer
20
+ * @param settingsInput - Global settings added by the developer
21
+ * @returns A promise that resolves with an object that handles the library functionality
22
+ */
23
+ export declare function init(settingsInput: ISettingsParamsBrowserPersonalize): Promise<void>;
24
+ export type ISettingsParamsBrowserPersonalize = {
25
+ webPersonalization?: boolean | IWebPersonalizationConfig;
26
+ } & ISettingsParamsBrowser;
27
+ export interface IBrowserPersonalizeSettings {
28
+ id: string;
29
+ settings: ISettings;
30
+ callFlowEdgeProxyClient: CallFlowEdgeProxyClient;
31
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.init = exports.getDependencies = exports.setDependencies = void 0;
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const consts_1 = require("../../consts");
7
+ const callflow_edge_proxy_client_1 = require("../../personalization/callflow-edge-proxy-client");
8
+ let dependencies = null;
9
+ /**
10
+ * Sets the personalize settings to be used by the application.
11
+ *
12
+ * @param settings - The personalize settings to be set, or `null` to clear the settings.
13
+ */
14
+ function setDependencies(settings) {
15
+ dependencies = settings;
16
+ }
17
+ exports.setDependencies = setDependencies;
18
+ /**
19
+ * Retrieves the browser personalize settings object.
20
+ *
21
+ * This function ensures that the browser personalize settings have been initialized and contain essential properties like `settings` and `callFlowCDPClient`.
22
+ *
23
+ * @returns The browser personalize settings object.
24
+ * @throws Error if the personalize settings haven't been initialized with the required properties.
25
+ */
26
+ function getDependencies() {
27
+ if (!dependencies) {
28
+ throw Error(`[IE-0008] You must first initialize the "personalize" module. Run the "init" function.`);
29
+ }
30
+ return dependencies;
31
+ }
32
+ exports.getDependencies = getDependencies;
33
+ /**
34
+ * Initiates the Engage library using the global settings added by the developer
35
+ * @param settingsInput - Global settings added by the developer
36
+ * @returns A promise that resolves with an object that handles the library functionality
37
+ */
38
+ async function init(settingsInput) {
39
+ if (typeof window === 'undefined') {
40
+ throw new Error(
41
+ // eslint-disable-next-line max-len
42
+ `[IE-0001] The "window" object is not available on the server side. Use the "window" object only on the client side, and in the correct execution context.`);
43
+ }
44
+ await (0, core_1.initCore)(settingsInput);
45
+ const settings = (0, core_1.getSettings)();
46
+ const id = (0, core_1.getBrowserId)();
47
+ const callFlowEdgeProxyClient = new callflow_edge_proxy_client_1.CallFlowEdgeProxyClient(settings);
48
+ window.Engage ?? (window.Engage = {});
49
+ setDependencies({
50
+ callFlowEdgeProxyClient,
51
+ id,
52
+ settings,
53
+ });
54
+ window.Engage = {
55
+ ...window.Engage,
56
+ getBrowserId: () => (0, core_1.getBrowserId)(),
57
+ versions: {
58
+ ...window.Engage.versions,
59
+ personalize: consts_1.LIBRARY_VERSION,
60
+ },
61
+ };
62
+ }
63
+ exports.init = init;
@@ -0,0 +1,23 @@
1
+ import { ISettingsParamsServer } from '@sitecore-cloudsdk/core';
2
+ import { IBrowserPersonalizeSettings } from '../client/initializer';
3
+ import { IHttpResponse, IMiddlewareNextResponse, TRequest } from '@sitecore-cloudsdk/utils';
4
+ /**
5
+ * Sets the personalize settings to be used by the application.
6
+ *
7
+ * @param settings - The personalize settings to be set, or `null` to clear the settings.
8
+ */
9
+ export declare function setDependencies(settings: IServerPersonalize | null): void;
10
+ /**
11
+ * Retrieves the personalize server settings used by the application.
12
+ *
13
+ * @returns The personalize server settings.
14
+ * @throws Error if the personalize server settings haven't been initialized.
15
+ */
16
+ export declare function getServerDependencies(): IServerPersonalize;
17
+ /**
18
+ * Initiates the server Engage library using the global settings added by the developer
19
+ * @param settings - Global settings added by the developer
20
+ * @returns A promise that resolves with an object that handles the library functionality
21
+ */
22
+ export declare function initServer<TResponse extends IMiddlewareNextResponse | IHttpResponse>(settingsInput: ISettingsParamsServer, request: TRequest, response: TResponse): Promise<void>;
23
+ export type IServerPersonalize = Omit<IBrowserPersonalizeSettings, 'id'>;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.initServer = exports.getServerDependencies = exports.setDependencies = void 0;
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const callflow_edge_proxy_client_1 = require("../../personalization/callflow-edge-proxy-client");
7
+ let serverDependencies = null;
8
+ /**
9
+ * Sets the personalize settings to be used by the application.
10
+ *
11
+ * @param settings - The personalize settings to be set, or `null` to clear the settings.
12
+ */
13
+ function setDependencies(settings) {
14
+ serverDependencies = settings;
15
+ }
16
+ exports.setDependencies = setDependencies;
17
+ /**
18
+ * Retrieves the personalize server settings used by the application.
19
+ *
20
+ * @returns The personalize server settings.
21
+ * @throws Error if the personalize server settings haven't been initialized.
22
+ */
23
+ function getServerDependencies() {
24
+ if (!serverDependencies) {
25
+ throw Error(`[IE-0009] You must first initialize the "personalize" module. Run the "initServer" function.`);
26
+ }
27
+ return serverDependencies;
28
+ }
29
+ exports.getServerDependencies = getServerDependencies;
30
+ /**
31
+ * Initiates the server Engage library using the global settings added by the developer
32
+ * @param settings - Global settings added by the developer
33
+ * @returns A promise that resolves with an object that handles the library functionality
34
+ */
35
+ async function initServer(settingsInput, request, response) {
36
+ await (0, core_1.initCoreServer)(settingsInput, request, response);
37
+ const settings = (0, core_1.getSettingsServer)();
38
+ const callFlowEdgeProxyClient = new callflow_edge_proxy_client_1.CallFlowEdgeProxyClient(settings);
39
+ setDependencies({
40
+ callFlowEdgeProxyClient,
41
+ settings: settings,
42
+ });
43
+ }
44
+ exports.initServer = initServer;
@@ -0,0 +1,61 @@
1
+ import { ISettings } from '@sitecore-cloudsdk/core';
2
+ import { INestedObject } from '@sitecore-cloudsdk/utils';
3
+ export declare class CallFlowEdgeProxyClient implements IPersonalizeClient {
4
+ settings: ISettings;
5
+ /**
6
+ * A helper class which handles the functionality for sending CALLFLOW requests
7
+ * @param personalizeData - The mandatory payload to be send to Sitecore CDP
8
+ * @param settings - The global settings
9
+ */
10
+ constructor(settings: ISettings);
11
+ /**
12
+ * A function that sends a CallFlow request to Sitecore CDP
13
+ * @param personalizeData - Properties to be send to Sitecore CDP
14
+ * @param timeout - Optional timeout in milliseconds to cancel the request
15
+ * @returns - A promise that resolves with either the Sitecore CDP response object or unknown
16
+ */
17
+ sendCallFlowsRequest(cdpCallFlowsBody: ICdpCallFlowsBody, timeout?: number): Promise<any>;
18
+ }
19
+ /**
20
+ * An interface with the basic functionality that the derived classes needs to implement
21
+ */
22
+ export interface IPersonalizeClient {
23
+ settings: ISettings;
24
+ sendCallFlowsRequest: (cdpCallFlowAttributes: ICdpCallFlowsBody, timeout?: number) => Promise<unknown | null | IFailedCalledFlowsResponse>;
25
+ }
26
+ /**
27
+ * An interface that describes the failed response model from Sitecore CDP
28
+ */
29
+ export interface IFailedCalledFlowsResponse {
30
+ status: string;
31
+ code: string;
32
+ message: string;
33
+ developerMessage: string;
34
+ moreInfoUrl: string;
35
+ }
36
+ /**
37
+ * An interface that describes the identifier model attributes for the library
38
+ */
39
+ export interface ICdpIdentifier {
40
+ id: string;
41
+ provider: string;
42
+ }
43
+ /**
44
+ * An interface that describes the payload sent to Sitecore CDP library
45
+ */
46
+ export interface ICdpCallFlowsBody {
47
+ browserId?: string;
48
+ email?: string;
49
+ friendlyId: string;
50
+ identifiers?: ICdpIdentifier;
51
+ channel: string;
52
+ clientKey: string;
53
+ currencyCode: string;
54
+ language: string | undefined;
55
+ params?: ICdpCallFlowsParams;
56
+ pointOfSale: string;
57
+ }
58
+ /**
59
+ * A type that describes the params property of the ICdpCallFlowsBody
60
+ */
61
+ export type ICdpCallFlowsParams = INestedObject;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.CallFlowEdgeProxyClient = void 0;
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const utils_1 = require("@sitecore-cloudsdk/utils");
7
+ const consts_1 = require("../consts");
8
+ class CallFlowEdgeProxyClient {
9
+ /**
10
+ * A helper class which handles the functionality for sending CALLFLOW requests
11
+ * @param personalizeData - The mandatory payload to be send to Sitecore CDP
12
+ * @param settings - The global settings
13
+ */
14
+ constructor(settings) {
15
+ this.settings = settings;
16
+ }
17
+ /**
18
+ * A function that sends a CallFlow request to Sitecore CDP
19
+ * @param personalizeData - Properties to be send to Sitecore CDP
20
+ * @param timeout - Optional timeout in milliseconds to cancel the request
21
+ * @returns - A promise that resolves with either the Sitecore CDP response object or unknown
22
+ */
23
+ async sendCallFlowsRequest(cdpCallFlowsBody, timeout) {
24
+ const requestUrl = `${core_1.TARGET_URL}/personalize/v2/callFlows?sitecoreContextId=${this.settings.sitecoreEdgeContextId}&siteId=${this.settings.siteName}`;
25
+ const fetchOptions = {
26
+ body: JSON.stringify(cdpCallFlowsBody),
27
+ headers: {
28
+ // eslint-disable-next-line @typescript-eslint/naming-convention
29
+ 'Content-Type': 'application/json',
30
+ // eslint-disable-next-line @typescript-eslint/naming-convention
31
+ 'X-Library-Version': consts_1.LIBRARY_VERSION,
32
+ },
33
+ method: 'POST',
34
+ };
35
+ if (timeout === undefined)
36
+ return fetch(requestUrl, fetchOptions)
37
+ .then((response) => response.json())
38
+ .then((data) => data)
39
+ .catch(() => {
40
+ return null;
41
+ });
42
+ return (0, utils_1.fetchWithTimeout)(requestUrl, timeout, fetchOptions);
43
+ }
44
+ }
45
+ exports.CallFlowEdgeProxyClient = CallFlowEdgeProxyClient;
@@ -0,0 +1,10 @@
1
+ import { IFailedCalledFlowsResponse } from './callflow-edge-proxy-client';
2
+ import { IPersonalizerInput } from './personalizer';
3
+ /**
4
+ * A function that executes an interactive experiment or web experiment over any web-based or mobile application.
5
+ * @param personalizeData - The required/optional attributes in order to create a flow execution
6
+ * @param timeout - Optional timeout in milliseconds.
7
+ * Used to abort the request to execute an interactive experiment or web experiment.
8
+ * @returns A flow execution response
9
+ */
10
+ export declare function personalize(personalizeData: IPersonalizerInput, timeout?: number): Promise<unknown | null | IFailedCalledFlowsResponse>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.personalize = void 0;
5
+ const initializer_1 = require("../initializer/client/initializer");
6
+ const personalizer_1 = require("./personalizer");
7
+ /**
8
+ * A function that executes an interactive experiment or web experiment over any web-based or mobile application.
9
+ * @param personalizeData - The required/optional attributes in order to create a flow execution
10
+ * @param timeout - Optional timeout in milliseconds.
11
+ * Used to abort the request to execute an interactive experiment or web experiment.
12
+ * @returns A flow execution response
13
+ */
14
+ function personalize(personalizeData, timeout) {
15
+ const { callFlowEdgeProxyClient, id } = (0, initializer_1.getDependencies)();
16
+ return new personalizer_1.Personalizer(callFlowEdgeProxyClient, id).getInteractiveExperienceData(personalizeData, timeout);
17
+ }
18
+ exports.personalize = personalize;
@@ -0,0 +1,12 @@
1
+ import { IFailedCalledFlowsResponse } from './callflow-edge-proxy-client';
2
+ import { TRequest } from '@sitecore-cloudsdk/utils';
3
+ import { IPersonalizerInput } from './personalizer';
4
+ /**
5
+ * A function that executes an interactive experiment or web experiment over any web-based or mobile application.
6
+ * @param personalizeData - The required/optional attributes in order to create a flow execution
7
+ * @param request - Interface with constraint for extending request
8
+ * @param timeout - Optional timeout in milliseconds.
9
+ * Used to abort the request to execute an interactive experiment or web experiment.
10
+ * @returns A flow execution response
11
+ */
12
+ export declare function personalizeServer<T extends TRequest>(personalizeData: IPersonalizerInput, request: T, timeout?: number): Promise<unknown | null | IFailedCalledFlowsResponse>;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.personalizeServer = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const initializer_1 = require("../initializer/server/initializer");
7
+ const personalizer_1 = require("./personalizer");
8
+ /**
9
+ * A function that executes an interactive experiment or web experiment over any web-based or mobile application.
10
+ * @param personalizeData - The required/optional attributes in order to create a flow execution
11
+ * @param request - Interface with constraint for extending request
12
+ * @param timeout - Optional timeout in milliseconds.
13
+ * Used to abort the request to execute an interactive experiment or web experiment.
14
+ * @returns A flow execution response
15
+ */
16
+ function personalizeServer(personalizeData, request, timeout) {
17
+ const { callFlowEdgeProxyClient, settings } = (0, initializer_1.getServerDependencies)();
18
+ const id = (0, core_1.getBrowserIdFromRequest)(request, settings.cookieSettings.cookieName);
19
+ return new personalizer_1.Personalizer(callFlowEdgeProxyClient, id).getInteractiveExperienceData(personalizeData, timeout);
20
+ }
21
+ exports.personalizeServer = personalizeServer;
@@ -0,0 +1,55 @@
1
+ import { IFailedCalledFlowsResponse, IPersonalizeClient } from './callflow-edge-proxy-client';
2
+ import { INestedObject } from '@sitecore-cloudsdk/utils';
3
+ export declare class Personalizer {
4
+ private personalizeClient;
5
+ private id;
6
+ /**
7
+ * The Personalizer Class runs a flow of interactive experiments.
8
+ * @param personalizeClient - The data to be send to Sitecore CDP
9
+ * @param infer - The source of methods to estimate language and page parameters
10
+ */
11
+ constructor(personalizeClient: IPersonalizeClient, id: string);
12
+ /**
13
+ * A function to make a request to the Sitecore CDP /callFlows API endpoint
14
+ * @param timeout - Optional timeout in milliseconds to cancel the request
15
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
16
+ */
17
+ getInteractiveExperienceData(personalizeInput: IPersonalizerInput, timeout?: number): Promise<unknown | null | IFailedCalledFlowsResponse>;
18
+ /**
19
+ * A function that sanitizes the personalize input data
20
+ * @returns - The sanitized object
21
+ */
22
+ private sanitizeInput;
23
+ /**
24
+ * A function that maps the personalize input data with the CDP
25
+ * @returns - The CDP object
26
+ */
27
+ private mapPersonalizeInputToCDPData;
28
+ /**
29
+ * A validation method to throw error for the mandatory property for runtime users
30
+ */
31
+ private validate;
32
+ }
33
+ /**
34
+ * An interface that describes the flow execution model attributes input for the library
35
+ */
36
+ export interface IPersonalizerInput {
37
+ channel: string;
38
+ currency: string;
39
+ email?: string;
40
+ friendlyId: string;
41
+ identifier?: IPersonalizeIdentifierInput;
42
+ language?: string;
43
+ params?: IPersonalizeInputParams;
44
+ }
45
+ /**
46
+ * An interface that describes the identifier model attributes for the library
47
+ */
48
+ export interface IPersonalizeIdentifierInput {
49
+ id: string;
50
+ provider: string;
51
+ }
52
+ /**
53
+ * A type that describes the params field
54
+ */
55
+ export type IPersonalizeInputParams = INestedObject;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Personalizer = void 0;
5
+ const core_1 = require("@sitecore-cloudsdk/core");
6
+ const utils_1 = require("@sitecore-cloudsdk/utils");
7
+ class Personalizer {
8
+ /**
9
+ * The Personalizer Class runs a flow of interactive experiments.
10
+ * @param personalizeClient - The data to be send to Sitecore CDP
11
+ * @param infer - The source of methods to estimate language and page parameters
12
+ */
13
+ constructor(personalizeClient, id) {
14
+ this.personalizeClient = personalizeClient;
15
+ this.id = id;
16
+ }
17
+ /**
18
+ * A function to make a request to the Sitecore CDP /callFlows API endpoint
19
+ * @param timeout - Optional timeout in milliseconds to cancel the request
20
+ * @returns - A promise that resolves with either the Sitecore CDP response object or null
21
+ */
22
+ async getInteractiveExperienceData(personalizeInput, timeout) {
23
+ this.validate(personalizeInput);
24
+ const sanitizedInput = this.sanitizeInput(personalizeInput);
25
+ const mappedData = this.mapPersonalizeInputToCDPData(sanitizedInput);
26
+ if (!mappedData.email && !mappedData.identifiers)
27
+ mappedData.browserId = this.id;
28
+ const response = await this.personalizeClient.sendCallFlowsRequest(mappedData, timeout);
29
+ return response;
30
+ }
31
+ /**
32
+ * A function that sanitizes the personalize input data
33
+ * @returns - The sanitized object
34
+ */
35
+ sanitizeInput(personalizerInput) {
36
+ const sanitizedInput = {
37
+ channel: personalizerInput.channel,
38
+ currency: personalizerInput.currency,
39
+ friendlyId: personalizerInput.friendlyId,
40
+ language: personalizerInput.language,
41
+ };
42
+ if (personalizerInput.identifier &&
43
+ personalizerInput.identifier.id &&
44
+ personalizerInput.identifier.id.trim().length > 0)
45
+ sanitizedInput.identifier = personalizerInput.identifier;
46
+ if (personalizerInput.email && personalizerInput.email.trim().length > 0)
47
+ sanitizedInput.email = personalizerInput.email;
48
+ if (personalizerInput.params && Object.keys(personalizerInput.params).length > 0)
49
+ sanitizedInput.params = (0, utils_1.flattenObject)({ object: personalizerInput.params });
50
+ return sanitizedInput;
51
+ }
52
+ /**
53
+ * A function that maps the personalize input data with the CDP
54
+ * @returns - The CDP object
55
+ */
56
+ mapPersonalizeInputToCDPData(input) {
57
+ const mappedData = {
58
+ channel: input.channel,
59
+ clientKey: '',
60
+ currencyCode: input.currency,
61
+ email: input.email,
62
+ friendlyId: input.friendlyId,
63
+ identifiers: input.identifier,
64
+ language: input.language ?? (0, core_1.language)(),
65
+ params: input.params,
66
+ pointOfSale: '',
67
+ };
68
+ return mappedData;
69
+ }
70
+ /**
71
+ * A validation method to throw error for the mandatory property for runtime users
72
+ */
73
+ validate({ friendlyId }) {
74
+ if (!friendlyId || friendlyId.trim().length === 0)
75
+ throw new Error(`[MV-0008] "friendlyId" is required.`);
76
+ }
77
+ }
78
+ exports.Personalizer = Personalizer;
@@ -0,0 +1,6 @@
1
+ export declare function appendScriptWithAttributes(attributes: IScriptAttributes): void;
2
+ interface IScriptAttributes {
3
+ async: boolean;
4
+ src: string;
5
+ }
6
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.appendScriptWithAttributes = void 0;
5
+ function appendScriptWithAttributes(attributes) {
6
+ const sdkScriptElement = document.createElement('script');
7
+ sdkScriptElement.type = 'text/javascript';
8
+ sdkScriptElement.src = attributes.src;
9
+ sdkScriptElement.async = attributes.async;
10
+ document.head.appendChild(sdkScriptElement);
11
+ }
12
+ exports.appendScriptWithAttributes = appendScriptWithAttributes;
@@ -0,0 +1,2 @@
1
+ export { initServer } from './lib/initializer/server/initializer';
2
+ export { personalizeServer } from './lib/personalization/personalizeServer';
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.personalizeServer = exports.initServer = void 0;
5
+ var initializer_1 = require("./lib/initializer/server/initializer");
6
+ Object.defineProperty(exports, "initServer", { enumerable: true, get: function () { return initializer_1.initServer; } });
7
+ var personalizeServer_1 = require("./lib/personalization/personalizeServer");
8
+ Object.defineProperty(exports, "personalizeServer", { enumerable: true, get: function () { return personalizeServer_1.personalizeServer; } });