@intlayer/core
Version:
Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.
135 lines (133 loc) • 4.39 kB
JavaScript
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
let __intlayer_config_client = require("@intlayer/config/client");
__intlayer_config_client = require_rolldown_runtime.__toESM(__intlayer_config_client);
//#region src/getStorageAttributes.ts
/**
* Creates a cookie entry with default values for missing attributes
*/
const createCookieEntry = (options) => {
const { name, path, expires, domain, secure, sameSite, httpOnly } = options ?? {};
return {
name: name ?? __intlayer_config_client.DefaultValues.Routing.COOKIE_NAME,
attributes: {
path,
expires,
domain,
secure,
sameSite,
httpOnly
}
};
};
/**
* Creates a web storage entry (localStorage or sessionStorage) with default name
*/
const createWebStorageEntry = (options) => {
const { name } = options ?? {};
return { name: name ?? __intlayer_config_client.DefaultValues.Routing.LOCALE_STORAGE_NAME };
};
/**
* Creates a header entry with default name
*/
const createHeaderEntry = (options) => {
const { name } = options ?? {};
return { name: name ?? __intlayer_config_client.DefaultValues.Routing.HEADER_NAME };
};
/**
* Determines if a storage entry is a cookie based on its properties
*/
const isCookieEntry = (entry) => {
return entry.type === "cookie" || "sameSite" in entry || "httpOnly" in entry || "secure" in entry;
};
/**
* Determines the storage type from a string literal
*/
const isStorageType = (value) => {
return value === "cookie" || value === "localStorage" || value === "sessionStorage" || value === "header";
};
/**
* Processes a single storage entry and returns the appropriate storage attributes
*/
const processStorageEntry = (entry) => {
if (typeof entry === "string") {
if (!isStorageType(entry)) return {
cookies: [],
localStorage: [],
sessionStorage: [],
headers: []
};
if (entry === "cookie") return { cookies: [createCookieEntry()] };
if (entry === "localStorage") return { localStorage: [createWebStorageEntry()] };
if (entry === "sessionStorage") return { sessionStorage: [createWebStorageEntry()] };
if (entry === "header") return { headers: [createHeaderEntry()] };
}
if (typeof entry === "object" && entry !== null) {
const typedEntry = entry;
if (isCookieEntry(typedEntry)) return { cookies: [createCookieEntry(typedEntry)] };
if ("type" in typedEntry && typedEntry.type === "localStorage") {
const { name: name$1,...rest$1 } = typedEntry;
return { localStorage: [createWebStorageEntry({
name: name$1,
...rest$1
})] };
}
if ("type" in typedEntry && typedEntry.type === "sessionStorage") {
const { name: name$1,...rest$1 } = typedEntry;
return { sessionStorage: [createWebStorageEntry({
name: name$1,
...rest$1
})] };
}
if ("type" in typedEntry && typedEntry.type === "header") {
const { name: name$1,...rest$1 } = typedEntry;
return { headers: [createHeaderEntry({
name: name$1,
...rest$1
})] };
}
const { name,...rest } = typedEntry;
return { localStorage: [createWebStorageEntry({
name,
...rest
})] };
}
return {
cookies: [],
localStorage: [],
sessionStorage: [],
headers: []
};
};
/**
* Merges multiple partial storage attributes into a single result
*/
const mergeStorageAttributes = (accumulated, partial) => {
return {
cookies: [...accumulated.cookies, ...partial.cookies ?? []],
localStorage: [...accumulated.localStorage, ...partial.localStorage ?? []],
sessionStorage: [...accumulated.sessionStorage, ...partial.sessionStorage ?? []],
headers: [...accumulated.headers, ...partial.headers ?? []]
};
};
/**
* Extracts and normalizes storage configuration into separate arrays for each storage type
*
* @param options - The storage configuration from IntlayerConfig
* @returns An object containing arrays for cookies, localStorage, and sessionStorage
*/
const getStorageAttributes = (options) => {
const emptyResult = {
cookies: [],
localStorage: [],
sessionStorage: [],
headers: []
};
if (options === false || options === void 0) return emptyResult;
if (Array.isArray(options)) return options.reduce((acc, entry) => {
return mergeStorageAttributes(acc, processStorageEntry(entry));
}, emptyResult);
return mergeStorageAttributes(emptyResult, processStorageEntry(options));
};
//#endregion
exports.getStorageAttributes = getStorageAttributes;
//# sourceMappingURL=getStorageAttributes.cjs.map