@commercetools/frontend-sdk
Advanced tools
Comparing version 1.8.1 to 1.8.2
## Version 1.8.2 (2023-08-16) | ||
* Deprecated Extension base class, replaced with Integration to correct terminolgy | ||
## Version 1.8.1 (2023-06-29) | ||
* Added support for 6 digit locales | ||
@@ -6,0 +10,0 @@ |
@@ -1,11 +0,18 @@ | ||
import { ServerOptions, TmpCookiesObj, CookieValueTypes } from './types.js'; | ||
import { CookieValueTypes, ServerOptions, TmpCookiesObj } from './types.js'; | ||
import { CookieManager } from '../interfaces/CookieManager.js'; | ||
import 'cookie'; | ||
import 'http'; | ||
declare const getCookies: (options?: ServerOptions) => TmpCookiesObj; | ||
declare const getCookie: (key: string, options?: ServerOptions) => CookieValueTypes; | ||
declare const setCookie: (key: string, data: any, options?: ServerOptions) => void; | ||
declare const deleteCookie: (key: string, options?: ServerOptions) => void; | ||
declare const hasCookie: (key: string, options?: ServerOptions) => boolean; | ||
declare class CookieHandler implements CookieManager { | ||
isClientSide(): boolean; | ||
stringify(value?: string): string; | ||
decode(str: string): string; | ||
processValue(value: string): CookieValueTypes; | ||
getCookies(options?: ServerOptions): TmpCookiesObj; | ||
hasCookie(key: string, options?: ServerOptions): boolean; | ||
setCookie(key: string, data: any, options?: ServerOptions): void; | ||
getCookie(key: string, options?: ServerOptions): CookieValueTypes; | ||
deleteCookie(key: string, options?: ServerOptions): void; | ||
} | ||
export { CookieValueTypes, deleteCookie, getCookie, getCookies, hasCookie, setCookie }; | ||
export { CookieHandler }; |
@@ -23,122 +23,122 @@ "use strict"; | ||
__export(cookieHandling_exports, { | ||
deleteCookie: () => deleteCookie, | ||
getCookie: () => getCookie, | ||
getCookies: () => getCookies, | ||
hasCookie: () => hasCookie, | ||
setCookie: () => setCookie | ||
CookieHandler: () => CookieHandler | ||
}); | ||
module.exports = __toCommonJS(cookieHandling_exports); | ||
var import_cookie = require("cookie"); | ||
var isClientSide = () => typeof window !== "undefined"; | ||
var processValue = (value) => { | ||
switch (value) { | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
case "undefined": | ||
return void 0; | ||
case "null": | ||
return null; | ||
default: | ||
var CookieHandler = class { | ||
isClientSide() { | ||
return typeof window !== "undefined"; | ||
} | ||
stringify(value = "") { | ||
try { | ||
const result = JSON.stringify(value); | ||
return /^[\{\[]/.test(result) ? result : value; | ||
} catch (e) { | ||
return value; | ||
} | ||
} | ||
}; | ||
var stringify = (value = "") => { | ||
try { | ||
const result = JSON.stringify(value); | ||
return /^[\{\[]/.test(result) ? result : value; | ||
} catch (e) { | ||
return value; | ||
decode(str) { | ||
return str ? str.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent) : str; | ||
} | ||
}; | ||
var decode = (str) => str ? str.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent) : str; | ||
var getCookies = (options) => { | ||
let req; | ||
if (options) { | ||
req = options.req; | ||
processValue(value) { | ||
switch (value) { | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
case "undefined": | ||
return void 0; | ||
case "null": | ||
return null; | ||
default: | ||
return value; | ||
} | ||
} | ||
if (!isClientSide()) { | ||
if (req?.cookies) { | ||
return req.cookies; | ||
getCookies(options) { | ||
let req; | ||
if (options) { | ||
req = options.req; | ||
} | ||
if (req?.headers?.cookie) { | ||
return (0, import_cookie.parse)(req.headers.cookie); | ||
if (!this.isClientSide()) { | ||
if (req?.cookies) { | ||
return req.cookies; | ||
} | ||
if (req?.headers?.cookie) { | ||
return (0, import_cookie.parse)(req.headers.cookie); | ||
} | ||
return {}; | ||
} | ||
return {}; | ||
const _cookies = {}; | ||
const documentCookies = document.cookie ? document.cookie.split("; ") : []; | ||
for (let i = 0, len = documentCookies.length; i < len; i++) { | ||
const cookieParts = documentCookies[i].split("="); | ||
const _cookie = cookieParts.slice(1).join("="); | ||
const name = cookieParts[0]; | ||
_cookies[name] = _cookie; | ||
} | ||
return _cookies; | ||
} | ||
const _cookies = {}; | ||
const documentCookies = document.cookie ? document.cookie.split("; ") : []; | ||
for (let i = 0, len = documentCookies.length; i < len; i++) { | ||
const cookieParts = documentCookies[i].split("="); | ||
const _cookie = cookieParts.slice(1).join("="); | ||
const name = cookieParts[0]; | ||
_cookies[name] = _cookie; | ||
hasCookie(key, options) { | ||
if (!key) { | ||
return false; | ||
} | ||
const cookie = this.getCookies(options); | ||
return cookie.hasOwnProperty(key); | ||
} | ||
return _cookies; | ||
}; | ||
var getCookie = (key, options) => { | ||
const _cookies = getCookies(options); | ||
const value = _cookies[key]; | ||
if (value === void 0) { | ||
return void 0; | ||
} | ||
return processValue(decode(value)); | ||
}; | ||
var setCookie = (key, data, options) => { | ||
let _cookieOptions; | ||
let _req; | ||
let _res; | ||
if (options) { | ||
const { req, res, ..._options } = options; | ||
_req = req; | ||
_res = res; | ||
_cookieOptions = _options; | ||
} | ||
const cookieStr = (0, import_cookie.serialize)(key, stringify(data), { | ||
path: "/", | ||
..._cookieOptions | ||
}); | ||
if (!isClientSide()) { | ||
if (_res && _req) { | ||
let currentCookies = _res.getHeader("Set-Cookie"); | ||
if (!Array.isArray(currentCookies)) { | ||
currentCookies = !currentCookies ? [] : [String(currentCookies)]; | ||
setCookie(key, data, options) { | ||
let _cookieOptions; | ||
let _req; | ||
let _res; | ||
if (options) { | ||
const { req, res, ..._options } = options; | ||
_req = req; | ||
_res = res; | ||
_cookieOptions = _options; | ||
} | ||
const cookieStr = (0, import_cookie.serialize)(key, this.stringify(data), { | ||
path: "/", | ||
..._cookieOptions | ||
}); | ||
if (!this.isClientSide()) { | ||
if (_res && _req) { | ||
let currentCookies = _res.getHeader("Set-Cookie"); | ||
if (!Array.isArray(currentCookies)) { | ||
currentCookies = !currentCookies ? [] : [String(currentCookies)]; | ||
} | ||
_res.setHeader("Set-Cookie", currentCookies.concat(cookieStr)); | ||
if (_req && _req.cookies) { | ||
const _cookies = _req.cookies; | ||
data === "" ? delete _cookies[key] : _cookies[key] = this.stringify(data); | ||
} | ||
if (_req && _req.headers && _req.headers.cookie) { | ||
const _cookies = (0, import_cookie.parse)(_req.headers.cookie); | ||
data === "" ? delete _cookies[key] : _cookies[key] = this.stringify(data); | ||
_req.headers.cookie = Object.entries(_cookies).reduce( | ||
(accum, item) => { | ||
return accum.concat(`${item[0]}=${item[1]};`); | ||
}, | ||
"" | ||
); | ||
} | ||
} | ||
_res.setHeader("Set-Cookie", currentCookies.concat(cookieStr)); | ||
if (_req && _req.cookies) { | ||
const _cookies = _req.cookies; | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
} | ||
if (_req && _req.headers && _req.headers.cookie) { | ||
const _cookies = (0, import_cookie.parse)(_req.headers.cookie); | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
_req.headers.cookie = Object.entries(_cookies).reduce( | ||
(accum, item) => { | ||
return accum.concat(`${item[0]}=${item[1]};`); | ||
}, | ||
"" | ||
); | ||
} | ||
} else { | ||
document.cookie = cookieStr; | ||
} | ||
} else { | ||
document.cookie = cookieStr; | ||
} | ||
}; | ||
var deleteCookie = (key, options) => setCookie(key, "", { ...options, maxAge: -1 }); | ||
var hasCookie = (key, options) => { | ||
if (!key) { | ||
return false; | ||
getCookie(key, options) { | ||
const _cookies = this.getCookies(options); | ||
const value = _cookies[key]; | ||
if (value === void 0) { | ||
return void 0; | ||
} | ||
return this.processValue(this.decode(value)); | ||
} | ||
const cookie = getCookies(options); | ||
return cookie.hasOwnProperty(key); | ||
deleteCookie(key, options) { | ||
this.setCookie(key, "", { ...options, maxAge: -1 }); | ||
} | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
deleteCookie, | ||
getCookie, | ||
getCookies, | ||
hasCookie, | ||
setCookie | ||
CookieHandler | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -28,101 +28,27 @@ "use strict"; | ||
// src/cookieHandling/index.ts | ||
var import_cookie = require("cookie"); | ||
var isClientSide = () => typeof window !== "undefined"; | ||
var processValue = (value) => { | ||
switch (value) { | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
case "undefined": | ||
return void 0; | ||
case "null": | ||
return null; | ||
default: | ||
return value; | ||
// src/helpers/injector.ts | ||
var DIContainer = class { | ||
hasBeenConfigured; | ||
_cookieHandler; | ||
constructor() { | ||
this.hasBeenConfigured = false; | ||
this._cookieHandler = {}; | ||
} | ||
}; | ||
var stringify = (value = "") => { | ||
try { | ||
const result = JSON.stringify(value); | ||
return /^[\{\[]/.test(result) ? result : value; | ||
} catch (e) { | ||
return value; | ||
get cookieHandler() { | ||
return this._cookieHandler; | ||
} | ||
}; | ||
var decode = (str) => str ? str.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent) : str; | ||
var getCookies = (options) => { | ||
let req; | ||
if (options) { | ||
req = options.req; | ||
set cookieHandler(cookierHandler) { | ||
this._cookieHandler = cookierHandler; | ||
} | ||
if (!isClientSide()) { | ||
if (req?.cookies) { | ||
return req.cookies; | ||
} | ||
if (req?.headers?.cookie) { | ||
return (0, import_cookie.parse)(req.headers.cookie); | ||
} | ||
return {}; | ||
} | ||
const _cookies = {}; | ||
const documentCookies = document.cookie ? document.cookie.split("; ") : []; | ||
for (let i = 0, len = documentCookies.length; i < len; i++) { | ||
const cookieParts = documentCookies[i].split("="); | ||
const _cookie = cookieParts.slice(1).join("="); | ||
const name = cookieParts[0]; | ||
_cookies[name] = _cookie; | ||
} | ||
return _cookies; | ||
}; | ||
var getCookie = (key, options) => { | ||
const _cookies = getCookies(options); | ||
const value = _cookies[key]; | ||
if (value === void 0) { | ||
return void 0; | ||
var diContainer = new DIContainer(); | ||
// src/helpers/isDIConfigured.ts | ||
var isDIConfigured = (diContainer2) => { | ||
if (!diContainer2.hasBeenConfigured) { | ||
throw new Error( | ||
`The SDK has not been configured. Please call .configure on the base SDK before you call any other methods` | ||
); | ||
} | ||
return processValue(decode(value)); | ||
}; | ||
var setCookie = (key, data, options) => { | ||
let _cookieOptions; | ||
let _req; | ||
let _res; | ||
if (options) { | ||
const { req, res, ..._options } = options; | ||
_req = req; | ||
_res = res; | ||
_cookieOptions = _options; | ||
} | ||
const cookieStr = (0, import_cookie.serialize)(key, stringify(data), { | ||
path: "/", | ||
..._cookieOptions | ||
}); | ||
if (!isClientSide()) { | ||
if (_res && _req) { | ||
let currentCookies = _res.getHeader("Set-Cookie"); | ||
if (!Array.isArray(currentCookies)) { | ||
currentCookies = !currentCookies ? [] : [String(currentCookies)]; | ||
} | ||
_res.setHeader("Set-Cookie", currentCookies.concat(cookieStr)); | ||
if (_req && _req.cookies) { | ||
const _cookies = _req.cookies; | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
} | ||
if (_req && _req.headers && _req.headers.cookie) { | ||
const _cookies = (0, import_cookie.parse)(_req.headers.cookie); | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
_req.headers.cookie = Object.entries(_cookies).reduce( | ||
(accum, item) => { | ||
return accum.concat(`${item[0]}=${item[1]};`); | ||
}, | ||
"" | ||
); | ||
} | ||
} | ||
} else { | ||
document.cookie = cookieStr; | ||
} | ||
}; | ||
var deleteCookie = (key, options) => setCookie(key, "", { ...options, maxAge: -1 }); | ||
@@ -140,3 +66,4 @@ // src/helpers/cookieManagement.ts | ||
get: function(serverOptions) { | ||
if (getCookie(REMEMBER_ME, serverOptions)) { | ||
isDIConfigured(diContainer); | ||
if (diContainer._cookieHandler.getCookie(REMEMBER_ME, serverOptions)) { | ||
return true; | ||
@@ -155,4 +82,9 @@ } | ||
set: function(rememberMe, serverOptions) { | ||
isDIConfigured(diContainer); | ||
if (rememberMe) { | ||
return setCookie(REMEMBER_ME, "1", serverOptions); | ||
return diContainer._cookieHandler.setCookie( | ||
REMEMBER_ME, | ||
"1", | ||
serverOptions | ||
); | ||
} else { | ||
@@ -170,3 +102,7 @@ this.remove(); | ||
remove: function(serverOptions) { | ||
return deleteCookie(REMEMBER_ME, serverOptions); | ||
isDIConfigured(diContainer); | ||
return diContainer._cookieHandler.deleteCookie( | ||
REMEMBER_ME, | ||
serverOptions | ||
); | ||
} | ||
@@ -182,3 +118,4 @@ }; | ||
get: function(req, res) { | ||
return getCookie("frontastic-session", { req, res })?.toString(); | ||
isDIConfigured(diContainer); | ||
return diContainer._cookieHandler.getCookie("frontastic-session", { req, res })?.toString(); | ||
} | ||
@@ -185,0 +122,0 @@ }; |
@@ -30,101 +30,27 @@ "use strict"; | ||
// src/cookieHandling/index.ts | ||
var import_cookie = require("cookie"); | ||
var isClientSide = () => typeof window !== "undefined"; | ||
var processValue = (value) => { | ||
switch (value) { | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
case "undefined": | ||
return void 0; | ||
case "null": | ||
return null; | ||
default: | ||
return value; | ||
// src/helpers/injector.ts | ||
var DIContainer = class { | ||
hasBeenConfigured; | ||
_cookieHandler; | ||
constructor() { | ||
this.hasBeenConfigured = false; | ||
this._cookieHandler = {}; | ||
} | ||
}; | ||
var stringify = (value = "") => { | ||
try { | ||
const result = JSON.stringify(value); | ||
return /^[\{\[]/.test(result) ? result : value; | ||
} catch (e) { | ||
return value; | ||
get cookieHandler() { | ||
return this._cookieHandler; | ||
} | ||
}; | ||
var decode = (str) => str ? str.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent) : str; | ||
var getCookies = (options) => { | ||
let req; | ||
if (options) { | ||
req = options.req; | ||
set cookieHandler(cookierHandler) { | ||
this._cookieHandler = cookierHandler; | ||
} | ||
if (!isClientSide()) { | ||
if (req?.cookies) { | ||
return req.cookies; | ||
} | ||
if (req?.headers?.cookie) { | ||
return (0, import_cookie.parse)(req.headers.cookie); | ||
} | ||
return {}; | ||
} | ||
const _cookies = {}; | ||
const documentCookies = document.cookie ? document.cookie.split("; ") : []; | ||
for (let i = 0, len = documentCookies.length; i < len; i++) { | ||
const cookieParts = documentCookies[i].split("="); | ||
const _cookie = cookieParts.slice(1).join("="); | ||
const name = cookieParts[0]; | ||
_cookies[name] = _cookie; | ||
} | ||
return _cookies; | ||
}; | ||
var getCookie = (key, options) => { | ||
const _cookies = getCookies(options); | ||
const value = _cookies[key]; | ||
if (value === void 0) { | ||
return void 0; | ||
var diContainer = new DIContainer(); | ||
// src/helpers/isDIConfigured.ts | ||
var isDIConfigured = (diContainer2) => { | ||
if (!diContainer2.hasBeenConfigured) { | ||
throw new Error( | ||
`The SDK has not been configured. Please call .configure on the base SDK before you call any other methods` | ||
); | ||
} | ||
return processValue(decode(value)); | ||
}; | ||
var setCookie = (key, data, options) => { | ||
let _cookieOptions; | ||
let _req; | ||
let _res; | ||
if (options) { | ||
const { req, res, ..._options } = options; | ||
_req = req; | ||
_res = res; | ||
_cookieOptions = _options; | ||
} | ||
const cookieStr = (0, import_cookie.serialize)(key, stringify(data), { | ||
path: "/", | ||
..._cookieOptions | ||
}); | ||
if (!isClientSide()) { | ||
if (_res && _req) { | ||
let currentCookies = _res.getHeader("Set-Cookie"); | ||
if (!Array.isArray(currentCookies)) { | ||
currentCookies = !currentCookies ? [] : [String(currentCookies)]; | ||
} | ||
_res.setHeader("Set-Cookie", currentCookies.concat(cookieStr)); | ||
if (_req && _req.cookies) { | ||
const _cookies = _req.cookies; | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
} | ||
if (_req && _req.headers && _req.headers.cookie) { | ||
const _cookies = (0, import_cookie.parse)(_req.headers.cookie); | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
_req.headers.cookie = Object.entries(_cookies).reduce( | ||
(accum, item) => { | ||
return accum.concat(`${item[0]}=${item[1]};`); | ||
}, | ||
"" | ||
); | ||
} | ||
} | ||
} else { | ||
document.cookie = cookieStr; | ||
} | ||
}; | ||
var deleteCookie = (key, options) => setCookie(key, "", { ...options, maxAge: -1 }); | ||
@@ -142,3 +68,4 @@ // src/helpers/cookieManagement.ts | ||
get: function(serverOptions) { | ||
if (getCookie(REMEMBER_ME, serverOptions)) { | ||
isDIConfigured(diContainer); | ||
if (diContainer._cookieHandler.getCookie(REMEMBER_ME, serverOptions)) { | ||
return true; | ||
@@ -157,4 +84,9 @@ } | ||
set: function(rememberMe, serverOptions) { | ||
isDIConfigured(diContainer); | ||
if (rememberMe) { | ||
return setCookie(REMEMBER_ME, "1", serverOptions); | ||
return diContainer._cookieHandler.setCookie( | ||
REMEMBER_ME, | ||
"1", | ||
serverOptions | ||
); | ||
} else { | ||
@@ -172,3 +104,7 @@ this.remove(); | ||
remove: function(serverOptions) { | ||
return deleteCookie(REMEMBER_ME, serverOptions); | ||
isDIConfigured(diContainer); | ||
return diContainer._cookieHandler.deleteCookie( | ||
REMEMBER_ME, | ||
serverOptions | ||
); | ||
} | ||
@@ -202,3 +138,7 @@ }; | ||
var fetcher = async (url, options, serverOptions, sessionLifetime) => { | ||
const sessionCookie = getCookie("frontastic-session", serverOptions) ?? ""; | ||
isDIConfigured(diContainer); | ||
const sessionCookie = diContainer._cookieHandler.getCookie( | ||
"frontastic-session", | ||
serverOptions | ||
) ?? ""; | ||
const incomingHeaders = serverOptions?.req ? { ...serverOptions.req.headers } : {}; | ||
@@ -224,3 +164,3 @@ delete incomingHeaders["host"]; | ||
} | ||
setCookie( | ||
diContainer._cookieHandler.setCookie( | ||
"frontastic-session", | ||
@@ -227,0 +167,0 @@ response.headers.get("Frontastic-Session"), |
export { rememberMeCookie, serverSession } from './helpers/cookieManagement.js'; | ||
export { Extension } from './library/Extension.js'; | ||
export { Integration } from './library/Integration.js'; | ||
export { SDK } from './library/SDK.js'; | ||
@@ -4,0 +5,0 @@ export { Event } from './library/Event.js'; |
274
lib/index.js
@@ -27,2 +27,3 @@ "use strict"; | ||
FetchError: () => FetchError, | ||
Integration: () => Integration, | ||
PageError: () => PageError, | ||
@@ -35,101 +36,27 @@ SDK: () => SDK, | ||
// src/cookieHandling/index.ts | ||
var import_cookie = require("cookie"); | ||
var isClientSide = () => typeof window !== "undefined"; | ||
var processValue = (value) => { | ||
switch (value) { | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
case "undefined": | ||
return void 0; | ||
case "null": | ||
return null; | ||
default: | ||
return value; | ||
// src/helpers/injector.ts | ||
var DIContainer = class { | ||
hasBeenConfigured; | ||
_cookieHandler; | ||
constructor() { | ||
this.hasBeenConfigured = false; | ||
this._cookieHandler = {}; | ||
} | ||
}; | ||
var stringify = (value = "") => { | ||
try { | ||
const result = JSON.stringify(value); | ||
return /^[\{\[]/.test(result) ? result : value; | ||
} catch (e) { | ||
return value; | ||
get cookieHandler() { | ||
return this._cookieHandler; | ||
} | ||
}; | ||
var decode = (str) => str ? str.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent) : str; | ||
var getCookies = (options) => { | ||
let req; | ||
if (options) { | ||
req = options.req; | ||
set cookieHandler(cookierHandler) { | ||
this._cookieHandler = cookierHandler; | ||
} | ||
if (!isClientSide()) { | ||
if (req?.cookies) { | ||
return req.cookies; | ||
} | ||
if (req?.headers?.cookie) { | ||
return (0, import_cookie.parse)(req.headers.cookie); | ||
} | ||
return {}; | ||
} | ||
const _cookies = {}; | ||
const documentCookies = document.cookie ? document.cookie.split("; ") : []; | ||
for (let i = 0, len = documentCookies.length; i < len; i++) { | ||
const cookieParts = documentCookies[i].split("="); | ||
const _cookie = cookieParts.slice(1).join("="); | ||
const name = cookieParts[0]; | ||
_cookies[name] = _cookie; | ||
} | ||
return _cookies; | ||
}; | ||
var getCookie = (key, options) => { | ||
const _cookies = getCookies(options); | ||
const value = _cookies[key]; | ||
if (value === void 0) { | ||
return void 0; | ||
var diContainer = new DIContainer(); | ||
// src/helpers/isDIConfigured.ts | ||
var isDIConfigured = (diContainer2) => { | ||
if (!diContainer2.hasBeenConfigured) { | ||
throw new Error( | ||
`The SDK has not been configured. Please call .configure on the base SDK before you call any other methods` | ||
); | ||
} | ||
return processValue(decode(value)); | ||
}; | ||
var setCookie = (key, data, options) => { | ||
let _cookieOptions; | ||
let _req; | ||
let _res; | ||
if (options) { | ||
const { req, res, ..._options } = options; | ||
_req = req; | ||
_res = res; | ||
_cookieOptions = _options; | ||
} | ||
const cookieStr = (0, import_cookie.serialize)(key, stringify(data), { | ||
path: "/", | ||
..._cookieOptions | ||
}); | ||
if (!isClientSide()) { | ||
if (_res && _req) { | ||
let currentCookies = _res.getHeader("Set-Cookie"); | ||
if (!Array.isArray(currentCookies)) { | ||
currentCookies = !currentCookies ? [] : [String(currentCookies)]; | ||
} | ||
_res.setHeader("Set-Cookie", currentCookies.concat(cookieStr)); | ||
if (_req && _req.cookies) { | ||
const _cookies = _req.cookies; | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
} | ||
if (_req && _req.headers && _req.headers.cookie) { | ||
const _cookies = (0, import_cookie.parse)(_req.headers.cookie); | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
_req.headers.cookie = Object.entries(_cookies).reduce( | ||
(accum, item) => { | ||
return accum.concat(`${item[0]}=${item[1]};`); | ||
}, | ||
"" | ||
); | ||
} | ||
} | ||
} else { | ||
document.cookie = cookieStr; | ||
} | ||
}; | ||
var deleteCookie = (key, options) => setCookie(key, "", { ...options, maxAge: -1 }); | ||
@@ -147,3 +74,4 @@ // src/helpers/cookieManagement.ts | ||
get: function(serverOptions) { | ||
if (getCookie(REMEMBER_ME, serverOptions)) { | ||
isDIConfigured(diContainer); | ||
if (diContainer._cookieHandler.getCookie(REMEMBER_ME, serverOptions)) { | ||
return true; | ||
@@ -162,4 +90,9 @@ } | ||
set: function(rememberMe, serverOptions) { | ||
isDIConfigured(diContainer); | ||
if (rememberMe) { | ||
return setCookie(REMEMBER_ME, "1", serverOptions); | ||
return diContainer._cookieHandler.setCookie( | ||
REMEMBER_ME, | ||
"1", | ||
serverOptions | ||
); | ||
} else { | ||
@@ -177,3 +110,7 @@ this.remove(); | ||
remove: function(serverOptions) { | ||
return deleteCookie(REMEMBER_ME, serverOptions); | ||
isDIConfigured(diContainer); | ||
return diContainer._cookieHandler.deleteCookie( | ||
REMEMBER_ME, | ||
serverOptions | ||
); | ||
} | ||
@@ -189,3 +126,4 @@ }; | ||
get: function(req, res) { | ||
return getCookie("frontastic-session", { req, res })?.toString(); | ||
isDIConfigured(diContainer); | ||
return diContainer._cookieHandler.getCookie("frontastic-session", { req, res })?.toString(); | ||
} | ||
@@ -210,2 +148,18 @@ }; | ||
// src/library/Integration.ts | ||
var Integration = class { | ||
/** | ||
* The sdk instance passed and assigned in the constructor. | ||
*/ | ||
sdk; | ||
/** | ||
* Contructor. | ||
* | ||
* @param {SDK} sdk - The singleton sdk instance created within your project. | ||
*/ | ||
constructor(sdk) { | ||
this.sdk = sdk; | ||
} | ||
}; | ||
// src/constants/defaultSessionLifetime.ts | ||
@@ -239,3 +193,7 @@ var DEFAULT_SESSION_LIFETIME = 7776e6; | ||
var fetcher = async (url, options, serverOptions, sessionLifetime) => { | ||
const sessionCookie = getCookie("frontastic-session", serverOptions) ?? ""; | ||
isDIConfigured(diContainer); | ||
const sessionCookie = diContainer._cookieHandler.getCookie( | ||
"frontastic-session", | ||
serverOptions | ||
) ?? ""; | ||
const incomingHeaders = serverOptions?.req ? { ...serverOptions.req.headers } : {}; | ||
@@ -261,3 +219,3 @@ delete incomingHeaders["host"]; | ||
} | ||
setCookie( | ||
diContainer._cookieHandler.setCookie( | ||
"frontastic-session", | ||
@@ -454,2 +412,117 @@ response.headers.get("Frontastic-Session"), | ||
// src/cookieHandling/index.ts | ||
var import_cookie = require("cookie"); | ||
var CookieHandler = class { | ||
isClientSide() { | ||
return typeof window !== "undefined"; | ||
} | ||
stringify(value = "") { | ||
try { | ||
const result = JSON.stringify(value); | ||
return /^[\{\[]/.test(result) ? result : value; | ||
} catch (e) { | ||
return value; | ||
} | ||
} | ||
decode(str) { | ||
return str ? str.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent) : str; | ||
} | ||
processValue(value) { | ||
switch (value) { | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
case "undefined": | ||
return void 0; | ||
case "null": | ||
return null; | ||
default: | ||
return value; | ||
} | ||
} | ||
getCookies(options) { | ||
let req; | ||
if (options) { | ||
req = options.req; | ||
} | ||
if (!this.isClientSide()) { | ||
if (req?.cookies) { | ||
return req.cookies; | ||
} | ||
if (req?.headers?.cookie) { | ||
return (0, import_cookie.parse)(req.headers.cookie); | ||
} | ||
return {}; | ||
} | ||
const _cookies = {}; | ||
const documentCookies = document.cookie ? document.cookie.split("; ") : []; | ||
for (let i = 0, len = documentCookies.length; i < len; i++) { | ||
const cookieParts = documentCookies[i].split("="); | ||
const _cookie = cookieParts.slice(1).join("="); | ||
const name = cookieParts[0]; | ||
_cookies[name] = _cookie; | ||
} | ||
return _cookies; | ||
} | ||
hasCookie(key, options) { | ||
if (!key) { | ||
return false; | ||
} | ||
const cookie = this.getCookies(options); | ||
return cookie.hasOwnProperty(key); | ||
} | ||
setCookie(key, data, options) { | ||
let _cookieOptions; | ||
let _req; | ||
let _res; | ||
if (options) { | ||
const { req, res, ..._options } = options; | ||
_req = req; | ||
_res = res; | ||
_cookieOptions = _options; | ||
} | ||
const cookieStr = (0, import_cookie.serialize)(key, this.stringify(data), { | ||
path: "/", | ||
..._cookieOptions | ||
}); | ||
if (!this.isClientSide()) { | ||
if (_res && _req) { | ||
let currentCookies = _res.getHeader("Set-Cookie"); | ||
if (!Array.isArray(currentCookies)) { | ||
currentCookies = !currentCookies ? [] : [String(currentCookies)]; | ||
} | ||
_res.setHeader("Set-Cookie", currentCookies.concat(cookieStr)); | ||
if (_req && _req.cookies) { | ||
const _cookies = _req.cookies; | ||
data === "" ? delete _cookies[key] : _cookies[key] = this.stringify(data); | ||
} | ||
if (_req && _req.headers && _req.headers.cookie) { | ||
const _cookies = (0, import_cookie.parse)(_req.headers.cookie); | ||
data === "" ? delete _cookies[key] : _cookies[key] = this.stringify(data); | ||
_req.headers.cookie = Object.entries(_cookies).reduce( | ||
(accum, item) => { | ||
return accum.concat(`${item[0]}=${item[1]};`); | ||
}, | ||
"" | ||
); | ||
} | ||
} | ||
} else { | ||
document.cookie = cookieStr; | ||
} | ||
} | ||
getCookie(key, options) { | ||
const _cookies = this.getCookies(options); | ||
const value = _cookies[key]; | ||
if (value === void 0) { | ||
return void 0; | ||
} | ||
return this.processValue(this.decode(value)); | ||
} | ||
deleteCookie(key, options) { | ||
this.setCookie(key, "", { ...options, maxAge: -1 }); | ||
} | ||
}; | ||
// src/library/SDK.ts | ||
@@ -552,2 +625,4 @@ var SDK = class extends EventManager { | ||
configure(config) { | ||
diContainer.cookieHandler = new CookieHandler(); | ||
diContainer.hasBeenConfigured = true; | ||
this.endpoint = config.endpoint; | ||
@@ -774,2 +849,3 @@ this.configureLocale(config); | ||
FetchError, | ||
Integration, | ||
PageError, | ||
@@ -776,0 +852,0 @@ SDK, |
@@ -24,2 +24,3 @@ import { SDK } from './SDK.js'; | ||
* The abstract base class signature to extend SDK integration classes. | ||
* @deprecated This class is deprecated and should not be used any more, extend the {@link Integration} class instead | ||
* | ||
@@ -39,10 +40,4 @@ * @param {Extension<CustomEvents>} CustomEvents - The generic argument defining any custom events on the integration. | ||
constructor(sdk: SDK<CustomEvents>); | ||
/** | ||
* The unregister method signature to implement to remove eventHandlers for short lived integrations. | ||
* | ||
* @returns {void} Void. | ||
*/ | ||
abstract unregisterExtension(): void; | ||
} | ||
export { Extension }; |
@@ -30,101 +30,27 @@ "use strict"; | ||
// src/cookieHandling/index.ts | ||
var import_cookie = require("cookie"); | ||
var isClientSide = () => typeof window !== "undefined"; | ||
var processValue = (value) => { | ||
switch (value) { | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
case "undefined": | ||
return void 0; | ||
case "null": | ||
return null; | ||
default: | ||
return value; | ||
// src/helpers/injector.ts | ||
var DIContainer = class { | ||
hasBeenConfigured; | ||
_cookieHandler; | ||
constructor() { | ||
this.hasBeenConfigured = false; | ||
this._cookieHandler = {}; | ||
} | ||
}; | ||
var stringify = (value = "") => { | ||
try { | ||
const result = JSON.stringify(value); | ||
return /^[\{\[]/.test(result) ? result : value; | ||
} catch (e) { | ||
return value; | ||
get cookieHandler() { | ||
return this._cookieHandler; | ||
} | ||
}; | ||
var decode = (str) => str ? str.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent) : str; | ||
var getCookies = (options) => { | ||
let req; | ||
if (options) { | ||
req = options.req; | ||
set cookieHandler(cookierHandler) { | ||
this._cookieHandler = cookierHandler; | ||
} | ||
if (!isClientSide()) { | ||
if (req?.cookies) { | ||
return req.cookies; | ||
} | ||
if (req?.headers?.cookie) { | ||
return (0, import_cookie.parse)(req.headers.cookie); | ||
} | ||
return {}; | ||
} | ||
const _cookies = {}; | ||
const documentCookies = document.cookie ? document.cookie.split("; ") : []; | ||
for (let i = 0, len = documentCookies.length; i < len; i++) { | ||
const cookieParts = documentCookies[i].split("="); | ||
const _cookie = cookieParts.slice(1).join("="); | ||
const name = cookieParts[0]; | ||
_cookies[name] = _cookie; | ||
} | ||
return _cookies; | ||
}; | ||
var getCookie = (key, options) => { | ||
const _cookies = getCookies(options); | ||
const value = _cookies[key]; | ||
if (value === void 0) { | ||
return void 0; | ||
var diContainer = new DIContainer(); | ||
// src/helpers/isDIConfigured.ts | ||
var isDIConfigured = (diContainer2) => { | ||
if (!diContainer2.hasBeenConfigured) { | ||
throw new Error( | ||
`The SDK has not been configured. Please call .configure on the base SDK before you call any other methods` | ||
); | ||
} | ||
return processValue(decode(value)); | ||
}; | ||
var setCookie = (key, data, options) => { | ||
let _cookieOptions; | ||
let _req; | ||
let _res; | ||
if (options) { | ||
const { req, res, ..._options } = options; | ||
_req = req; | ||
_res = res; | ||
_cookieOptions = _options; | ||
} | ||
const cookieStr = (0, import_cookie.serialize)(key, stringify(data), { | ||
path: "/", | ||
..._cookieOptions | ||
}); | ||
if (!isClientSide()) { | ||
if (_res && _req) { | ||
let currentCookies = _res.getHeader("Set-Cookie"); | ||
if (!Array.isArray(currentCookies)) { | ||
currentCookies = !currentCookies ? [] : [String(currentCookies)]; | ||
} | ||
_res.setHeader("Set-Cookie", currentCookies.concat(cookieStr)); | ||
if (_req && _req.cookies) { | ||
const _cookies = _req.cookies; | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
} | ||
if (_req && _req.headers && _req.headers.cookie) { | ||
const _cookies = (0, import_cookie.parse)(_req.headers.cookie); | ||
data === "" ? delete _cookies[key] : _cookies[key] = stringify(data); | ||
_req.headers.cookie = Object.entries(_cookies).reduce( | ||
(accum, item) => { | ||
return accum.concat(`${item[0]}=${item[1]};`); | ||
}, | ||
"" | ||
); | ||
} | ||
} | ||
} else { | ||
document.cookie = cookieStr; | ||
} | ||
}; | ||
var deleteCookie = (key, options) => setCookie(key, "", { ...options, maxAge: -1 }); | ||
@@ -142,3 +68,4 @@ // src/helpers/cookieManagement.ts | ||
get: function(serverOptions) { | ||
if (getCookie(REMEMBER_ME, serverOptions)) { | ||
isDIConfigured(diContainer); | ||
if (diContainer._cookieHandler.getCookie(REMEMBER_ME, serverOptions)) { | ||
return true; | ||
@@ -157,4 +84,9 @@ } | ||
set: function(rememberMe, serverOptions) { | ||
isDIConfigured(diContainer); | ||
if (rememberMe) { | ||
return setCookie(REMEMBER_ME, "1", serverOptions); | ||
return diContainer._cookieHandler.setCookie( | ||
REMEMBER_ME, | ||
"1", | ||
serverOptions | ||
); | ||
} else { | ||
@@ -172,3 +104,7 @@ this.remove(); | ||
remove: function(serverOptions) { | ||
return deleteCookie(REMEMBER_ME, serverOptions); | ||
isDIConfigured(diContainer); | ||
return diContainer._cookieHandler.deleteCookie( | ||
REMEMBER_ME, | ||
serverOptions | ||
); | ||
} | ||
@@ -202,3 +138,7 @@ }; | ||
var fetcher = async (url, options, serverOptions, sessionLifetime) => { | ||
const sessionCookie = getCookie("frontastic-session", serverOptions) ?? ""; | ||
isDIConfigured(diContainer); | ||
const sessionCookie = diContainer._cookieHandler.getCookie( | ||
"frontastic-session", | ||
serverOptions | ||
) ?? ""; | ||
const incomingHeaders = serverOptions?.req ? { ...serverOptions.req.headers } : {}; | ||
@@ -224,3 +164,3 @@ delete incomingHeaders["host"]; | ||
} | ||
setCookie( | ||
diContainer._cookieHandler.setCookie( | ||
"frontastic-session", | ||
@@ -417,2 +357,117 @@ response.headers.get("Frontastic-Session"), | ||
// src/cookieHandling/index.ts | ||
var import_cookie = require("cookie"); | ||
var CookieHandler = class { | ||
isClientSide() { | ||
return typeof window !== "undefined"; | ||
} | ||
stringify(value = "") { | ||
try { | ||
const result = JSON.stringify(value); | ||
return /^[\{\[]/.test(result) ? result : value; | ||
} catch (e) { | ||
return value; | ||
} | ||
} | ||
decode(str) { | ||
return str ? str.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent) : str; | ||
} | ||
processValue(value) { | ||
switch (value) { | ||
case "true": | ||
return true; | ||
case "false": | ||
return false; | ||
case "undefined": | ||
return void 0; | ||
case "null": | ||
return null; | ||
default: | ||
return value; | ||
} | ||
} | ||
getCookies(options) { | ||
let req; | ||
if (options) { | ||
req = options.req; | ||
} | ||
if (!this.isClientSide()) { | ||
if (req?.cookies) { | ||
return req.cookies; | ||
} | ||
if (req?.headers?.cookie) { | ||
return (0, import_cookie.parse)(req.headers.cookie); | ||
} | ||
return {}; | ||
} | ||
const _cookies = {}; | ||
const documentCookies = document.cookie ? document.cookie.split("; ") : []; | ||
for (let i = 0, len = documentCookies.length; i < len; i++) { | ||
const cookieParts = documentCookies[i].split("="); | ||
const _cookie = cookieParts.slice(1).join("="); | ||
const name = cookieParts[0]; | ||
_cookies[name] = _cookie; | ||
} | ||
return _cookies; | ||
} | ||
hasCookie(key, options) { | ||
if (!key) { | ||
return false; | ||
} | ||
const cookie = this.getCookies(options); | ||
return cookie.hasOwnProperty(key); | ||
} | ||
setCookie(key, data, options) { | ||
let _cookieOptions; | ||
let _req; | ||
let _res; | ||
if (options) { | ||
const { req, res, ..._options } = options; | ||
_req = req; | ||
_res = res; | ||
_cookieOptions = _options; | ||
} | ||
const cookieStr = (0, import_cookie.serialize)(key, this.stringify(data), { | ||
path: "/", | ||
..._cookieOptions | ||
}); | ||
if (!this.isClientSide()) { | ||
if (_res && _req) { | ||
let currentCookies = _res.getHeader("Set-Cookie"); | ||
if (!Array.isArray(currentCookies)) { | ||
currentCookies = !currentCookies ? [] : [String(currentCookies)]; | ||
} | ||
_res.setHeader("Set-Cookie", currentCookies.concat(cookieStr)); | ||
if (_req && _req.cookies) { | ||
const _cookies = _req.cookies; | ||
data === "" ? delete _cookies[key] : _cookies[key] = this.stringify(data); | ||
} | ||
if (_req && _req.headers && _req.headers.cookie) { | ||
const _cookies = (0, import_cookie.parse)(_req.headers.cookie); | ||
data === "" ? delete _cookies[key] : _cookies[key] = this.stringify(data); | ||
_req.headers.cookie = Object.entries(_cookies).reduce( | ||
(accum, item) => { | ||
return accum.concat(`${item[0]}=${item[1]};`); | ||
}, | ||
"" | ||
); | ||
} | ||
} | ||
} else { | ||
document.cookie = cookieStr; | ||
} | ||
} | ||
getCookie(key, options) { | ||
const _cookies = this.getCookies(options); | ||
const value = _cookies[key]; | ||
if (value === void 0) { | ||
return void 0; | ||
} | ||
return this.processValue(this.decode(value)); | ||
} | ||
deleteCookie(key, options) { | ||
this.setCookie(key, "", { ...options, maxAge: -1 }); | ||
} | ||
}; | ||
// src/library/SDK.ts | ||
@@ -515,2 +570,4 @@ var SDK = class extends EventManager { | ||
configure(config) { | ||
diContainer.cookieHandler = new CookieHandler(); | ||
diContainer.hasBeenConfigured = true; | ||
this.endpoint = config.endpoint; | ||
@@ -517,0 +574,0 @@ this.configureLocale(config); |
{ | ||
"name": "@commercetools/frontend-sdk", | ||
"version": "1.8.1", | ||
"version": "1.8.2", | ||
"license": "UNLICENSED", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
359290
196
4713