@sitecore-cloudsdk/personalize 0.1.3 → 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 (35) hide show
  1. package/dist/cjs/package.json +8 -4
  2. package/dist/cjs/src/lib/consts.d.ts +6 -0
  3. package/dist/cjs/src/lib/consts.js +8 -1
  4. package/dist/cjs/src/lib/initializer/client/initializer.d.ts +6 -22
  5. package/dist/cjs/src/lib/initializer/client/initializer.js +21 -41
  6. package/dist/cjs/src/lib/initializer/server/initializer.d.ts +0 -15
  7. package/dist/cjs/src/lib/initializer/server/initializer.js +1 -31
  8. package/dist/cjs/src/lib/personalization/personalize.d.ts +1 -1
  9. package/dist/cjs/src/lib/personalization/personalize.js +7 -5
  10. package/dist/cjs/src/lib/personalization/personalizeServer.d.ts +1 -1
  11. package/dist/cjs/src/lib/personalization/personalizeServer.js +2 -3
  12. package/dist/cjs/src/lib/personalization/personalizer.d.ts +4 -4
  13. package/dist/cjs/src/lib/personalization/personalizer.js +11 -7
  14. package/dist/cjs/src/lib/personalization/{callflow-edge-proxy-client.d.ts → send-call-flows-request.d.ts} +8 -16
  15. package/dist/cjs/src/lib/personalization/send-call-flows-request.js +42 -0
  16. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  17. package/dist/esm/package.json +8 -4
  18. package/dist/esm/src/lib/consts.d.ts +6 -0
  19. package/dist/esm/src/lib/consts.js +7 -0
  20. package/dist/esm/src/lib/initializer/client/initializer.d.ts +6 -22
  21. package/dist/esm/src/lib/initializer/client/initializer.js +20 -39
  22. package/dist/esm/src/lib/initializer/server/initializer.d.ts +0 -15
  23. package/dist/esm/src/lib/initializer/server/initializer.js +1 -29
  24. package/dist/esm/src/lib/personalization/personalize.d.ts +1 -1
  25. package/dist/esm/src/lib/personalization/personalize.js +7 -5
  26. package/dist/esm/src/lib/personalization/personalizeServer.d.ts +1 -1
  27. package/dist/esm/src/lib/personalization/personalizeServer.js +3 -4
  28. package/dist/esm/src/lib/personalization/personalizer.d.ts +4 -4
  29. package/dist/esm/src/lib/personalization/personalizer.js +10 -6
  30. package/dist/esm/src/lib/personalization/{callflow-edge-proxy-client.d.ts → send-call-flows-request.d.ts} +8 -16
  31. package/dist/esm/src/lib/personalization/send-call-flows-request.js +38 -0
  32. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  33. package/package.json +8 -4
  34. package/dist/cjs/src/lib/personalization/callflow-edge-proxy-client.js +0 -44
  35. package/dist/esm/src/lib/personalization/callflow-edge-proxy-client.js +0 -40
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-cloudsdk/personalize",
3
- "version": "0.1.3",
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;