index.d.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import * as pinia from 'pinia';
  2. import { StateTree, PiniaPluginContext, PiniaPlugin } from 'pinia';
  3. type Prettify<T> = {
  4. [K in keyof T]: T[K];
  5. };
  6. type StorageLike = Pick<Storage, 'getItem' | 'setItem'>;
  7. interface Serializer {
  8. /**
  9. * Serializes state into string before storing
  10. * @default JSON.stringify
  11. */
  12. serialize: (value: StateTree) => string;
  13. /**
  14. * Deserializes string into state before hydrating
  15. * @default JSON.parse
  16. */
  17. deserialize: (value: string) => StateTree;
  18. }
  19. interface PersistedStateOptions {
  20. /**
  21. * Storage key to use.
  22. * @default $store.id
  23. */
  24. key?: string | ((id: string) => string);
  25. /**
  26. * Where to store persisted state.
  27. * @default localStorage
  28. */
  29. storage?: StorageLike;
  30. /**
  31. * Dot-notation paths to partially save state. Saves everything if undefined.
  32. * @default undefined
  33. */
  34. paths?: Array<string>;
  35. /**
  36. * Customer serializer to serialize/deserialize state.
  37. */
  38. serializer?: Serializer;
  39. /**
  40. * Hook called before state is hydrated from storage.
  41. * @default null
  42. */
  43. beforeRestore?: (context: PiniaPluginContext) => void;
  44. /**
  45. * Hook called after state is hydrated from storage.
  46. * @default undefined
  47. */
  48. afterRestore?: (context: PiniaPluginContext) => void;
  49. /**
  50. * Logs errors in console when enabled.
  51. * @default false
  52. */
  53. debug?: boolean;
  54. }
  55. type PersistedStateFactoryOptions = Prettify<Pick<PersistedStateOptions, 'storage' | 'serializer' | 'afterRestore' | 'beforeRestore' | 'debug'> & {
  56. /**
  57. * Global key generator, allows pre/postfixing store keys.
  58. * @default storeKey => storeKey
  59. */
  60. key?: (storeKey: string) => string;
  61. /**
  62. * Automatically persists all stores, opt-out individually.
  63. * @default false
  64. */
  65. auto?: boolean;
  66. }>;
  67. declare module 'pinia' {
  68. interface DefineStoreOptionsBase<S extends StateTree, Store> {
  69. /**
  70. * Persists store in storage.
  71. * @see https://prazdevs.github.io/pinia-plugin-persistedstate
  72. */
  73. persist?: boolean | PersistedStateOptions | PersistedStateOptions[];
  74. unistorage?: boolean | PersistedStateOptions | PersistedStateOptions[];
  75. }
  76. interface PiniaCustomProperties {
  77. /**
  78. * Rehydrates store from persisted state
  79. * Warning: this is for advances usecases, make sure you know what you're doing.
  80. * @see https://prazdevs.github.io/pinia-plugin-persistedstate/guide/advanced.html#forcing-the-rehydration
  81. */
  82. $hydrate: (opts?: {
  83. runHooks?: boolean;
  84. }) => void;
  85. /**
  86. * Persists store into configured storage
  87. * Warning: this is for advances usecases, make sure you know what you're doing.
  88. * @see https://prazdevs.github.io/pinia-plugin-persistedstate/guide/advanced.html#forcing-the-persistence
  89. */
  90. $persist: () => void;
  91. }
  92. }
  93. /**
  94. * Creates a pinia persistence plugin
  95. * @param factoryOptions global persistence options
  96. * @returns pinia plugin
  97. */
  98. declare function createPersistedState(factoryOptions?: PersistedStateFactoryOptions): PiniaPlugin;
  99. declare const _default: pinia.PiniaPlugin;
  100. export { PersistedStateFactoryOptions, PersistedStateOptions, Serializer, StorageLike, createPersistedState, _default as default, createUnistorage };
  101. /**
  102. * Creates a pinia persistence plugin with uniapp
  103. * @param factoryOptions global persistence options
  104. * @returns pinia plugin
  105. */
  106. declare function createUnistorage(factoryOptions?: PersistedStateFactoryOptions): PiniaPlugin;