@sitecore-cloudsdk/core
Advanced tools
Comparing version 0.1.0-rc.4 to 0.1.0-rc.5
{ | ||
"name": "@sitecore-cloudsdk/core", | ||
"version": "0.1.0-rc.4", | ||
"version": "0.1.0-rc.5", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/src/index.js", |
@@ -8,3 +8,3 @@ export { createCookie } from './lib/cookie/create-cookie'; | ||
export { getBrowserId } from './lib/init/get-browser-id'; | ||
export { getProxySettings } from './lib/init/get-proxy-settings'; | ||
export { fetchBrowserIdFromEdgeProxy } from './lib/init/fetch-browser-id-from-edge-proxy'; | ||
export { getGuestId } from './lib/init/get-guest-id'; | ||
@@ -17,4 +17,4 @@ export { createSettings } from './lib/settings/create-settings'; | ||
export { getBrowserIdFromRequest } from './lib/cookie/get-browser-id-from-request'; | ||
export { API_VERSION, BID_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, } from './lib/consts'; | ||
export { API_VERSION, COOKIE_NAME_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, } from './lib/consts'; | ||
export type { ICdpResponse, IInfer } from './lib/interfaces'; | ||
export type { ICookieSettings, ISettings, ISettingsParamsBrowser, ISettingsParamsServer, IWebExperiencesSettings, IWebPersonalizationConfig, } from './lib/settings/interfaces'; |
"use strict"; | ||
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S. | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SITECORE_EDGE_URL = exports.LIBRARY_VERSION = exports.DEFAULT_COOKIE_EXPIRY_DAYS = exports.DAILY_SECONDS = exports.BID_PREFIX = exports.API_VERSION = exports.getBrowserIdFromRequest = exports.pageName = exports.language = exports.validateSettings = exports.initCoreServer = exports.getSettingsServer = exports.initCore = exports.getSettings = exports.createSettings = exports.getGuestId = exports.getProxySettings = exports.getBrowserId = exports.handleServerCookie = exports.handleNextJsMiddlewareCookie = exports.handleHttpCookie = exports.getDefaultCookieAttributes = exports.getBrowserIdFromMiddlewareRequest = exports.createCookie = void 0; | ||
exports.SITECORE_EDGE_URL = exports.LIBRARY_VERSION = exports.DEFAULT_COOKIE_EXPIRY_DAYS = exports.DAILY_SECONDS = exports.COOKIE_NAME_PREFIX = exports.API_VERSION = exports.getBrowserIdFromRequest = exports.pageName = exports.language = exports.validateSettings = exports.initCoreServer = exports.getSettingsServer = exports.initCore = exports.getSettings = exports.createSettings = exports.getGuestId = exports.fetchBrowserIdFromEdgeProxy = exports.getBrowserId = exports.handleServerCookie = exports.handleNextJsMiddlewareCookie = exports.handleHttpCookie = exports.getDefaultCookieAttributes = exports.getBrowserIdFromMiddlewareRequest = exports.createCookie = void 0; | ||
var create_cookie_1 = require("./lib/cookie/create-cookie"); | ||
@@ -19,4 +19,4 @@ Object.defineProperty(exports, "createCookie", { enumerable: true, get: function () { return create_cookie_1.createCookie; } }); | ||
Object.defineProperty(exports, "getBrowserId", { enumerable: true, get: function () { return get_browser_id_1.getBrowserId; } }); | ||
var get_proxy_settings_1 = require("./lib/init/get-proxy-settings"); | ||
Object.defineProperty(exports, "getProxySettings", { enumerable: true, get: function () { return get_proxy_settings_1.getProxySettings; } }); | ||
var fetch_browser_id_from_edge_proxy_1 = require("./lib/init/fetch-browser-id-from-edge-proxy"); | ||
Object.defineProperty(exports, "fetchBrowserIdFromEdgeProxy", { enumerable: true, get: function () { return fetch_browser_id_from_edge_proxy_1.fetchBrowserIdFromEdgeProxy; } }); | ||
var get_guest_id_1 = require("./lib/init/get-guest-id"); | ||
@@ -41,3 +41,3 @@ Object.defineProperty(exports, "getGuestId", { enumerable: true, get: function () { return get_guest_id_1.getGuestId; } }); | ||
Object.defineProperty(exports, "API_VERSION", { enumerable: true, get: function () { return consts_1.API_VERSION; } }); | ||
Object.defineProperty(exports, "BID_PREFIX", { enumerable: true, get: function () { return consts_1.BID_PREFIX; } }); | ||
Object.defineProperty(exports, "COOKIE_NAME_PREFIX", { enumerable: true, get: function () { return consts_1.COOKIE_NAME_PREFIX; } }); | ||
Object.defineProperty(exports, "DAILY_SECONDS", { enumerable: true, get: function () { return consts_1.DAILY_SECONDS; } }); | ||
@@ -44,0 +44,0 @@ Object.defineProperty(exports, "DEFAULT_COOKIE_EXPIRY_DAYS", { enumerable: true, get: function () { return consts_1.DEFAULT_COOKIE_EXPIRY_DAYS; } }); |
export declare const LIBRARY_VERSION: string; | ||
export declare const BID_PREFIX = "bid_"; | ||
export declare const COOKIE_NAME_PREFIX = "sc_"; | ||
export declare const DEFAULT_COOKIE_EXPIRY_DAYS = 730; | ||
@@ -4,0 +4,0 @@ export declare const DAILY_SECONDS = 86400; |
@@ -7,7 +7,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SITECORE_EDGE_URL = exports.API_VERSION = exports.DAILY_SECONDS = exports.DEFAULT_COOKIE_EXPIRY_DAYS = exports.BID_PREFIX = exports.LIBRARY_VERSION = void 0; | ||
exports.SITECORE_EDGE_URL = exports.API_VERSION = exports.DAILY_SECONDS = exports.DEFAULT_COOKIE_EXPIRY_DAYS = exports.COOKIE_NAME_PREFIX = exports.LIBRARY_VERSION = void 0; | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const package_json_1 = __importDefault(require("../../package.json")); | ||
exports.LIBRARY_VERSION = package_json_1.default.version; | ||
exports.BID_PREFIX = 'bid_'; | ||
exports.COOKIE_NAME_PREFIX = 'sc_'; | ||
exports.DEFAULT_COOKIE_EXPIRY_DAYS = 730; | ||
@@ -14,0 +14,0 @@ exports.DAILY_SECONDS = 86400; |
import { ISettings } from '../settings/interfaces'; | ||
/** | ||
* Creates and adds the cookie to the document | ||
* @param sitecoreEdgeContextId - The sitecoreEdgeContextId from global settings | ||
* @param settings - The ICookieSettings settings object | ||
@@ -6,0 +5,0 @@ * @returns - browserId or undefined on error |
@@ -7,7 +7,5 @@ "use strict"; | ||
const get_default_cookie_attributes_1 = require("./get-default-cookie-attributes"); | ||
const get_proxy_settings_1 = require("../init/get-proxy-settings"); | ||
const consts_1 = require("../consts"); | ||
const fetch_browser_id_from_edge_proxy_1 = require("../init/fetch-browser-id-from-edge-proxy"); | ||
/** | ||
* Creates and adds the cookie to the document | ||
* @param sitecoreEdgeContextId - The sitecoreEdgeContextId from global settings | ||
* @param settings - The ICookieSettings settings object | ||
@@ -17,9 +15,10 @@ * @returns - browserId or undefined on error | ||
async function createCookie(settings) { | ||
const { browserId, clientKey } = await (0, get_proxy_settings_1.getProxySettings)(settings.sitecoreEdgeContextId, settings.sitecoreEdgeUrl); | ||
settings.cookieSettings.cookieName = `${consts_1.BID_PREFIX}${clientKey}`; | ||
if ((0, utils_1.cookieExists)(window.document.cookie, settings.cookieSettings.cookieName) || !browserId) | ||
if ((0, utils_1.cookieExists)(window.document.cookie, settings.cookieSettings.cookieName)) | ||
return; | ||
const attributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(settings.cookieSettings.cookieExpiryDays, settings.cookieSettings.cookieDomain); | ||
document.cookie = (0, utils_1.createCookieString)(settings.cookieSettings.cookieName, browserId, attributes); | ||
const { browserId } = await (0, fetch_browser_id_from_edge_proxy_1.fetchBrowserIdFromEdgeProxy)(settings.sitecoreEdgeUrl, settings.sitecoreEdgeContextId); | ||
if (browserId) { | ||
const attributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(settings.cookieSettings.cookieExpiryDays, settings.cookieSettings.cookieDomain); | ||
document.cookie = (0, utils_1.createCookieString)(settings.cookieSettings.cookieName, browserId, attributes); | ||
} | ||
} | ||
exports.createCookie = createCookie; |
@@ -15,12 +15,12 @@ "use strict"; | ||
function getBrowserIdFromRequest(request, cookieName) { | ||
let bid = undefined; | ||
let browserId = undefined; | ||
if ((0, utils_1.isNextJsMiddlewareRequest)(request)) { | ||
bid = (0, get_browser_id_from_middleware_request_1.getBrowserIdFromMiddlewareRequest)(request, cookieName); | ||
browserId = (0, get_browser_id_from_middleware_request_1.getBrowserIdFromMiddlewareRequest)(request, cookieName); | ||
} | ||
else if ((0, utils_1.isHttpRequest)(request)) { | ||
const cookieHeader = request.headers.cookie; | ||
bid = (0, utils_1.getCookieServerSide)(cookieHeader, cookieName)?.value; | ||
browserId = (0, utils_1.getCookieServerSide)(cookieHeader, cookieName)?.value; | ||
} | ||
return bid ?? ''; | ||
return browserId ?? ''; | ||
} | ||
exports.getBrowserIdFromRequest = getBrowserIdFromRequest; |
@@ -7,4 +7,5 @@ import type { IHttpRequest, IHttpResponse } from '@sitecore-cloudsdk/utils'; | ||
* @param request - The HTTP Request object containing request headers and data. | ||
* @param response - The HTTP Response object. | ||
* @param options - The settings object containing configuration options. | ||
* @param response - The Middleware Next Response or HTTP Response object. | ||
* @param timeout - The timeout for the call to proxy to get browserId. | ||
* @returns A Promise that resolves once the browser ID cookie is handled. | ||
@@ -14,2 +15,2 @@ * | ||
*/ | ||
export declare function handleHttpCookie(request: IHttpRequest, response: IHttpResponse, options: ISettings, cookieTempValue: string): void; | ||
export declare function handleHttpCookie(request: IHttpRequest, response: IHttpResponse, options: ISettings, timeout?: number): Promise<void>; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const utils_1 = require("@sitecore-cloudsdk/utils"); | ||
const fetch_browser_id_from_edge_proxy_1 = require("../init/fetch-browser-id-from-edge-proxy"); | ||
/** | ||
@@ -12,4 +13,5 @@ * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response. | ||
* @param request - The HTTP Request object containing request headers and data. | ||
* @param response - The HTTP Response object. | ||
* @param options - The settings object containing configuration options. | ||
* @param response - The Middleware Next Response or HTTP Response object. | ||
* @param timeout - The timeout for the call to proxy to get browserId. | ||
* @returns A Promise that resolves once the browser ID cookie is handled. | ||
@@ -19,3 +21,3 @@ * | ||
*/ | ||
function handleHttpCookie(request, response, options, cookieTempValue) { | ||
async function handleHttpCookie(request, response, options, timeout) { | ||
const { cookieName } = options.cookieSettings; | ||
@@ -30,4 +32,8 @@ const cookieValueFromRequest = request.headers.cookie; | ||
} | ||
if (!cookieValue) | ||
cookieValue = cookieTempValue; | ||
if (!cookieValue) { | ||
const { browserId } = await (0, fetch_browser_id_from_edge_proxy_1.fetchBrowserIdFromEdgeProxy)(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout); | ||
if (!browserId) | ||
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.'); | ||
cookieValue = browserId; | ||
} | ||
const defaultCookieAttributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain); | ||
@@ -34,0 +40,0 @@ const cookieString = (0, utils_1.createCookieString)(cookieName, cookieValue, defaultCookieAttributes); |
@@ -5,13 +5,13 @@ import { IMiddlewareNextResponse, IMiddlewareRequest } from '@sitecore-cloudsdk/utils'; | ||
* Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'. | ||
* If 'cookieValue' is not present in the request, it fetches it using the 'getProxySettings' function | ||
* If 'cookieValue' is not present in the request, it fetches it using the 'fetchBrowserIdFromEdgeProxy' function | ||
* and stores it in the request's cookies with the specified 'defaultCookieAttributes'. | ||
* | ||
* @param request - The Middleware Request object. | ||
* @param cookieName - The name of the cookie to set. | ||
* @param response - The Middleware Response object. | ||
* @param options - The settings object containing configuration options. | ||
* @param defaultCookieAttributes - The default attributes for the cookie. | ||
* | ||
* @throws [IE-0003] - This exception is thrown in the case getProxySettings wasn't able to retrieve a browser id and client key. | ||
* @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId. | ||
* | ||
*/ | ||
export declare function handleNextJsMiddlewareCookie(request: IMiddlewareRequest, response: IMiddlewareNextResponse, options: ISettings, cookieTempValue: string): void; | ||
export declare function handleNextJsMiddlewareCookie(request: IMiddlewareRequest, response: IMiddlewareNextResponse, options: ISettings, timeout?: number): Promise<void>; |
@@ -7,18 +7,25 @@ "use strict"; | ||
const get_default_cookie_attributes_1 = require("./get-default-cookie-attributes"); | ||
const fetch_browser_id_from_edge_proxy_1 = require("../init/fetch-browser-id-from-edge-proxy"); | ||
/** | ||
* Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'. | ||
* If 'cookieValue' is not present in the request, it fetches it using the 'getProxySettings' function | ||
* If 'cookieValue' is not present in the request, it fetches it using the 'fetchBrowserIdFromEdgeProxy' function | ||
* and stores it in the request's cookies with the specified 'defaultCookieAttributes'. | ||
* | ||
* @param request - The Middleware Request object. | ||
* @param cookieName - The name of the cookie to set. | ||
* @param response - The Middleware Response object. | ||
* @param options - The settings object containing configuration options. | ||
* @param defaultCookieAttributes - The default attributes for the cookie. | ||
* | ||
* @throws [IE-0003] - This exception is thrown in the case getProxySettings wasn't able to retrieve a browser id and client key. | ||
* @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId. | ||
* | ||
*/ | ||
function handleNextJsMiddlewareCookie(request, response, options, cookieTempValue) { | ||
async function handleNextJsMiddlewareCookie(request, response, options, timeout) { | ||
const { cookieName } = options.cookieSettings; | ||
const cookieValue = (0, get_browser_id_from_middleware_request_1.getBrowserIdFromMiddlewareRequest)(request, cookieName) ?? cookieTempValue; | ||
let cookieValue = (0, get_browser_id_from_middleware_request_1.getBrowserIdFromMiddlewareRequest)(request, cookieName); | ||
if (!cookieValue) { | ||
const { browserId } = await (0, fetch_browser_id_from_edge_proxy_1.fetchBrowserIdFromEdgeProxy)(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout); | ||
if (!browserId) | ||
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.'); | ||
cookieValue = browserId; | ||
} | ||
const defaultCookieAttributes = (0, get_default_cookie_attributes_1.getDefaultCookieAttributes)(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain); | ||
@@ -25,0 +32,0 @@ request.cookies.set(cookieName, cookieValue, defaultCookieAttributes); |
@@ -7,5 +7,5 @@ import { type TRequest, type IMiddlewareNextResponse, type IHttpResponse } from '@sitecore-cloudsdk/utils'; | ||
* @param response - The response object, either a Middleware Next Response or an HTTP Response. | ||
* @param options - The settings object containing configuration options. | ||
* @param timeout - The timeout for the call to proxy to get browserId. | ||
* @returns A Promise that resolves once the cookie handling is complete. | ||
*/ | ||
export declare function handleServerCookie<T extends TRequest, X extends IMiddlewareNextResponse | IHttpResponse>(request: T, response: X, timeout?: number): Promise<void>; |
@@ -9,4 +9,2 @@ "use strict"; | ||
const handle_http_cookie_1 = require("./handle-http-cookie"); | ||
const get_proxy_settings_1 = require("../init/get-proxy-settings"); | ||
const consts_1 = require("../consts"); | ||
/** | ||
@@ -17,3 +15,3 @@ * Handles server-side cookie operations based on the provided 'request' and 'response' objects. | ||
* @param response - The response object, either a Middleware Next Response or an HTTP Response. | ||
* @param options - The settings object containing configuration options. | ||
* @param timeout - The timeout for the call to proxy to get browserId. | ||
* @returns A Promise that resolves once the cookie handling is complete. | ||
@@ -23,13 +21,9 @@ */ | ||
const settings = (0, init_core_server_1.getSettingsServer)(); | ||
const { browserId, clientKey } = await (0, get_proxy_settings_1.getProxySettings)(settings.sitecoreEdgeContextId, settings.sitecoreEdgeUrl, timeout); | ||
if (!clientKey) | ||
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.'); | ||
settings.cookieSettings.cookieName = `${consts_1.BID_PREFIX}${clientKey}`; | ||
if ((0, utils_1.isNextJsMiddlewareRequest)(request) && (0, utils_1.isNextJsMiddlewareResponse)(response)) { | ||
(0, handle_next_js_middleware_cookie_1.handleNextJsMiddlewareCookie)(request, response, settings, browserId); | ||
await (0, handle_next_js_middleware_cookie_1.handleNextJsMiddlewareCookie)(request, response, settings, timeout); | ||
} | ||
else if ((0, utils_1.isHttpRequest)(request) && (0, utils_1.isHttpResponse)(response)) { | ||
(0, handle_http_cookie_1.handleHttpCookie)(request, response, settings, browserId); | ||
await (0, handle_http_cookie_1.handleHttpCookie)(request, response, settings, timeout); | ||
} | ||
} | ||
exports.handleServerCookie = handleServerCookie; |
@@ -12,3 +12,2 @@ /** | ||
browserId: string; | ||
clientKey: string; | ||
} | ||
@@ -15,0 +14,0 @@ /** |
@@ -19,3 +19,3 @@ "use strict"; | ||
cookieExpiryDays: cookieExpiryDays || consts_1.DEFAULT_COOKIE_EXPIRY_DAYS, | ||
cookieName: '', | ||
cookieName: `${consts_1.COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}`, | ||
cookiePath: cookiePath || '/', | ||
@@ -22,0 +22,0 @@ }, |
@@ -16,2 +16,3 @@ /** | ||
timeout?: number; | ||
sitecoreEdgeUrl?: string; | ||
} | ||
@@ -24,3 +25,2 @@ /** | ||
siteName: string; | ||
sitecoreEdgeUrl?: string; | ||
} | ||
@@ -27,0 +27,0 @@ interface ICookieSettingsInput { |
@@ -18,3 +18,3 @@ "use strict"; | ||
throw new Error(`[MV-0002] "siteName" is required.`); | ||
if (sitecoreEdgeUrl) { | ||
if (sitecoreEdgeUrl !== undefined) { | ||
try { | ||
@@ -21,0 +21,0 @@ new URL(sitecoreEdgeUrl); |
{ | ||
"name": "@sitecore-cloudsdk/core", | ||
"version": "0.1.0-rc.4", | ||
"version": "0.1.0-rc.5", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/src/index.js", |
@@ -8,3 +8,3 @@ export { createCookie } from './lib/cookie/create-cookie'; | ||
export { getBrowserId } from './lib/init/get-browser-id'; | ||
export { getProxySettings } from './lib/init/get-proxy-settings'; | ||
export { fetchBrowserIdFromEdgeProxy } from './lib/init/fetch-browser-id-from-edge-proxy'; | ||
export { getGuestId } from './lib/init/get-guest-id'; | ||
@@ -17,4 +17,4 @@ export { createSettings } from './lib/settings/create-settings'; | ||
export { getBrowserIdFromRequest } from './lib/cookie/get-browser-id-from-request'; | ||
export { API_VERSION, BID_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, } from './lib/consts'; | ||
export { API_VERSION, COOKIE_NAME_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, } from './lib/consts'; | ||
export type { ICdpResponse, IInfer } from './lib/interfaces'; | ||
export type { ICookieSettings, ISettings, ISettingsParamsBrowser, ISettingsParamsServer, IWebExperiencesSettings, IWebPersonalizationConfig, } from './lib/settings/interfaces'; |
@@ -9,3 +9,3 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S. | ||
export { getBrowserId } from './lib/init/get-browser-id'; | ||
export { getProxySettings } from './lib/init/get-proxy-settings'; | ||
export { fetchBrowserIdFromEdgeProxy } from './lib/init/fetch-browser-id-from-edge-proxy'; | ||
export { getGuestId } from './lib/init/get-guest-id'; | ||
@@ -18,2 +18,2 @@ export { createSettings } from './lib/settings/create-settings'; | ||
export { getBrowserIdFromRequest } from './lib/cookie/get-browser-id-from-request'; | ||
export { API_VERSION, BID_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, } from './lib/consts'; | ||
export { API_VERSION, COOKIE_NAME_PREFIX, DAILY_SECONDS, DEFAULT_COOKIE_EXPIRY_DAYS, LIBRARY_VERSION, SITECORE_EDGE_URL, } from './lib/consts'; |
export declare const LIBRARY_VERSION: string; | ||
export declare const BID_PREFIX = "bid_"; | ||
export declare const COOKIE_NAME_PREFIX = "sc_"; | ||
export declare const DEFAULT_COOKIE_EXPIRY_DAYS = 730; | ||
@@ -4,0 +4,0 @@ export declare const DAILY_SECONDS = 86400; |
@@ -5,3 +5,3 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S. | ||
export const LIBRARY_VERSION = packageJson.version; | ||
export const BID_PREFIX = 'bid_'; | ||
export const COOKIE_NAME_PREFIX = 'sc_'; | ||
export const DEFAULT_COOKIE_EXPIRY_DAYS = 730; | ||
@@ -8,0 +8,0 @@ export const DAILY_SECONDS = 86400; |
import { ISettings } from '../settings/interfaces'; | ||
/** | ||
* Creates and adds the cookie to the document | ||
* @param sitecoreEdgeContextId - The sitecoreEdgeContextId from global settings | ||
* @param settings - The ICookieSettings settings object | ||
@@ -6,0 +5,0 @@ * @returns - browserId or undefined on error |
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S. | ||
import { cookieExists, createCookieString } from '@sitecore-cloudsdk/utils'; | ||
import { getDefaultCookieAttributes } from './get-default-cookie-attributes'; | ||
import { getProxySettings } from '../init/get-proxy-settings'; | ||
import { BID_PREFIX } from '../consts'; | ||
import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-proxy'; | ||
/** | ||
* Creates and adds the cookie to the document | ||
* @param sitecoreEdgeContextId - The sitecoreEdgeContextId from global settings | ||
* @param settings - The ICookieSettings settings object | ||
@@ -13,8 +11,9 @@ * @returns - browserId or undefined on error | ||
export async function createCookie(settings) { | ||
const { browserId, clientKey } = await getProxySettings(settings.sitecoreEdgeContextId, settings.sitecoreEdgeUrl); | ||
settings.cookieSettings.cookieName = `${BID_PREFIX}${clientKey}`; | ||
if (cookieExists(window.document.cookie, settings.cookieSettings.cookieName) || !browserId) | ||
if (cookieExists(window.document.cookie, settings.cookieSettings.cookieName)) | ||
return; | ||
const attributes = getDefaultCookieAttributes(settings.cookieSettings.cookieExpiryDays, settings.cookieSettings.cookieDomain); | ||
document.cookie = createCookieString(settings.cookieSettings.cookieName, browserId, attributes); | ||
const { browserId } = await fetchBrowserIdFromEdgeProxy(settings.sitecoreEdgeUrl, settings.sitecoreEdgeContextId); | ||
if (browserId) { | ||
const attributes = getDefaultCookieAttributes(settings.cookieSettings.cookieExpiryDays, settings.cookieSettings.cookieDomain); | ||
document.cookie = createCookieString(settings.cookieSettings.cookieName, browserId, attributes); | ||
} | ||
} |
@@ -12,11 +12,11 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S. | ||
export function getBrowserIdFromRequest(request, cookieName) { | ||
let bid = undefined; | ||
let browserId = undefined; | ||
if (isNextJsMiddlewareRequest(request)) { | ||
bid = getBrowserIdFromMiddlewareRequest(request, cookieName); | ||
browserId = getBrowserIdFromMiddlewareRequest(request, cookieName); | ||
} | ||
else if (isHttpRequest(request)) { | ||
const cookieHeader = request.headers.cookie; | ||
bid = getCookieServerSide(cookieHeader, cookieName)?.value; | ||
browserId = getCookieServerSide(cookieHeader, cookieName)?.value; | ||
} | ||
return bid ?? ''; | ||
return browserId ?? ''; | ||
} |
@@ -7,4 +7,5 @@ import type { IHttpRequest, IHttpResponse } from '@sitecore-cloudsdk/utils'; | ||
* @param request - The HTTP Request object containing request headers and data. | ||
* @param response - The HTTP Response object. | ||
* @param options - The settings object containing configuration options. | ||
* @param response - The Middleware Next Response or HTTP Response object. | ||
* @param timeout - The timeout for the call to proxy to get browserId. | ||
* @returns A Promise that resolves once the browser ID cookie is handled. | ||
@@ -14,2 +15,2 @@ * | ||
*/ | ||
export declare function handleHttpCookie(request: IHttpRequest, response: IHttpResponse, options: ISettings, cookieTempValue: string): void; | ||
export declare function handleHttpCookie(request: IHttpRequest, response: IHttpResponse, options: ISettings, timeout?: number): Promise<void>; |
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S. | ||
import { getDefaultCookieAttributes } from './get-default-cookie-attributes'; | ||
import { createCookieString, getCookieServerSide } from '@sitecore-cloudsdk/utils'; | ||
import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-proxy'; | ||
/** | ||
@@ -8,4 +9,5 @@ * Handles HTTP Cookie operations for setting the browser ID cookie in the request and response. | ||
* @param request - The HTTP Request object containing request headers and data. | ||
* @param response - The HTTP Response object. | ||
* @param options - The settings object containing configuration options. | ||
* @param response - The Middleware Next Response or HTTP Response object. | ||
* @param timeout - The timeout for the call to proxy to get browserId. | ||
* @returns A Promise that resolves once the browser ID cookie is handled. | ||
@@ -15,3 +17,3 @@ * | ||
*/ | ||
export function handleHttpCookie(request, response, options, cookieTempValue) { | ||
export async function handleHttpCookie(request, response, options, timeout) { | ||
const { cookieName } = options.cookieSettings; | ||
@@ -26,4 +28,8 @@ const cookieValueFromRequest = request.headers.cookie; | ||
} | ||
if (!cookieValue) | ||
cookieValue = cookieTempValue; | ||
if (!cookieValue) { | ||
const { browserId } = await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout); | ||
if (!browserId) | ||
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.'); | ||
cookieValue = browserId; | ||
} | ||
const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain); | ||
@@ -30,0 +36,0 @@ const cookieString = createCookieString(cookieName, cookieValue, defaultCookieAttributes); |
@@ -5,13 +5,13 @@ import { IMiddlewareNextResponse, IMiddlewareRequest } from '@sitecore-cloudsdk/utils'; | ||
* Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'. | ||
* If 'cookieValue' is not present in the request, it fetches it using the 'getProxySettings' function | ||
* If 'cookieValue' is not present in the request, it fetches it using the 'fetchBrowserIdFromEdgeProxy' function | ||
* and stores it in the request's cookies with the specified 'defaultCookieAttributes'. | ||
* | ||
* @param request - The Middleware Request object. | ||
* @param cookieName - The name of the cookie to set. | ||
* @param response - The Middleware Response object. | ||
* @param options - The settings object containing configuration options. | ||
* @param defaultCookieAttributes - The default attributes for the cookie. | ||
* | ||
* @throws [IE-0003] - This exception is thrown in the case getProxySettings wasn't able to retrieve a browser id and client key. | ||
* @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId. | ||
* | ||
*/ | ||
export declare function handleNextJsMiddlewareCookie(request: IMiddlewareRequest, response: IMiddlewareNextResponse, options: ISettings, cookieTempValue: string): void; | ||
export declare function handleNextJsMiddlewareCookie(request: IMiddlewareRequest, response: IMiddlewareNextResponse, options: ISettings, timeout?: number): Promise<void>; |
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S. | ||
import { getBrowserIdFromMiddlewareRequest } from './get-browser-id-from-middleware-request'; | ||
import { getDefaultCookieAttributes } from './get-default-cookie-attributes'; | ||
import { fetchBrowserIdFromEdgeProxy } from '../init/fetch-browser-id-from-edge-proxy'; | ||
/** | ||
* Handles the Middleware Request and sets a cookie with the provided 'cookieName' and 'cookieValue'. | ||
* If 'cookieValue' is not present in the request, it fetches it using the 'getProxySettings' function | ||
* If 'cookieValue' is not present in the request, it fetches it using the 'fetchBrowserIdFromEdgeProxy' function | ||
* and stores it in the request's cookies with the specified 'defaultCookieAttributes'. | ||
* | ||
* @param request - The Middleware Request object. | ||
* @param cookieName - The name of the cookie to set. | ||
* @param response - The Middleware Response object. | ||
* @param options - The settings object containing configuration options. | ||
* @param defaultCookieAttributes - The default attributes for the cookie. | ||
* | ||
* @throws [IE-0003] - This exception is thrown in the case getProxySettings wasn't able to retrieve a browser id and client key. | ||
* @throws [IE-0003] - This exception is thrown in the case fetchBrowserIdFromEdgeProxy wasn't able to retrieve a browserId. | ||
* | ||
*/ | ||
export function handleNextJsMiddlewareCookie(request, response, options, cookieTempValue) { | ||
export async function handleNextJsMiddlewareCookie(request, response, options, timeout) { | ||
const { cookieName } = options.cookieSettings; | ||
const cookieValue = getBrowserIdFromMiddlewareRequest(request, cookieName) ?? cookieTempValue; | ||
let cookieValue = getBrowserIdFromMiddlewareRequest(request, cookieName); | ||
if (!cookieValue) { | ||
const { browserId } = await fetchBrowserIdFromEdgeProxy(options.sitecoreEdgeUrl, options.sitecoreEdgeContextId, timeout); | ||
if (!browserId) | ||
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.'); | ||
cookieValue = browserId; | ||
} | ||
const defaultCookieAttributes = getDefaultCookieAttributes(options.cookieSettings.cookieExpiryDays, options.cookieSettings.cookieDomain); | ||
@@ -21,0 +28,0 @@ request.cookies.set(cookieName, cookieValue, defaultCookieAttributes); |
@@ -7,5 +7,5 @@ import { type TRequest, type IMiddlewareNextResponse, type IHttpResponse } from '@sitecore-cloudsdk/utils'; | ||
* @param response - The response object, either a Middleware Next Response or an HTTP Response. | ||
* @param options - The settings object containing configuration options. | ||
* @param timeout - The timeout for the call to proxy to get browserId. | ||
* @returns A Promise that resolves once the cookie handling is complete. | ||
*/ | ||
export declare function handleServerCookie<T extends TRequest, X extends IMiddlewareNextResponse | IHttpResponse>(request: T, response: X, timeout?: number): Promise<void>; |
@@ -6,4 +6,2 @@ // © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S. | ||
import { handleHttpCookie } from './handle-http-cookie'; | ||
import { getProxySettings } from '../init/get-proxy-settings'; | ||
import { BID_PREFIX } from '../consts'; | ||
/** | ||
@@ -14,3 +12,3 @@ * Handles server-side cookie operations based on the provided 'request' and 'response' objects. | ||
* @param response - The response object, either a Middleware Next Response or an HTTP Response. | ||
* @param options - The settings object containing configuration options. | ||
* @param timeout - The timeout for the call to proxy to get browserId. | ||
* @returns A Promise that resolves once the cookie handling is complete. | ||
@@ -20,12 +18,8 @@ */ | ||
const settings = getSettingsServer(); | ||
const { browserId, clientKey } = await getProxySettings(settings.sitecoreEdgeContextId, settings.sitecoreEdgeUrl, timeout); | ||
if (!clientKey) | ||
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.'); | ||
settings.cookieSettings.cookieName = `${BID_PREFIX}${clientKey}`; | ||
if (isNextJsMiddlewareRequest(request) && isNextJsMiddlewareResponse(response)) { | ||
handleNextJsMiddlewareCookie(request, response, settings, browserId); | ||
await handleNextJsMiddlewareCookie(request, response, settings, timeout); | ||
} | ||
else if (isHttpRequest(request) && isHttpResponse(response)) { | ||
handleHttpCookie(request, response, settings, browserId); | ||
await handleHttpCookie(request, response, settings, timeout); | ||
} | ||
} |
@@ -12,3 +12,2 @@ /** | ||
browserId: string; | ||
clientKey: string; | ||
} | ||
@@ -15,0 +14,0 @@ /** |
// © Sitecore Corporation A/S. All rights reserved. Sitecore® is a registered trademark of Sitecore Corporation A/S. | ||
import { DEFAULT_COOKIE_EXPIRY_DAYS, SITECORE_EDGE_URL } from '../consts'; | ||
import { COOKIE_NAME_PREFIX, DEFAULT_COOKIE_EXPIRY_DAYS, SITECORE_EDGE_URL } from '../consts'; | ||
import { validateSettings } from './validate-settings'; | ||
@@ -16,3 +16,3 @@ /** | ||
cookieExpiryDays: cookieExpiryDays || DEFAULT_COOKIE_EXPIRY_DAYS, | ||
cookieName: '', | ||
cookieName: `${COOKIE_NAME_PREFIX}${sitecoreEdgeContextId}`, | ||
cookiePath: cookiePath || '/', | ||
@@ -19,0 +19,0 @@ }, |
@@ -16,2 +16,3 @@ /** | ||
timeout?: number; | ||
sitecoreEdgeUrl?: string; | ||
} | ||
@@ -24,3 +25,2 @@ /** | ||
siteName: string; | ||
sitecoreEdgeUrl?: string; | ||
} | ||
@@ -27,0 +27,0 @@ interface ICookieSettingsInput { |
@@ -15,3 +15,3 @@ /** | ||
throw new Error(`[MV-0002] "siteName" is required.`); | ||
if (sitecoreEdgeUrl) { | ||
if (sitecoreEdgeUrl !== undefined) { | ||
try { | ||
@@ -18,0 +18,0 @@ new URL(sitecoreEdgeUrl); |
{ | ||
"name": "@sitecore-cloudsdk/core", | ||
"version": "0.1.0-rc.4", | ||
"version": "0.1.0-rc.5", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/src/index.js", |
# core | ||
This library was generated with [Nx](https://nx.dev). | ||
© Sitecore Corporation A/S. All rights reserved. Sitecore© is a registered trademark of Sitecore Corporation A/S. | ||
## Building | ||
Run `nx build core` to build the library. | ||
## Running unit tests | ||
Run `nx test core` to execute the unit tests via [Jest](https://jestjs.io). | ||
This is an internal package used by other Sitecore Cloud SDK packages. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
137951
1601
5