版博士V2.0程序
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

104 lignes
3.2 KiB

  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;
  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. }
  75. interface PiniaCustomProperties {
  76. /**
  77. * Rehydrates store from persisted state
  78. * Warning: this is for advances usecases, make sure you know what you're doing.
  79. * @see https://prazdevs.github.io/pinia-plugin-persistedstate/guide/advanced.html#forcing-the-rehydration
  80. */
  81. $hydrate: (opts?: {
  82. runHooks?: boolean;
  83. }) => void;
  84. /**
  85. * Persists store into configured storage
  86. * Warning: this is for advances usecases, make sure you know what you're doing.
  87. * @see https://prazdevs.github.io/pinia-plugin-persistedstate/guide/advanced.html#forcing-the-persistence
  88. */
  89. $persist: () => void;
  90. }
  91. }
  92. /**
  93. * Creates a pinia persistence plugin
  94. * @param factoryOptions global persistence options
  95. * @returns pinia plugin
  96. */
  97. declare function createPersistedState(factoryOptions?: PersistedStateFactoryOptions): PiniaPlugin;
  98. declare const _default: pinia.PiniaPlugin;
  99. export { PersistedStateFactoryOptions, PersistedStateOptions, Serializer, StorageLike, createPersistedState, _default as default };