@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.
- package/dist/cjs/package.json +3 -4
- package/dist/cjs/src/index.d.ts +8 -7
- package/dist/cjs/src/index.js +14 -10
- package/dist/cjs/src/lib/consts.d.ts +9 -0
- package/dist/cjs/src/lib/consts.js +13 -1
- package/dist/cjs/src/lib/cookie/create-cookies.d.ts +6 -0
- package/dist/cjs/src/lib/cookie/create-cookies.js +28 -0
- package/dist/cjs/src/lib/cookie/get-browser-id.js +1 -1
- package/dist/cjs/src/lib/cookie/get-cookie-value-from-middleware-request.d.ts +11 -0
- package/dist/cjs/src/lib/cookie/{get-browser-id-from-middleware-request.js → get-cookie-value-from-middleware-request.js} +6 -6
- package/dist/cjs/src/lib/cookie/get-cookie-value-from-request.d.ts +9 -0
- package/dist/cjs/src/lib/cookie/get-cookie-value-from-request.js +24 -0
- package/dist/cjs/src/lib/cookie/get-default-cookie-attributes.d.ts +1 -1
- package/dist/cjs/src/lib/cookie/get-default-cookie-attributes.js +4 -5
- package/dist/cjs/src/lib/cookie/handle-http-cookie.d.ts +1 -1
- package/dist/cjs/src/lib/cookie/handle-http-cookie.js +30 -17
- package/dist/cjs/src/lib/cookie/handle-next-js-middleware-cookie.d.ts +3 -3
- package/dist/cjs/src/lib/cookie/handle-next-js-middleware-cookie.js +18 -10
- package/dist/cjs/src/lib/cookie/handle-server-cookie.d.ts +1 -1
- package/dist/cjs/src/lib/cookie/handle-server-cookie.js +3 -5
- package/dist/cjs/src/lib/correlation-id/generate-correlation-id.d.ts +6 -0
- package/dist/cjs/src/lib/correlation-id/generate-correlation-id.js +14 -0
- package/dist/cjs/src/lib/debug/debug.d.ts +6 -0
- package/dist/cjs/src/lib/debug/debug.js +20 -3
- package/dist/cjs/src/lib/init/construct-get-browser-id-url.js +1 -0
- package/dist/cjs/src/lib/init/fetch-browser-id-from-edge-proxy.d.ts +1 -1
- package/dist/cjs/src/lib/init/fetch-browser-id-from-edge-proxy.js +8 -11
- package/dist/cjs/src/lib/init/get-guest-id.js +2 -0
- package/dist/cjs/src/lib/init/init-core-server.d.ts +7 -5
- package/dist/cjs/src/lib/init/init-core-server.js +10 -9
- package/dist/cjs/src/lib/init/init-core.d.ts +6 -4
- package/dist/cjs/src/lib/init/init-core.js +14 -13
- package/dist/cjs/src/lib/interfaces.d.ts +15 -0
- package/dist/cjs/src/lib/settings/create-settings.d.ts +1 -1
- package/dist/cjs/src/lib/settings/create-settings.js +6 -3
- package/dist/cjs/src/lib/settings/handle-get-settings-error.d.ts +1 -1
- package/dist/cjs/src/lib/settings/interfaces.d.ts +8 -11
- package/dist/cjs/src/lib/settings/validate-settings.d.ts +3 -2
- package/dist/cjs/src/lib/settings/validate-settings.js +8 -6
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/package.json +3 -4
- package/dist/esm/src/index.d.ts +8 -7
- package/dist/esm/src/index.js +6 -5
- package/dist/esm/src/lib/consts.d.ts +9 -0
- package/dist/esm/src/lib/consts.js +12 -0
- package/dist/esm/src/lib/cookie/create-cookies.d.ts +6 -0
- package/dist/esm/src/lib/cookie/create-cookies.js +24 -0
- package/dist/esm/src/lib/cookie/get-browser-id.js +1 -1
- package/dist/esm/src/lib/cookie/get-cookie-value-from-middleware-request.d.ts +11 -0
- package/dist/esm/src/lib/cookie/{get-browser-id-from-middleware-request.js → get-cookie-value-from-middleware-request.js} +4 -4
- package/dist/esm/src/lib/cookie/get-cookie-value-from-request.d.ts +9 -0
- package/dist/esm/src/lib/cookie/get-cookie-value-from-request.js +20 -0
- package/dist/esm/src/lib/cookie/get-default-cookie-attributes.d.ts +1 -1
- package/dist/esm/src/lib/cookie/get-default-cookie-attributes.js +2 -3
- package/dist/esm/src/lib/cookie/handle-http-cookie.d.ts +1 -1
- package/dist/esm/src/lib/cookie/handle-http-cookie.js +30 -17
- package/dist/esm/src/lib/cookie/handle-next-js-middleware-cookie.d.ts +3 -3
- package/dist/esm/src/lib/cookie/handle-next-js-middleware-cookie.js +18 -10
- package/dist/esm/src/lib/cookie/handle-server-cookie.d.ts +1 -1
- package/dist/esm/src/lib/cookie/handle-server-cookie.js +4 -6
- package/dist/esm/src/lib/correlation-id/generate-correlation-id.d.ts +6 -0
- package/dist/esm/src/lib/correlation-id/generate-correlation-id.js +10 -0
- package/dist/esm/src/lib/debug/debug.d.ts +6 -0
- package/dist/esm/src/lib/debug/debug.js +19 -3
- package/dist/esm/src/lib/init/construct-get-browser-id-url.js +1 -0
- package/dist/esm/src/lib/init/fetch-browser-id-from-edge-proxy.d.ts +1 -1
- package/dist/esm/src/lib/init/fetch-browser-id-from-edge-proxy.js +9 -12
- package/dist/esm/src/lib/init/get-guest-id.js +3 -1
- package/dist/esm/src/lib/init/init-core-server.d.ts +7 -5
- package/dist/esm/src/lib/init/init-core-server.js +10 -9
- package/dist/esm/src/lib/init/init-core.d.ts +6 -4
- package/dist/esm/src/lib/init/init-core.js +14 -13
- package/dist/esm/src/lib/interfaces.d.ts +15 -0
- package/dist/esm/src/lib/settings/create-settings.d.ts +1 -1
- package/dist/esm/src/lib/settings/create-settings.js +6 -3
- package/dist/esm/src/lib/settings/handle-get-settings-error.d.ts +1 -1
- package/dist/esm/src/lib/settings/interfaces.d.ts +8 -11
- package/dist/esm/src/lib/settings/validate-settings.d.ts +3 -2
- package/dist/esm/src/lib/settings/validate-settings.js +8 -6
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -4
- package/dist/cjs/src/lib/cookie/create-cookie.d.ts +0 -7
- package/dist/cjs/src/lib/cookie/create-cookie.js +0 -20
- package/dist/cjs/src/lib/cookie/get-browser-id-from-middleware-request.d.ts +0 -11
- package/dist/cjs/src/lib/cookie/get-browser-id-from-request.d.ts +0 -9
- package/dist/cjs/src/lib/cookie/get-browser-id-from-request.js +0 -25
- package/dist/cjs/src/lib/init/get-browser-id.d.ts +0 -5
- package/dist/cjs/src/lib/init/get-browser-id.js +0 -15
- package/dist/esm/src/lib/cookie/create-cookie.d.ts +0 -7
- package/dist/esm/src/lib/cookie/create-cookie.js +0 -16
- package/dist/esm/src/lib/cookie/get-browser-id-from-middleware-request.d.ts +0 -11
- package/dist/esm/src/lib/cookie/get-browser-id-from-request.d.ts +0 -9
- package/dist/esm/src/lib/cookie/get-browser-id-from-request.js +0 -21
- package/dist/esm/src/lib/init/get-browser-id.d.ts +0 -5
- 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,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.
|
|
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
|
-
*
|
|
4
|
-
*
|
|
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
|
|
9
|
+
* @returns The cookie value extracted from the cookie, or undefined if not found.
|
|
10
10
|
*/
|
|
11
|
-
export function
|
|
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 {
|
|
18
|
-
const
|
|
19
|
-
let
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
|
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
|
|
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 {
|
|
20
|
-
let
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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(
|
|
26
|
-
|
|
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
|
|
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 {
|
|
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,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,6 +1,6 @@
|
|
|
1
1
|
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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.
|
|
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(
|
|
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 {
|
|
2
|
-
import {
|
|
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,
|
|
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,
|
|
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
|
|
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 {
|
|
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 {
|
|
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,
|
|
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(
|
|
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,
|
|
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 {
|
|
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
|
|
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,
|
|
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:
|
|
21
|
+
export declare function initCore(settingsInput: BrowserSettings): Promise<void>;
|
|
20
22
|
/**
|
|
21
23
|
* Helper functions for tests
|
|
22
24
|
*/
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
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
|
|
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(
|
|
23
|
-
}
|
|
22
|
+
if (!coreSettings)
|
|
23
|
+
throw Error(ErrorMessages.IE_0008);
|
|
24
24
|
return coreSettings;
|
|
25
25
|
}
|
|
26
|
-
let
|
|
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,
|
|
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 &&
|
|
40
|
-
|
|
41
|
-
await
|
|
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
|
-
|
|
51
|
+
createCookiesPromise = promise;
|
|
51
52
|
}
|