diff --git a/src/index.ts b/src/index.ts index d36621f..5f53665 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 } /** @@ -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, + ) } } diff --git a/src/module.ts b/src/module.ts index 86c793d..85990f9 100644 --- a/src/module.ts +++ b/src/module.ts @@ -28,6 +28,11 @@ type ModuleOptions = Pick & { CookieOptions, 'encode' | 'decode' | 'default' | 'watch' | 'readonly' | 'filter' > + + /** + * Automatically persist all stores with global defaults, opt-out individually. + */ + auto?: boolean } export default defineNuxtModule({ diff --git a/src/runtime/core.ts b/src/runtime/core.ts index c41d85d..0d78701 100644 --- a/src/runtime/core.ts +++ b/src/runtime/core.ts @@ -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 diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 70f06f4..9de6cd1 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -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 }) => {