Skip to content
This repository was archived by the owner on Aug 31, 2025. It is now read-only.

Commit f377002

Browse files
committed
feat(nuxt): add encode/decode to cookies options
1 parent 8c6ae57 commit f377002

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

docs/frameworks/nuxt.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export const useStore = defineStore('main', {
9090
> The `persistedState.cookies` method accept an object parameter to configure cookies with the following options (inherited from Nuxt's `useCookie`):
9191
>
9292
> - [`domain`](https://nuxt.com/docs/api/composables/use-cookie#domain)
93+
> - [`encode`](https://nuxt.com/docs/api/composables/use-cookie#encode)/[`decode`](https://nuxt.com/docs/api/composables/use-cookie#decode)
9394
> - [`expires`](https://nuxt.com/docs/api/composables/use-cookie#maxage-expires)
9495
> - [`httpOnly`](https://nuxt.com/docs/api/composables/use-cookie#httponly)
9596
> - [`maxAge`](https://nuxt.com/docs/api/composables/use-cookie#maxage-expires)
@@ -143,7 +144,7 @@ export const useStore = defineStore('main', {
143144

144145
The module accepts some options defined in `nuxt.config.ts` under the `piniaPluginPersistedstate` key:
145146

146-
- [`cookieOptions`](#cookies)
147+
- [`cookieOptions`](#cookies) (except `decode` and `encode` as functions are not supported)
147148
- `debug`
148149
- [`key`](#global-key)
149150
- `storage`

src/module.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CookieOptions } from 'nuxt/app'
1+
import type { CookiesStorageOptions } from './runtime/storages'
22
import type { PersistenceOptions } from './types'
33
import {
44
addImports,
@@ -15,19 +15,18 @@ type ModuleOptions = Pick<PersistenceOptions, 'debug'> & {
1515
* Default storage for persistence. Only accepts presets.
1616
*/
1717
storage?: 'cookies' | 'localStorage' | 'sessionStorage'
18+
1819
/**
1920
* Global key template, allow pre/postfixing store keys.
2021
* @example 'my-%id-persistence' will yield 'my-<store-id>-persistence'
2122
*/
2223
key?: `${string}%id${string}`
24+
2325
/**
2426
* Options used globally by default cookie storage.
2527
* Ignored for other storages.
2628
*/
27-
cookieOptions?: Omit<
28-
CookieOptions,
29-
'encode' | 'decode' | 'default' | 'watch' | 'readonly' | 'filter'
30-
>
29+
cookieOptions?: Omit<CookiesStorageOptions, 'encode' | 'decode'>
3130

3231
/**
3332
* Automatically persist all stores with global defaults, opt-out individually.

src/runtime/storages.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import type { PublicRuntimeConfig } from '@nuxt/schema'
1+
import type { CookieOptions } from '#app'
22
import type { StorageLike } from '../types'
33
import { useCookie, useRuntimeConfig } from '#app'
44

5-
type CookiesStorageOptions = PublicRuntimeConfig['piniaPluginPersistedstate']['cookieOptions']
5+
export type CookiesStorageOptions = Omit<
6+
CookieOptions,
7+
'default' | 'watch' | 'readonly' | 'filter'
8+
>
69

710
/**
811
* Cookie-based storage. Cookie options can be passed as parameter.
@@ -14,15 +17,15 @@ function cookies(options?: CookiesStorageOptions): StorageLike {
1417
key,
1518
{
1619
...(options ?? useRuntimeConfig().public.piniaPluginPersistedstate.cookieOptions ?? {}),
17-
decode: decodeURIComponent,
20+
decode: options?.decode ?? decodeURIComponent,
1821
readonly: true,
1922
},
2023
).value,
2124
setItem: (key, value) => useCookie<string>(
2225
key,
2326
{
2427
...(options ?? useRuntimeConfig().public.piniaPluginPersistedstate.cookieOptions ?? {}),
25-
encode: encodeURIComponent,
28+
encode: options?.encode ?? encodeURIComponent,
2629
},
2730
).value = value,
2831
}

0 commit comments

Comments
 (0)