UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

45 lines (44 loc) 1.83 kB
//#region src/utils/cookieExpiry.ts /** * Resolves a normalized cookie `expires` value to an absolute timestamp * (epoch ms). The value is produced by the config-build merge step, so it is * already unambiguous: * * - `number` → seconds from now (relative lifetime); * - `string` → an absolute ISO date. * * Returns `undefined` when no usable expiry is provided so callers fall back * to a session cookie. * * @param expires - The normalized `expires` value from the built config. * @returns The absolute expiry in epoch milliseconds, or `undefined`. */ const resolveExpiresToTimestamp = (expires) => { if (typeof expires === "number") return Date.now() + expires * 1e3; if (typeof expires === "string") { const time = Date.parse(expires); return Number.isNaN(time) ? void 0 : time; } }; /** * Serializes a cookie into a `document.cookie` string. Expiry is emitted as an * absolute `Expires=` attribute resolved from the normalized `expires` value. * * @param name - The cookie name. * @param value - The cookie value (URL-encoded by this helper). * @param attributes - The normalized cookie attributes. * @returns The serialized cookie string. */ const buildCookieString = (name, value, attributes) => { const parts = [`${name}=${encodeURIComponent(value)}`]; if (attributes.path) parts.push(`Path=${attributes.path}`); if (attributes.domain) parts.push(`Domain=${attributes.domain}`); const expiresTimestamp = resolveExpiresToTimestamp(attributes.expires); if (expiresTimestamp !== void 0) parts.push(`Expires=${new Date(expiresTimestamp).toUTCString()}`); if (attributes.secure) parts.push("Secure"); if (attributes.sameSite) parts.push(`SameSite=${attributes.sameSite}`); return parts.join("; "); }; //#endregion export { buildCookieString, resolveExpiresToTimestamp }; //# sourceMappingURL=cookieExpiry.mjs.map