@sitecore-cloudsdk/core 0.2.4 → 0.3.1-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 (95) hide show
  1. package/dist/cjs/package.json +3 -4
  2. package/dist/cjs/src/index.d.ts +8 -7
  3. package/dist/cjs/src/index.js +14 -10
  4. package/dist/cjs/src/lib/consts.d.ts +9 -0
  5. package/dist/cjs/src/lib/consts.js +13 -1
  6. package/dist/cjs/src/lib/cookie/create-cookies.d.ts +6 -0
  7. package/dist/cjs/src/lib/cookie/create-cookies.js +28 -0
  8. package/dist/cjs/src/lib/cookie/get-browser-id.js +1 -1
  9. package/dist/cjs/src/lib/cookie/get-cookie-value-from-middleware-request.d.ts +11 -0
  10. package/dist/cjs/src/lib/cookie/{get-browser-id-from-middleware-request.js → get-cookie-value-from-middleware-request.js} +6 -6
  11. package/dist/cjs/src/lib/cookie/get-cookie-value-from-request.d.ts +9 -0
  12. package/dist/cjs/src/lib/cookie/get-cookie-value-from-request.js +24 -0
  13. package/dist/cjs/src/lib/cookie/get-default-cookie-attributes.d.ts +1 -1
  14. package/dist/cjs/src/lib/cookie/get-default-cookie-attributes.js +4 -5
  15. package/dist/cjs/src/lib/cookie/handle-http-cookie.d.ts +1 -1
  16. package/dist/cjs/src/lib/cookie/handle-http-cookie.js +30 -17
  17. package/dist/cjs/src/lib/cookie/handle-next-js-middleware-cookie.d.ts +3 -3
  18. package/dist/cjs/src/lib/cookie/handle-next-js-middleware-cookie.js +18 -10
  19. package/dist/cjs/src/lib/cookie/handle-server-cookie.d.ts +1 -1
  20. package/dist/cjs/src/lib/cookie/handle-server-cookie.js +3 -5
  21. package/dist/cjs/src/lib/correlation-id/generate-correlation-id.d.ts +6 -0
  22. package/dist/cjs/src/lib/correlation-id/generate-correlation-id.js +14 -0
  23. package/dist/cjs/src/lib/debug/debug.d.ts +6 -0
  24. package/dist/cjs/src/lib/debug/debug.js +20 -3
  25. package/dist/cjs/src/lib/init/construct-get-browser-id-url.js +1 -0
  26. package/dist/cjs/src/lib/init/fetch-browser-id-from-edge-proxy.d.ts +1 -1
  27. package/dist/cjs/src/lib/init/fetch-browser-id-from-edge-proxy.js +8 -11
  28. package/dist/cjs/src/lib/init/get-guest-id.js +2 -0
  29. package/dist/cjs/src/lib/init/init-core-server.d.ts +7 -5
  30. package/dist/cjs/src/lib/init/init-core-server.js +10 -9
  31. package/dist/cjs/src/lib/init/init-core.d.ts +6 -4
  32. package/dist/cjs/src/lib/init/init-core.js +14 -13
  33. package/dist/cjs/src/lib/interfaces.d.ts +15 -0
  34. package/dist/cjs/src/lib/settings/create-settings.d.ts +1 -1
  35. package/dist/cjs/src/lib/settings/create-settings.js +6 -3
  36. package/dist/cjs/src/lib/settings/handle-get-settings-error.d.ts +1 -1
  37. package/dist/cjs/src/lib/settings/interfaces.d.ts +8 -11
  38. package/dist/cjs/src/lib/settings/validate-settings.d.ts +3 -2
  39. package/dist/cjs/src/lib/settings/validate-settings.js +8 -6
  40. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  41. package/dist/esm/package.json +3 -4
  42. package/dist/esm/src/index.d.ts +8 -7
  43. package/dist/esm/src/index.js +6 -5
  44. package/dist/esm/src/lib/consts.d.ts +9 -0
  45. package/dist/esm/src/lib/consts.js +12 -0
  46. package/dist/esm/src/lib/cookie/create-cookies.d.ts +6 -0
  47. package/dist/esm/src/lib/cookie/create-cookies.js +24 -0
  48. package/dist/esm/src/lib/cookie/get-browser-id.js +1 -1
  49. package/dist/esm/src/lib/cookie/get-cookie-value-from-middleware-request.d.ts +11 -0
  50. package/dist/esm/src/lib/cookie/{get-browser-id-from-middleware-request.js → get-cookie-value-from-middleware-request.js} +4 -4
  51. package/dist/esm/src/lib/cookie/get-cookie-value-from-request.d.ts +9 -0
  52. package/dist/esm/src/lib/cookie/get-cookie-value-from-request.js +20 -0
  53. package/dist/esm/src/lib/cookie/get-default-cookie-attributes.d.ts +1 -1
  54. package/dist/esm/src/lib/cookie/get-default-cookie-attributes.js +2 -3
  55. package/dist/esm/src/lib/cookie/handle-http-cookie.d.ts +1 -1
  56. package/dist/esm/src/lib/cookie/handle-http-cookie.js +30 -17
  57. package/dist/esm/src/lib/cookie/handle-next-js-middleware-cookie.d.ts +3 -3
  58. package/dist/esm/src/lib/cookie/handle-next-js-middleware-cookie.js +18 -10
  59. package/dist/esm/src/lib/cookie/handle-server-cookie.d.ts +1 -1
  60. package/dist/esm/src/lib/cookie/handle-server-cookie.js +4 -6
  61. package/dist/esm/src/lib/correlation-id/generate-correlation-id.d.ts +6 -0
  62. package/dist/esm/src/lib/correlation-id/generate-correlation-id.js +10 -0
  63. package/dist/esm/src/lib/debug/debug.d.ts +6 -0
  64. package/dist/esm/src/lib/debug/debug.js +19 -3
  65. package/dist/esm/src/lib/init/construct-get-browser-id-url.js +1 -0
  66. package/dist/esm/src/lib/init/fetch-browser-id-from-edge-proxy.d.ts +1 -1
  67. package/dist/esm/src/lib/init/fetch-browser-id-from-edge-proxy.js +9 -12
  68. package/dist/esm/src/lib/init/get-guest-id.js +3 -1
  69. package/dist/esm/src/lib/init/init-core-server.d.ts +7 -5
  70. package/dist/esm/src/lib/init/init-core-server.js +10 -9
  71. package/dist/esm/src/lib/init/init-core.d.ts +6 -4
  72. package/dist/esm/src/lib/init/init-core.js +14 -13
  73. package/dist/esm/src/lib/interfaces.d.ts +15 -0
  74. package/dist/esm/src/lib/settings/create-settings.d.ts +1 -1
  75. package/dist/esm/src/lib/settings/create-settings.js +6 -3
  76. package/dist/esm/src/lib/settings/handle-get-settings-error.d.ts +1 -1
  77. package/dist/esm/src/lib/settings/interfaces.d.ts +8 -11
  78. package/dist/esm/src/lib/settings/validate-settings.d.ts +3 -2
  79. package/dist/esm/src/lib/settings/validate-settings.js +8 -6
  80. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  81. package/package.json +3 -4
  82. package/dist/cjs/src/lib/cookie/create-cookie.d.ts +0 -7
  83. package/dist/cjs/src/lib/cookie/create-cookie.js +0 -20
  84. package/dist/cjs/src/lib/cookie/get-browser-id-from-middleware-request.d.ts +0 -11
  85. package/dist/cjs/src/lib/cookie/get-browser-id-from-request.d.ts +0 -9
  86. package/dist/cjs/src/lib/cookie/get-browser-id-from-request.js +0 -25
  87. package/dist/cjs/src/lib/init/get-browser-id.d.ts +0 -5
  88. package/dist/cjs/src/lib/init/get-browser-id.js +0 -15
  89. package/dist/esm/src/lib/cookie/create-cookie.d.ts +0 -7
  90. package/dist/esm/src/lib/cookie/create-cookie.js +0 -16
  91. package/dist/esm/src/lib/cookie/get-browser-id-from-middleware-request.d.ts +0 -11
  92. package/dist/esm/src/lib/cookie/get-browser-id-from-request.d.ts +0 -9
  93. package/dist/esm/src/lib/cookie/get-browser-id-from-request.js +0 -21
  94. package/dist/esm/src/lib/init/get-browser-id.d.ts +0 -5
  95. package/dist/esm/src/lib/init/get-browser-id.js +0 -11
@@ -7,3 +7,15 @@ export const DEFAULT_COOKIE_EXPIRY_DAYS = 730;
7
7
  export const DAILY_SECONDS = 86400;
8
8
  export const API_VERSION = 'v1.2';
9
9
  export const SITECORE_EDGE_URL = 'https://edge-platform.sitecorecloud.io';
10
+ export const CORRELATION_ID_HEADER = 'x-sc-correlation-id';
11
+ /* eslint-disable max-len */
12
+ export var ErrorMessages;
13
+ (function (ErrorMessages) {
14
+ ErrorMessages["IE_0003"] = "[IE-0003] Unable to set the \"sc_{SitecoreEdgeContextId}\" cookie because the browser ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.";
15
+ ErrorMessages["IE_0008"] = "[IE-0008] You must first initialize the \"core\" package. Run the \"init\" function.";
16
+ ErrorMessages["IE_0011"] = "[IE-0011] Unable to set the \"sc_{SitecoreEdgeContextId}_personalize\" cookie because the guest ID could not be retrieved from the server. Make sure to set the correct values for \"sitecoreEdgeContextId\" and \"siteName\". If the issue persists, try again later or use try-catch blocks to handle this error.";
17
+ ErrorMessages["IV_0001"] = "[IV-0001] Incorrect value for \"sitecoreEdgeUrl\". Set the value to a valid URL string.";
18
+ ErrorMessages["MV_0001"] = "[MV-0001] \"sitecoreEdgeContextId\" is required.";
19
+ ErrorMessages["MV_0002"] = "[MV-0002] \"siteName\" is required.";
20
+ })(ErrorMessages || (ErrorMessages = {}));
21
+ /* eslint-enable max-len */
@@ -0,0 +1,6 @@
1
+ import type { Settings } from '../settings/interfaces';
2
+ /**
3
+ * Creates and adds the cookie to the document
4
+ * @param settings - The CookieSettings settings object
5
+ */
6
+ export declare function createCookies(settings: Settings): Promise<void>;
@@ -0,0 +1,24 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { createCookieString, getCookie } from '@sitecore-cloudsdk/utils';
3
+ import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-proxy';
4
+ import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
5
+ import { getGuestId } from '../init/get-guest-id';
6
+ /**
7
+ * Creates and adds the cookie to the document
8
+ * @param settings - The CookieSettings settings object
9
+ */
10
+ export async function createCookies(settings) {
11
+ const attributes = getDefaultCookieAttributes(settings.cookieSettings.cookieExpiryDays, settings.cookieSettings.cookieDomain);
12
+ const browserIdCookie = getCookie(window.document.cookie, settings.cookieSettings.cookieNames.browserId);
13
+ if (browserIdCookie) {
14
+ const guestIdCookie = getCookie(window.document.cookie, settings.cookieSettings.cookieNames.guestId);
15
+ if (guestIdCookie)
16
+ return;
17
+ const guestId = await getGuestId(browserIdCookie.value, settings.sitecoreEdgeContextId, settings.sitecoreEdgeUrl);
18
+ document.cookie = createCookieString(settings.cookieSettings.cookieNames.guestId, guestId, attributes);
19
+ return;
20
+ }
21
+ const { browserId, guestId } = await fetchBrowserIdFromEdgeProxy(settings.sitecoreEdgeUrl, settings.sitecoreEdgeContextId);
22
+ document.cookie = createCookieString(settings.cookieSettings.cookieNames.browserId, browserId, attributes);
23
+ document.cookie = createCookieString(settings.cookieSettings.cookieNames.guestId, guestId, attributes);
24
+ }
@@ -7,5 +7,5 @@ import { getSettings } from '../init/init-core';
7
7
  */
8
8
  export function getBrowserId() {
9
9
  const settings = getSettings();
10
- return getCookieValueClientSide(settings.cookieSettings.cookieName);
10
+ return getCookieValueClientSide(settings.cookieSettings.cookieNames.browserId);
11
11
  }
@@ -0,0 +1,11 @@
1
+ import type { MiddlewareRequest } from '@sitecore-cloudsdk/utils';
2
+ /**
3
+ * Extracts the cookie value from the provided Middleware Request by reading the
4
+ * given `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 cookie value extracted from the cookie, or undefined if not found.
10
+ */
11
+ export declare function getCookieValueFromMiddlewareRequest(request: MiddlewareRequest, cookieName: string): string | undefined;
@@ -1,14 +1,14 @@
1
1
  // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
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,
3
+ * Extracts the cookie value from the provided Middleware Request by reading the
4
+ * given `cookieName` The function first checks for Next.js v12 cookie values,
5
5
  * and if not found, it checks for Next.js v13 cookie values.
6
6
  *
7
7
  * @param request - The Middleware Request object.
8
8
  * @param cookieName - The name of the cookie to retrieve.
9
- * @returns The browser ID extracted from the cookie, or undefined if not found.
9
+ * @returns The cookie value extracted from the cookie, or undefined if not found.
10
10
  */
11
- export function getBrowserIdFromMiddlewareRequest(request, cookieName) {
11
+ export function getCookieValueFromMiddlewareRequest(request, cookieName) {
12
12
  const cookieValueFromRequest = request.cookies.get(cookieName);
13
13
  // It checks nextjs v12 cookie values
14
14
  if (typeof cookieValueFromRequest === 'string')
@@ -0,0 +1,9 @@
1
+ import type { Request } from '@sitecore-cloudsdk/utils';
2
+ /**
3
+ * Retrieves the cookie value 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 cookie value from.
7
+ * @returns The cookie value extracted from the cookie or an empty string if not found.
8
+ */
9
+ export declare function getCookieValueFromRequest<T extends Request>(request: T, cookieName: string): string;
@@ -0,0 +1,20 @@
1
+ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { getCookieServerSide, isHttpRequest, isNextJsMiddlewareRequest } from '@sitecore-cloudsdk/utils';
3
+ import { getCookieValueFromMiddlewareRequest } from './get-cookie-value-from-middleware-request';
4
+ /**
5
+ * Retrieves the cookie value from the provided request object ('T'), using the specified 'cookieName'.
6
+ *
7
+ * @param request - The request object, either a Middleware Request or an HTTP Request.
8
+ * @param cookieName - The name of the cookie to retrieve the cookie value from.
9
+ * @returns The cookie value extracted from the cookie or an empty string if not found.
10
+ */
11
+ export function getCookieValueFromRequest(request, cookieName) {
12
+ let cookieValue = undefined;
13
+ if (isNextJsMiddlewareRequest(request))
14
+ cookieValue = getCookieValueFromMiddlewareRequest(request, cookieName);
15
+ else if (isHttpRequest(request)) {
16
+ const cookieHeader = request.headers.cookie;
17
+ cookieValue = getCookieServerSide(cookieHeader, cookieName)?.value;
18
+ }
19
+ return cookieValue ?? '';
20
+ }
@@ -1,7 +1,7 @@
1
+ import type { CookieProperties } from '@sitecore-cloudsdk/utils';
1
2
  /**
2
3
  * Gets the default Cookie Attributes
3
4
  * @param maxAge - Set the cookie "Max-Age" attribute in days.
4
5
  * @returns the default configuration settings for the cookie string
5
6
  */
6
- import { CookieProperties } from '@sitecore-cloudsdk/utils';
7
7
  export declare function getDefaultCookieAttributes(maxAge?: number, cookieDomain?: string): CookieProperties;
@@ -1,17 +1,16 @@
1
1
  // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
+ import { DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS } from '../consts';
2
3
  /**
3
4
  * Gets the default Cookie Attributes
4
5
  * @param maxAge - Set the cookie "Max-Age" attribute in days.
5
6
  * @returns the default configuration settings for the cookie string
6
7
  */
7
- import { DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS } from '../consts';
8
- // eslint-disable-next-line max-len
9
8
  export function getDefaultCookieAttributes(maxAge = DEFAULT_COOKIE_EXPIRY_DAYS, cookieDomain) {
10
9
  return {
11
10
  domain: cookieDomain,
12
11
  maxAge: maxAge * DAILY_SECONDS,
13
12
  path: '/',
14
13
  sameSite: 'None',
15
- secure: true,
14
+ secure: true
16
15
  };
17
16
  }
@@ -1,5 +1,5 @@
1
1
  import type { HttpRequest, HttpResponse } from '@sitecore-cloudsdk/utils';
2
- import { Settings } from '../settings/interfaces';
2
+ import type { Settings } from '../settings/interfaces';
3
3
  /**
4
4
  * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.
5
5
  *
@@ -1,7 +1,8 @@
1
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
2
  import { createCookieString, getCookieServerSide } from '@sitecore-cloudsdk/utils';
4
3
  import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-proxy';
4
+ import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
5
+ import { getGuestId } from '../init/get-guest-id';
5
6
  /**
6
7
  * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.
7
8
  *
@@ -14,21 +15,33 @@ import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-
14
15
  * @throws [IE-0003] - This exception is thrown in the case getBrowserIdFromEP wasn't able to retrieve a browser id.
15
16
  */
16
17
  export async function handleHttpCookie(request, response, options, timeout) {
17
- const { cookieName } = options.cookieSettings;
18
- const cookieValueFromRequest = request.headers.cookie;
19
- let cookie;
20
- let cookieValue;
21
- if (cookieValueFromRequest) {
22
- cookie = getCookieServerSide(cookieValueFromRequest, cookieName);
23
- if (cookie)
24
- cookieValue = cookie.value;
25
- }
26
- if (!cookieValue)
27
- cookieValue = (await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout))
28
- .browserId;
18
+ const { browserId, guestId } = options.cookieSettings.cookieNames;
19
+ const browserIdCookie = getCookieServerSide(request.headers.cookie, browserId);
20
+ let browserIdCookieValue;
21
+ const guestIdCookie = getCookieServerSide(request.headers.cookie, guestId);
22
+ let guestIdCookieValue;
29
23
  const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
30
- const cookieString = createCookieString(cookieName, cookieValue, defaultCookieAttributes);
31
- if (!cookie)
32
- request.headers.cookie = cookieValueFromRequest ? cookieValueFromRequest + '; ' + cookieString : cookieString;
33
- response.setHeader('Set-Cookie', cookieString);
24
+ if (!browserIdCookie) {
25
+ const cookieValues = await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout);
26
+ browserIdCookieValue = cookieValues.browserId;
27
+ guestIdCookieValue = cookieValues.guestId;
28
+ }
29
+ else {
30
+ browserIdCookieValue = browserIdCookie.value;
31
+ if (!guestIdCookie)
32
+ guestIdCookieValue = await getGuestId(browserIdCookie.value, options.sitecoreEdgeContextId, options.sitecoreEdgeUrl);
33
+ else
34
+ guestIdCookieValue = guestIdCookie.value;
35
+ }
36
+ const browserIdCookieString = createCookieString(browserId, browserIdCookieValue, defaultCookieAttributes);
37
+ const guestIdCookieString = createCookieString(guestId, guestIdCookieValue, defaultCookieAttributes);
38
+ if (!browserIdCookie)
39
+ request.headers.cookie = request.headers.cookie
40
+ ? request.headers.cookie + '; ' + browserIdCookieString
41
+ : browserIdCookieString;
42
+ if (!guestIdCookie)
43
+ request.headers.cookie = request.headers.cookie
44
+ ? request.headers.cookie + '; ' + guestIdCookieString
45
+ : guestIdCookieString;
46
+ response.setHeader('Set-Cookie', [browserIdCookieString, guestIdCookieString]);
34
47
  }
@@ -1,5 +1,5 @@
1
- import { MiddlewareNextResponse, MiddlewareRequest } from '@sitecore-cloudsdk/utils';
2
- import { Settings } from '../settings/interfaces';
1
+ import type { MiddlewareNextResponse, MiddlewareRequest } from '@sitecore-cloudsdk/utils';
2
+ import type { Settings } from '../settings/interfaces';
3
3
  /**
4
4
  * Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.
5
5
  * If 'cookieValue' is not present in the request, it fetches it using the 'fetchBrowserIdFromEdgeProxy' function
@@ -10,7 +10,7 @@ import { Settings } from '../settings/interfaces';
10
10
  * @param options - The settings object containing configuration options.
11
11
  * @param defaultCookieAttributes - The default attributes for the cookie.
12
12
  *
13
- * @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
13
+ * @throws [IE-0003] - This exception is thrown when fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
14
14
  *
15
15
  */
16
16
  export declare function handleNextJsMiddlewareCookie(request: MiddlewareRequest, response: MiddlewareNextResponse, options: Settings, timeout?: number): Promise<void>;
@@ -1,7 +1,8 @@
1
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
2
  import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-proxy';
3
+ import { getCookieValueFromMiddlewareRequest } from './get-cookie-value-from-middleware-request';
4
+ import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
5
+ import { getGuestId } from '../init/get-guest-id';
5
6
  /**
6
7
  * Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.
7
8
  * If 'cookieValue' is not present in the request, it fetches it using the 'fetchBrowserIdFromEdgeProxy' function
@@ -12,16 +13,23 @@ import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-
12
13
  * @param options - The settings object containing configuration options.
13
14
  * @param defaultCookieAttributes - The default attributes for the cookie.
14
15
  *
15
- * @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
16
+ * @throws [IE-0003] - This exception is thrown when fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
16
17
  *
17
18
  */
18
19
  export async function handleNextJsMiddlewareCookie(request, response, options, timeout) {
19
- const { cookieName } = options.cookieSettings;
20
- let cookieValue = getBrowserIdFromMiddlewareRequest(request, cookieName);
21
- if (!cookieValue)
22
- cookieValue = (await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout))
23
- .browserId;
20
+ const { browserId, guestId } = options.cookieSettings.cookieNames;
21
+ let browserIdCookieValue = getCookieValueFromMiddlewareRequest(request, browserId);
22
+ let guestIdCookieValue = getCookieValueFromMiddlewareRequest(request, guestId);
23
+ if (!browserIdCookieValue) {
24
+ const cookieValues = await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout);
25
+ browserIdCookieValue = cookieValues.browserId;
26
+ guestIdCookieValue = cookieValues.guestId;
27
+ }
28
+ else if (!guestIdCookieValue)
29
+ guestIdCookieValue = await getGuestId(browserIdCookieValue, options.sitecoreEdgeContextId, options.sitecoreEdgeUrl);
24
30
  const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
25
- request.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
26
- response.cookies.set(cookieName, cookieValue, defaultCookieAttributes);
31
+ request.cookies.set(browserId, browserIdCookieValue, defaultCookieAttributes);
32
+ request.cookies.set(guestId, guestIdCookieValue, defaultCookieAttributes);
33
+ response.cookies.set(browserId, browserIdCookieValue, defaultCookieAttributes);
34
+ response.cookies.set(guestId, guestIdCookieValue, defaultCookieAttributes);
27
35
  }
@@ -1,4 +1,4 @@
1
- import { type Request, type MiddlewareNextResponse, type HttpResponse } from '@sitecore-cloudsdk/utils';
1
+ import { type HttpResponse, type MiddlewareNextResponse, type Request } from '@sitecore-cloudsdk/utils';
2
2
  /**
3
3
  * Handles server-side cookie operations based on the provided 'request' and 'response' objects.
4
4
  *
@@ -1,8 +1,8 @@
1
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';
2
+ import { isHttpRequest, isHttpResponse, isNextJsMiddlewareRequest, isNextJsMiddlewareResponse } from '@sitecore-cloudsdk/utils';
3
3
  import { getSettingsServer } from '../init/init-core-server';
4
- import { handleNextJsMiddlewareCookie } from './handle-next-js-middleware-cookie';
5
4
  import { handleHttpCookie } from './handle-http-cookie';
5
+ import { handleNextJsMiddlewareCookie } from './handle-next-js-middleware-cookie';
6
6
  /**
7
7
  * Handles server-side cookie operations based on the provided 'request' and 'response' objects.
8
8
  *
@@ -13,10 +13,8 @@ import { handleHttpCookie } from './handle-http-cookie';
13
13
  */
14
14
  export async function handleServerCookie(request, response, timeout) {
15
15
  const settings = getSettingsServer();
16
- if (isNextJsMiddlewareRequest(request) && isNextJsMiddlewareResponse(response)) {
16
+ if (isNextJsMiddlewareRequest(request) && isNextJsMiddlewareResponse(response))
17
17
  await handleNextJsMiddlewareCookie(request, response, settings, timeout);
18
- }
19
- else if (isHttpRequest(request) && isHttpResponse(response)) {
18
+ else if (isHttpRequest(request) && isHttpResponse(response))
20
19
  await handleHttpCookie(request, response, settings, timeout);
21
- }
22
20
  }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * A function that generates a correlation id
3
+ *
4
+ * @returns A correlation id string
5
+ */
6
+ export declare function generateCorrelationId(): 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 { generateV4UUID } from '@sitecore-cloudsdk/utils';
3
+ /**
4
+ * A function that generates a correlation id
5
+ *
6
+ * @returns A correlation id string
7
+ */
8
+ export function generateCorrelationId() {
9
+ return generateV4UUID().replace(/-/g, '');
10
+ }
@@ -1,2 +1,8 @@
1
1
  import debug from 'debug';
2
2
  export { debug };
3
+ /**
4
+ * Extracts debug information from an HTTP response if debugging is enabled.
5
+ * @param response - The HTTP response object from fetch.
6
+ * @returns An object containing selected response details for debugging purposes.
7
+ */
8
+ export declare function processDebugResponse(namespace: string, response: Response): object;
@@ -1,10 +1,26 @@
1
- // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
1
  import debug from 'debug';
2
+ import { normalizeHeaders } from '@sitecore-cloudsdk/utils';
3
3
  if (process.env &&
4
4
  process.env.DEBUG_MULTILINE === 'true' &&
5
5
  debug.formatters &&
6
6
  debug.formatters.o &&
7
- debug.formatters.O) {
7
+ debug.formatters.O)
8
8
  debug.formatters.o = debug.formatters.O;
9
- }
10
9
  export { debug };
10
+ /**
11
+ * Extracts debug information from an HTTP response if debugging is enabled.
12
+ * @param response - The HTTP response object from fetch.
13
+ * @returns An object containing selected response details for debugging purposes.
14
+ */
15
+ export function processDebugResponse(namespace, response) {
16
+ let debugResponse = {};
17
+ if (debug.enabled(namespace))
18
+ debugResponse = {
19
+ headers: normalizeHeaders(response.headers),
20
+ redirected: response.redirected,
21
+ status: response.status,
22
+ statusText: response.statusText,
23
+ url: response.url
24
+ };
25
+ return debugResponse;
26
+ }
@@ -7,5 +7,6 @@ import { API_VERSION } from '../consts';
7
7
  * @returns The URL string for retrieving the browser ID and ClientKey
8
8
  */
9
9
  export function constructGetBrowserIdUrl(sitecoreEdgeUrl, sitecoreEdgeContextId) {
10
+ // eslint-disable-next-line max-len
10
11
  return `${sitecoreEdgeUrl}/v1/events/${API_VERSION}/browser/create.json?sitecoreContextId=${sitecoreEdgeContextId}&client_key=`;
11
12
  }
@@ -1,4 +1,4 @@
1
- import { ProxySettings } from '../interfaces';
1
+ import type { ProxySettings } from '../interfaces';
2
2
  /**
3
3
  * Gets the browser ID and Client Key from Sitecore Edge Proxy
4
4
  * @param sitecoreEdgeUrl - The baseURL for the Edge Proxy API.
@@ -1,6 +1,6 @@
1
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';
2
+ import { ErrorMessages, LIBRARY_VERSION } from '../consts';
3
+ import { ErrorMessages as UtilsErrorMessages, fetchWithTimeout } from '@sitecore-cloudsdk/utils';
4
4
  import { constructGetBrowserIdUrl } from './construct-get-browser-id-url';
5
5
  /**
6
6
  * Gets the browser ID and Client Key from Sitecore Edge Proxy
@@ -12,29 +12,26 @@ import { constructGetBrowserIdUrl } from './construct-get-browser-id-url';
12
12
  export async function fetchBrowserIdFromEdgeProxy(sitecoreEdgeUrl, sitecoreEdgeContextId, timeout) {
13
13
  const fetchOptions = {
14
14
  // eslint-disable-next-line @typescript-eslint/naming-convention
15
- headers: { 'X-Library-Version': LIBRARY_VERSION },
15
+ headers: { 'X-Library-Version': LIBRARY_VERSION }
16
16
  };
17
17
  const url = constructGetBrowserIdUrl(sitecoreEdgeUrl, sitecoreEdgeContextId);
18
18
  let payload;
19
- if (timeout !== undefined) {
19
+ if (timeout !== undefined)
20
20
  payload = await fetchWithTimeout(url, timeout, fetchOptions)
21
21
  .then((response) => {
22
22
  return (response && response.json()) || null;
23
23
  })
24
24
  .catch((err) => {
25
- if (err.message.includes('IV-0006') || err.message.includes('IE-0002')) {
25
+ if (err.message === UtilsErrorMessages.IV_0006 || err.message === UtilsErrorMessages.IE_0002)
26
26
  throw new Error(err.message);
27
- }
28
27
  return null;
29
28
  });
30
- }
31
- else {
29
+ else
32
30
  payload = await fetch(url, fetchOptions)
33
31
  .then((res) => res.json())
34
32
  .catch(() => undefined);
35
- }
36
33
  if (!payload?.ref)
37
- 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.');
38
- const { ref: browserId } = payload;
39
- return { browserId };
34
+ throw new Error(ErrorMessages.IE_0003);
35
+ const { ref: browserId, customer_ref: guestId } = payload;
36
+ return { browserId, guestId };
40
37
  }
@@ -1,5 +1,5 @@
1
1
  // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
2
- import { API_VERSION, LIBRARY_VERSION } from '../consts';
2
+ import { API_VERSION, ErrorMessages, LIBRARY_VERSION } from '../consts';
3
3
  /**
4
4
  * A function that gets the guest ref from EP.
5
5
  * @param browserId - The browser id of the client
@@ -17,5 +17,7 @@ export async function getGuestId(browserId, sitecoreEdgeContextId, sitecoreEdgeU
17
17
  const { error_msg: errorMsg, moreInfo } = data;
18
18
  throw new Error(`${errorMsg}, for more info: ${moreInfo}`);
19
19
  }
20
+ if (!data.customer.ref)
21
+ throw new Error(ErrorMessages.IE_0011);
20
22
  return data.customer.ref;
21
23
  }
@@ -1,10 +1,11 @@
1
- import { HttpResponse, MiddlewareNextResponse, Request } from '@sitecore-cloudsdk/utils';
2
- import { Settings, SettingsParamsServer } from '../settings/interfaces';
1
+ import type { Request, Response } from '@sitecore-cloudsdk/utils';
2
+ import type { ServerSettings, Settings } from '../settings/interfaces';
3
3
  export declare function setCoreSettings(settings: Settings): void;
4
4
  /**
5
5
  * Initializes the core settings for browser-based applications.
6
6
  *
7
- * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
7
+ * This function initializes core settings for the application,
8
+ * including creating settings and handling cookies if enabled.
8
9
  *
9
10
  * @param settingsInput - The settings input to configure the core settings.
10
11
  * @returns A Promise that resolves when initialization is complete.
@@ -13,11 +14,12 @@ export declare function getSettingsServer(): Settings;
13
14
  /**
14
15
  * Initializes the core settings for browser-based applications.
15
16
  *
16
- * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
17
+ * This function initializes core settings for the application,
18
+ * including creating settings and handling cookies if enabled.
17
19
  *
18
20
  * @param settingsInput - The settings input to configure the core settings.
19
21
  * @param request - A request object of type HttpRequest or MiddlewareRequest
20
22
  * @param response - A response object of type HttpResponse or MiddlewareNextResponse
21
23
  * @returns A Promise that resolves when initialization is complete.
22
24
  */
23
- export declare function initCoreServer<Response extends MiddlewareNextResponse | HttpResponse>(settingsInput: SettingsParamsServer, request: Request, response: Response): Promise<void>;
25
+ export declare function initCoreServer(settingsInput: ServerSettings, request: Request, response: Response): Promise<void>;
@@ -1,8 +1,9 @@
1
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';
2
+ import { CORE_NAMESPACE } from '../debug/namespaces';
3
+ import { ErrorMessages } from '../consts';
3
4
  import { createSettings } from '../settings/create-settings';
4
5
  import { debug } from '../debug/debug';
5
- import { CORE_NAMESPACE } from '../debug/namespaces';
6
+ import { handleServerCookie } from '../cookie/handle-server-cookie';
6
7
  /**
7
8
  * Internal settings object to be used by all functions in module caching.
8
9
  * It starts with a null value and is set to the proper object by the function. *
@@ -15,21 +16,22 @@ export function setCoreSettings(settings) {
15
16
  /**
16
17
  * Initializes the core settings for browser-based applications.
17
18
  *
18
- * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
19
+ * This function initializes core settings for the application,
20
+ * including creating settings and handling cookies if enabled.
19
21
  *
20
22
  * @param settingsInput - The settings input to configure the core settings.
21
23
  * @returns A Promise that resolves when initialization is complete.
22
24
  */
23
25
  export function getSettingsServer() {
24
- if (!coreSettings) {
25
- throw Error('[IE-0008] You must first initialize the "core" package. Run the "init" function.');
26
- }
26
+ if (!coreSettings)
27
+ throw Error(ErrorMessages.IE_0008);
27
28
  return coreSettings;
28
29
  }
29
30
  /**
30
31
  * Initializes the core settings for browser-based applications.
31
32
  *
32
- * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
33
+ * This function initializes core settings for the application,
34
+ * including creating settings and handling cookies if enabled.
33
35
  *
34
36
  * @param settingsInput - The settings input to configure the core settings.
35
37
  * @param request - A request object of type HttpRequest or MiddlewareRequest
@@ -40,7 +42,6 @@ export async function initCoreServer(settingsInput, request, response) {
40
42
  debug(CORE_NAMESPACE)('initializing %o', 'initCoreServer initialized');
41
43
  if (!coreSettings)
42
44
  coreSettings = createSettings(settingsInput);
43
- if (settingsInput.enableServerCookie) {
45
+ if (settingsInput.enableServerCookie)
44
46
  await handleServerCookie(request, response, settingsInput.timeout);
45
- }
46
47
  }
@@ -1,8 +1,9 @@
1
- import { Settings, SettingsParamsBrowser } from '../settings/interfaces';
1
+ import type { BrowserSettings, Settings } from '../settings/interfaces';
2
2
  /**
3
3
  * Retrieves the core settings object.
4
4
  *
5
- * This function ensures that the core settings have been initialized and contain essential properties like `clientKey`, `cookieSettings`, and `targetURL`.
5
+ * This function ensures that the core settings have been initialized and contain essential properties
6
+ * like `clientKey`, `cookieSettings`, and `targetURL`.
6
7
  *
7
8
  * @returns The core settings object.
8
9
  * @throws Error if the core settings haven't been initialized with the required properties.
@@ -11,12 +12,13 @@ export declare function getSettings(): Settings;
11
12
  /**
12
13
  * Initializes the core settings for browser-based applications.
13
14
  *
14
- * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
15
+ * This function initializes core settings for the application,
16
+ * including creating settings and handling cookies if enabled.
15
17
  *
16
18
  * @param settingsInput - The settings input to configure the core settings.
17
19
  * @returns A Promise that resolves when initialization is complete.
18
20
  */
19
- export declare function initCore(settingsInput: SettingsParamsBrowser): Promise<void>;
21
+ export declare function initCore(settingsInput: BrowserSettings): Promise<void>;
20
22
  /**
21
23
  * Helper functions for tests
22
24
  */
@@ -1,8 +1,8 @@
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';
1
+ import { CORE_NAMESPACE } from '../debug/namespaces';
2
+ import { ErrorMessages } from '../consts';
3
+ import { createCookies } from '../cookie/create-cookies';
3
4
  import { createSettings } from '../settings/create-settings';
4
5
  import { debug } from '../debug/debug';
5
- import { CORE_NAMESPACE } from '../debug/namespaces';
6
6
  /**
7
7
  * Internal settings object to be used by all functions in module caching.
8
8
  * It starts with a null value and is set to the proper object by the function. *
@@ -12,22 +12,23 @@ let coreSettings = null;
12
12
  /**
13
13
  * Retrieves the core settings object.
14
14
  *
15
- * This function ensures that the core settings have been initialized and contain essential properties like `clientKey`, `cookieSettings`, and `targetURL`.
15
+ * This function ensures that the core settings have been initialized and contain essential properties
16
+ * like `clientKey`, `cookieSettings`, and `targetURL`.
16
17
  *
17
18
  * @returns The core settings object.
18
19
  * @throws Error if the core settings haven't been initialized with the required properties.
19
20
  */
20
21
  export function getSettings() {
21
- if (!coreSettings) {
22
- throw Error(`[IE-0008] You must first initialize the "core" package. Run the "init" function.`);
23
- }
22
+ if (!coreSettings)
23
+ throw Error(ErrorMessages.IE_0008);
24
24
  return coreSettings;
25
25
  }
26
- let createCookiePromise = null;
26
+ let createCookiesPromise = null;
27
27
  /**
28
28
  * Initializes the core settings for browser-based applications.
29
29
  *
30
- * This function initializes core settings for the application, including creating settings and handling cookies if enabled.
30
+ * This function initializes core settings for the application,
31
+ * including creating settings and handling cookies if enabled.
31
32
  *
32
33
  * @param settingsInput - The settings input to configure the core settings.
33
34
  * @returns A Promise that resolves when initialization is complete.
@@ -36,9 +37,9 @@ export async function initCore(settingsInput) {
36
37
  debug(CORE_NAMESPACE)('coreClient library initialized');
37
38
  if (coreSettings === null)
38
39
  coreSettings = createSettings(settingsInput);
39
- if (settingsInput.enableBrowserCookie && createCookiePromise === null)
40
- createCookiePromise = createCookie(coreSettings);
41
- await createCookiePromise;
40
+ if (settingsInput.enableBrowserCookie && createCookiesPromise === null)
41
+ createCookiesPromise = createCookies(coreSettings);
42
+ await createCookiesPromise;
42
43
  }
43
44
  /**
44
45
  * Helper functions for tests
@@ -47,5 +48,5 @@ export function setCoreSettings(settings) {
47
48
  coreSettings = settings;
48
49
  }
49
50
  export function setCookiePromise(promise) {
50
- createCookiePromise = promise;
51
+ createCookiesPromise = promise;
51
52
  }