Skip to content

Commit

Permalink
feat(nuxt): global key template option
Browse files Browse the repository at this point in the history
  • Loading branch information
prazdevs committed Sep 1, 2024
1 parent c7fd2db commit f1541ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@ import type { CookieOptions } from 'nuxt/app'
import type { PersistenceOptions } from './types'

type ModuleOptions = Pick<PersistenceOptions, 'debug'> & {
/**
* Default storage for persistence. Only accepts presets.
*/
storage?: 'cookies' | 'localStorage' | 'sessionStorage'
/**
* Global key template, allow pre/postfixing store keys.
* @example 'my-%id-persistence' will yield 'my-<store-id>-persistence'
*/
key?: `${string}%id${string}`
/**
* Options used globally by default cookie storage.
* Ignored for other storages.
*/
cookieOptions?: Omit<
CookieOptions,
'encode' | 'decode' | 'default' | 'watch' | 'readonly' | 'filter'
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ function piniaPlugin(context: PiniaPluginContext) {
const options = config.public.piniaPluginPersistedstate

createPersistence(context, p => ({
key: p.key ?? context.store.$id,
key: options.key
? options.key.replace(/%id/g, p.key ?? context.store.$id)
: (p.key ?? context.store.$id),
debug: p.debug ?? options.debug ?? false,
serializer: p.serializer ?? {
serialize: data => JSON.stringify(data),
Expand Down

0 comments on commit f1541ae

Please sign in to comment.