@sitecore-cloudsdk/core 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 (86) hide show
  1. package/README.md +11 -0
  2. package/dist/cjs/package.json +30 -0
  3. package/dist/cjs/src/index.d.ts +18 -0
  4. package/dist/cjs/src/index.js +44 -0
  5. package/dist/cjs/src/lib/consts.d.ts +6 -0
  6. package/dist/cjs/src/lib/consts.js +15 -0
  7. package/dist/cjs/src/lib/cookie/create-cookie.d.ts +8 -0
  8. package/dist/cjs/src/lib/cookie/create-cookie.js +23 -0
  9. package/dist/cjs/src/lib/cookie/get-browser-id-from-middleware-request.d.ts +11 -0
  10. package/dist/cjs/src/lib/cookie/get-browser-id-from-middleware-request.js +24 -0
  11. package/dist/cjs/src/lib/cookie/get-browser-id-from-request.d.ts +9 -0
  12. package/dist/cjs/src/lib/cookie/get-browser-id-from-request.js +25 -0
  13. package/dist/cjs/src/lib/cookie/get-default-cookie-attributes.d.ts +7 -0
  14. package/dist/cjs/src/lib/cookie/get-default-cookie-attributes.js +21 -0
  15. package/dist/cjs/src/lib/cookie/handle-http-cookie.d.ts +13 -0
  16. package/dist/cjs/src/lib/cookie/handle-http-cookie.js +35 -0
  17. package/dist/cjs/src/lib/cookie/handle-next-js-middleware-cookie.d.ts +16 -0
  18. package/dist/cjs/src/lib/cookie/handle-next-js-middleware-cookie.js +27 -0
  19. package/dist/cjs/src/lib/cookie/handle-server-cookie.d.ts +10 -0
  20. package/dist/cjs/src/lib/cookie/handle-server-cookie.js +32 -0
  21. package/dist/cjs/src/lib/infer/infer.d.ts +12 -0
  22. package/dist/cjs/src/lib/infer/infer.js +26 -0
  23. package/dist/cjs/src/lib/init/construct-get-proxy-settings-url.d.ts +6 -0
  24. package/dist/cjs/src/lib/init/construct-get-proxy-settings-url.js +14 -0
  25. package/dist/cjs/src/lib/init/get-browser-id.d.ts +5 -0
  26. package/dist/cjs/src/lib/init/get-browser-id.js +15 -0
  27. package/dist/cjs/src/lib/init/get-guest-id.d.ts +24 -0
  28. package/dist/cjs/src/lib/init/get-guest-id.js +25 -0
  29. package/dist/cjs/src/lib/init/get-proxy-settings.d.ts +8 -0
  30. package/dist/cjs/src/lib/init/get-proxy-settings.js +35 -0
  31. package/dist/cjs/src/lib/init/init-core-server.d.ts +21 -0
  32. package/dist/cjs/src/lib/init/init-core-server.js +47 -0
  33. package/dist/cjs/src/lib/init/init-core.d.ts +32 -0
  34. package/dist/cjs/src/lib/init/init-core.js +69 -0
  35. package/dist/cjs/src/lib/interfaces.d.ts +20 -0
  36. package/dist/cjs/src/lib/interfaces.js +3 -0
  37. package/dist/cjs/src/lib/settings/create-settings.d.ts +7 -0
  38. package/dist/cjs/src/lib/settings/create-settings.js +26 -0
  39. package/dist/cjs/src/lib/settings/interfaces.d.ts +61 -0
  40. package/dist/cjs/src/lib/settings/interfaces.js +3 -0
  41. package/dist/cjs/src/lib/settings/validate-settings.d.ts +10 -0
  42. package/dist/cjs/src/lib/settings/validate-settings.js +19 -0
  43. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -0
  44. package/dist/esm/package.json +30 -0
  45. package/dist/esm/src/index.d.ts +18 -0
  46. package/dist/esm/src/index.js +17 -0
  47. package/dist/esm/src/lib/consts.d.ts +6 -0
  48. package/dist/esm/src/lib/consts.js +9 -0
  49. package/dist/esm/src/lib/cookie/create-cookie.d.ts +8 -0
  50. package/dist/esm/src/lib/cookie/create-cookie.js +19 -0
  51. package/dist/esm/src/lib/cookie/get-browser-id-from-middleware-request.d.ts +11 -0
  52. package/dist/esm/src/lib/cookie/get-browser-id-from-middleware-request.js +20 -0
  53. package/dist/esm/src/lib/cookie/get-browser-id-from-request.d.ts +9 -0
  54. package/dist/esm/src/lib/cookie/get-browser-id-from-request.js +21 -0
  55. package/dist/esm/src/lib/cookie/get-default-cookie-attributes.d.ts +7 -0
  56. package/dist/esm/src/lib/cookie/get-default-cookie-attributes.js +17 -0
  57. package/dist/esm/src/lib/cookie/handle-http-cookie.d.ts +13 -0
  58. package/dist/esm/src/lib/cookie/handle-http-cookie.js +31 -0
  59. package/dist/esm/src/lib/cookie/handle-next-js-middleware-cookie.d.ts +16 -0
  60. package/dist/esm/src/lib/cookie/handle-next-js-middleware-cookie.js +23 -0
  61. package/dist/esm/src/lib/cookie/handle-server-cookie.d.ts +10 -0
  62. package/dist/esm/src/lib/cookie/handle-server-cookie.js +28 -0
  63. package/dist/esm/src/lib/infer/infer.d.ts +12 -0
  64. package/dist/esm/src/lib/infer/infer.js +21 -0
  65. package/dist/esm/src/lib/init/construct-get-proxy-settings-url.d.ts +6 -0
  66. package/dist/esm/src/lib/init/construct-get-proxy-settings-url.js +10 -0
  67. package/dist/esm/src/lib/init/get-browser-id.d.ts +5 -0
  68. package/dist/esm/src/lib/init/get-browser-id.js +11 -0
  69. package/dist/esm/src/lib/init/get-guest-id.d.ts +24 -0
  70. package/dist/esm/src/lib/init/get-guest-id.js +21 -0
  71. package/dist/esm/src/lib/init/get-proxy-settings.d.ts +8 -0
  72. package/dist/esm/src/lib/init/get-proxy-settings.js +31 -0
  73. package/dist/esm/src/lib/init/init-core-server.d.ts +21 -0
  74. package/dist/esm/src/lib/init/init-core-server.js +41 -0
  75. package/dist/esm/src/lib/init/init-core.d.ts +32 -0
  76. package/dist/esm/src/lib/init/init-core.js +62 -0
  77. package/dist/esm/src/lib/interfaces.d.ts +20 -0
  78. package/dist/esm/src/lib/interfaces.js +2 -0
  79. package/dist/esm/src/lib/settings/create-settings.d.ts +7 -0
  80. package/dist/esm/src/lib/settings/create-settings.js +22 -0
  81. package/dist/esm/src/lib/settings/interfaces.d.ts +61 -0
  82. package/dist/esm/src/lib/settings/interfaces.js +2 -0
  83. package/dist/esm/src/lib/settings/validate-settings.d.ts +10 -0
  84. package/dist/esm/src/lib/settings/validate-settings.js +15 -0
  85. package/dist/esm/tsconfig.tsbuildinfo +1 -0
  86. package/package.json +30 -0
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # core
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build core` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test core` to execute the unit tests via [Jest](https://jestjs.io).
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@sitecore-cloudsdk/core",
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/utils": "0.x"
20
+ },
21
+ "scripts": {
22
+ "build": "npm run build:cjs && npm run build:es",
23
+ "build:cjs": "tsc -b tsconfig.cjs.json",
24
+ "build:es": "tsc -b ."
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "README.md"
29
+ ]
30
+ }
@@ -0,0 +1,18 @@
1
+ export { createCookie } from './lib/cookie/create-cookie';
2
+ export { getBrowserIdFromMiddlewareRequest } from './lib/cookie/get-browser-id-from-middleware-request';
3
+ export { getDefaultCookieAttributes } from './lib/cookie/get-default-cookie-attributes';
4
+ export { handleHttpCookie } from './lib/cookie/handle-http-cookie';
5
+ export { handleNextJsMiddlewareCookie } from './lib/cookie/handle-next-js-middleware-cookie';
6
+ export { handleServerCookie } from './lib/cookie/handle-server-cookie';
7
+ export { getBrowserId } from './lib/init/get-browser-id';
8
+ export { getProxySettings } from './lib/init/get-proxy-settings';
9
+ export { getGuestId } from './lib/init/get-guest-id';
10
+ export { createSettings } from './lib/settings/create-settings';
11
+ export { getSettings, initCore } from './lib/init/init-core';
12
+ export { getSettingsServer, initCoreServer } from './lib/init/init-core-server';
13
+ export { validateSettings } from './lib/settings/validate-settings';
14
+ export { language, pageName } from './lib/infer/infer';
15
+ export { getBrowserIdFromRequest } from './lib/cookie/get-browser-id-from-request';
16
+ export { API_VERSION, BID_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, TARGET_URL, } from './lib/consts';
17
+ export type { ICdpResponse, IInfer } from './lib/interfaces';
18
+ export type { ICookieSettings, ISettings, ISettingsParamsBrowser, ISettingsParamsServer, IWebExperiencesSettings, IWebPersonalizationConfig, } from './lib/settings/interfaces';
@@ -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.TARGET_URL = exports.LIBRARY_VERSION = exports.DEFAULT_COOKIE_EXPIRY_DAYS = exports.DAILY_SECONDS = exports.BID_PREFIX = exports.API_VERSION = exports.getBrowserIdFromRequest = exports.pageName = exports.language = exports.validateSettings = exports.initCoreServer = exports.getSettingsServer = exports.initCore = exports.getSettings = exports.createSettings = exports.getGuestId = exports.getProxySettings = exports.getBrowserId = exports.handleServerCookie = exports.handleNextJsMiddlewareCookie = exports.handleHttpCookie = exports.getDefaultCookieAttributes = exports.getBrowserIdFromMiddlewareRequest = exports.createCookie = void 0;
5
+ var create_cookie_1 = require("./lib/cookie/create-cookie");
6
+ Object.defineProperty(exports, "createCookie", { enumerable: true, get: function () { return create_cookie_1.createCookie; } });
7
+ var get_browser_id_from_middleware_request_1 = require("./lib/cookie/get-browser-id-from-middleware-request");
8
+ Object.defineProperty(exports, "getBrowserIdFromMiddlewareRequest", { enumerable: true, get: function () { return get_browser_id_from_middleware_request_1.getBrowserIdFromMiddlewareRequest; } });
9
+ var get_default_cookie_attributes_1 = require("./lib/cookie/get-default-cookie-attributes");
10
+ Object.defineProperty(exports, "getDefaultCookieAttributes", { enumerable: true, get: function () { return get_default_cookie_attributes_1.getDefaultCookieAttributes; } });
11
+ var handle_http_cookie_1 = require("./lib/cookie/handle-http-cookie");
12
+ Object.defineProperty(exports, "handleHttpCookie", { enumerable: true, get: function () { return handle_http_cookie_1.handleHttpCookie; } });
13
+ var handle_next_js_middleware_cookie_1 = require("./lib/cookie/handle-next-js-middleware-cookie");
14
+ Object.defineProperty(exports, "handleNextJsMiddlewareCookie", { enumerable: true, get: function () { return handle_next_js_middleware_cookie_1.handleNextJsMiddlewareCookie; } });
15
+ var handle_server_cookie_1 = require("./lib/cookie/handle-server-cookie");
16
+ Object.defineProperty(exports, "handleServerCookie", { enumerable: true, get: function () { return handle_server_cookie_1.handleServerCookie; } });
17
+ var get_browser_id_1 = require("./lib/init/get-browser-id");
18
+ Object.defineProperty(exports, "getBrowserId", { enumerable: true, get: function () { return get_browser_id_1.getBrowserId; } });
19
+ var get_proxy_settings_1 = require("./lib/init/get-proxy-settings");
20
+ Object.defineProperty(exports, "getProxySettings", { enumerable: true, get: function () { return get_proxy_settings_1.getProxySettings; } });
21
+ var get_guest_id_1 = require("./lib/init/get-guest-id");
22
+ Object.defineProperty(exports, "getGuestId", { enumerable: true, get: function () { return get_guest_id_1.getGuestId; } });
23
+ var create_settings_1 = require("./lib/settings/create-settings");
24
+ Object.defineProperty(exports, "createSettings", { enumerable: true, get: function () { return create_settings_1.createSettings; } });
25
+ var init_core_1 = require("./lib/init/init-core");
26
+ Object.defineProperty(exports, "getSettings", { enumerable: true, get: function () { return init_core_1.getSettings; } });
27
+ Object.defineProperty(exports, "initCore", { enumerable: true, get: function () { return init_core_1.initCore; } });
28
+ var init_core_server_1 = require("./lib/init/init-core-server");
29
+ Object.defineProperty(exports, "getSettingsServer", { enumerable: true, get: function () { return init_core_server_1.getSettingsServer; } });
30
+ Object.defineProperty(exports, "initCoreServer", { enumerable: true, get: function () { return init_core_server_1.initCoreServer; } });
31
+ var validate_settings_1 = require("./lib/settings/validate-settings");
32
+ Object.defineProperty(exports, "validateSettings", { enumerable: true, get: function () { return validate_settings_1.validateSettings; } });
33
+ var infer_1 = require("./lib/infer/infer");
34
+ Object.defineProperty(exports, "language", { enumerable: true, get: function () { return infer_1.language; } });
35
+ Object.defineProperty(exports, "pageName", { enumerable: true, get: function () { return infer_1.pageName; } });
36
+ var get_browser_id_from_request_1 = require("./lib/cookie/get-browser-id-from-request");
37
+ Object.defineProperty(exports, "getBrowserIdFromRequest", { enumerable: true, get: function () { return get_browser_id_from_request_1.getBrowserIdFromRequest; } });
38
+ var consts_1 = require("./lib/consts");
39
+ Object.defineProperty(exports, "API_VERSION", { enumerable: true, get: function () { return consts_1.API_VERSION; } });
40
+ Object.defineProperty(exports, "BID_PREFIX", { enumerable: true, get: function () { return consts_1.BID_PREFIX; } });
41
+ Object.defineProperty(exports, "DAILY_SECONDS", { enumerable: true, get: function () { return consts_1.DAILY_SECONDS; } });
42
+ Object.defineProperty(exports, "DEFAULT_COOKIE_EXPIRY_DAYS", { enumerable: true, get: function () { return consts_1.DEFAULT_COOKIE_EXPIRY_DAYS; } });
43
+ Object.defineProperty(exports, "LIBRARY_VERSION", { enumerable: true, get: function () { return consts_1.LIBRARY_VERSION; } });
44
+ Object.defineProperty(exports, "TARGET_URL", { enumerable: true, get: function () { return consts_1.TARGET_URL; } });
@@ -0,0 +1,6 @@
1
+ export declare const LIBRARY_VERSION: string;
2
+ export declare const BID_PREFIX = "bid_";
3
+ export declare const DEFAULT_COOKIE_EXPIRY_DAYS = 730;
4
+ export declare const DAILY_SECONDS = 86400;
5
+ export declare const API_VERSION = "v1.2";
6
+ export declare const TARGET_URL = "https://edge-platform.sitecorecloud.io";
@@ -0,0 +1,15 @@
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.TARGET_URL = exports.API_VERSION = exports.DAILY_SECONDS = exports.DEFAULT_COOKIE_EXPIRY_DAYS = exports.BID_PREFIX = exports.LIBRARY_VERSION = void 0;
8
+ /* eslint-disable @typescript-eslint/naming-convention */
9
+ const package_json_1 = __importDefault(require("../../package.json"));
10
+ exports.LIBRARY_VERSION = package_json_1.default.version;
11
+ exports.BID_PREFIX = 'bid_';
12
+ exports.DEFAULT_COOKIE_EXPIRY_DAYS = 730;
13
+ exports.DAILY_SECONDS = 86400;
14
+ exports.API_VERSION = 'v1.2';
15
+ exports.TARGET_URL = 'https://edge-platform.sitecorecloud.io';
@@ -0,0 +1,8 @@
1
+ import { ISettings } from '../settings/interfaces';
2
+ /**
3
+ * Creates and adds the cookie to the document
4
+ * @param sitecoreEdgeContextId - The sitecoreEdgeContextId from global settings
5
+ * @param settings - The ICookieSettings settings object
6
+ * @returns - browserId or undefined on error
7
+ */
8
+ export declare function createCookie(settings: ISettings): Promise<void>;
@@ -0,0 +1,23 @@
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.createCookie = void 0;
5
+ const utils_1 = require("@sitecore-cloudsdk/utils");
6
+ const get_default_cookie_attributes_1 = require("./get-default-cookie-attributes");
7
+ const get_proxy_settings_1 = require("../init/get-proxy-settings");
8
+ const consts_1 = require("../consts");
9
+ /**
10
+ * Creates and adds the cookie to the document
11
+ * @param sitecoreEdgeContextId - The sitecoreEdgeContextId from global settings
12
+ * @param settings - The ICookieSettings settings object
13
+ * @returns - browserId or undefined on error
14
+ */
15
+ async function createCookie(settings) {
16
+ const { browserId, clientKey } = await (0, get_proxy_settings_1.getProxySettings)(settings.sitecoreEdgeContextId);
17
+ settings.cookieSettings.cookieName = `${consts_1.BID_PREFIX}${clientKey}`;
18
+ if ((0, utils_1.cookieExists)(window.document.cookie, settings.cookieSettings.cookieName) || !browserId)
19
+ return;
20
+ const attributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(settings.cookieSettings.cookieExpiryDays, settings.cookieSettings.cookieDomain);
21
+ document.cookie = (0, utils_1.createCookieString)(settings.cookieSettings.cookieName, browserId, attributes);
22
+ }
23
+ exports.createCookie = createCookie;
@@ -0,0 +1,11 @@
1
+ import { IMiddlewareRequest } from '@sitecore-cloudsdk/utils';
2
+ /**
3
+ * Retrieves the browser ID from the provided Middleware Request by extracting the cookie value
4
+ * associated with the specified 'cookieName'. The function first checks for Next.js v12 cookie values,
5
+ * and if not found, it checks for Next.js v13 cookie values.
6
+ *
7
+ * @param request - The Middleware Request object.
8
+ * @param cookieName - The name of the cookie to retrieve.
9
+ * @returns The browser ID extracted from the cookie, or undefined if not found.
10
+ */
11
+ export declare function getBrowserIdFromMiddlewareRequest(request: IMiddlewareRequest, cookieName: string): string | undefined;
@@ -0,0 +1,24 @@
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.getBrowserIdFromMiddlewareRequest = void 0;
5
+ /**
6
+ * Retrieves the browser ID from the provided Middleware Request by extracting the cookie value
7
+ * associated with the specified 'cookieName'. The function first checks for Next.js v12 cookie values,
8
+ * and if not found, it checks for Next.js v13 cookie values.
9
+ *
10
+ * @param request - The Middleware Request object.
11
+ * @param cookieName - The name of the cookie to retrieve.
12
+ * @returns The browser ID extracted from the cookie, or undefined if not found.
13
+ */
14
+ function getBrowserIdFromMiddlewareRequest(request, cookieName) {
15
+ const cookieValueFromRequest = request.cookies.get(cookieName);
16
+ // It checks nextjs v12 cookie values
17
+ if (typeof cookieValueFromRequest === 'string')
18
+ return cookieValueFromRequest;
19
+ // It checks nextjs v13 cookie values
20
+ if (typeof cookieValueFromRequest === 'object')
21
+ return cookieValueFromRequest.value;
22
+ return undefined;
23
+ }
24
+ exports.getBrowserIdFromMiddlewareRequest = getBrowserIdFromMiddlewareRequest;
@@ -0,0 +1,9 @@
1
+ import { TRequest } from '@sitecore-cloudsdk/utils';
2
+ /**
3
+ * Retrieves the browser ID from the provided request object ('T'), using the specified 'cookieName'.
4
+ *
5
+ * @param request - The request object, either a Middleware Request or an HTTP Request.
6
+ * @param cookieName - The name of the cookie to retrieve the browser ID from.
7
+ * @returns The browser ID extracted from the cookie or an empty string if not found.
8
+ */
9
+ export declare function getBrowserIdFromRequest<T extends TRequest>(request: T, cookieName: string): string;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBrowserIdFromRequest = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const utils_1 = require("@sitecore-cloudsdk/utils");
6
+ const get_browser_id_from_middleware_request_1 = require("./get-browser-id-from-middleware-request");
7
+ /**
8
+ * Retrieves the browser ID from the provided request object ('T'), using the specified 'cookieName'.
9
+ *
10
+ * @param request - The request object, either a Middleware Request or an HTTP Request.
11
+ * @param cookieName - The name of the cookie to retrieve the browser ID from.
12
+ * @returns The browser ID extracted from the cookie or an empty string if not found.
13
+ */
14
+ function getBrowserIdFromRequest(request, cookieName) {
15
+ let bid = undefined;
16
+ if ((0, utils_1.isNextJsMiddlewareRequest)(request)) {
17
+ bid = (0, get_browser_id_from_middleware_request_1.getBrowserIdFromMiddlewareRequest)(request, cookieName);
18
+ }
19
+ else if ((0, utils_1.isHttpRequest)(request)) {
20
+ const cookieHeader = request.headers.cookie;
21
+ bid = (0, utils_1.getCookieServerSide)(cookieHeader, cookieName)?.value;
22
+ }
23
+ return bid ?? '';
24
+ }
25
+ exports.getBrowserIdFromRequest = getBrowserIdFromRequest;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Gets the default Cookie Attributes
3
+ * @param maxAge - Set the cookie "Max-Age" attribute in days.
4
+ * @returns the default configuration settings for the cookie string
5
+ */
6
+ import { ICookieProperties } from '@sitecore-cloudsdk/utils';
7
+ export declare function getDefaultCookieAttributes(maxAge?: number, cookieDomain?: string): ICookieProperties;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
3
+ /**
4
+ * Gets the default Cookie Attributes
5
+ * @param maxAge - Set the cookie "Max-Age" attribute in days.
6
+ * @returns the default configuration settings for the cookie string
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.getDefaultCookieAttributes = void 0;
10
+ const consts_1 = require("../consts");
11
+ // eslint-disable-next-line max-len
12
+ function getDefaultCookieAttributes(maxAge = consts_1.DEFAULT_COOKIE_EXPIRY_DAYS, cookieDomain) {
13
+ return {
14
+ domain: cookieDomain,
15
+ maxAge: maxAge * consts_1.DAILY_SECONDS,
16
+ path: '/',
17
+ sameSite: 'None',
18
+ secure: true,
19
+ };
20
+ }
21
+ exports.getDefaultCookieAttributes = getDefaultCookieAttributes;
@@ -0,0 +1,13 @@
1
+ import type { IHttpRequest, IHttpResponse } from '@sitecore-cloudsdk/utils';
2
+ import { ISettings } from '../settings/interfaces';
3
+ /**
4
+ * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.
5
+ *
6
+ * @param request - The HTTP Request object containing request headers and data.
7
+ * @param options - The settings object containing configuration options.
8
+ * @param response - The Middleware Next Response or HTTP Response object.
9
+ * @returns A Promise that resolves once the browser ID cookie is handled.
10
+ *
11
+ * @throws [IE-0003] - This exception is thrown in the case getBrowserIdFromCdp wasn't able to retrieve a browser id.
12
+ */
13
+ export declare function handleHttpCookie(request: IHttpRequest, response: IHttpResponse, options: ISettings, cookieTempValue: string): void;
@@ -0,0 +1,35 @@
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.handleHttpCookie = void 0;
5
+ const get_default_cookie_attributes_1 = require("./get-default-cookie-attributes");
6
+ const utils_1 = require("@sitecore-cloudsdk/utils");
7
+ /**
8
+ * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.
9
+ *
10
+ * @param request - The HTTP Request object containing request headers and data.
11
+ * @param options - The settings object containing configuration options.
12
+ * @param response - The Middleware Next Response or HTTP Response object.
13
+ * @returns A Promise that resolves once the browser ID cookie is handled.
14
+ *
15
+ * @throws [IE-0003] - This exception is thrown in the case getBrowserIdFromCdp wasn't able to retrieve a browser id.
16
+ */
17
+ function handleHttpCookie(request, response, options, cookieTempValue) {
18
+ const { cookieName } = options.cookieSettings;
19
+ const cookieValueFromRequest = request.headers.cookie;
20
+ let cookie;
21
+ let cookieValue;
22
+ if (cookieValueFromRequest) {
23
+ cookie = (0, utils_1.getCookieServerSide)(cookieValueFromRequest, cookieName);
24
+ if (cookie)
25
+ cookieValue = cookie.value;
26
+ }
27
+ if (!cookieValue)
28
+ cookieValue = cookieTempValue;
29
+ const defaultCookieAttributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
30
+ const cookieString = (0, utils_1.createCookieString)(cookieName, cookieValue, defaultCookieAttributes);
31
+ if (!cookie)
32
+ request.headers.cookie = cookieValueFromRequest ? cookieValueFromRequest + '; ' + cookieString : cookieString;
33
+ response.setHeader('Set-Cookie', cookieString);
34
+ }
35
+ exports.handleHttpCookie = handleHttpCookie;
@@ -0,0 +1,16 @@
1
+ import { IMiddlewareNextResponse, IMiddlewareRequest } from '@sitecore-cloudsdk/utils';
2
+ import { ISettings } from '../settings/interfaces';
3
+ /**
4
+ * Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.
5
+ * If 'cookieValue' is not present in the request, it fetches it using the 'getProxySettings' function
6
+ * and stores it in the request's cookies with the specified 'defaultCookieAttributes'.
7
+ *
8
+ * @param request - The Middleware Request object.
9
+ * @param cookieName - The name of the cookie to set.
10
+ * @param options - The settings object containing configuration options.
11
+ * @param defaultCookieAttributes - The default attributes for the cookie.
12
+ *
13
+ * @throws [IE-0003] - This exception is thrown in the case getProxySettings wasn't able to retrieve a browser id and client key.
14
+ *
15
+ */
16
+ export declare function handleNextJsMiddlewareCookie(request: IMiddlewareRequest, response: IMiddlewareNextResponse, options: ISettings, cookieTempValue: string): void;
@@ -0,0 +1,27 @@
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.handleNextJsMiddlewareCookie = void 0;
5
+ const get_browser_id_from_middleware_request_1 = require("./get-browser-id-from-middleware-request");
6
+ const get_default_cookie_attributes_1 = require("./get-default-cookie-attributes");
7
+ /**
8
+ * Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.
9
+ * If 'cookieValue' is not present in the request, it fetches it using the 'getProxySettings' function
10
+ * and stores it in the request's cookies with the specified 'defaultCookieAttributes'.
11
+ *
12
+ * @param request - The Middleware Request object.
13
+ * @param cookieName - The name of the cookie to set.
14
+ * @param options - The settings object containing configuration options.
15
+ * @param defaultCookieAttributes - The default attributes for the cookie.
16
+ *
17
+ * @throws [IE-0003] - This exception is thrown in the case getProxySettings wasn't able to retrieve a browser id and client key.
18
+ *
19
+ */
20
+ function handleNextJsMiddlewareCookie(request, response, options, cookieTempValue) {
21
+ const { cookieName } = options.cookieSettings;
22
+ const cookieValue = (0, get_browser_id_from_middleware_request_1.getBrowserIdFromMiddlewareRequest)(request, cookieName) ?? cookieTempValue;
23
+ const defaultCookieAttributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
24
+ request.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
25
+ response.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
26
+ }
27
+ exports.handleNextJsMiddlewareCookie = handleNextJsMiddlewareCookie;
@@ -0,0 +1,10 @@
1
+ import { type TRequest, type IMiddlewareNextResponse, type IHttpResponse } from '@sitecore-cloudsdk/utils';
2
+ /**
3
+ * Handles server-side cookie operations based on the provided 'request' and 'response' objects.
4
+ *
5
+ * @param request - The request object, either a Middleware Request or an HTTP Request.
6
+ * @param response - The response object, either a Middleware Next Response or an HTTP Response.
7
+ * @param options - The settings object containing configuration options.
8
+ * @returns A Promise that resolves once the cookie handling is complete.
9
+ */
10
+ export declare function handleServerCookie<T extends TRequest, X extends IMiddlewareNextResponse | IHttpResponse>(request: T, response: X, timeout?: number): Promise<void>;
@@ -0,0 +1,32 @@
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.handleServerCookie = void 0;
5
+ const utils_1 = require("@sitecore-cloudsdk/utils");
6
+ const init_core_server_1 = require("../init/init-core-server");
7
+ const handle_next_js_middleware_cookie_1 = require("./handle-next-js-middleware-cookie");
8
+ const handle_http_cookie_1 = require("./handle-http-cookie");
9
+ const get_proxy_settings_1 = require("../init/get-proxy-settings");
10
+ const consts_1 = require("../consts");
11
+ /**
12
+ * Handles server-side cookie operations based on the provided 'request' and 'response' objects.
13
+ *
14
+ * @param request - The request object, either a Middleware Request or an HTTP Request.
15
+ * @param response - The response object, either a Middleware Next Response or an HTTP Response.
16
+ * @param options - The settings object containing configuration options.
17
+ * @returns A Promise that resolves once the cookie handling is complete.
18
+ */
19
+ async function handleServerCookie(request, response, timeout) {
20
+ const settings = (0, init_core_server_1.getSettingsServer)();
21
+ const { browserId, clientKey } = await (0, get_proxy_settings_1.getProxySettings)(settings.sitecoreEdgeContextId, timeout);
22
+ if (!clientKey)
23
+ throw new Error('[IE-0003] Unable to set the cookie because the browser ID could not be retrieved from the server. Try again later, or use try-catch blocks to handle this error.');
24
+ settings.cookieSettings.cookieName = `${consts_1.BID_PREFIX}${clientKey}`;
25
+ if ((0, utils_1.isNextJsMiddlewareRequest)(request) && (0, utils_1.isNextJsMiddlewareResponse)(response)) {
26
+ (0, handle_next_js_middleware_cookie_1.handleNextJsMiddlewareCookie)(request, response, settings, browserId);
27
+ }
28
+ else if ((0, utils_1.isHttpRequest)(request) && (0, utils_1.isHttpResponse)(response)) {
29
+ (0, handle_http_cookie_1.handleHttpCookie)(request, response, settings, browserId);
30
+ }
31
+ }
32
+ exports.handleServerCookie = handleServerCookie;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Returns the uppercase language code of the current web page's root HTML element, using the "lang" attribute.
3
+ * If unavailable or invalid, an undefined is returned.
4
+ * @returns - Language attribute or undefined
5
+ */
6
+ export declare function language(): string | undefined;
7
+ /**
8
+ * Returns the name of the current page extracted from the URL's pathname.
9
+ * If it's the home page, it returns 'Home Page'.
10
+ * @returns - Home Page if root or pathname
11
+ */
12
+ export declare function pageName(): string;
@@ -0,0 +1,26 @@
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.pageName = exports.language = void 0;
5
+ /**
6
+ * Returns the uppercase language code of the current web page's root HTML element, using the "lang" attribute.
7
+ * If unavailable or invalid, an undefined is returned.
8
+ * @returns - Language attribute or undefined
9
+ */
10
+ function language() {
11
+ return typeof window === 'undefined' || window.document.documentElement.lang.length <= 1
12
+ ? undefined
13
+ : new Intl.Locale(window.document.documentElement.lang).language.toLocaleUpperCase();
14
+ }
15
+ exports.language = language;
16
+ /**
17
+ * Returns the name of the current page extracted from the URL's pathname.
18
+ * If it's the home page, it returns 'Home Page'.
19
+ * @returns - Home Page if root or pathname
20
+ */
21
+ function pageName() {
22
+ if (typeof window === 'undefined')
23
+ return '';
24
+ return window.location.pathname === '/' ? 'Home Page' : window.location.pathname.split('/').pop();
25
+ }
26
+ exports.pageName = pageName;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Constructs the URL for retrieving the proxy settings from EDGE events proxy
3
+ * @param sitecoreEdgeContextId - From global settings
4
+ * @returns The URL string for retrieving the browser ID and ClientKey
5
+ */
6
+ export declare function constructGetProxySettingsUrl(sitecoreEdgeContextId: string): string;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.constructGetProxySettingsUrl = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const consts_1 = require("../consts");
6
+ /**
7
+ * Constructs the URL for retrieving the proxy settings from EDGE events proxy
8
+ * @param sitecoreEdgeContextId - From global settings
9
+ * @returns The URL string for retrieving the browser ID and ClientKey
10
+ */
11
+ function constructGetProxySettingsUrl(sitecoreEdgeContextId) {
12
+ return `${consts_1.TARGET_URL}/events/${consts_1.API_VERSION}/browser/create.json?sitecoreContextId=${sitecoreEdgeContextId}&client_key=`;
13
+ }
14
+ exports.constructGetProxySettingsUrl = constructGetProxySettingsUrl;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Get the browser ID from the cookie
3
+ * @returns The browser ID if the cookie exists
4
+ */
5
+ export declare function getBrowserId(): string;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBrowserId = void 0;
4
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
5
+ const utils_1 = require("@sitecore-cloudsdk/utils");
6
+ const init_core_1 = require("./init-core");
7
+ /**
8
+ * Get the browser ID from the cookie
9
+ * @returns The browser ID if the cookie exists
10
+ */
11
+ function getBrowserId() {
12
+ const settings = (0, init_core_1.getSettings)();
13
+ return (0, utils_1.getCookieValueClientSide)(settings.cookieSettings.cookieName);
14
+ }
15
+ exports.getBrowserId = getBrowserId;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * A function that gets the guest ref from CDP.
3
+ * @param browserId - The browser id of the client
4
+ * @param sitecoreEdgeContextId - The sitecoreEdgeContextId
5
+ * @returns - A promise that resolves with the guest ref
6
+ * @throws - Will throw an error if the clientKey/browser id is invalid
7
+ */
8
+ export declare function getGuestId(browserId: string, sitecoreEdgeContextId: string): Promise<string>;
9
+ interface IGetGuestRefCommon {
10
+ status: string;
11
+ version: string;
12
+ clientKey: string;
13
+ }
14
+ export interface IGetGuestRefResponse extends IGetGuestRefCommon {
15
+ ref: string;
16
+ customer: {
17
+ ref: string;
18
+ };
19
+ }
20
+ export interface IGetGuestRefResponseError extends IGetGuestRefCommon {
21
+ error_msg: string;
22
+ moreInfo: string;
23
+ }
24
+ export {};
@@ -0,0 +1,25 @@
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.getGuestId = void 0;
5
+ const consts_1 = require("../consts");
6
+ /**
7
+ * A function that gets the guest ref from CDP.
8
+ * @param browserId - The browser id of the client
9
+ * @param sitecoreEdgeContextId - The sitecoreEdgeContextId
10
+ * @returns - A promise that resolves with the guest ref
11
+ * @throws - Will throw an error if the clientKey/browser id is invalid
12
+ */
13
+ async function getGuestId(browserId, sitecoreEdgeContextId) {
14
+ // eslint-disable-next-line max-len
15
+ const url = `${consts_1.TARGET_URL}/events/${consts_1.API_VERSION}/browser/${browserId}/show.json?sitecoreContextId=${sitecoreEdgeContextId}&client_key=&api_token=`;
16
+ // eslint-disable-next-line @typescript-eslint/naming-convention
17
+ const response = await fetch(url, { headers: { 'X-Library-Version': consts_1.LIBRARY_VERSION } });
18
+ const data = await response.json();
19
+ if (!response.ok) {
20
+ const { error_msg: errorMsg, moreInfo } = data;
21
+ throw new Error(`${errorMsg}, for more info: ${moreInfo}`);
22
+ }
23
+ return data.customer.ref;
24
+ }
25
+ exports.getGuestId = getGuestId;
@@ -0,0 +1,8 @@
1
+ import { IProxySettings } from '../interfaces';
2
+ /**
3
+ * Gets the browser ID and Client Key from Sitecore Edge Proxy
4
+ * @param sitecoreEdgeContextId - The sitecoreEdgeContextId
5
+ * @param timeout - The timeout for the call to proxy
6
+ * @returns the browser ID and Client Key
7
+ */
8
+ export declare function getProxySettings(sitecoreEdgeContextId: string, timeout?: number): Promise<IProxySettings>;
@@ -0,0 +1,35 @@
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.getProxySettings = void 0;
5
+ const utils_1 = require("@sitecore-cloudsdk/utils");
6
+ const consts_1 = require("../consts");
7
+ const construct_get_proxy_settings_url_1 = require("./construct-get-proxy-settings-url");
8
+ /**
9
+ * Gets the browser ID and Client Key from Sitecore Edge Proxy
10
+ * @param sitecoreEdgeContextId - The sitecoreEdgeContextId
11
+ * @param timeout - The timeout for the call to proxy
12
+ * @returns the browser ID and Client Key
13
+ */
14
+ async function getProxySettings(sitecoreEdgeContextId, timeout) {
15
+ const fetchOptions = {
16
+ // eslint-disable-next-line @typescript-eslint/naming-convention
17
+ headers: { 'X-Library-Version': consts_1.LIBRARY_VERSION },
18
+ };
19
+ let response;
20
+ if (timeout !== undefined) {
21
+ response = await (0, utils_1.fetchWithTimeout)((0, construct_get_proxy_settings_url_1.constructGetProxySettingsUrl)(sitecoreEdgeContextId), timeout, fetchOptions);
22
+ }
23
+ else {
24
+ response = await fetch((0, construct_get_proxy_settings_url_1.constructGetProxySettingsUrl)(sitecoreEdgeContextId), fetchOptions)
25
+ .then((res) => res.json())
26
+ .then((data) => data)
27
+ .catch(() => undefined);
28
+ }
29
+ if (!response)
30
+ return { browserId: '', clientKey: '' };
31
+ // eslint-disable-next-line @typescript-eslint/naming-convention
32
+ const { client_key: clientKey, ref: browserId } = response;
33
+ return { browserId, clientKey };
34
+ }
35
+ exports.getProxySettings = getProxySettings;
@@ -0,0 +1,21 @@
1
+ import { IHttpResponse, IMiddlewareNextResponse, TRequest } from '@sitecore-cloudsdk/utils';
2
+ import { ISettings, ISettingsParamsServer } from '../settings/interfaces';
3
+ export declare function setCoreSettings(settings: ISettings): void;
4
+ /**
5
+ * Initializes the core settings for browser-based applications.
6
+ *
7
+ * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
8
+ *
9
+ * @param settingsInput - The settings input to configure the core settings.
10
+ * @returns A Promise that resolves when initialization is complete.
11
+ */
12
+ export declare function getSettingsServer(): ISettings;
13
+ /**
14
+ * Initializes the core settings for browser-based applications.
15
+ *
16
+ * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
17
+ *
18
+ * @param settingsInput - The settings input to configure the core settings.
19
+ * @returns A Promise that resolves when initialization is complete.
20
+ */
21
+ export declare function initCoreServer<TResponse extends IMiddlewareNextResponse | IHttpResponse>(settingsInput: ISettingsParamsServer, request: TRequest, response: TResponse): Promise<void>;