// Ported from [Nuxt](https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/cookie.ts) import type { CookieParseOptions, CookieSerializeOptions } from 'cookie-es' import { parse, serialize } from 'cookie-es' import { destr } from 'destr' type _CookieOptions = Omit export interface CookieOptions extends _CookieOptions { decode?(value: string): T encode?(value: T): string default?: () => T | Ref watch?: boolean | 'shallow' } export type CookieRef = Ref const CookieDefaults: CookieOptions = { path: '/', watch: true, decode: val => destr(decodeURIComponent(val)), encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val)), } export const useCookie = (name: string, _opts?: CookieOptions): CookieRef => { const opts = { ...CookieDefaults, ..._opts || {} } const cookies = parse(document.cookie, opts) const cookie = ref(cookies[name] as any ?? opts.default?.()) watch(cookie, () => { document.cookie = serializeCookie(name, cookie.value, opts) }) return cookie as CookieRef } function serializeCookie(name: string, value: any, opts: CookieSerializeOptions = {}) { if (value === null || value === undefined) return serialize(name, value, { ...opts, maxAge: -1 }) return serialize(name, value, { ...opts, maxAge: 60 * 60 * 24 * 30 }) }