Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto mode #327

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 +73,10 @@ function persistState(
export function createPersistence(
context: PiniaPluginContext,
optionsParser: (p: PersistenceOptions) => Persistence,
auto: boolean,
runWithContext: (fn: () => void) => void = fn => fn(),
) {
const { pinia, store, options: { persist } } = context
const { pinia, store, options: { persist = auto } } = context

if (!persist)
return
Expand Down
43 changes: 24 additions & 19 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,30 @@ 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,
}), nuxtApp.runWithContext)
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,
nuxtApp.runWithContext,
)
}

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