From b42d13163039c93a555902aad4ef96966d79c74f Mon Sep 17 00:00:00 2001 From: Prazdevs Date: Sun, 1 Sep 2024 02:11:18 +0200 Subject: [PATCH] feat: setup global options --- src/index.ts | 17 +++++++++++++---- src/module.ts | 20 ++++++++++++++++++-- src/runtime/plugin.ts | 15 ++++++++++----- src/runtime/storages.ts | 2 +- src/types.ts | 2 +- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index a7940c2..a49ac3f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,16 @@ import { createPersistence } from './runtime/core' import type { PersistenceOptions, Serializer, StorageLike } from './types' export type { PersistenceOptions, Serializer, StorageLike } -export type PluginOptions = Pick + +/** + * Options passed to `createPersistedState` to apply globally. + */ +export type PluginOptions = Pick< + PersistenceOptions, + 'storage' | 'debug' | 'serializer' +> & { + key?: (k: string) => string +} /** * Create a Pinia persistence plugin. @@ -13,9 +22,9 @@ export type PluginOptions = Pick export function createPersistedState(options: PluginOptions = {}) { return function (context: PiniaPluginContext) { createPersistence(context, p => ({ - key: p.key ?? context.store.$id, - debug: p.debug, - serializer: p.serializer ?? { + 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), }, diff --git a/src/module.ts b/src/module.ts index 9ea37ed..ab79d2f 100644 --- a/src/module.ts +++ b/src/module.ts @@ -6,8 +6,14 @@ import { hasNuxtModule, useLogger, } from '@nuxt/kit' +import { defu } from 'defu' +import type { CookiesStorageOptions } from './runtime/storages' +import type { PersistenceOptions } from './types' -interface ModuleOptions {} +type ModuleOptions = Pick & { + storage?: 'cookies' | 'localStorage' | 'sessionStorage' + cookieOptions?: CookiesStorageOptions +} export default defineNuxtModule({ meta: { @@ -18,7 +24,7 @@ export default defineNuxtModule({ }, }, defaults: {}, - setup(_options, nuxt) { + setup(options, nuxt) { const resolver = createResolver(import.meta.url) const logger = useLogger() @@ -29,11 +35,21 @@ export default defineNuxtModule({ nuxt.options.build.transpile.push(resolver.resolve('./runtime')) + nuxt.options.runtimeConfig.public.piniaPluginPersistedstate + = defu(nuxt.options.runtimeConfig.public.piniaPluginPersistedstate, options) + addImports({ name: 'storages', from: resolver.resolve('./runtime/storages'), as: 'piniaPluginPersistedstate', }) + addPlugin(resolver.resolve('./runtime/plugin')) }, }) + +declare module 'nuxt/schema' { + interface PublicRuntimeConfig { + piniaPluginPersistedstate: ModuleOptions + } +} diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index e304269..70c852c 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -1,28 +1,33 @@ import { destr } from 'destr' import type { Pinia, PiniaPluginContext } from 'pinia' -import type { Persistence } from '../types' import { createPersistence } from './core' import { storages } from './storages' -import { defineNuxtPlugin, useNuxtApp } from '#app' +import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from '#app' function piniaPlugin(context: PiniaPluginContext) { const nuxtApp = useNuxtApp() + const config = useRuntimeConfig() + const options = config.public.piniaPluginPersistedstate createPersistence(context, p => ({ key: p.key ?? context.store.$id, - debug: p.debug, + debug: p.debug ?? options.debug ?? false, serializer: p.serializer ?? { serialize: data => JSON.stringify(data), deserialize: data => destr(data), }, - storage: p.storage ?? storages.cookies(), + storage: p.storage ?? (options.storage + ? options.storage === 'cookies' + ? storages.cookies(options.cookieOptions) + : storages[options.storage]() + : storages.cookies()), beforeHydrate: p.beforeHydrate, afterHydrate: p.afterHydrate, beforePersist: p.beforePersist, afterPersist: p.afterPersist, pick: p.pick, omit: p.omit, - } satisfies Persistence), nuxtApp.runWithContext) + }), nuxtApp.runWithContext) } export default defineNuxtPlugin(({ $pinia }) => { diff --git a/src/runtime/storages.ts b/src/runtime/storages.ts index e196c12..bdbc65a 100644 --- a/src/runtime/storages.ts +++ b/src/runtime/storages.ts @@ -2,7 +2,7 @@ import type { StorageLike } from '../types' import type { CookieOptions } from '#app' import { useCookie } from '#app' -type CookiesStorageOptions = Omit< +export type CookiesStorageOptions = Omit< CookieOptions, 'encode' | 'decode' | 'default' | 'watch' | 'readonly' | 'filter' > diff --git a/src/types.ts b/src/types.ts index ff73da9..8c2d50f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -47,7 +47,7 @@ export interface Persistence { * Log errors in console. * @default false */ - debug?: boolean + debug: boolean /** * Synchronous storage to persist the state.