first commit
This commit is contained in:
28
resources/js/@core/composable/useCookie.js
Normal file
28
resources/js/@core/composable/useCookie.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// Ported from [Nuxt](https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/cookie.ts)
|
||||
import { parse, serialize } from 'cookie-es'
|
||||
import { destr } from 'destr'
|
||||
|
||||
const CookieDefaults = {
|
||||
path: '/',
|
||||
watch: true,
|
||||
decode: val => destr(decodeURIComponent(val)),
|
||||
encode: val => encodeURIComponent(typeof val === 'string' ? val : JSON.stringify(val)),
|
||||
}
|
||||
|
||||
export const useCookie = (name, _opts) => {
|
||||
const opts = { ...CookieDefaults, ..._opts || {} }
|
||||
const cookies = parse(document.cookie, opts)
|
||||
const cookie = ref(cookies[name] ?? opts.default?.())
|
||||
|
||||
watch(cookie, () => {
|
||||
document.cookie = serializeCookie(name, cookie.value, opts)
|
||||
})
|
||||
|
||||
return cookie
|
||||
}
|
||||
function serializeCookie(name, value, opts = {}) {
|
||||
if (value === null || value === undefined)
|
||||
return serialize(name, value, { ...opts, maxAge: -1 })
|
||||
|
||||
return serialize(name, value, opts)
|
||||
}
|
Reference in New Issue
Block a user