|
type CookieOptions = { |
|
expires?: number; |
|
domain?: string; |
|
path?: string; |
|
sameSite?: 'strict' | 'lax' | 'none'; |
|
}; |
|
|
|
type CookieItem = { |
|
name: string; |
|
value: string; |
|
domain?: string; |
|
path: string; |
|
expires?: number; |
|
secure?: boolean; |
|
sameSite?: 'strict' | 'lax' | 'none'; |
|
}; |
|
|
|
class CookieStoreShim { |
|
static isSupported(): boolean { |
|
return typeof (window as any).CookieStore !== 'undefined'; |
|
} |
|
|
|
async get(nameOrOptions: string | { name: string }): Promise<CookieItem | null> { |
|
if (CookieStoreShim.isSupported()) { |
|
return (window as any).cookieStore.get(nameOrOptions); |
|
} else { |
|
const name = typeof nameOrOptions === 'string' ? nameOrOptions : nameOrOptions.name; |
|
const value = this._getCookie(name); |
|
return value ? { name, value } : null; |
|
} |
|
} |
|
|
|
async getAll(nameOrOptions: string | { name: string }): Promise<CookieItem[]> { |
|
if (CookieStoreShim.isSupported()) { |
|
return (window as any).cookieStore.getAll(nameOrOptions); |
|
} else { |
|
const name = typeof nameOrOptions === 'string' ? nameOrOptions : nameOrOptions.name; |
|
return this._getAllCookies().filter((cookie: CookieItem) => cookie.name === name); |
|
} |
|
} |
|
|
|
async set(nameOrOptions: string | CookieOptions & { name: string; value: string }, value?: string): Promise<void> { |
|
if (CookieStoreShim.isSupported()) { |
|
if (typeof nameOrOptions === 'string') { |
|
return (window as any).cookieStore.set({ name: nameOrOptions, value }); |
|
} else { |
|
return (window as any).cookieStore.set(nameOrOptions); |
|
} |
|
} else { |
|
if (typeof nameOrOptions === 'string') { |
|
this._setCookie(nameOrOptions, value!); |
|
} else { |
|
this._setCookie(nameOrOptions.name, nameOrOptions.value, nameOrOptions); |
|
} |
|
return Promise.resolve(); |
|
} |
|
} |
|
|
|
async delete(nameOrOptions: string | { name: string }): Promise<void> { |
|
if (CookieStoreShim.isSupported()) { |
|
return (window as any).cookieStore.delete(nameOrOptions); |
|
} else { |
|
const name = typeof nameOrOptions === 'string' ? nameOrOptions : nameOrOptions.name; |
|
this._setCookie(name, '', { expires: -1 }); |
|
return Promise.resolve(); |
|
} |
|
} |
|
|
|
private _getCookie(name: string): string | null { |
|
const value = `; ${document.cookie}`; |
|
const parts = value.split(`; ${name}=`); |
|
if (parts.length === 2) { |
|
return parts.pop().split(';').shift()!; |
|
} |
|
return null; |
|
} |
|
|
|
private _getAllCookies(): CookieItem[] { |
|
const cookies = document.cookie.split('; '); |
|
return cookies.map((cookie) => { |
|
const [name, value] = cookie.split('='); |
|
return { name, value }; |
|
}); |
|
} |
|
|
|
private _setCookie(name: string, value: string, options: CookieOptions = {}): void { |
|
let attributes = ''; |
|
if (options.expires) { |
|
const date = new Date(options.expires); |
|
attributes += `; expires=${date.toUTCString()}`; |
|
} |
|
if (options.domain) { |
|
attributes += `; domain=${options.domain}`; |
|
} |
|
|
|
if (options.path) { |
|
attributes += ; path=${options.path}; |
|
} |
|
if (options.sameSite) { |
|
attributes += ; samesite=${options.sameSite}; |
|
} |
|
document.cookie = ${name}=${value}${attributes}; |
|
} |
|
} |
|
|
|
const cookieStoreShim = new CookieStoreShim(); |