Skip to content

Commit

Permalink
feat(nuxt): infer global cookieOptions to specified cookie storag…
Browse files Browse the repository at this point in the history
…e without options
  • Loading branch information
prazdevs committed Dec 16, 2024
1 parent 0b201b2 commit 3c55848
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/runtime/storages.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import type { PublicRuntimeConfig } from '@nuxt/schema'
import type { StorageLike } from '../types'
import { useCookie } from '#app'
import { useCookie, useRuntimeConfig } from '#app'

type CookiesStorageOptions = PublicRuntimeConfig['piniaPluginPersistedstate']['cookieOptions']

/**
* Cookie-based storage. Cookie options can be passed as parameter.
* Uses Nuxt's `useCookie` under the hood.
*/
function cookies(options: CookiesStorageOptions = {}): StorageLike {
function cookies(options?: CookiesStorageOptions): StorageLike {
return {
getItem: key => useCookie<string | null>(
key,
{
...options,
...(options ?? useRuntimeConfig().public.piniaPluginPersistedstate.cookieOptions ?? {}),
decode: decodeURIComponent,
readonly: true,
},
).value,
setItem: (key, value) => useCookie<string>(
key,
{
...options,
...(options ?? useRuntimeConfig().public.piniaPluginPersistedstate.cookieOptions ?? {}),
encode: encodeURIComponent,
},
).value = value,
Expand Down Expand Up @@ -62,5 +62,4 @@ export const storages = {
cookies,
localStorage,
sessionStorage,

}

0 comments on commit 3c55848

Please sign in to comment.