@sitecore-cloudsdk/personalize 0.1.2 → 0.1.4

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 (36) hide show
  1. package/README.md +32 -25
  2. package/dist/cjs/package.json +8 -4
  3. package/dist/cjs/src/lib/consts.d.ts +6 -0
  4. package/dist/cjs/src/lib/consts.js +8 -1
  5. package/dist/cjs/src/lib/initializer/client/initializer.d.ts +6 -22
  6. package/dist/cjs/src/lib/initializer/client/initializer.js +21 -41
  7. package/dist/cjs/src/lib/initializer/server/initializer.d.ts +0 -15
  8. package/dist/cjs/src/lib/initializer/server/initializer.js +1 -31
  9. package/dist/cjs/src/lib/personalization/personalize.d.ts +1 -1
  10. package/dist/cjs/src/lib/personalization/personalize.js +7 -5
  11. package/dist/cjs/src/lib/personalization/personalizeServer.d.ts +1 -1
  12. package/dist/cjs/src/lib/personalization/personalizeServer.js +2 -3
  13. package/dist/cjs/src/lib/personalization/personalizer.d.ts +4 -4
  14. package/dist/cjs/src/lib/personalization/personalizer.js +11 -7
  15. package/dist/cjs/src/lib/personalization/{callflow-edge-proxy-client.d.ts → send-call-flows-request.d.ts} +8 -16
  16. package/dist/cjs/src/lib/personalization/send-call-flows-request.js +42 -0
  17. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  18. package/dist/esm/package.json +8 -4
  19. package/dist/esm/src/lib/consts.d.ts +6 -0
  20. package/dist/esm/src/lib/consts.js +7 -0
  21. package/dist/esm/src/lib/initializer/client/initializer.d.ts +6 -22
  22. package/dist/esm/src/lib/initializer/client/initializer.js +20 -39
  23. package/dist/esm/src/lib/initializer/server/initializer.d.ts +0 -15
  24. package/dist/esm/src/lib/initializer/server/initializer.js +1 -29
  25. package/dist/esm/src/lib/personalization/personalize.d.ts +1 -1
  26. package/dist/esm/src/lib/personalization/personalize.js +7 -5
  27. package/dist/esm/src/lib/personalization/personalizeServer.d.ts +1 -1
  28. package/dist/esm/src/lib/personalization/personalizeServer.js +3 -4
  29. package/dist/esm/src/lib/personalization/personalizer.d.ts +4 -4
  30. package/dist/esm/src/lib/personalization/personalizer.js +10 -6
  31. package/dist/esm/src/lib/personalization/{callflow-edge-proxy-client.d.ts → send-call-flows-request.d.ts} +8 -16
  32. package/dist/esm/src/lib/personalization/send-call-flows-request.js +38 -0
  33. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  34. package/package.json +8 -4
  35. package/dist/cjs/src/lib/personalization/callflow-edge-proxy-client.js +0 -44
  36. package/dist/esm/src/lib/personalization/callflow-edge-proxy-client.js +0 -40
package/README.md CHANGED
@@ -6,10 +6,9 @@ This package provides browser- and server-side functions to run personalization
6
6
 
7
7
  ## Prerequisites
8
8
 
9
- To use the Sitecore Cloud SDK, you need:
9
+ To use the Sitecore Cloud SDK, you need an XM Cloud project. This project has to be created from the [XM Cloud foundation template](https://github.com/sitecorelabs/xmcloud-foundation-head) and deployed on XM Cloud.
10
10
 
11
- - A Next.js 13 app deployed on Sitecore XM Cloud.
12
- - An XM Cloud Plus subscription.
11
+ The foundation template contains an XM Cloud JSS Next.js app. You use the Sitecore Cloud SDK in this app. To be able to use the Sitecore Cloud SDK, you need JSS version 21.6.0 or newer.
13
12
 
14
13
  ## Installation
15
14
 
@@ -24,12 +23,20 @@ npm install @sitecore-cloudsdk/personalize
24
23
 
25
24
  ## Code examples
26
25
 
26
+ ---
27
+
28
+ **NOTE**
29
+
30
+ These code examples illustrate how the Sitecore Cloud SDK works in a standalone Next.js app. In production, you implement Sitecore Cloud SDK functionality differently, in a JSS Next.js app. See code examples for that environment in the official documentation.
31
+
32
+ ---
33
+
27
34
  Run personalization from the browser side:
28
35
 
29
36
  ```ts
30
- "use client";
31
- import { useEffect } from "react";
32
- import { init, personalize } from "@sitecore-cloudsdk/personalize/browser";
37
+ 'use client';
38
+ import { useEffect } from 'react';
39
+ import { init, personalize } from '@sitecore-cloudsdk/personalize/browser';
33
40
 
34
41
  export default function Home() {
35
42
  useEffect(() => {
@@ -38,8 +45,8 @@ export default function Home() {
38
45
 
39
46
  const initPersonalize = async () => {
40
47
  await init({
41
- sitecoreEdgeContextId: process.env.NEXT_PUBLIC_SITECORE_EDGE_CONTEXT_ID || "",
42
- siteName: process.env.NEXT_PUBLIC_SITENAME || "",
48
+ sitecoreEdgeContextId: process.env.NEXT_PUBLIC_SITECORE_EDGE_CONTEXT_ID || '',
49
+ siteName: process.env.NEXT_PUBLIC_SITENAME || '',
43
50
  enableBrowserCookie: true,
44
51
  });
45
52
 
@@ -48,15 +55,15 @@ export default function Home() {
48
55
 
49
56
  const runPersonalization = async () => {
50
57
  const personalizationData = {
51
- channel: "WEB",
52
- currency: "USD",
53
- friendlyId: "personalize_test",
54
- language: "EN",
58
+ channel: 'WEB',
59
+ currency: 'USD',
60
+ friendlyId: 'personalize_test',
61
+ language: 'EN',
55
62
  };
56
63
 
57
64
  await personalize(personalizationData);
58
65
 
59
- console.log("Ran personalization.");
66
+ console.log('Ran personalization.');
60
67
  };
61
68
 
62
69
  return (
@@ -70,17 +77,17 @@ export default function Home() {
70
77
  Run personalization from the server side:
71
78
 
72
79
  ```ts
73
- import { NextResponse } from "next/server";
74
- import type { NextRequest } from "next/server";
75
- import { init, personalize } from "@sitecore-cloudsdk/personalize/server";
80
+ import { NextResponse } from 'next/server';
81
+ import type { NextRequest } from 'next/server';
82
+ import { init, personalize } from '@sitecore-cloudsdk/personalize/server';
76
83
 
77
84
  export async function middleware(req: NextRequest) {
78
85
  const res = NextResponse.next();
79
86
 
80
87
  await init(
81
88
  {
82
- sitecoreEdgeContextId: process.env.NEXT_PUBLIC_SITECORE_EDGE_CONTEXT_ID || "",
83
- siteName: process.env.NEXT_PUBLIC_SITENAME || "",
89
+ sitecoreEdgeContextId: process.env.NEXT_PUBLIC_SITECORE_EDGE_CONTEXT_ID || '',
90
+ siteName: process.env.NEXT_PUBLIC_SITENAME || '',
84
91
  enableServerCookie: true,
85
92
  },
86
93
  req,
@@ -90,15 +97,15 @@ export async function middleware(req: NextRequest) {
90
97
  console.log(`Initialized "@sitecore-cloudsdk/personalize/server".`);
91
98
 
92
99
  const personalizationData = {
93
- channel: "WEB",
94
- currency: "EUR",
95
- friendlyId: "personalize_test",
96
- language: "EN",
100
+ channel: 'WEB',
101
+ currency: 'EUR',
102
+ friendlyId: 'personalize_test',
103
+ language: 'EN',
97
104
  };
98
105
 
99
106
  const personalizeRes = await personalize(personalizationData, req);
100
107
 
101
- console.log("personalizeResponse:", personalizeRes);
108
+ console.log('personalizeResponse:', personalizeRes);
102
109
 
103
110
  return res;
104
111
  }
@@ -106,8 +113,8 @@ export async function middleware(req: NextRequest) {
106
113
 
107
114
  ## Documentation
108
115
 
109
- Coming soon.
116
+ [Official Sitecore Cloud SDK documentation](https://doc.sitecore.com/xmc/en/developers/xm-cloud/sitecore-cloud-sdk.html)
110
117
 
111
118
  ### License
112
119
 
113
- The Sitecore Cloud SDK uses the [Apache 2.0 license](https://www.apache.org/licenses/LICENSE-2.0).
120
+ The Sitecore Cloud SDK uses the [Apache 2.0 license](https://www.apache.org/licenses/LICENSE-2.0).
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-cloudsdk/personalize",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "license": "Apache-2.0",
5
5
  "exports": {
6
6
  "./server": {
@@ -15,8 +15,8 @@
15
15
  }
16
16
  },
17
17
  "dependencies": {
18
- "@sitecore-cloudsdk/core": "^0.1.0-rc.2",
19
- "@sitecore-cloudsdk/utils": "^0.1.0-rc.2"
18
+ "@sitecore-cloudsdk/core": "^0.2.0",
19
+ "@sitecore-cloudsdk/utils": "^0.2.0"
20
20
  },
21
21
  "scripts": {
22
22
  "build": "npm run build:cjs && npm run build:es",
@@ -32,5 +32,9 @@
32
32
  "server.cjs",
33
33
  "server.js",
34
34
  "server.d.ts"
35
- ]
35
+ ],
36
+ "engines": {
37
+ "node": ">=18",
38
+ "npm": ">=9"
39
+ }
36
40
  }
@@ -2,3 +2,9 @@
2
2
  * Returns the version of the library.
3
3
  */
4
4
  export declare const LIBRARY_VERSION: string;
5
+ export declare enum ErrorMessages {
6
+ IE_0001 = "[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.",
7
+ IE_0006 = "[IE-0006] You must first initialize the \"personalize/browser\" module. Run the \"init\" function.",
8
+ IE_0007 = "[IE-0007] You must first initialize the \"personalize/server\" module. Run the \"init\" function.",
9
+ MV_0004 = "[MV-0004] \"friendlyId\" is required."
10
+ }
@@ -4,10 +4,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
5
5
  };
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.LIBRARY_VERSION = void 0;
7
+ exports.ErrorMessages = exports.LIBRARY_VERSION = void 0;
8
8
  /* eslint-disable @typescript-eslint/naming-convention */
9
9
  const package_json_1 = __importDefault(require("../../package.json"));
10
10
  /**
11
11
  * Returns the version of the library.
12
12
  */
13
13
  exports.LIBRARY_VERSION = package_json_1.default.version;
14
+ var ErrorMessages;
15
+ (function (ErrorMessages) {
16
+ ErrorMessages["IE_0001"] = "[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.";
17
+ ErrorMessages["IE_0006"] = "[IE-0006] You must first initialize the \"personalize/browser\" module. Run the \"init\" function.";
18
+ ErrorMessages["IE_0007"] = "[IE-0007] You must first initialize the \"personalize/server\" module. Run the \"init\" function.";
19
+ ErrorMessages["MV_0004"] = "[MV-0004] \"friendlyId\" is required.";
20
+ })(ErrorMessages || (exports.ErrorMessages = ErrorMessages = {}));
@@ -1,28 +1,12 @@
1
- import { Settings, SettingsParamsBrowser } 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: BrowserPersonalizeSettings | 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 `callFlowEPClient`.
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(): BrowserPersonalizeSettings;
1
+ import { SettingsParamsBrowser } from '@sitecore-cloudsdk/core';
2
+ export declare let initPromise: Promise<void> | null;
18
3
  /**
19
4
  * Initiates the Engage library using the global settings added by the developer
20
5
  * @param settingsInput - Global settings added by the developer
21
6
  * @returns A promise that resolves with an object that handles the library functionality
22
7
  */
23
8
  export declare function init(settingsInput: SettingsParamsBrowser): Promise<void>;
24
- export interface BrowserPersonalizeSettings {
25
- id: string;
26
- settings: Settings;
27
- callFlowEdgeProxyClient: CallFlowEdgeProxyClient;
28
- }
9
+ /**
10
+ * A function that handles the async browser init logic. Throws an error or awaits the promise.
11
+ */
12
+ export declare function awaitInit(): Promise<void>;
@@ -1,56 +1,27 @@
1
1
  "use strict";
2
- // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.init = exports.getDependencies = exports.setDependencies = void 0;
3
+ exports.awaitInit = exports.init = exports.initPromise = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
5
  const core_1 = require("@sitecore-cloudsdk/core");
6
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 `callFlowEPClient`.
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-0006] You must first initialize the "personalize/browser" module. Run the "init" function.`);
29
- }
30
- return dependencies;
31
- }
32
- exports.getDependencies = getDependencies;
7
+ exports.initPromise = null;
33
8
  /**
34
9
  * Initiates the Engage library using the global settings added by the developer
35
10
  * @param settingsInput - Global settings added by the developer
36
11
  * @returns A promise that resolves with an object that handles the library functionality
37
12
  */
38
13
  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.`);
14
+ if (typeof window === 'undefined')
15
+ throw new Error(consts_1.ErrorMessages.IE_0001);
16
+ try {
17
+ exports.initPromise = (0, core_1.initCore)(settingsInput);
18
+ await exports.initPromise;
19
+ }
20
+ catch (error) {
21
+ exports.initPromise = null;
22
+ throw new Error(error);
43
23
  }
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
24
  window.Engage ?? (window.Engage = {});
49
- setDependencies({
50
- callFlowEdgeProxyClient,
51
- id,
52
- settings,
53
- });
54
25
  window.Engage = {
55
26
  ...window.Engage,
56
27
  getBrowserId: () => (0, core_1.getBrowserId)(),
@@ -61,3 +32,12 @@ async function init(settingsInput) {
61
32
  };
62
33
  }
63
34
  exports.init = init;
35
+ /**
36
+ * A function that handles the async browser init logic. Throws an error or awaits the promise.
37
+ */
38
+ async function awaitInit() {
39
+ if (exports.initPromise === null)
40
+ throw new Error(consts_1.ErrorMessages.IE_0006);
41
+ await exports.initPromise;
42
+ }
43
+ exports.awaitInit = awaitInit;
@@ -1,23 +1,8 @@
1
1
  import { SettingsParamsServer } from '@sitecore-cloudsdk/core';
2
- import { BrowserPersonalizeSettings } from '../client/initializer';
3
2
  import { HttpResponse, MiddlewareNextResponse, Request } 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: ServerPersonalize | 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(): ServerPersonalize;
17
3
  /**
18
4
  * Initiates the server Engage library using the global settings added by the developer
19
5
  * @param settings - Global settings added by the developer
20
6
  * @returns A promise that resolves with an object that handles the library functionality
21
7
  */
22
8
  export declare function initServer<Response extends MiddlewareNextResponse | HttpResponse>(settingsInput: SettingsParamsServer, request: Request, response: Response): Promise<void>;
23
- export type ServerPersonalize = Omit<BrowserPersonalizeSettings, 'id'>;
@@ -1,32 +1,8 @@
1
1
  "use strict";
2
2
  // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.initServer = exports.getServerDependencies = exports.setDependencies = void 0;
4
+ exports.initServer = void 0;
5
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-0007] You must first initialize the "personalize/server" module. Run the "init" function.`);
26
- }
27
- return serverDependencies;
28
- }
29
- exports.getServerDependencies = getServerDependencies;
30
6
  /**
31
7
  * Initiates the server Engage library using the global settings added by the developer
32
8
  * @param settings - Global settings added by the developer
@@ -34,11 +10,5 @@ exports.getServerDependencies = getServerDependencies;
34
10
  */
35
11
  async function initServer(settingsInput, request, response) {
36
12
  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
13
  }
44
14
  exports.initServer = initServer;
@@ -1,4 +1,4 @@
1
- import { FailedCalledFlowsResponse } from './callflow-edge-proxy-client';
1
+ import { FailedCalledFlowsResponse } from './send-call-flows-request';
2
2
  import { PersonalizerInput } from './personalizer';
3
3
  /**
4
4
  * A function that executes an interactive experiment or web experiment over any web-based or mobile application.
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
- // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.personalize = void 0;
5
- const initializer_1 = require("../initializer/client/initializer");
4
+ const core_1 = require("@sitecore-cloudsdk/core");
6
5
  const personalizer_1 = require("./personalizer");
6
+ const initializer_1 = require("../initializer/client/initializer");
7
7
  /**
8
8
  * A function that executes an interactive experiment or web experiment over any web-based or mobile application.
9
9
  * @param personalizeData - The required/optional attributes in order to create a flow execution
@@ -11,8 +11,10 @@ const personalizer_1 = require("./personalizer");
11
11
  * Used to abort the request to execute an interactive experiment or web experiment.
12
12
  * @returns A flow execution response
13
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);
14
+ async function personalize(personalizeData, timeout) {
15
+ await (0, initializer_1.awaitInit)();
16
+ const id = (0, core_1.getBrowserId)();
17
+ const settings = (0, core_1.getSettings)();
18
+ return new personalizer_1.Personalizer(id).getInteractiveExperienceData(personalizeData, settings, timeout);
17
19
  }
18
20
  exports.personalize = personalize;
@@ -1,4 +1,4 @@
1
- import { FailedCalledFlowsResponse } from './callflow-edge-proxy-client';
1
+ import { FailedCalledFlowsResponse } from './send-call-flows-request';
2
2
  import { Request } from '@sitecore-cloudsdk/utils';
3
3
  import { PersonalizerInput } from './personalizer';
4
4
  /**
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.personalizeServer = void 0;
4
4
  // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
5
  const core_1 = require("@sitecore-cloudsdk/core");
6
- const initializer_1 = require("../initializer/server/initializer");
7
6
  const personalizer_1 = require("./personalizer");
8
7
  /**
9
8
  * A function that executes an interactive experiment or web experiment over any web-based or mobile application.
@@ -14,8 +13,8 @@ const personalizer_1 = require("./personalizer");
14
13
  * @returns A flow execution response
15
14
  */
16
15
  function personalizeServer(personalizeData, request, timeout) {
17
- const { callFlowEdgeProxyClient, settings } = (0, initializer_1.getServerDependencies)();
16
+ const settings = (0, core_1.getSettingsServer)();
18
17
  const id = (0, core_1.getBrowserIdFromRequest)(request, settings.cookieSettings.cookieName);
19
- return new personalizer_1.Personalizer(callFlowEdgeProxyClient, id).getInteractiveExperienceData(personalizeData, timeout);
18
+ return new personalizer_1.Personalizer(id).getInteractiveExperienceData(personalizeData, settings, timeout);
20
19
  }
21
20
  exports.personalizeServer = personalizeServer;
@@ -1,20 +1,20 @@
1
- import { FailedCalledFlowsResponse, PersonalizeClient } from './callflow-edge-proxy-client';
1
+ import { Settings } from '@sitecore-cloudsdk/core';
2
+ import { FailedCalledFlowsResponse } from './send-call-flows-request';
2
3
  import { NestedObject } from '@sitecore-cloudsdk/utils';
3
4
  export declare class Personalizer {
4
- private personalizeClient;
5
5
  private id;
6
6
  /**
7
7
  * The Personalizer Class runs a flow of interactive experiments.
8
8
  * @param personalizeClient - The data to be send to Sitecore EP
9
9
  * @param infer - The source of methods to estimate language and page parameters
10
10
  */
11
- constructor(personalizeClient: PersonalizeClient, id: string);
11
+ constructor(id: string);
12
12
  /**
13
13
  * A function to make a request to the Sitecore EP /callFlows API endpoint
14
14
  * @param timeout - Optional timeout in milliseconds to cancel the request
15
15
  * @returns - A promise that resolves with either the Sitecore EP response object or null
16
16
  */
17
- getInteractiveExperienceData(personalizeInput: PersonalizerInput, timeout?: number): Promise<unknown | null | FailedCalledFlowsResponse>;
17
+ getInteractiveExperienceData(personalizeInput: PersonalizerInput, settings: Settings, timeout?: number): Promise<unknown | null | FailedCalledFlowsResponse>;
18
18
  /**
19
19
  * A function that sanitizes the personalize input data
20
20
  * @returns - The sanitized object
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
- // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.Personalizer = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
5
  const core_1 = require("@sitecore-cloudsdk/core");
6
+ const send_call_flows_request_1 = require("./send-call-flows-request");
6
7
  const utils_1 = require("@sitecore-cloudsdk/utils");
8
+ const consts_1 = require("../consts");
7
9
  class Personalizer {
8
10
  /**
9
11
  * The Personalizer Class runs a flow of interactive experiments.
10
12
  * @param personalizeClient - The data to be send to Sitecore EP
11
13
  * @param infer - The source of methods to estimate language and page parameters
12
14
  */
13
- constructor(personalizeClient, id) {
14
- this.personalizeClient = personalizeClient;
15
+ constructor(id) {
15
16
  this.id = id;
16
17
  }
17
18
  /**
@@ -19,14 +20,17 @@ class Personalizer {
19
20
  * @param timeout - Optional timeout in milliseconds to cancel the request
20
21
  * @returns - A promise that resolves with either the Sitecore EP response object or null
21
22
  */
22
- async getInteractiveExperienceData(personalizeInput, timeout) {
23
+ async getInteractiveExperienceData(personalizeInput, settings, timeout) {
23
24
  this.validate(personalizeInput);
24
25
  const sanitizedInput = this.sanitizeInput(personalizeInput);
25
26
  const mappedData = this.mapPersonalizeInputToEPData(sanitizedInput);
26
27
  if (!mappedData.email && !mappedData.identifiers)
27
28
  mappedData.browserId = this.id;
28
- const response = await this.personalizeClient.sendCallFlowsRequest(mappedData, timeout);
29
- return response;
29
+ return (0, send_call_flows_request_1.sendCallFlowsRequest)(mappedData, settings, timeout)
30
+ .then((payload) => payload)
31
+ .catch((err) => {
32
+ throw err;
33
+ });
30
34
  }
31
35
  /**
32
36
  * A function that sanitizes the personalize input data
@@ -72,7 +76,7 @@ class Personalizer {
72
76
  */
73
77
  validate({ friendlyId }) {
74
78
  if (!friendlyId || friendlyId.trim().length === 0)
75
- throw new Error(`[MV-0004] "friendlyId" is required.`);
79
+ throw new Error(consts_1.ErrorMessages.MV_0004);
76
80
  }
77
81
  }
78
82
  exports.Personalizer = Personalizer;
@@ -1,21 +1,13 @@
1
1
  import { Settings } from '@sitecore-cloudsdk/core';
2
2
  import { NestedObject } from '@sitecore-cloudsdk/utils';
3
- export declare class CallFlowEdgeProxyClient implements PersonalizeClient {
4
- settings: Settings;
5
- /**
6
- * A helper class which handles the functionality for sending CALLFLOW requests
7
- * @param personalizeData - The mandatory payload to be send to Sitecore EP
8
- * @param settings - The global settings
9
- */
10
- constructor(settings: Settings);
11
- /**
12
- * A function that sends a CallFlow request to Sitecore EP
13
- * @param personalizeData - Properties to be send to Sitecore EP
14
- * @param timeout - Optional timeout in milliseconds to cancel the request
15
- * @returns - A promise that resolves with either the Sitecore EP response object or unknown
16
- */
17
- sendCallFlowsRequest(epCallFlowsBody: EPCallFlowsBody, timeout?: number): Promise<any>;
18
- }
3
+ /**
4
+ * A function that sends a CallFlow request to Sitecore EP
5
+ * @param personalizeData - Properties to be send to Sitecore EP
6
+ * @param settings - settings for the url params
7
+ * @param timeout - Optional timeout in milliseconds to cancel the request
8
+ * @returns - A promise that resolves with either the Sitecore EP response object or unknown
9
+ */
10
+ export declare function sendCallFlowsRequest(epCallFlowsBody: EPCallFlowsBody, settings: Settings, timeout?: number): Promise<any>;
19
11
  /**
20
12
  * An interface with the basic functionality that the derived classes needs to implement
21
13
  */
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sendCallFlowsRequest = void 0;
4
+ const utils_1 = require("@sitecore-cloudsdk/utils");
5
+ const consts_1 = require("../consts");
6
+ /**
7
+ * A function that sends a CallFlow request to Sitecore EP
8
+ * @param personalizeData - Properties to be send to Sitecore EP
9
+ * @param settings - settings for the url params
10
+ * @param timeout - Optional timeout in milliseconds to cancel the request
11
+ * @returns - A promise that resolves with either the Sitecore EP response object or unknown
12
+ */
13
+ async function sendCallFlowsRequest(epCallFlowsBody, settings, timeout) {
14
+ const requestUrl = `${settings.sitecoreEdgeUrl}/personalize/v2/callFlows?sitecoreContextId=${settings.sitecoreEdgeContextId}&siteId=${settings.siteName}`;
15
+ const fetchOptions = {
16
+ body: JSON.stringify(epCallFlowsBody),
17
+ headers: {
18
+ // eslint-disable-next-line @typescript-eslint/naming-convention
19
+ 'Content-Type': 'application/json',
20
+ // eslint-disable-next-line @typescript-eslint/naming-convention
21
+ 'X-Library-Version': consts_1.LIBRARY_VERSION,
22
+ },
23
+ method: 'POST',
24
+ };
25
+ if (timeout === undefined)
26
+ return fetch(requestUrl, fetchOptions)
27
+ .then((response) => response.json())
28
+ .catch(() => {
29
+ return null;
30
+ });
31
+ return (0, utils_1.fetchWithTimeout)(requestUrl, timeout, fetchOptions)
32
+ .then((response) => {
33
+ return (response && response.json()) || null;
34
+ })
35
+ .catch((err) => {
36
+ if (err.message.includes('IV-0006') || err.message.includes('IE-0002')) {
37
+ throw new Error(err.message);
38
+ }
39
+ return null;
40
+ });
41
+ }
42
+ exports.sendCallFlowsRequest = sendCallFlowsRequest;