module.d.mts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import * as _nuxt_schema from '@nuxt/schema';
  2. import { CookiesStorageOptions } from '../dist/nuxt/runtime/storages.js';
  3. import { Path } from 'deep-pick-omit';
  4. import { StateTree, PiniaPluginContext } from 'pinia';
  5. /**
  6. * Synchronous storage based on Web Storage API.
  7. * @see https://developer.mozilla.org/en-US/docs/Web/API/Storage
  8. */
  9. interface StorageLike {
  10. /**
  11. * Get a key's value if it exists.
  12. */
  13. getItem: (key: string) => string | null;
  14. /**
  15. * Set a key with a value, or update it if it exists.
  16. */
  17. setItem: (key: string, value: string) => void;
  18. }
  19. /**
  20. * Serializer implementation to stringify/parse state.
  21. */
  22. interface Serializer {
  23. /**
  24. * Serialize state into string before storing.
  25. * @default JSON.stringify
  26. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
  27. */
  28. serialize: (data: StateTree) => string;
  29. /**
  30. * Deserializes string into state before hydrating.
  31. * @default destr
  32. * @see https://github.com/unjs/destr
  33. */
  34. deserialize: (data: string) => StateTree;
  35. }
  36. interface Persistence<State extends StateTree = StateTree> {
  37. /**
  38. * Storage key to use.
  39. * @default $store.id
  40. */
  41. key: string;
  42. /**
  43. * Log errors in console.
  44. * @default false
  45. */
  46. debug: boolean;
  47. /**
  48. * Synchronous storage to persist the state.
  49. */
  50. storage: StorageLike;
  51. /**
  52. * Serializer to serialize/deserialize state into storage.
  53. */
  54. serializer: Serializer;
  55. /**
  56. * Hook called before hydrating store.
  57. */
  58. beforeHydrate?: (context: PiniaPluginContext) => void;
  59. /**
  60. * Hook called after hydrating store.
  61. */
  62. afterHydrate?: (context: PiniaPluginContext) => void;
  63. /**
  64. * Dot-notation paths to pick from state before persisting.
  65. */
  66. pick?: Path<State>[] | string[];
  67. /**
  68. * Dot-notation paths to omit from state before persisting.
  69. */
  70. omit?: Path<State>[] | string[];
  71. }
  72. type PersistenceOptions<State extends StateTree = StateTree> = Partial<Persistence<State>>;
  73. declare module 'pinia' {
  74. interface DefineStoreOptionsBase<S extends StateTree, Store> {
  75. /**
  76. * Persist store in storage
  77. * @see https://prazdevs.github.io/pinia-plugin-persistedstate
  78. */
  79. persist?: boolean | PersistenceOptions<S> | PersistenceOptions<S>[];
  80. }
  81. interface PiniaCustomProperties {
  82. /**
  83. * Hydrate store from configured storage
  84. * Warning: this is for advances usecases, make sure you know what you're doing
  85. */
  86. $hydrate: (opts?: {
  87. runHooks?: boolean;
  88. }) => void;
  89. /**
  90. * Persist store into configured storage
  91. * Warning: this is for advances usecases, make sure you know what you're doing
  92. */
  93. $persist: () => void;
  94. }
  95. }
  96. type ModuleOptions = Pick<PersistenceOptions, 'debug'> & {
  97. /**
  98. * Default storage for persistence. Only accepts presets.
  99. */
  100. storage?: 'cookies' | 'localStorage' | 'sessionStorage';
  101. /**
  102. * Global key template, allow pre/postfixing store keys.
  103. * @example 'my-%id-persistence' will yield 'my-<store-id>-persistence'
  104. */
  105. key?: `${string}%id${string}`;
  106. /**
  107. * Options used globally by default cookie storage.
  108. * Ignored for other storages.
  109. */
  110. cookieOptions?: Omit<CookiesStorageOptions, 'encode' | 'decode'>;
  111. /**
  112. * Automatically persist all stores with global defaults, opt-out individually.
  113. */
  114. auto?: boolean;
  115. };
  116. declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
  117. declare module '@nuxt/schema' {
  118. interface PublicRuntimeConfig {
  119. piniaPluginPersistedstate: ModuleOptions;
  120. }
  121. }
  122. export { _default as default };