|
| 1 | +export type CookieOptions = { |
| 2 | + /** The number of days until the cookie expires. If not defined, the cookies expires at the end of the session */ |
| 3 | + expires?: number | Date; |
| 4 | + /** The path the cookie is valid for. Defaults to "/" */ |
| 5 | + path?: string; |
| 6 | + /** The domain the cookie is valid for. Defaults to current domain */ |
| 7 | + domain?: string; |
| 8 | + /** The SameSite attribute of the cookie. Defaults to "strict" */ |
| 9 | + sameSite?: "strict" | "lax" | "none"; |
| 10 | + /** Should the cookie only be sent over HTTPS? Defaults to true (if on a https site) */ |
| 11 | + secure?: boolean; |
| 12 | +}; |
| 13 | + |
| 14 | +function stringifyOptions(options: CookieOptions) { |
| 15 | + return Object.entries(options) |
| 16 | + .map(([key, value]) => { |
| 17 | + if (!value) { |
| 18 | + return undefined; |
| 19 | + } |
| 20 | + if (value === true) { |
| 21 | + return key; |
| 22 | + } |
| 23 | + if (key === "expires") { |
| 24 | + const expires = options[key] || 0; |
| 25 | + if (!expires) return undefined; |
| 26 | + if (expires instanceof Date) { |
| 27 | + return `expires=${expires.toUTCString()}`; |
| 28 | + } |
| 29 | + return `expires=${new Date( |
| 30 | + Date.now() + expires * 864e5, |
| 31 | + ).toUTCString()}`; |
| 32 | + } |
| 33 | + return `${key}=${value}`; |
| 34 | + }) |
| 35 | + .filter(Boolean) |
| 36 | + .join("; "); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Set a cookie, with a value and options. |
| 41 | + * @param name {string} The name of the cookie. |
| 42 | + * @param value {string} |
| 43 | + * @param options |
| 44 | + */ |
| 45 | +export function setCookie( |
| 46 | + name: string, |
| 47 | + value?: string, |
| 48 | + options: CookieOptions = {}, |
| 49 | +) { |
| 50 | + const optionsWithDefault: CookieOptions = { |
| 51 | + path: options.path || "/", |
| 52 | + sameSite: options.sameSite || "strict", |
| 53 | + secure: options.secure ?? document.location.protocol === "https:", |
| 54 | + // If expires is not set, set it to -1 (the past), so the cookie is deleted immediately |
| 55 | + expires: !value ? -1 : options.expires ?? 0, |
| 56 | + domain: options.domain, |
| 57 | + }; |
| 58 | + |
| 59 | + const encodedValue = encodeURIComponent(value || ""); |
| 60 | + const optionsString = stringifyOptions(optionsWithDefault); |
| 61 | + |
| 62 | + document.cookie = `${name}=${encodedValue}; ${optionsString}`; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Get a cookie by name. |
| 67 | + * @param name {string} The name of the cookie. |
| 68 | + * @param cookies {string} The cookies string to parse it from. Set this to `document.cookie` on the client. |
| 69 | + */ |
| 70 | +export function getCookie(name: string, cookies: string) { |
| 71 | + const value = cookies |
| 72 | + .split("; ") |
| 73 | + .find((row) => row.startsWith(`${name}=`)) |
| 74 | + ?.split("=")[1]; |
| 75 | + |
| 76 | + return value ? decodeURIComponent(value) : undefined; |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * Convert the cookies object to a string. |
| 81 | + * @param cookies |
| 82 | + */ |
| 83 | +export function stringifyCookies( |
| 84 | + cookies: Record<string, string | undefined> = {}, |
| 85 | +) { |
| 86 | + return Object.keys(cookies) |
| 87 | + .map((key) => `${key}=${cookies[key]}`) |
| 88 | + .join("; "); |
| 89 | +} |
0 commit comments