Skip to content

Commit

Permalink
feat: auto mode (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
prazdevs authored Oct 4, 2024
1 parent 52676be commit a207241
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
35 changes: 22 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export type PluginOptions = Pick<
* Global key generator, allow pre/postfixing store keys.
*/
key?: (storeKey: string) => string

/**
* Automatically persist all stores with global defaults, opt-out individually.
*/
auto?: boolean
}

/**
Expand All @@ -24,19 +29,23 @@ export type PluginOptions = Pick<
*/
export function createPersistedState(options: PluginOptions = {}) {
return function (context: PiniaPluginContext) {
createPersistence(context, p => ({
key: (options.key ? options.key : (x: string) => x)(p.key ?? context.store.$id),
debug: p.debug ?? options.debug ?? false,
serializer: p.serializer ?? options.serializer ?? {
serialize: data => JSON.stringify(data),
deserialize: data => destr(data),
},
storage: p.storage ?? options.storage ?? window.localStorage,
beforeHydrate: p.beforeHydrate,
afterHydrate: p.afterHydrate,
pick: p.pick,
omit: p.omit,
}))
createPersistence(
context,
p => ({
key: (options.key ? options.key : (x: string) => x)(p.key ?? context.store.$id),
debug: p.debug ?? options.debug ?? false,
serializer: p.serializer ?? options.serializer ?? {
serialize: data => JSON.stringify(data),
deserialize: data => destr(data),
},
storage: p.storage ?? options.storage ?? window.localStorage,
beforeHydrate: p.beforeHydrate,
afterHydrate: p.afterHydrate,
pick: p.pick,
omit: p.omit,
}),
options.auto ?? false,
)
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type ModuleOptions = Pick<PersistenceOptions, 'debug'> & {
CookieOptions,
'encode' | 'decode' | 'default' | 'watch' | 'readonly' | 'filter'
>

/**
* Automatically persist all stores with global defaults, opt-out individually.
*/
auto?: boolean
}

export default defineNuxtModule<ModuleOptions>({
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ function persistState(
export function createPersistence(
context: PiniaPluginContext,
optionsParser: (p: PersistenceOptions) => Persistence,
auto: boolean,
) {
const { pinia, store, options: { persist } } = context
const { pinia, store, options: { persist = auto } } = context

if (!persist)
return
Expand Down
42 changes: 23 additions & 19 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ function piniaPlugin(context: PiniaPluginContext) {
const config = useRuntimeConfig()
const options = config.public.piniaPluginPersistedstate

createPersistence(context, p => ({
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),
deserialize: data => destr(data),
},
storage: p.storage ?? (options.storage
? options.storage === 'cookies'
? storages.cookies(options.cookieOptions)
: storages[options.storage]()
: storages.cookies()),
beforeHydrate: p.beforeHydrate,
afterHydrate: p.afterHydrate,
pick: p.pick,
omit: p.omit,
}))
createPersistence(
context,
p => ({
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),
deserialize: data => destr(data),
},
storage: p.storage ?? (options.storage
? options.storage === 'cookies'
? storages.cookies(options.cookieOptions)
: storages[options.storage]()
: storages.cookies()),
beforeHydrate: p.beforeHydrate,
afterHydrate: p.afterHydrate,
pick: p.pick,
omit: p.omit,
}),
options.auto ?? false,
)
}

export default defineNuxtPlugin(({ $pinia }) => {
Expand Down

0 comments on commit a207241

Please sign in to comment.