index.d.ts 3.6 KB

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