@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
@@ -0,0 +1,31 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
3
+ import { createCookieString, getCookieServerSide } from '@sitecore-cloudsdk/utils';
4
+ /**
5
+ * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.
6
+ *
7
+ * @param request - The HTTP Request object containing request headers and data.
8
+ * @param options - The settings object containing configuration options.
9
+ * @param response - The Middleware Next Response or HTTP Response object.
10
+ * @returns A Promise that resolves once the browser ID cookie is handled.
11
+ *
12
+ * @throws [IE-0003] - This exception is thrown in the case getBrowserIdFromCdp wasn't able to retrieve a browser id.
13
+ */
14
+ export function handleHttpCookie(request, response, options, cookieTempValue) {
15
+ const { cookieName } = options.cookieSettings;
16
+ const cookieValueFromRequest = request.headers.cookie;
17
+ let cookie;
18
+ let cookieValue;
19
+ if (cookieValueFromRequest) {
20
+ cookie = getCookieServerSide(cookieValueFromRequest, cookieName);
21
+ if (cookie)
22
+ cookieValue = cookie.value;
23
+ }
24
+ if (!cookieValue)
25
+ cookieValue = cookieTempValue;
26
+ const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
27
+ const cookieString = createCookieString(cookieName, cookieValue, defaultCookieAttributes);
28
+ if (!cookie)
29
+ request.headers.cookie = cookieValueFromRequest ? cookieValueFromRequest + '; ' + cookieString : cookieString;
30
+ response.setHeader('Set-Cookie', cookieString);
31
+ }
@@ -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,23 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { getBrowserIdFromMiddlewareRequest } from './get-browser-id-from-middleware-request';
3
+ import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
4
+ /**
5
+ * Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.
6
+ * If 'cookieValue' is not present in the request, it fetches it using the 'getProxySettings' function
7
+ * and stores it in the request's cookies with the specified 'defaultCookieAttributes'.
8
+ *
9
+ * @param request - The Middleware Request object.
10
+ * @param cookieName - The name of the cookie to set.
11
+ * @param options - The settings object containing configuration options.
12
+ * @param defaultCookieAttributes - The default attributes for the cookie.
13
+ *
14
+ * @throws [IE-0003] - This exception is thrown in the case getProxySettings wasn't able to retrieve a browser id and client key.
15
+ *
16
+ */
17
+ export function handleNextJsMiddlewareCookie(request, response, options, cookieTempValue) {
18
+ const { cookieName } = options.cookieSettings;
19
+ const cookieValue = getBrowserIdFromMiddlewareRequest(request, cookieName) ?? cookieTempValue;
20
+ const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
21
+ request.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
22
+ response.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
23
+ }
@@ -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,28 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { isNextJsMiddlewareRequest, isNextJsMiddlewareResponse, isHttpRequest, isHttpResponse, } from '@sitecore-cloudsdk/utils';
3
+ import { getSettingsServer } from '../init/init-core-server';
4
+ import { handleNextJsMiddlewareCookie } from './handle-next-js-middleware-cookie';
5
+ import { handleHttpCookie } from './handle-http-cookie';
6
+ import { getProxySettings } from '../init/get-proxy-settings';
7
+ import { BID_PREFIX } from '../consts';
8
+ /**
9
+ * Handles server-side cookie operations based on the provided 'request' and 'response' objects.
10
+ *
11
+ * @param request - The request object, either a Middleware Request or an HTTP Request.
12
+ * @param response - The response object, either a Middleware Next Response or an HTTP Response.
13
+ * @param options - The settings object containing configuration options.
14
+ * @returns A Promise that resolves once the cookie handling is complete.
15
+ */
16
+ export async function handleServerCookie(request, response, timeout) {
17
+ const settings = getSettingsServer();
18
+ const { browserId, clientKey } = await getProxySettings(settings.sitecoreEdgeContextId, timeout);
19
+ if (!clientKey)
20
+ 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.');
21
+ settings.cookieSettings.cookieName = `${BID_PREFIX}${clientKey}`;
22
+ if (isNextJsMiddlewareRequest(request) && isNextJsMiddlewareResponse(response)) {
23
+ handleNextJsMiddlewareCookie(request, response, settings, browserId);
24
+ }
25
+ else if (isHttpRequest(request) && isHttpResponse(response)) {
26
+ handleHttpCookie(request, response, settings, browserId);
27
+ }
28
+ }
@@ -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,21 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ /**
3
+ * Returns the uppercase language code of the current web page's root HTML element, using the "lang" attribute.
4
+ * If unavailable or invalid, an undefined is returned.
5
+ * @returns - Language attribute or undefined
6
+ */
7
+ export function language() {
8
+ return typeof window === 'undefined' || window.document.documentElement.lang.length <= 1
9
+ ? undefined
10
+ : new Intl.Locale(window.document.documentElement.lang).language.toLocaleUpperCase();
11
+ }
12
+ /**
13
+ * Returns the name of the current page extracted from the URL's pathname.
14
+ * If it's the home page, it returns 'Home Page'.
15
+ * @returns - Home Page if root or pathname
16
+ */
17
+ export function pageName() {
18
+ if (typeof window === 'undefined')
19
+ return '';
20
+ return window.location.pathname === '/' ? 'Home Page' : window.location.pathname.split('/').pop();
21
+ }
@@ -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,10 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { API_VERSION, TARGET_URL } from '../consts';
3
+ /**
4
+ * Constructs the URL for retrieving the proxy settings from EDGE events proxy
5
+ * @param sitecoreEdgeContextId - From global settings
6
+ * @returns The URL string for retrieving the browser ID and ClientKey
7
+ */
8
+ export function constructGetProxySettingsUrl(sitecoreEdgeContextId) {
9
+ return `${TARGET_URL}/events/${API_VERSION}/browser/create.json?sitecoreContextId=${sitecoreEdgeContextId}&client_key=`;
10
+ }
@@ -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,11 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { getCookieValueClientSide } from '@sitecore-cloudsdk/utils';
3
+ import { getSettings } from './init-core';
4
+ /**
5
+ * Get the browser ID from the cookie
6
+ * @returns The browser ID if the cookie exists
7
+ */
8
+ export function getBrowserId() {
9
+ const settings = getSettings();
10
+ return getCookieValueClientSide(settings.cookieSettings.cookieName);
11
+ }
@@ -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,21 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { API_VERSION, LIBRARY_VERSION, TARGET_URL } from '../consts';
3
+ /**
4
+ * A function that gets the guest ref from CDP.
5
+ * @param browserId - The browser id of the client
6
+ * @param sitecoreEdgeContextId - The sitecoreEdgeContextId
7
+ * @returns - A promise that resolves with the guest ref
8
+ * @throws - Will throw an error if the clientKey/browser id is invalid
9
+ */
10
+ export async function getGuestId(browserId, sitecoreEdgeContextId) {
11
+ // eslint-disable-next-line max-len
12
+ const url = `${TARGET_URL}/events/${API_VERSION}/browser/${browserId}/show.json?sitecoreContextId=${sitecoreEdgeContextId}&client_key=&api_token=`;
13
+ // eslint-disable-next-line @typescript-eslint/naming-convention
14
+ const response = await fetch(url, { headers: { 'X-Library-Version': LIBRARY_VERSION } });
15
+ const data = await response.json();
16
+ if (!response.ok) {
17
+ const { error_msg: errorMsg, moreInfo } = data;
18
+ throw new Error(`${errorMsg}, for more info: ${moreInfo}`);
19
+ }
20
+ return data.customer.ref;
21
+ }
@@ -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,31 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { fetchWithTimeout } from '@sitecore-cloudsdk/utils';
3
+ import { LIBRARY_VERSION } from '../consts';
4
+ import { constructGetProxySettingsUrl } from './construct-get-proxy-settings-url';
5
+ /**
6
+ * Gets the browser ID and Client Key from Sitecore Edge Proxy
7
+ * @param sitecoreEdgeContextId - The sitecoreEdgeContextId
8
+ * @param timeout - The timeout for the call to proxy
9
+ * @returns the browser ID and Client Key
10
+ */
11
+ export async function getProxySettings(sitecoreEdgeContextId, timeout) {
12
+ const fetchOptions = {
13
+ // eslint-disable-next-line @typescript-eslint/naming-convention
14
+ headers: { 'X-Library-Version': LIBRARY_VERSION },
15
+ };
16
+ let response;
17
+ if (timeout !== undefined) {
18
+ response = await fetchWithTimeout(constructGetProxySettingsUrl(sitecoreEdgeContextId), timeout, fetchOptions);
19
+ }
20
+ else {
21
+ response = await fetch(constructGetProxySettingsUrl(sitecoreEdgeContextId), fetchOptions)
22
+ .then((res) => res.json())
23
+ .then((data) => data)
24
+ .catch(() => undefined);
25
+ }
26
+ if (!response)
27
+ return { browserId: '', clientKey: '' };
28
+ // eslint-disable-next-line @typescript-eslint/naming-convention
29
+ const { client_key: clientKey, ref: browserId } = response;
30
+ return { browserId, clientKey };
31
+ }
@@ -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>;
@@ -0,0 +1,41 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { handleServerCookie } from '../cookie/handle-server-cookie';
3
+ import { createSettings } from '../settings/create-settings';
4
+ /**
5
+ * Internal settings object to be used by all functions in module caching.
6
+ * It starts with a null value and is set to the proper object by the function. *
7
+ * Can be retrieved only through the function.
8
+ */
9
+ let coreSettings = null;
10
+ export function setCoreSettings(settings) {
11
+ coreSettings = settings;
12
+ }
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 function getSettingsServer() {
22
+ if (!coreSettings) {
23
+ throw Error('[IE-0005] You must first initialize the "core" module. Run the "initServer" function.');
24
+ }
25
+ return coreSettings;
26
+ }
27
+ /**
28
+ * Initializes the core settings for browser-based applications.
29
+ *
30
+ * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
31
+ *
32
+ * @param settingsInput - The settings input to configure the core settings.
33
+ * @returns A Promise that resolves when initialization is complete.
34
+ */
35
+ export async function initCoreServer(settingsInput, request, response) {
36
+ if (!coreSettings)
37
+ coreSettings = createSettings(settingsInput);
38
+ if (settingsInput.enableServerCookie) {
39
+ await handleServerCookie(request, response, settingsInput.timeout);
40
+ }
41
+ }
@@ -0,0 +1,32 @@
1
+ import { ISettings, ISettingsParamsBrowser } from '../settings/interfaces';
2
+ /**
3
+ * Enum representing the initialization statuses.
4
+ * - `NOT_STARTED`: The initialization process has not started.
5
+ * - `INITIALIZING`: The initialization process is in progress.
6
+ * - `DONE`: The initialization process is complete.
7
+ */
8
+ export declare enum INIT_STATUSES {
9
+ NOT_STARTED = 0,
10
+ INITIALIZING = 1,
11
+ DONE = 2
12
+ }
13
+ export declare function setInitStatus(status: INIT_STATUSES): void;
14
+ export declare function setCoreSettings(settings: ISettings): void;
15
+ /**
16
+ * Retrieves the core settings object.
17
+ *
18
+ * This function ensures that the core settings have been initialized and contain essential properties like `clientKey`, `cookieSettings`, and `targetURL`.
19
+ *
20
+ * @returns The core settings object.
21
+ * @throws Error if the core settings haven't been initialized with the required properties.
22
+ */
23
+ export declare function getSettings(): ISettings;
24
+ /**
25
+ * Initializes the core settings for browser-based applications.
26
+ *
27
+ * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
28
+ *
29
+ * @param settingsInput - The settings input to configure the core settings.
30
+ * @returns A Promise that resolves when initialization is complete.
31
+ */
32
+ export declare function initCore(settingsInput: ISettingsParamsBrowser): Promise<void>;
@@ -0,0 +1,62 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { createCookie } from '../cookie/create-cookie';
3
+ import { createSettings } from '../settings/create-settings';
4
+ /**
5
+ * Enum representing the initialization statuses.
6
+ * - `NOT_STARTED`: The initialization process has not started.
7
+ * - `INITIALIZING`: The initialization process is in progress.
8
+ * - `DONE`: The initialization process is complete.
9
+ */
10
+ // eslint-disable-next-line @typescript-eslint/naming-convention
11
+ export var INIT_STATUSES;
12
+ (function (INIT_STATUSES) {
13
+ INIT_STATUSES[INIT_STATUSES["NOT_STARTED"] = 0] = "NOT_STARTED";
14
+ INIT_STATUSES[INIT_STATUSES["INITIALIZING"] = 1] = "INITIALIZING";
15
+ INIT_STATUSES[INIT_STATUSES["DONE"] = 2] = "DONE";
16
+ })(INIT_STATUSES || (INIT_STATUSES = {}));
17
+ /**
18
+ * Internal settings object to be used by all functions in module caching.
19
+ * It starts with a null value and is set to the proper object by the function. *
20
+ * Can be retrieved only through the function.
21
+ */
22
+ let coreSettings = null;
23
+ let initStatus = INIT_STATUSES.NOT_STARTED;
24
+ export function setInitStatus(status) {
25
+ initStatus = status;
26
+ }
27
+ export function setCoreSettings(settings) {
28
+ coreSettings = settings;
29
+ }
30
+ /**
31
+ * Retrieves the core settings object.
32
+ *
33
+ * This function ensures that the core settings have been initialized and contain essential properties like `clientKey`, `cookieSettings`, and `targetURL`.
34
+ *
35
+ * @returns The core settings object.
36
+ * @throws Error if the core settings haven't been initialized with the required properties.
37
+ */
38
+ export function getSettings() {
39
+ if (!coreSettings) {
40
+ throw Error(`[IE-0004] You must first initialize the "core" module. Run the "init" function.`);
41
+ }
42
+ return coreSettings;
43
+ }
44
+ /**
45
+ * Initializes the core settings for browser-based applications.
46
+ *
47
+ * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
48
+ *
49
+ * @param settingsInput - The settings input to configure the core settings.
50
+ * @returns A Promise that resolves when initialization is complete.
51
+ */
52
+ export async function initCore(settingsInput) {
53
+ if (initStatus != INIT_STATUSES.NOT_STARTED)
54
+ return;
55
+ setInitStatus(INIT_STATUSES.INITIALIZING);
56
+ coreSettings = createSettings(settingsInput);
57
+ setCoreSettings(coreSettings);
58
+ if (settingsInput.enableBrowserCookie) {
59
+ await createCookie(coreSettings);
60
+ }
61
+ setInitStatus(INIT_STATUSES.DONE);
62
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The response object that Sitecore CDP returns
3
+ */
4
+ export interface ICdpResponse {
5
+ ref: string;
6
+ status: string;
7
+ version: string;
8
+ client_key: string;
9
+ }
10
+ export interface IProxySettings {
11
+ browserId: string;
12
+ clientKey: string;
13
+ }
14
+ /**
15
+ * Interface for supporting response IncomingMessage http node type
16
+ */
17
+ export interface IInfer {
18
+ language: () => string | undefined;
19
+ pageName: () => string;
20
+ }
@@ -0,0 +1,2 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ export {};
@@ -0,0 +1,7 @@
1
+ import { ISettings, ISettingsParams } from './interfaces';
2
+ /**
3
+ * Creates the global settings object, to be used by the library
4
+ * @param settingsInput - Global settings added by the developer.
5
+ * @returns an ISettings with the settings added by the developer
6
+ */
7
+ export declare function createSettings(settingsInput: ISettingsParams): ISettings;
@@ -0,0 +1,22 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { DEFAULT_COOKIE_EXPIRY_DAYS } from '../consts';
3
+ import { validateSettings } from './validate-settings';
4
+ /**
5
+ * Creates the global settings object, to be used by the library
6
+ * @param settingsInput - Global settings added by the developer.
7
+ * @returns an ISettings with the settings added by the developer
8
+ */
9
+ export function createSettings(settingsInput) {
10
+ validateSettings(settingsInput);
11
+ const { siteName, sitecoreEdgeContextId, cookieDomain, cookiePath, cookieExpiryDays } = settingsInput;
12
+ return {
13
+ cookieSettings: {
14
+ cookieDomain,
15
+ cookieExpiryDays: cookieExpiryDays || DEFAULT_COOKIE_EXPIRY_DAYS,
16
+ cookieName: '',
17
+ cookiePath: cookiePath || '/',
18
+ },
19
+ siteName,
20
+ sitecoreEdgeContextId,
21
+ };
22
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Extends the global settings object with additional properties
3
+ */
4
+ export interface ISettings extends IBasicSettings {
5
+ cookieSettings: ICookieSettings;
6
+ }
7
+ export interface ISettingsParamsBrowser extends ISettingsParams {
8
+ enableBrowserCookie?: boolean;
9
+ }
10
+ export interface ISettingsParamsServer extends ISettingsParams {
11
+ enableServerCookie?: boolean;
12
+ }
13
+ export interface ISettingsParams extends IBasicSettings, ICookieSettingsInput {
14
+ timeout?: number;
15
+ }
16
+ /**
17
+ * Properties for the global settings object
18
+ */
19
+ interface IBasicSettings {
20
+ sitecoreEdgeContextId: string;
21
+ siteName: string;
22
+ }
23
+ interface ICookieSettingsInput {
24
+ cookiePath?: string;
25
+ cookieExpiryDays?: number;
26
+ cookieDomain?: string;
27
+ }
28
+ /**
29
+ * Properties for the cookie object
30
+ */
31
+ export interface ICookieSettings {
32
+ cookieName: string;
33
+ cookieDomain?: string;
34
+ cookieExpiryDays: number;
35
+ cookiePath?: string;
36
+ }
37
+ /**
38
+ * Properties for the cookie object
39
+ */
40
+ export interface ICookieSettingsBrowser extends ICookieSettings {
41
+ enableBrowserCookie: boolean;
42
+ }
43
+ /**
44
+ * Properties for the Web Flow configuration
45
+ */
46
+ export interface IWebPersonalizationConfig {
47
+ asyncScriptLoading?: boolean;
48
+ deferScriptLoading?: boolean;
49
+ baseURLOverride?: string;
50
+ }
51
+ export interface IWebExperiencesSettings {
52
+ targetURL: string;
53
+ pointOfSale: string;
54
+ client_key: string;
55
+ web_flow_config: {
56
+ async: boolean;
57
+ defer: boolean;
58
+ };
59
+ web_flow_target?: string;
60
+ }
61
+ export {};
@@ -0,0 +1,2 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ export {};
@@ -0,0 +1,10 @@
1
+ import { ISettingsParams } from './interfaces';
2
+ /**
3
+ * Validates the core settings to ensure they meet required criteria.
4
+ *
5
+ * This function validates the provided core settings object to ensure that essential properties such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
6
+ *
7
+ * @param settings - The core settings object to validate.
8
+ * @throws Error with specific error codes if any required property is missing or empty.
9
+ */
10
+ export declare function validateSettings(settings: ISettingsParams): void;
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Validates the core settings to ensure they meet required criteria.
3
+ *
4
+ * This function validates the provided core settings object to ensure that essential properties such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
5
+ *
6
+ * @param settings - The core settings object to validate.
7
+ * @throws Error with specific error codes if any required property is missing or empty.
8
+ */
9
+ export function validateSettings(settings) {
10
+ const { sitecoreEdgeContextId, siteName } = settings;
11
+ if (!sitecoreEdgeContextId || sitecoreEdgeContextId.trim().length === 0)
12
+ throw new Error(`[MV-0001] "sitecoreEdgeContextId" is required.`);
13
+ if (!siteName || siteName.trim().length === 0)
14
+ throw new Error(`[MV-0002] "siteName" is required.`);
15
+ }