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

Merged
merged 2 commits into from
Oct 4, 2024
Merged
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,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