@sitecore-cloudsdk/core 0.4.4 → 0.5.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/README.md +9 -3
- package/dist/cjs/package.json +2 -2
- package/dist/cjs/src/internal.d.ts +2 -14
- package/dist/cjs/src/internal.js +3 -25
- package/dist/cjs/src/lib/browser-id/get-browser-id.js +2 -9
- package/dist/cjs/src/lib/consts.d.ts +0 -1
- package/dist/cjs/src/lib/consts.js +0 -1
- package/dist/cjs/src/lib/guest-id/get-guest-id-server.d.ts +7 -0
- package/dist/cjs/src/lib/guest-id/get-guest-id-server.js +17 -0
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/package.json +2 -2
- package/dist/esm/src/internal.d.ts +2 -14
- package/dist/esm/src/internal.js +1 -11
- package/dist/esm/src/lib/browser-id/get-browser-id.js +3 -10
- package/dist/esm/src/lib/consts.d.ts +0 -1
- package/dist/esm/src/lib/consts.js +0 -1
- package/dist/esm/src/lib/guest-id/get-guest-id-server.d.ts +7 -0
- package/dist/esm/src/lib/guest-id/get-guest-id-server.js +13 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/dist/cjs/src/lib/cookie/create-cookies.d.ts +0 -6
- package/dist/cjs/src/lib/cookie/create-cookies.js +0 -28
- package/dist/cjs/src/lib/cookie/handle-http-cookie.d.ts +0 -14
- package/dist/cjs/src/lib/cookie/handle-http-cookie.js +0 -50
- package/dist/cjs/src/lib/cookie/handle-next-js-middleware-cookie.d.ts +0 -16
- package/dist/cjs/src/lib/cookie/handle-next-js-middleware-cookie.js +0 -38
- package/dist/cjs/src/lib/cookie/handle-server-cookie.d.ts +0 -10
- package/dist/cjs/src/lib/cookie/handle-server-cookie.js +0 -24
- package/dist/cjs/src/lib/init/get-guest-id.d.ts +0 -24
- package/dist/cjs/src/lib/init/get-guest-id.js +0 -27
- package/dist/cjs/src/lib/init/init-core-server.d.ts +0 -26
- package/dist/cjs/src/lib/init/init-core-server.js +0 -56
- package/dist/cjs/src/lib/init/init-core.d.ts +0 -41
- package/dist/cjs/src/lib/init/init-core.js +0 -62
- package/dist/cjs/src/lib/settings/create-settings.d.ts +0 -7
- package/dist/cjs/src/lib/settings/create-settings.js +0 -30
- package/dist/cjs/src/lib/settings/handle-get-settings-error.d.ts +0 -2
- package/dist/cjs/src/lib/settings/handle-get-settings-error.js +0 -12
- package/dist/cjs/src/lib/settings/interfaces.d.ts +0 -42
- package/dist/cjs/src/lib/settings/interfaces.js +0 -3
- package/dist/cjs/src/lib/settings/validate-settings.d.ts +0 -11
- package/dist/cjs/src/lib/settings/validate-settings.js +0 -29
- package/dist/esm/src/lib/cookie/create-cookies.d.ts +0 -6
- package/dist/esm/src/lib/cookie/create-cookies.js +0 -24
- package/dist/esm/src/lib/cookie/handle-http-cookie.d.ts +0 -14
- package/dist/esm/src/lib/cookie/handle-http-cookie.js +0 -46
- package/dist/esm/src/lib/cookie/handle-next-js-middleware-cookie.d.ts +0 -16
- package/dist/esm/src/lib/cookie/handle-next-js-middleware-cookie.js +0 -34
- package/dist/esm/src/lib/cookie/handle-server-cookie.d.ts +0 -10
- package/dist/esm/src/lib/cookie/handle-server-cookie.js +0 -20
- package/dist/esm/src/lib/init/get-guest-id.d.ts +0 -24
- package/dist/esm/src/lib/init/get-guest-id.js +0 -23
- package/dist/esm/src/lib/init/init-core-server.d.ts +0 -26
- package/dist/esm/src/lib/init/init-core-server.js +0 -50
- package/dist/esm/src/lib/init/init-core.d.ts +0 -41
- package/dist/esm/src/lib/init/init-core.js +0 -55
- package/dist/esm/src/lib/settings/create-settings.d.ts +0 -7
- package/dist/esm/src/lib/settings/create-settings.js +0 -26
- package/dist/esm/src/lib/settings/handle-get-settings-error.d.ts +0 -2
- package/dist/esm/src/lib/settings/handle-get-settings-error.js +0 -8
- package/dist/esm/src/lib/settings/interfaces.d.ts +0 -42
- package/dist/esm/src/lib/settings/interfaces.js +0 -2
- package/dist/esm/src/lib/settings/validate-settings.d.ts +0 -11
- package/dist/esm/src/lib/settings/validate-settings.js +0 -25
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setCookiePromise = exports.setCoreSettings = exports.initCore = exports.getSettings = void 0;
|
|
4
|
-
const namespaces_1 = require("../debug/namespaces");
|
|
5
|
-
const consts_1 = require("../consts");
|
|
6
|
-
const create_cookies_1 = require("../cookie/create-cookies");
|
|
7
|
-
const create_settings_1 = require("../settings/create-settings");
|
|
8
|
-
const debug_1 = require("../debug/debug");
|
|
9
|
-
/**
|
|
10
|
-
* Internal settings object to be used by all functions in module caching.
|
|
11
|
-
* It starts with a null value and is set to the proper object by the function. *
|
|
12
|
-
* Can be retrieved only through the function.
|
|
13
|
-
*/
|
|
14
|
-
let coreSettings = null;
|
|
15
|
-
/**
|
|
16
|
-
* Retrieves the core settings object.
|
|
17
|
-
*
|
|
18
|
-
* This function ensures that the core settings have been initialized and contain essential properties
|
|
19
|
-
* like `clientKey`, `cookieSettings`, and `targetURL`.
|
|
20
|
-
*
|
|
21
|
-
* @returns The core settings object.
|
|
22
|
-
* @throws Error if the core settings haven't been initialized with the required properties.
|
|
23
|
-
*/
|
|
24
|
-
function getSettings() {
|
|
25
|
-
if (!coreSettings)
|
|
26
|
-
throw Error(consts_1.ErrorMessages.IE_0008);
|
|
27
|
-
return coreSettings;
|
|
28
|
-
}
|
|
29
|
-
exports.getSettings = getSettings;
|
|
30
|
-
let createCookiesPromise = null;
|
|
31
|
-
/* eslint-disable max-len */
|
|
32
|
-
/**
|
|
33
|
-
* Initializes the core settings for browser-based applications.
|
|
34
|
-
*
|
|
35
|
-
* This function initializes core settings for the application,
|
|
36
|
-
* including creating settings and handling cookies if enabled.
|
|
37
|
-
*
|
|
38
|
-
* @param settingsInput - The settings input to configure the core settings.
|
|
39
|
-
* @returns A Promise that resolves when initialization is complete.
|
|
40
|
-
* @deprecated Cloud SDK v0.4 introduces a new initialization logic. If you are upgrading from v0.3, we recommend that you upgrade your initialization code.The v0.3 initialization logic is now deprecated and will be removed in a future Cloud SDK release.
|
|
41
|
-
*/
|
|
42
|
-
/* eslint-enable max-len */
|
|
43
|
-
async function initCore(settingsInput) {
|
|
44
|
-
(0, debug_1.debug)(namespaces_1.CORE_NAMESPACE)('coreClient library initialized');
|
|
45
|
-
if (coreSettings === null)
|
|
46
|
-
coreSettings = (0, create_settings_1.createSettings)(settingsInput);
|
|
47
|
-
if (settingsInput.enableBrowserCookie && createCookiesPromise === null)
|
|
48
|
-
createCookiesPromise = (0, create_cookies_1.createCookies)(coreSettings);
|
|
49
|
-
await createCookiesPromise;
|
|
50
|
-
}
|
|
51
|
-
exports.initCore = initCore;
|
|
52
|
-
/**
|
|
53
|
-
* Helper functions for tests
|
|
54
|
-
*/
|
|
55
|
-
function setCoreSettings(settings) {
|
|
56
|
-
coreSettings = settings;
|
|
57
|
-
}
|
|
58
|
-
exports.setCoreSettings = setCoreSettings;
|
|
59
|
-
function setCookiePromise(promise) {
|
|
60
|
-
createCookiesPromise = promise;
|
|
61
|
-
}
|
|
62
|
-
exports.setCookiePromise = setCookiePromise;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Settings, SettingsParams } 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 Settings with the settings added by the developer
|
|
6
|
-
*/
|
|
7
|
-
export declare function createSettings(settingsInput: SettingsParams): Settings;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createSettings = void 0;
|
|
4
|
-
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
|
|
5
|
-
const consts_1 = require("../consts");
|
|
6
|
-
const validate_settings_1 = require("./validate-settings");
|
|
7
|
-
/**
|
|
8
|
-
* Creates the global settings object, to be used by the library
|
|
9
|
-
* @param settingsInput - Global settings added by the developer.
|
|
10
|
-
* @returns an Settings with the settings added by the developer
|
|
11
|
-
*/
|
|
12
|
-
function createSettings(settingsInput) {
|
|
13
|
-
(0, validate_settings_1.validateSettings)(settingsInput);
|
|
14
|
-
const { siteName, sitecoreEdgeContextId, cookieDomain, cookiePath, cookieExpiryDays, sitecoreEdgeUrl } = settingsInput;
|
|
15
|
-
return {
|
|
16
|
-
cookieSettings: {
|
|
17
|
-
cookieDomain,
|
|
18
|
-
cookieExpiryDays: cookieExpiryDays || consts_1.DEFAULT_COOKIE_EXPIRY_DAYS,
|
|
19
|
-
cookieNames: {
|
|
20
|
-
browserId: `${consts_1.COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}`,
|
|
21
|
-
guestId: `${consts_1.COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}_personalize`
|
|
22
|
-
},
|
|
23
|
-
cookiePath: cookiePath || '/'
|
|
24
|
-
},
|
|
25
|
-
siteName,
|
|
26
|
-
sitecoreEdgeContextId,
|
|
27
|
-
sitecoreEdgeUrl: sitecoreEdgeUrl ?? consts_1.SITECORE_EDGE_URL
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
exports.createSettings = createSettings;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handleGetSettingsError = void 0;
|
|
4
|
-
function handleGetSettingsError(getSettingsFn, newError) {
|
|
5
|
-
try {
|
|
6
|
-
return getSettingsFn();
|
|
7
|
-
}
|
|
8
|
-
catch {
|
|
9
|
-
throw new Error(newError);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
exports.handleGetSettingsError = handleGetSettingsError;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extends the global settings object with additional properties
|
|
3
|
-
*/
|
|
4
|
-
export interface Settings extends BasicSettings {
|
|
5
|
-
cookieSettings: CookieSettings;
|
|
6
|
-
sitecoreEdgeUrl: string;
|
|
7
|
-
}
|
|
8
|
-
export interface BrowserSettings extends SettingsParams {
|
|
9
|
-
enableBrowserCookie?: boolean;
|
|
10
|
-
}
|
|
11
|
-
export interface ServerSettings extends SettingsParams {
|
|
12
|
-
enableServerCookie?: boolean;
|
|
13
|
-
}
|
|
14
|
-
export interface SettingsParams extends BasicSettings, CookieSettingsInput {
|
|
15
|
-
timeout?: number;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Properties for the global settings object
|
|
19
|
-
*/
|
|
20
|
-
export interface BasicSettings {
|
|
21
|
-
sitecoreEdgeContextId: string;
|
|
22
|
-
siteName: string;
|
|
23
|
-
sitecoreEdgeUrl?: string;
|
|
24
|
-
}
|
|
25
|
-
interface CookieSettingsInput {
|
|
26
|
-
cookiePath?: string;
|
|
27
|
-
cookieExpiryDays?: number;
|
|
28
|
-
cookieDomain?: string;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Properties for the cookie object
|
|
32
|
-
*/
|
|
33
|
-
export interface CookieSettings {
|
|
34
|
-
cookieNames: {
|
|
35
|
-
browserId: string;
|
|
36
|
-
guestId: string;
|
|
37
|
-
};
|
|
38
|
-
cookieDomain?: string;
|
|
39
|
-
cookieExpiryDays: number;
|
|
40
|
-
cookiePath?: string;
|
|
41
|
-
}
|
|
42
|
-
export {};
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { SettingsParams } 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
|
|
6
|
-
* such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
|
|
7
|
-
*
|
|
8
|
-
* @param settings - The core settings object to validate.
|
|
9
|
-
* @throws Error with specific error codes if any required property is missing or empty.
|
|
10
|
-
*/
|
|
11
|
-
export declare function validateSettings(settings: SettingsParams): void;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateSettings = void 0;
|
|
4
|
-
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
|
|
5
|
-
const consts_1 = require("../consts");
|
|
6
|
-
/**
|
|
7
|
-
* Validates the core settings to ensure they meet required criteria.
|
|
8
|
-
*
|
|
9
|
-
* This function validates the provided core settings object to ensure that essential properties
|
|
10
|
-
* such as "sitecoreEdgeContextId," and "siteName" meet specific criteria and are not empty.
|
|
11
|
-
*
|
|
12
|
-
* @param settings - The core settings object to validate.
|
|
13
|
-
* @throws Error with specific error codes if any required property is missing or empty.
|
|
14
|
-
*/
|
|
15
|
-
function validateSettings(settings) {
|
|
16
|
-
const { sitecoreEdgeContextId, siteName, sitecoreEdgeUrl } = settings;
|
|
17
|
-
if (!sitecoreEdgeContextId || sitecoreEdgeContextId.trim().length === 0)
|
|
18
|
-
throw new Error(consts_1.ErrorMessages.MV_0001);
|
|
19
|
-
if (!siteName || siteName.trim().length === 0)
|
|
20
|
-
throw new Error(consts_1.ErrorMessages.MV_0002);
|
|
21
|
-
if (sitecoreEdgeUrl !== undefined)
|
|
22
|
-
try {
|
|
23
|
-
new URL(sitecoreEdgeUrl);
|
|
24
|
-
}
|
|
25
|
-
catch (e) {
|
|
26
|
-
throw new Error(consts_1.ErrorMessages.IV_0001);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.validateSettings = validateSettings;
|
|
@@ -1,24 +0,0 @@
|
|
|
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 '../browser-id/fetch-browser-id-from-edge-proxy';
|
|
4
|
-
import { getGuestId } from '../init/get-guest-id';
|
|
5
|
-
import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
|
|
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
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { HttpRequest, HttpResponse } from '@sitecore-cloudsdk/utils';
|
|
2
|
-
import type { Settings } from '../settings/interfaces';
|
|
3
|
-
/**
|
|
4
|
-
* Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.
|
|
5
|
-
*
|
|
6
|
-
* @param request - The HTTP Request object containing request headers and data.
|
|
7
|
-
* @param response - The HTTP Response object.
|
|
8
|
-
* @param options - The settings object containing configuration options.
|
|
9
|
-
* @param timeout - The timeout for the call to proxy to get browserId.
|
|
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 getBrowserIdFromEP wasn't able to retrieve a browser id.
|
|
13
|
-
*/
|
|
14
|
-
export declare function handleHttpCookie(request: HttpRequest, response: HttpResponse, options: Settings, timeout?: number): Promise<void>;
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { createCookieString, getCookieServerSide } from '@sitecore-cloudsdk/utils';
|
|
2
|
-
import { fetchBrowserIdFromEdgeProxy } from '../browser-id/fetch-browser-id-from-edge-proxy';
|
|
3
|
-
import { getGuestId } from '../init/get-guest-id';
|
|
4
|
-
import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
|
|
5
|
-
/**
|
|
6
|
-
* Handles HTTP Cookie operations for setting the browser ID cookie in the request and response.
|
|
7
|
-
*
|
|
8
|
-
* @param request - The HTTP Request object containing request headers and data.
|
|
9
|
-
* @param response - The HTTP Response object.
|
|
10
|
-
* @param options - The settings object containing configuration options.
|
|
11
|
-
* @param timeout - The timeout for the call to proxy to get browserId.
|
|
12
|
-
* @returns A Promise that resolves once the browser ID cookie is handled.
|
|
13
|
-
*
|
|
14
|
-
* @throws [IE-0003] - This exception is thrown in the case getBrowserIdFromEP wasn't able to retrieve a browser id.
|
|
15
|
-
*/
|
|
16
|
-
export async function handleHttpCookie(request, response, options, timeout) {
|
|
17
|
-
const { browserId, guestId } = options.cookieSettings.cookieNames;
|
|
18
|
-
const browserIdCookie = getCookieServerSide(request.headers.cookie, browserId);
|
|
19
|
-
let browserIdCookieValue;
|
|
20
|
-
const guestIdCookie = getCookieServerSide(request.headers.cookie, guestId);
|
|
21
|
-
let guestIdCookieValue;
|
|
22
|
-
const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
|
|
23
|
-
if (!browserIdCookie) {
|
|
24
|
-
const cookieValues = await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout);
|
|
25
|
-
browserIdCookieValue = cookieValues.browserId;
|
|
26
|
-
guestIdCookieValue = cookieValues.guestId;
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
browserIdCookieValue = browserIdCookie.value;
|
|
30
|
-
if (!guestIdCookie)
|
|
31
|
-
guestIdCookieValue = await getGuestId(browserIdCookie.value, options.sitecoreEdgeContextId, options.sitecoreEdgeUrl);
|
|
32
|
-
else
|
|
33
|
-
guestIdCookieValue = guestIdCookie.value;
|
|
34
|
-
}
|
|
35
|
-
const browserIdCookieString = createCookieString(browserId, browserIdCookieValue, defaultCookieAttributes);
|
|
36
|
-
const guestIdCookieString = createCookieString(guestId, guestIdCookieValue, defaultCookieAttributes);
|
|
37
|
-
if (!browserIdCookie)
|
|
38
|
-
request.headers.cookie = request.headers.cookie
|
|
39
|
-
? request.headers.cookie + '; ' + browserIdCookieString
|
|
40
|
-
: browserIdCookieString;
|
|
41
|
-
if (!guestIdCookie)
|
|
42
|
-
request.headers.cookie = request.headers.cookie
|
|
43
|
-
? request.headers.cookie + '; ' + guestIdCookieString
|
|
44
|
-
: guestIdCookieString;
|
|
45
|
-
response.setHeader('Set-Cookie', [browserIdCookieString, guestIdCookieString]);
|
|
46
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { MiddlewareNextResponse, MiddlewareRequest } from '@sitecore-cloudsdk/utils';
|
|
2
|
-
import type { Settings } 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 'fetchBrowserIdFromEdgeProxy' function
|
|
6
|
-
* and stores it in the request's cookies with the specified 'defaultCookieAttributes'.
|
|
7
|
-
*
|
|
8
|
-
* @param request - The Middleware Request object.
|
|
9
|
-
* @param response - The Middleware Response object.
|
|
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 when fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
|
|
14
|
-
*
|
|
15
|
-
*/
|
|
16
|
-
export declare function handleNextJsMiddlewareCookie(request: MiddlewareRequest, response: MiddlewareNextResponse, options: Settings, timeout?: number): Promise<void>;
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { fetchBrowserIdFromEdgeProxy } from '../browser-id/fetch-browser-id-from-edge-proxy';
|
|
2
|
-
import { getGuestId } from '../init/get-guest-id';
|
|
3
|
-
import { getCookieValueFromMiddlewareRequest } from './get-cookie-value-from-middleware-request';
|
|
4
|
-
import { getDefaultCookieAttributes } from './get-default-cookie-attributes';
|
|
5
|
-
/**
|
|
6
|
-
* Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'.
|
|
7
|
-
* If 'cookieValue' is not present in the request, it fetches it using the 'fetchBrowserIdFromEdgeProxy' function
|
|
8
|
-
* and stores it in the request's cookies with the specified 'defaultCookieAttributes'.
|
|
9
|
-
*
|
|
10
|
-
* @param request - The Middleware Request object.
|
|
11
|
-
* @param response - The Middleware Response object.
|
|
12
|
-
* @param options - The settings object containing configuration options.
|
|
13
|
-
* @param defaultCookieAttributes - The default attributes for the cookie.
|
|
14
|
-
*
|
|
15
|
-
* @throws [IE-0003] - This exception is thrown when fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId.
|
|
16
|
-
*
|
|
17
|
-
*/
|
|
18
|
-
export async function handleNextJsMiddlewareCookie(request, response, options, timeout) {
|
|
19
|
-
const { browserId, guestId } = options.cookieSettings.cookieNames;
|
|
20
|
-
let browserIdCookieValue = getCookieValueFromMiddlewareRequest(request, browserId);
|
|
21
|
-
let guestIdCookieValue = getCookieValueFromMiddlewareRequest(request, guestId);
|
|
22
|
-
if (!browserIdCookieValue) {
|
|
23
|
-
const cookieValues = await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout);
|
|
24
|
-
browserIdCookieValue = cookieValues.browserId;
|
|
25
|
-
guestIdCookieValue = cookieValues.guestId;
|
|
26
|
-
}
|
|
27
|
-
else if (!guestIdCookieValue)
|
|
28
|
-
guestIdCookieValue = await getGuestId(browserIdCookieValue, options.sitecoreEdgeContextId, options.sitecoreEdgeUrl);
|
|
29
|
-
const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain);
|
|
30
|
-
request.cookies.set(browserId, browserIdCookieValue, defaultCookieAttributes);
|
|
31
|
-
request.cookies.set(guestId, guestIdCookieValue, defaultCookieAttributes);
|
|
32
|
-
response.cookies.set(browserId, browserIdCookieValue, defaultCookieAttributes);
|
|
33
|
-
response.cookies.set(guestId, guestIdCookieValue, defaultCookieAttributes);
|
|
34
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { type HttpResponse, type MiddlewareNextResponse, type Request } 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 timeout - The timeout for the call to proxy to get browserId.
|
|
8
|
-
* @returns A Promise that resolves once the cookie handling is complete.
|
|
9
|
-
*/
|
|
10
|
-
export declare function handleServerCookie<T extends Request, X extends MiddlewareNextResponse | HttpResponse>(request: T, response: X, timeout?: number): Promise<void>;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
|
|
2
|
-
import { isHttpRequest, isHttpResponse, isNextJsMiddlewareRequest, isNextJsMiddlewareResponse } from '@sitecore-cloudsdk/utils';
|
|
3
|
-
import { getSettingsServer } from '../init/init-core-server';
|
|
4
|
-
import { handleHttpCookie } from './handle-http-cookie';
|
|
5
|
-
import { handleNextJsMiddlewareCookie } from './handle-next-js-middleware-cookie';
|
|
6
|
-
/**
|
|
7
|
-
* Handles server-side cookie operations based on the provided 'request' and 'response' objects.
|
|
8
|
-
*
|
|
9
|
-
* @param request - The request object, either a Middleware Request or an HTTP Request.
|
|
10
|
-
* @param response - The response object, either a Middleware Next Response or an HTTP Response.
|
|
11
|
-
* @param timeout - The timeout for the call to proxy to get browserId.
|
|
12
|
-
* @returns A Promise that resolves once the cookie handling is complete.
|
|
13
|
-
*/
|
|
14
|
-
export async function handleServerCookie(request, response, timeout) {
|
|
15
|
-
const settings = getSettingsServer();
|
|
16
|
-
if (isNextJsMiddlewareRequest(request) && isNextJsMiddlewareResponse(response))
|
|
17
|
-
await handleNextJsMiddlewareCookie(request, response, settings, timeout);
|
|
18
|
-
else if (isHttpRequest(request) && isHttpResponse(response))
|
|
19
|
-
await handleHttpCookie(request, response, settings, timeout);
|
|
20
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A function that gets the guest ref from EP.
|
|
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, sitecoreEdgeUrl: string): Promise<string>;
|
|
9
|
-
interface GetGuestRefCommon {
|
|
10
|
-
status: string;
|
|
11
|
-
version: string;
|
|
12
|
-
clientKey: string;
|
|
13
|
-
}
|
|
14
|
-
export interface GetGuestRefResponse extends GetGuestRefCommon {
|
|
15
|
-
ref: string;
|
|
16
|
-
customer: {
|
|
17
|
-
ref: string;
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
export interface GetGuestRefResponseError extends GetGuestRefCommon {
|
|
21
|
-
error_msg: string;
|
|
22
|
-
moreInfo: string;
|
|
23
|
-
}
|
|
24
|
-
export {};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
|
|
2
|
-
import { API_VERSION, ErrorMessages, LIBRARY_VERSION } from '../consts';
|
|
3
|
-
/**
|
|
4
|
-
* A function that gets the guest ref from EP.
|
|
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, sitecoreEdgeUrl) {
|
|
11
|
-
// eslint-disable-next-line max-len
|
|
12
|
-
const url = `${sitecoreEdgeUrl}/v1/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
|
-
if (!data.customer.ref)
|
|
21
|
-
throw new Error(ErrorMessages.IE_0011);
|
|
22
|
-
return data.customer.ref;
|
|
23
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { Request, Response } from '@sitecore-cloudsdk/utils';
|
|
2
|
-
import type { ServerSettings, Settings } from '../settings/interfaces';
|
|
3
|
-
export declare function setCoreSettings(settings: Settings): void;
|
|
4
|
-
/**
|
|
5
|
-
* Initializes the core settings for browser-based applications.
|
|
6
|
-
*
|
|
7
|
-
* This function initializes core settings for the application,
|
|
8
|
-
* including creating settings and handling cookies if enabled.
|
|
9
|
-
*
|
|
10
|
-
* @param settingsInput - The settings input to configure the core settings.
|
|
11
|
-
* @returns A Promise that resolves when initialization is complete.
|
|
12
|
-
* @deprecated Cloud SDK v0.4 introduces a new initialization logic. If you are upgrading from v0.3, we recommend that you upgrade your initialization code.The v0.3 initialization logic is now deprecated and will be removed in a future Cloud SDK release.
|
|
13
|
-
*/
|
|
14
|
-
export declare function getSettingsServer(): Settings;
|
|
15
|
-
/**
|
|
16
|
-
* Initializes the core settings for browser-based applications.
|
|
17
|
-
*
|
|
18
|
-
* This function initializes core settings for the application,
|
|
19
|
-
* including creating settings and handling cookies if enabled.
|
|
20
|
-
*
|
|
21
|
-
* @param settingsInput - The settings input to configure the core settings.
|
|
22
|
-
* @param request - A request object of type HttpRequest or MiddlewareRequest
|
|
23
|
-
* @param response - A response object of type HttpResponse or MiddlewareNextResponse
|
|
24
|
-
* @returns A Promise that resolves when initialization is complete.
|
|
25
|
-
*/
|
|
26
|
-
export declare function initCoreServer(settingsInput: ServerSettings, request: Request, response: Response): Promise<void>;
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S.
|
|
2
|
-
import { CORE_NAMESPACE } from '../debug/namespaces';
|
|
3
|
-
import { ErrorMessages } from '../consts';
|
|
4
|
-
import { createSettings } from '../settings/create-settings';
|
|
5
|
-
import { debug } from '../debug/debug';
|
|
6
|
-
import { handleServerCookie } from '../cookie/handle-server-cookie';
|
|
7
|
-
/**
|
|
8
|
-
* Internal settings object to be used by all functions in module caching.
|
|
9
|
-
* It starts with a null value and is set to the proper object by the function. *
|
|
10
|
-
* Can be retrieved only through the function.
|
|
11
|
-
*/
|
|
12
|
-
let coreSettings = null;
|
|
13
|
-
export function setCoreSettings(settings) {
|
|
14
|
-
coreSettings = settings;
|
|
15
|
-
}
|
|
16
|
-
/* eslint-disable max-len */
|
|
17
|
-
/**
|
|
18
|
-
* Initializes the core settings for browser-based applications.
|
|
19
|
-
*
|
|
20
|
-
* This function initializes core settings for the application,
|
|
21
|
-
* including creating settings and handling cookies if enabled.
|
|
22
|
-
*
|
|
23
|
-
* @param settingsInput - The settings input to configure the core settings.
|
|
24
|
-
* @returns A Promise that resolves when initialization is complete.
|
|
25
|
-
* @deprecated Cloud SDK v0.4 introduces a new initialization logic. If you are upgrading from v0.3, we recommend that you upgrade your initialization code.The v0.3 initialization logic is now deprecated and will be removed in a future Cloud SDK release.
|
|
26
|
-
*/
|
|
27
|
-
/* eslint-enable max-len */
|
|
28
|
-
export function getSettingsServer() {
|
|
29
|
-
if (!coreSettings)
|
|
30
|
-
throw Error(ErrorMessages.IE_0008);
|
|
31
|
-
return coreSettings;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Initializes the core settings for browser-based applications.
|
|
35
|
-
*
|
|
36
|
-
* This function initializes core settings for the application,
|
|
37
|
-
* including creating settings and handling cookies if enabled.
|
|
38
|
-
*
|
|
39
|
-
* @param settingsInput - The settings input to configure the core settings.
|
|
40
|
-
* @param request - A request object of type HttpRequest or MiddlewareRequest
|
|
41
|
-
* @param response - A response object of type HttpResponse or MiddlewareNextResponse
|
|
42
|
-
* @returns A Promise that resolves when initialization is complete.
|
|
43
|
-
*/
|
|
44
|
-
export async function initCoreServer(settingsInput, request, response) {
|
|
45
|
-
debug(CORE_NAMESPACE)('initializing %o', 'initCoreServer initialized');
|
|
46
|
-
if (!coreSettings)
|
|
47
|
-
coreSettings = createSettings(settingsInput);
|
|
48
|
-
if (settingsInput.enableServerCookie)
|
|
49
|
-
await handleServerCookie(request, response, settingsInput.timeout);
|
|
50
|
-
}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { BrowserSettings, Settings } from '../settings/interfaces';
|
|
2
|
-
/**
|
|
3
|
-
* Retrieves the core settings object.
|
|
4
|
-
*
|
|
5
|
-
* This function ensures that the core settings have been initialized and contain essential properties
|
|
6
|
-
* like `clientKey`, `cookieSettings`, and `targetURL`.
|
|
7
|
-
*
|
|
8
|
-
* @returns The core settings object.
|
|
9
|
-
* @throws Error if the core settings haven't been initialized with the required properties.
|
|
10
|
-
*/
|
|
11
|
-
export declare function getSettings(): Settings;
|
|
12
|
-
/**
|
|
13
|
-
* Initializes the core settings for browser-based applications.
|
|
14
|
-
*
|
|
15
|
-
* This function initializes core settings for the application,
|
|
16
|
-
* 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
|
-
* @deprecated Cloud SDK v0.4 introduces a new initialization logic. If you are upgrading from v0.3, we recommend that you upgrade your initialization code.The v0.3 initialization logic is now deprecated and will be removed in a future Cloud SDK release.
|
|
21
|
-
*/
|
|
22
|
-
export declare function initCore(settingsInput: BrowserSettings): Promise<void>;
|
|
23
|
-
/**
|
|
24
|
-
* Helper functions for tests
|
|
25
|
-
*/
|
|
26
|
-
export declare function setCoreSettings(settings: Settings): void;
|
|
27
|
-
export declare function setCookiePromise(promise: Promise<void>): void;
|
|
28
|
-
interface Engage {
|
|
29
|
-
[key: string]: any;
|
|
30
|
-
getBrowserId?: () => string;
|
|
31
|
-
versions?: {
|
|
32
|
-
personalize?: string | undefined;
|
|
33
|
-
events?: string | undefined;
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
declare global {
|
|
37
|
-
interface Window {
|
|
38
|
-
Engage: Engage;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
export {};
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { CORE_NAMESPACE } from '../debug/namespaces';
|
|
2
|
-
import { ErrorMessages } from '../consts';
|
|
3
|
-
import { createCookies } from '../cookie/create-cookies';
|
|
4
|
-
import { createSettings } from '../settings/create-settings';
|
|
5
|
-
import { debug } from '../debug/debug';
|
|
6
|
-
/**
|
|
7
|
-
* Internal settings object to be used by all functions in module caching.
|
|
8
|
-
* It starts with a null value and is set to the proper object by the function. *
|
|
9
|
-
* Can be retrieved only through the function.
|
|
10
|
-
*/
|
|
11
|
-
let coreSettings = null;
|
|
12
|
-
/**
|
|
13
|
-
* Retrieves the core settings object.
|
|
14
|
-
*
|
|
15
|
-
* This function ensures that the core settings have been initialized and contain essential properties
|
|
16
|
-
* like `clientKey`, `cookieSettings`, and `targetURL`.
|
|
17
|
-
*
|
|
18
|
-
* @returns The core settings object.
|
|
19
|
-
* @throws Error if the core settings haven't been initialized with the required properties.
|
|
20
|
-
*/
|
|
21
|
-
export function getSettings() {
|
|
22
|
-
if (!coreSettings)
|
|
23
|
-
throw Error(ErrorMessages.IE_0008);
|
|
24
|
-
return coreSettings;
|
|
25
|
-
}
|
|
26
|
-
let createCookiesPromise = null;
|
|
27
|
-
/* eslint-disable max-len */
|
|
28
|
-
/**
|
|
29
|
-
* Initializes the core settings for browser-based applications.
|
|
30
|
-
*
|
|
31
|
-
* This function initializes core settings for the application,
|
|
32
|
-
* including creating settings and handling cookies if enabled.
|
|
33
|
-
*
|
|
34
|
-
* @param settingsInput - The settings input to configure the core settings.
|
|
35
|
-
* @returns A Promise that resolves when initialization is complete.
|
|
36
|
-
* @deprecated Cloud SDK v0.4 introduces a new initialization logic. If you are upgrading from v0.3, we recommend that you upgrade your initialization code.The v0.3 initialization logic is now deprecated and will be removed in a future Cloud SDK release.
|
|
37
|
-
*/
|
|
38
|
-
/* eslint-enable max-len */
|
|
39
|
-
export async function initCore(settingsInput) {
|
|
40
|
-
debug(CORE_NAMESPACE)('coreClient library initialized');
|
|
41
|
-
if (coreSettings === null)
|
|
42
|
-
coreSettings = createSettings(settingsInput);
|
|
43
|
-
if (settingsInput.enableBrowserCookie && createCookiesPromise === null)
|
|
44
|
-
createCookiesPromise = createCookies(coreSettings);
|
|
45
|
-
await createCookiesPromise;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Helper functions for tests
|
|
49
|
-
*/
|
|
50
|
-
export function setCoreSettings(settings) {
|
|
51
|
-
coreSettings = settings;
|
|
52
|
-
}
|
|
53
|
-
export function setCookiePromise(promise) {
|
|
54
|
-
createCookiesPromise = promise;
|
|
55
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Settings, SettingsParams } 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 Settings with the settings added by the developer
|
|
6
|
-
*/
|
|
7
|
-
export declare function createSettings(settingsInput: SettingsParams): Settings;
|