Skip to content

Commit

Permalink
feat: setup global options
Browse files Browse the repository at this point in the history
  • Loading branch information
prazdevs committed Sep 1, 2024
1 parent eb00d1a commit b42d131
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
17 changes: 13 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PersistenceOptions, 'storage'>

/**
* Options passed to `createPersistedState` to apply globally.
*/
export type PluginOptions = Pick<
PersistenceOptions,
'storage' | 'debug' | 'serializer'
> & {
key?: (k: string) => string
}

/**
* Create a Pinia persistence plugin.
Expand All @@ -13,9 +22,9 @@ export type PluginOptions = Pick<PersistenceOptions, 'storage'>
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),
},
Expand Down
20 changes: 18 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PersistenceOptions, 'debug'> & {
storage?: 'cookies' | 'localStorage' | 'sessionStorage'
cookieOptions?: CookiesStorageOptions
}

export default defineNuxtModule<ModuleOptions>({
meta: {
Expand All @@ -18,7 +24,7 @@ export default defineNuxtModule<ModuleOptions>({
},
},
defaults: {},
setup(_options, nuxt) {
setup(options, nuxt) {
const resolver = createResolver(import.meta.url)
const logger = useLogger()

Expand All @@ -29,11 +35,21 @@ export default defineNuxtModule<ModuleOptions>({

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
}
}
15 changes: 10 additions & 5 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -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 }) => {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/storages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
>
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export interface Persistence<State extends StateTree = StateTree> {
* Log errors in console.
* @default false
*/
debug?: boolean
debug: boolean

/**
* Synchronous storage to persist the state.
Expand Down

0 comments on commit b42d131

Please sign in to comment.