2022-08-12 17:47:58 +00:00
|
|
|
import { ref, Ref, watch } from 'vue'
|
2021-11-22 23:20:20 +00:00
|
|
|
import { parse, serialize, CookieParseOptions, CookieSerializeOptions } from 'cookie-es'
|
2021-11-22 20:43:00 +00:00
|
|
|
import { appendHeader } from 'h3'
|
2022-04-07 11:28:04 +00:00
|
|
|
import type { CompatibilityEvent } from 'h3'
|
2021-11-22 20:43:00 +00:00
|
|
|
import destr from 'destr'
|
2022-08-04 10:58:15 +00:00
|
|
|
import { isEqual } from 'ohash'
|
2022-04-07 11:28:04 +00:00
|
|
|
import { useRequestEvent } from './ssr'
|
2021-11-22 20:43:00 +00:00
|
|
|
import { useNuxtApp } from '#app'
|
|
|
|
|
|
|
|
type _CookieOptions = Omit<CookieSerializeOptions & CookieParseOptions, 'decode' | 'encode'>
|
2021-11-29 10:40:12 +00:00
|
|
|
|
2022-09-07 08:34:16 +00:00
|
|
|
export interface CookieOptions<T = any> extends _CookieOptions {
|
2021-11-22 20:43:00 +00:00
|
|
|
decode?(value: string): T
|
2022-09-07 08:34:16 +00:00
|
|
|
encode?(value: T): string
|
2022-04-13 17:41:41 +00:00
|
|
|
default?: () => T | Ref<T>
|
2021-11-22 20:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface CookieRef<T> extends Ref<T> {}
|
|
|
|
|
|
|
|
const CookieDefaults: CookieOptions<any> = {
|
2022-04-07 11:35:46 +00:00
|
|
|
path: '/',
|
2021-11-22 20:43:00 +00:00
|
|
|
decode: val => destr(decodeURIComponent(val)),
|
|
|
|
encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val))
|
|
|
|
}
|
|
|
|
|
2022-08-12 17:47:58 +00:00
|
|
|
export function useCookie <T = string> (name: string, _opts?: CookieOptions<T>): CookieRef<T> {
|
2021-11-22 20:43:00 +00:00
|
|
|
const opts = { ...CookieDefaults, ..._opts }
|
2022-08-12 17:47:58 +00:00
|
|
|
const cookies = readRawCookies(opts) || {}
|
2021-11-22 20:43:00 +00:00
|
|
|
|
2022-08-12 17:47:58 +00:00
|
|
|
const cookie = ref<T | undefined>(cookies[name] as any ?? opts.default?.())
|
2021-11-22 20:43:00 +00:00
|
|
|
|
|
|
|
if (process.client) {
|
2021-11-29 10:40:12 +00:00
|
|
|
watch(cookie, () => { writeClientCookie(name, cookie.value, opts as CookieSerializeOptions) })
|
2021-11-22 20:43:00 +00:00
|
|
|
} else if (process.server) {
|
|
|
|
const nuxtApp = useNuxtApp()
|
2022-04-19 19:13:11 +00:00
|
|
|
const writeFinalCookieValue = () => {
|
2022-08-04 10:58:15 +00:00
|
|
|
if (!isEqual(cookie.value, cookies[name])) {
|
2022-04-07 11:28:04 +00:00
|
|
|
writeServerCookie(useRequestEvent(nuxtApp), name, cookie.value, opts)
|
2021-11-22 20:43:00 +00:00
|
|
|
}
|
2022-04-19 19:13:11 +00:00
|
|
|
}
|
2022-09-07 08:34:16 +00:00
|
|
|
const unhook = nuxtApp.hooks.hookOnce('app:rendered', writeFinalCookieValue)
|
|
|
|
nuxtApp.hooks.hookOnce('app:redirected', () => {
|
|
|
|
// don't write cookie subsequently when app:rendered is called
|
|
|
|
unhook()
|
|
|
|
return writeFinalCookieValue()
|
|
|
|
})
|
2021-11-22 20:43:00 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 10:40:12 +00:00
|
|
|
return cookie as CookieRef<T>
|
2021-11-22 20:43:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 17:47:58 +00:00
|
|
|
function readRawCookies (opts: CookieOptions = {}): Record<string, string> | undefined {
|
2021-11-22 20:43:00 +00:00
|
|
|
if (process.server) {
|
2022-04-07 11:28:04 +00:00
|
|
|
return parse(useRequestEvent()?.req.headers.cookie || '', opts)
|
2021-11-22 20:43:00 +00:00
|
|
|
} else if (process.client) {
|
2021-11-22 23:20:20 +00:00
|
|
|
return parse(document.cookie, opts)
|
2021-11-22 20:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeCookie (name: string, value: any, opts: CookieSerializeOptions = {}) {
|
|
|
|
if (value === null || value === undefined) {
|
2021-12-21 12:02:55 +00:00
|
|
|
return serialize(name, value, { ...opts, maxAge: -1 })
|
2021-11-22 20:43:00 +00:00
|
|
|
}
|
2021-11-22 23:20:20 +00:00
|
|
|
return serialize(name, value, opts)
|
2021-11-22 20:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function writeClientCookie (name: string, value: any, opts: CookieSerializeOptions = {}) {
|
|
|
|
if (process.client) {
|
|
|
|
document.cookie = serializeCookie(name, value, opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:28:04 +00:00
|
|
|
function writeServerCookie (event: CompatibilityEvent, name: string, value: any, opts: CookieSerializeOptions = {}) {
|
|
|
|
if (event) {
|
2021-11-29 10:40:12 +00:00
|
|
|
// TODO: Try to smart join with existing Set-Cookie headers
|
2022-04-07 11:28:04 +00:00
|
|
|
appendHeader(event, 'Set-Cookie', serializeCookie(name, value, opts))
|
2021-11-22 20:43:00 +00:00
|
|
|
}
|
|
|
|
}
|