‘首次‘
This commit is contained in:
+4
@@ -0,0 +1,4 @@
|
||||
# Changelog
|
||||
|
||||
This project adheres to [Semantic Versioning](http://semver.org/).
|
||||
Every release is documented on the Github [Releases](https://github.com/robinvdvleuten/vuex-persistedstate/releases) page.
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Robin van der Vleuten <robin@webstronauts.co>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+326
@@ -0,0 +1,326 @@
|
||||
# vuex-persistedstate
|
||||
|
||||
Persist and rehydrate your [Vuex](http://vuex.vuejs.org/) state between page reloads.
|
||||
|
||||
<hr />
|
||||
|
||||
[](https://github.com/robinvdvleuten/vuex-persistedstate/actions?query=workflow%3Atest)
|
||||
[](https://www.npmjs.com/package/vuex-persistedstate)
|
||||
[](https://www.npmjs.com/package/vuex-persistedstate)
|
||||
[](https://github.com/prettier/prettier)
|
||||
[](https://github.com/robinvdvleuten/vuex-persistedstate/blob/master/LICENSE)
|
||||
|
||||
[](http://makeapullrequest.com)
|
||||
[](https://github.com/robinvdvleuten/vuex-persistedstate/blob/master/.github/CODE_OF_CONDUCT.md)
|
||||
|
||||
<a href="https://webstronauts.com/">
|
||||
<img src="https://webstronauts.com/badges/sponsored-by-webstronauts.svg" alt="Sponsored by The Webstronauts" width="200" height="65">
|
||||
</a>
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install --save vuex-persistedstate
|
||||
```
|
||||
|
||||
The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/vuex-persistedstate/dist/vuex-persistedstate.umd.js"></script>
|
||||
```
|
||||
|
||||
You can find the library on `window.createPersistedState`.
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import { createStore } from "vuex";
|
||||
import createPersistedState from "vuex-persistedstate";
|
||||
|
||||
const store = createStore({
|
||||
// ...
|
||||
plugins: [createPersistedState()],
|
||||
});
|
||||
```
|
||||
|
||||
For usage with for Vuex 3 and Vue 2, please see [3.x.x branch](https://github.com/robinvdvleuten/vuex-persistedstate/tree/3.x.x).
|
||||
|
||||
## Examples
|
||||
|
||||
Check out a basic example on [CodeSandbox](https://codesandbox.io).
|
||||
|
||||
[](https://codesandbox.io/s/80k4m2598)
|
||||
|
||||
Or configured to use with [js-cookie](https://github.com/js-cookie/js-cookie).
|
||||
|
||||
[](https://codesandbox.io/s/xl356qvvkz)
|
||||
|
||||
Or configured to use with [secure-ls](https://github.com/softvar/secure-ls)
|
||||
|
||||
[](https://codesandbox.io/s/vuex-persistedstate-with-secure-ls-encrypted-data-7l9wb?fontsize=14)
|
||||
|
||||
### Example with Vuex modules
|
||||
|
||||
New plugin instances can be created in separate files, but must be imported and added to plugins object in the main Vuex file.
|
||||
|
||||
```js
|
||||
/* module.js */
|
||||
export const dataStore = {
|
||||
state: {
|
||||
data: []
|
||||
}
|
||||
}
|
||||
|
||||
/* store.js */
|
||||
import { dataStore } from './module'
|
||||
|
||||
const dataState = createPersistedState({
|
||||
paths: ['data']
|
||||
})
|
||||
|
||||
export new Vuex.Store({
|
||||
modules: {
|
||||
dataStore
|
||||
},
|
||||
plugins: [dataState]
|
||||
})
|
||||
```
|
||||
|
||||
### Example with Nuxt.js
|
||||
|
||||
It is possible to use vuex-persistedstate with Nuxt.js. It must be included as a NuxtJS plugin:
|
||||
|
||||
#### With local storage (client-side only)
|
||||
|
||||
```javascript
|
||||
// nuxt.config.js
|
||||
|
||||
...
|
||||
/*
|
||||
* Naming your plugin 'xxx.client.js' will make it execute only on the client-side.
|
||||
* https://nuxtjs.org/guide/plugins/#name-conventional-plugin
|
||||
*/
|
||||
plugins: [{ src: '~/plugins/persistedState.client.js' }]
|
||||
...
|
||||
```
|
||||
|
||||
```javascript
|
||||
// ~/plugins/persistedState.client.js
|
||||
|
||||
import createPersistedState from 'vuex-persistedstate'
|
||||
|
||||
export default ({store}) => {
|
||||
createPersistedState({
|
||||
key: 'yourkey',
|
||||
paths: [...]
|
||||
...
|
||||
})(store)
|
||||
}
|
||||
```
|
||||
|
||||
#### Using cookies (universal client + server-side)
|
||||
|
||||
Add `cookie` and `js-cookie`:
|
||||
|
||||
`npm install --save cookie js-cookie`
|
||||
or `yarn add cookie js-cookie`
|
||||
|
||||
```javascript
|
||||
// nuxt.config.js
|
||||
...
|
||||
plugins: [{ src: '~/plugins/persistedState.js'}]
|
||||
...
|
||||
```
|
||||
|
||||
```javascript
|
||||
// ~/plugins/persistedState.js
|
||||
|
||||
import createPersistedState from 'vuex-persistedstate';
|
||||
import * as Cookies from 'js-cookie';
|
||||
import cookie from 'cookie';
|
||||
|
||||
export default ({ store, req }) => {
|
||||
createPersistedState({
|
||||
paths: [...],
|
||||
storage: {
|
||||
getItem: (key) => {
|
||||
// See https://nuxtjs.org/guide/plugins/#using-process-flags
|
||||
if (process.server) {
|
||||
const parsedCookies = cookie.parse(req.headers.cookie);
|
||||
return parsedCookies[key];
|
||||
} else {
|
||||
return Cookies.get(key);
|
||||
}
|
||||
},
|
||||
// Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON.
|
||||
setItem: (key, value) =>
|
||||
Cookies.set(key, value, { expires: 365, secure: false }),
|
||||
removeItem: key => Cookies.remove(key)
|
||||
}
|
||||
})(store);
|
||||
};
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `createPersistedState([options])`
|
||||
|
||||
Creates a new instance of the plugin with the given options. The following options
|
||||
can be provided to configure the plugin for your specific needs:
|
||||
|
||||
- `key <String>`: The key to store the persisted state under. Defaults to `vuex`.
|
||||
- `paths <Array>`: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. If an empty array is given, no state is persisted. Paths must be specified using dot notation. If using modules, include the module name. eg: "auth.user" Defaults to `undefined`.
|
||||
- `reducer <Function>`: A function that will be called to reduce the state to persist based on the given paths. Defaults to include the values.
|
||||
- `subscriber <Function>`: A function called to setup mutation subscription. Defaults to `store => handler => store.subscribe(handler)`.
|
||||
|
||||
- `storage <Object>`: Instead of (or in combination with) `getState` and `setState`. Defaults to localStorage.
|
||||
- `getState <Function>`: A function that will be called to rehydrate a previously persisted state. Defaults to using `storage`.
|
||||
- `setState <Function>`: A function that will be called to persist the given state. Defaults to using `storage`.
|
||||
- `filter <Function>`: A function that will be called to filter any mutations which will trigger `setState` on storage eventually. Defaults to `() => true`.
|
||||
- `overwrite <Boolean>`: When rehydrating, whether to overwrite the existing state with the output from `getState` directly, instead of merging the two objects with `deepmerge`. Defaults to `false`.
|
||||
- `arrayMerger <Function>`: A function for merging arrays when rehydrating state. Defaults to `function (store, saved) { return saved }` (saved state replaces supplied state).
|
||||
- `rehydrated <Function>`: A function that will be called when the rehydration is finished. Useful when you are using Nuxt.js, which the rehydration of the persisted state happens asynchronously. Defaults to `store => {}`
|
||||
- `fetchBeforeUse <Boolean>`: A boolean indicating if the state should be fetched from storage before the plugin is used. Defaults to `false`.
|
||||
- `assertStorage <Function>`: An overridable function to ensure storage is available, fired on plugins's initialization. Default one is performing a Write-Delete operation on the given Storage instance. Note, default behaviour could throw an error (like `DOMException: QuotaExceededError`).
|
||||
|
||||
## Customize Storage
|
||||
|
||||
If it's not ideal to have the state of the Vuex store inside localstorage. One can easily implement the functionality to use [cookies](https://github.com/js-cookie/js-cookie) for that (or any other you can think of);
|
||||
|
||||
[](https://codesandbox.io/s/xl356qvvkz?autoresize=1)
|
||||
|
||||
```js
|
||||
import { Store } from "vuex";
|
||||
import createPersistedState from "vuex-persistedstate";
|
||||
import * as Cookies from "js-cookie";
|
||||
|
||||
const store = new Store({
|
||||
// ...
|
||||
plugins: [
|
||||
createPersistedState({
|
||||
storage: {
|
||||
getItem: (key) => Cookies.get(key),
|
||||
// Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON.
|
||||
setItem: (key, value) =>
|
||||
Cookies.set(key, value, { expires: 3, secure: true }),
|
||||
removeItem: (key) => Cookies.remove(key),
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
In fact, any object following the Storage protocol (getItem, setItem, removeItem, etc) could be passed:
|
||||
|
||||
```js
|
||||
createPersistedState({ storage: window.sessionStorage });
|
||||
```
|
||||
|
||||
This is especially useful when you are using this plugin in combination with server-side rendering, where one could pass an instance of [dom-storage](https://www.npmjs.com/package/dom-storage).
|
||||
|
||||
### 🔐Obfuscate Local Storage
|
||||
|
||||
If you need to use **Local Storage** (or you want to) but want to prevent attackers from easily inspecting the stored data, you can [obfuscate it]('https://github.com/softvar/secure-ls').
|
||||
|
||||
**Important ⚠️** Obfuscating the Vuex store means to prevent attackers from easily gaining access to the data. This is not a secure way of storing sensitive data (like passwords, personal information, etc.), and always needs to be used in conjunction with some other authentication method of keeping the data (such as Firebase or your own server).
|
||||
|
||||
[](https://codesandbox.io/s/vuex-persistedstate-with-secure-ls-encrypted-data-7l9wb?fontsize=14)
|
||||
|
||||
```js
|
||||
import { Store } from "vuex";
|
||||
import createPersistedState from "vuex-persistedstate";
|
||||
import SecureLS from "secure-ls";
|
||||
var ls = new SecureLS({ isCompression: false });
|
||||
|
||||
// https://github.com/softvar/secure-ls
|
||||
|
||||
const store = new Store({
|
||||
// ...
|
||||
plugins: [
|
||||
createPersistedState({
|
||||
storage: {
|
||||
getItem: (key) => ls.get(key),
|
||||
setItem: (key, value) => ls.set(key, value),
|
||||
removeItem: (key) => ls.remove(key),
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
### ⚠️ LocalForage ⚠️
|
||||
|
||||
As it maybe seems at first sight, it's not possible to pass a [LocalForage](https://github.com/localForage/localForage) instance as `storage` property. This is due the fact that all getters and setters must be synchronous and [LocalForage's methods](https://github.com/localForage/localForage#callbacks-vs-promises) are asynchronous.
|
||||
|
||||
## Changelog
|
||||
|
||||
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
|
||||
|
||||
## Contributors ✨
|
||||
|
||||
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
||||
|
||||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
||||
<!-- prettier-ignore-start -->
|
||||
<!-- markdownlint-disable -->
|
||||
<table>
|
||||
<tr>
|
||||
<td align="center"><a href="https://robinvdvleuten.nl"><img src="https://avatars3.githubusercontent.com/u/238295?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Robin van der Vleuten</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=robinvdvleuten" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=robinvdvleuten" title="Documentation">📖</a> <a href="#infra-robinvdvleuten" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=robinvdvleuten" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="https://github.com/zweizeichen"><img src="https://avatars1.githubusercontent.com/u/654071?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sebastian</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=zweizeichen" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=zweizeichen" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/boris-graeff"><img src="https://avatars1.githubusercontent.com/u/3204379?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Boris Graeff</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=boris-graeff" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://ciceropablo.github.io"><img src="https://avatars3.githubusercontent.com/u/174275?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Cícero Pablo</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=ciceropablo" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://gatwal.com"><img src="https://avatars1.githubusercontent.com/u/7547554?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gurpreet Atwal</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=gurpreetatwal" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="https://jcubed.me"><img src="https://avatars0.githubusercontent.com/u/43069023?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jakub Koralewski</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=JakubKoralewski" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://jankeesvw.com"><img src="https://avatars0.githubusercontent.com/u/167882?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jankees van Woezik</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=jankeesvw" title="Documentation">📖</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://randomcodetips.com"><img src="https://avatars2.githubusercontent.com/u/8638243?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jofferson Ramirez Tiquez</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=jofftiquez" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/DevoidCoding"><img src="https://avatars1.githubusercontent.com/u/21159634?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jordan Deprez</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=DevoidCoding" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/juanvillegas"><img src="https://avatars3.githubusercontent.com/u/773149?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Juan Villegas</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=juanvillegas" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="http://jrast.ch"><img src="https://avatars3.githubusercontent.com/u/146369?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jürg Rast</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=jrast" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/antixrist"><img src="https://avatars3.githubusercontent.com/u/2387592?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kartashov Alexey</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=antixrist" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://twitter.com/LeonardPauli"><img src="https://avatars0.githubusercontent.com/u/1329834?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Leonard Pauli</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=leonardpauli" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=leonardpauli" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/nelsliu9121"><img src="https://avatars2.githubusercontent.com/u/1268682?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nelson Liu</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=nelsliu9121" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=nelsliu9121" title="Documentation">📖</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=nelsliu9121" title="Tests">⚠️</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/NLNicoo"><img src="https://avatars2.githubusercontent.com/u/6526666?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nico</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=NLNicoo" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=NLNicoo" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="https://www.qkdreyer.dev"><img src="https://avatars3.githubusercontent.com/u/717869?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Quentin Dreyer</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=qkdreyer" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://raphaelsaunier.com"><img src="https://avatars2.githubusercontent.com/u/170256?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Raphael Saunier</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=raphaelsaunier" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://rodneyrehm.de"><img src="https://avatars3.githubusercontent.com/u/186837?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Rodney Rehm</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=rodneyrehm" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=rodneyrehm" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="http://wongyouth.github.io"><img src="https://avatars1.githubusercontent.com/u/944583?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ryan Wang</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=wongyouth" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=wongyouth" title="Documentation">📖</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=wongyouth" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="https://atinux.com"><img src="https://avatars2.githubusercontent.com/u/904724?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Sébastien Chopin</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=Atinux" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/zgayjjf"><img src="https://avatars1.githubusercontent.com/u/24718872?v=4?s=100" width="100px;" alt=""/><br /><sub><b>jeffjing</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=zgayjjf" title="Code">💻</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/macarthuror"><img src="https://avatars0.githubusercontent.com/u/24395219?v=4?s=100" width="100px;" alt=""/><br /><sub><b>macarthuror</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=macarthuror" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/gangsthub"><img src="https://avatars2.githubusercontent.com/u/6775220?s=460&v=4?s=100" width="100px;" alt=""/><br /><sub><b>Paul Melero</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=gangsthub" title="Documentation">📖</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=gangsthub" title="Code">💻</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=gangsthub" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="https://github.com/WTDuck"><img src="https://avatars0.githubusercontent.com/u/16686729?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Guillaume da Silva</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=WTDuck" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/SanterreJo"><img src="https://avatars2.githubusercontent.com/u/6465769?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jonathan Santerre</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=SanterreJo" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://www.linkedin.com/in/fabiofdsantos/"><img src="https://avatars3.githubusercontent.com/u/8303937?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Fábio Santos</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=fabiofdsantos" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/robertgr991"><img src="https://avatars0.githubusercontent.com/u/36689800?v=4?s=100" width="100px;" alt=""/><br /><sub><b>robertgr991</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=robertgr991" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://github.com/YuraKolesnikov"><img src="https://avatars3.githubusercontent.com/u/28485518?v=4?s=100" width="100px;" alt=""/><br /><sub><b>JurijsKolesnikovs</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=YuraKolesnikov" title="Documentation">📖</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://davidsbond.github.io"><img src="https://avatars3.githubusercontent.com/u/6227720?v=4?s=100" width="100px;" alt=""/><br /><sub><b>David Bond</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=davidsbond" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="http://www.freekvanrijt.nl"><img src="https://avatars1.githubusercontent.com/u/417416?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Freek van Rijt</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=FreekVR" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://github.com/yachaka"><img src="https://avatars2.githubusercontent.com/u/8074336?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ilyes Hermellin</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=yachaka" title="Code">💻</a></td>
|
||||
<td align="center"><a href="http://www.inventage.com"><img src="https://avatars1.githubusercontent.com/u/63866?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Peter Siska</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=peschee" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="http://adm1t.github.io"><img src="https://avatars2.githubusercontent.com/u/26100455?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dmitry Filippov</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=adm1t" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://retailify.de"><img src="https://avatars0.githubusercontent.com/u/5236353?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Thomas Meitz</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=retailify" title="Documentation">📖</a> <a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=retailify" title="Tests">⚠️</a></td>
|
||||
<td align="center"><a href="http://neeron.me"><img src="https://avatars.githubusercontent.com/u/33238007?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Neeron Bhatta</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=NeuronButter" title="Documentation">📖</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/joaoaraujo-hotmart"><img src="https://avatars.githubusercontent.com/u/15874735?v=4?s=100" width="100px;" alt=""/><br /><sub><b>joaoaraujo-hotmart</b></sub></a><br /><a href="https://github.com/robinvdvleuten/vuex-persistedstate/commits?author=joaoaraujo-hotmart" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<!-- markdownlint-restore -->
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
||||
|
||||
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
||||
|
||||
## License
|
||||
|
||||
The MIT License (MIT). Please see [License File](LICENSE) for more information.
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
import { Store, MutationPayload } from "vuex";
|
||||
interface Storage {
|
||||
getItem: (key: string) => any;
|
||||
setItem: (key: string, value: any) => void;
|
||||
removeItem: (key: string) => void;
|
||||
}
|
||||
interface Options<State> {
|
||||
key?: string;
|
||||
paths?: string[];
|
||||
reducer?: (state: State, paths: string[]) => object;
|
||||
subscriber?: (store: Store<State>) => (handler: (mutation: any, state: State) => void) => void;
|
||||
storage?: Storage;
|
||||
getState?: (key: string, storage: Storage) => any;
|
||||
setState?: (key: string, state: any, storage: Storage) => void;
|
||||
filter?: (mutation: MutationPayload) => boolean;
|
||||
arrayMerger?: (state: any[], saved: any[]) => any;
|
||||
rehydrated?: (store: Store<State>) => void;
|
||||
fetchBeforeUse?: boolean;
|
||||
overwrite?: boolean;
|
||||
assertStorage?: (storage: Storage) => void | Error;
|
||||
}
|
||||
export default function <State>(options?: Options<State>): (store: Store<State>) => void;
|
||||
export {};
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
var r=function(r){return function(r){return!!r&&"object"==typeof r}(r)&&!function(r){var t=Object.prototype.toString.call(r);return"[object RegExp]"===t||"[object Date]"===t||function(r){return r.$$typeof===e}(r)}(r)},e="function"==typeof Symbol&&Symbol.for?Symbol.for("react.element"):60103;function t(r,e){return!1!==e.clone&&e.isMergeableObject(r)?u(Array.isArray(r)?[]:{},r,e):r}function n(r,e,n){return r.concat(e).map(function(r){return t(r,n)})}function o(r){return Object.keys(r).concat(function(r){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(r).filter(function(e){return r.propertyIsEnumerable(e)}):[]}(r))}function c(r,e){try{return e in r}catch(r){return!1}}function u(e,i,a){(a=a||{}).arrayMerge=a.arrayMerge||n,a.isMergeableObject=a.isMergeableObject||r,a.cloneUnlessOtherwiseSpecified=t;var f=Array.isArray(i);return f===Array.isArray(e)?f?a.arrayMerge(e,i,a):function(r,e,n){var i={};return n.isMergeableObject(r)&&o(r).forEach(function(e){i[e]=t(r[e],n)}),o(e).forEach(function(o){(function(r,e){return c(r,e)&&!(Object.hasOwnProperty.call(r,e)&&Object.propertyIsEnumerable.call(r,e))})(r,o)||(i[o]=c(r,o)&&n.isMergeableObject(e[o])?function(r,e){if(!e.customMerge)return u;var t=e.customMerge(r);return"function"==typeof t?t:u}(o,n)(r[o],e[o],n):t(e[o],n))}),i}(e,i,a):t(i,a)}u.all=function(r,e){if(!Array.isArray(r))throw new Error("first argument should be an array");return r.reduce(function(r,t){return u(r,t,e)},{})};var i=u;function a(r){var e=(r=r||{}).storage||window&&window.localStorage,t=r.key||"vuex";function n(r,e){var t=e.getItem(r);try{return"string"==typeof t?JSON.parse(t):"object"==typeof t?t:void 0}catch(r){}}function o(){return!0}function c(r,e,t){return t.setItem(r,JSON.stringify(e))}function u(r,e){return Array.isArray(e)?e.reduce(function(e,t){return function(r,e,t,n){return!/^(__proto__|constructor|prototype)$/.test(e)&&((e=e.split?e.split("."):e.slice(0)).slice(0,-1).reduce(function(r,e){return r[e]=r[e]||{}},r)[e.pop()]=t),r}(e,t,(n=r,void 0===(n=((o=t).split?o.split("."):o).reduce(function(r,e){return r&&r[e]},n))?void 0:n));var n,o},{}):r}function a(r){return function(e){return r.subscribe(e)}}(r.assertStorage||function(){e.setItem("@@",1),e.removeItem("@@")})(e);var f,s=function(){return(r.getState||n)(t,e)};return r.fetchBeforeUse&&(f=s()),function(n){r.fetchBeforeUse||(f=s()),"object"==typeof f&&null!==f&&(n.replaceState(r.overwrite?f:i(n.state,f,{arrayMerge:r.arrayMerger||function(r,e){return e},clone:!1})),(r.rehydrated||function(){})(n)),(r.subscriber||a)(n)(function(n,i){(r.filter||o)(n)&&(r.setState||c)(t,(r.reducer||u)(i,r.paths),e)})}}export default a;
|
||||
//# sourceMappingURL=vuex-persistedstate.es.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+2
@@ -0,0 +1,2 @@
|
||||
var r=function(r){return function(r){return!!r&&"object"==typeof r}(r)&&!function(r){var t=Object.prototype.toString.call(r);return"[object RegExp]"===t||"[object Date]"===t||function(r){return r.$$typeof===e}(r)}(r)},e="function"==typeof Symbol&&Symbol.for?Symbol.for("react.element"):60103;function t(r,e){return!1!==e.clone&&e.isMergeableObject(r)?u(Array.isArray(r)?[]:{},r,e):r}function n(r,e,n){return r.concat(e).map(function(r){return t(r,n)})}function o(r){return Object.keys(r).concat(function(r){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(r).filter(function(e){return r.propertyIsEnumerable(e)}):[]}(r))}function c(r,e){try{return e in r}catch(r){return!1}}function u(e,i,a){(a=a||{}).arrayMerge=a.arrayMerge||n,a.isMergeableObject=a.isMergeableObject||r,a.cloneUnlessOtherwiseSpecified=t;var f=Array.isArray(i);return f===Array.isArray(e)?f?a.arrayMerge(e,i,a):function(r,e,n){var i={};return n.isMergeableObject(r)&&o(r).forEach(function(e){i[e]=t(r[e],n)}),o(e).forEach(function(o){(function(r,e){return c(r,e)&&!(Object.hasOwnProperty.call(r,e)&&Object.propertyIsEnumerable.call(r,e))})(r,o)||(i[o]=c(r,o)&&n.isMergeableObject(e[o])?function(r,e){if(!e.customMerge)return u;var t=e.customMerge(r);return"function"==typeof t?t:u}(o,n)(r[o],e[o],n):t(e[o],n))}),i}(e,i,a):t(i,a)}u.all=function(r,e){if(!Array.isArray(r))throw new Error("first argument should be an array");return r.reduce(function(r,t){return u(r,t,e)},{})};var i=u;module.exports=function(r){var e=(r=r||{}).storage||window&&window.localStorage,t=r.key||"vuex";function n(r,e){var t=e.getItem(r);try{return"string"==typeof t?JSON.parse(t):"object"==typeof t?t:void 0}catch(r){}}function o(){return!0}function c(r,e,t){return t.setItem(r,JSON.stringify(e))}function u(r,e){return Array.isArray(e)?e.reduce(function(e,t){return function(r,e,t,n){return!/^(__proto__|constructor|prototype)$/.test(e)&&((e=e.split?e.split("."):e.slice(0)).slice(0,-1).reduce(function(r,e){return r[e]=r[e]||{}},r)[e.pop()]=t),r}(e,t,(n=r,void 0===(n=((o=t).split?o.split("."):o).reduce(function(r,e){return r&&r[e]},n))?void 0:n));var n,o},{}):r}function a(r){return function(e){return r.subscribe(e)}}(r.assertStorage||function(){e.setItem("@@",1),e.removeItem("@@")})(e);var f,s=function(){return(r.getState||n)(t,e)};return r.fetchBeforeUse&&(f=s()),function(n){r.fetchBeforeUse||(f=s()),"object"==typeof f&&null!==f&&(n.replaceState(r.overwrite?f:i(n.state,f,{arrayMerge:r.arrayMerger||function(r,e){return e},clone:!1})),(r.rehydrated||function(){})(n)),(r.subscriber||a)(n)(function(n,i){(r.filter||o)(n)&&(r.setState||c)(t,(r.reducer||u)(i,r.paths),e)})}};
|
||||
//# sourceMappingURL=vuex-persistedstate.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+2
@@ -0,0 +1,2 @@
|
||||
var r=function(r){return function(r){return!!r&&"object"==typeof r}(r)&&!function(r){var t=Object.prototype.toString.call(r);return"[object RegExp]"===t||"[object Date]"===t||function(r){return r.$$typeof===e}(r)}(r)},e="function"==typeof Symbol&&Symbol.for?Symbol.for("react.element"):60103;function t(r,e){return!1!==e.clone&&e.isMergeableObject(r)?u(Array.isArray(r)?[]:{},r,e):r}function n(r,e,n){return r.concat(e).map(function(r){return t(r,n)})}function o(r){return Object.keys(r).concat(function(r){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(r).filter(function(e){return r.propertyIsEnumerable(e)}):[]}(r))}function c(r,e){try{return e in r}catch(r){return!1}}function u(e,i,a){(a=a||{}).arrayMerge=a.arrayMerge||n,a.isMergeableObject=a.isMergeableObject||r,a.cloneUnlessOtherwiseSpecified=t;var f=Array.isArray(i);return f===Array.isArray(e)?f?a.arrayMerge(e,i,a):function(r,e,n){var i={};return n.isMergeableObject(r)&&o(r).forEach(function(e){i[e]=t(r[e],n)}),o(e).forEach(function(o){(function(r,e){return c(r,e)&&!(Object.hasOwnProperty.call(r,e)&&Object.propertyIsEnumerable.call(r,e))})(r,o)||(i[o]=c(r,o)&&n.isMergeableObject(e[o])?function(r,e){if(!e.customMerge)return u;var t=e.customMerge(r);return"function"==typeof t?t:u}(o,n)(r[o],e[o],n):t(e[o],n))}),i}(e,i,a):t(i,a)}u.all=function(r,e){if(!Array.isArray(r))throw new Error("first argument should be an array");return r.reduce(function(r,t){return u(r,t,e)},{})};var i=u;function a(r){const e=(r=r||{}).storage||window&&window.localStorage,t=r.key||"vuex";function n(r,e){const t=e.getItem(r);try{return"string"==typeof t?JSON.parse(t):"object"==typeof t?t:void 0}catch(r){}}function o(){return!0}function c(r,e,t){return t.setItem(r,JSON.stringify(e))}function u(r,e){return Array.isArray(e)?e.reduce(function(e,t){return function(r,e,t,n){return!/^(__proto__|constructor|prototype)$/.test(e)&&((e=e.split?e.split("."):e.slice(0)).slice(0,-1).reduce(function(r,e){return r[e]=r[e]||{}},r)[e.pop()]=t),r}(e,t,(n=r,void 0===(n=((o=t).split?o.split("."):o).reduce(function(r,e){return r&&r[e]},n))?void 0:n));var n,o},{}):r}function a(r){return function(e){return r.subscribe(e)}}(r.assertStorage||(()=>{e.setItem("@@",1),e.removeItem("@@")}))(e);const f=()=>(r.getState||n)(t,e);let s;return r.fetchBeforeUse&&(s=f()),function(n){r.fetchBeforeUse||(s=f()),"object"==typeof s&&null!==s&&(n.replaceState(r.overwrite?s:i(n.state,s,{arrayMerge:r.arrayMerger||function(r,e){return e},clone:!1})),(r.rehydrated||function(){})(n)),(r.subscriber||a)(n)(function(n,i){(r.filter||o)(n)&&(r.setState||c)(t,(r.reducer||u)(i,r.paths),e)})}}export default a;
|
||||
//# sourceMappingURL=vuex-persistedstate.modern.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+2
@@ -0,0 +1,2 @@
|
||||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e||self).createPersistedState=r()}(this,function(){var e=function(e){return function(e){return!!e&&"object"==typeof e}(e)&&!function(e){var t=Object.prototype.toString.call(e);return"[object RegExp]"===t||"[object Date]"===t||function(e){return e.$$typeof===r}(e)}(e)},r="function"==typeof Symbol&&Symbol.for?Symbol.for("react.element"):60103;function t(e,r){return!1!==r.clone&&r.isMergeableObject(e)?u(Array.isArray(e)?[]:{},e,r):e}function n(e,r,n){return e.concat(r).map(function(e){return t(e,n)})}function o(e){return Object.keys(e).concat(function(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter(function(r){return e.propertyIsEnumerable(r)}):[]}(e))}function c(e,r){try{return r in e}catch(e){return!1}}function u(r,i,a){(a=a||{}).arrayMerge=a.arrayMerge||n,a.isMergeableObject=a.isMergeableObject||e,a.cloneUnlessOtherwiseSpecified=t;var f=Array.isArray(i);return f===Array.isArray(r)?f?a.arrayMerge(r,i,a):function(e,r,n){var i={};return n.isMergeableObject(e)&&o(e).forEach(function(r){i[r]=t(e[r],n)}),o(r).forEach(function(o){(function(e,r){return c(e,r)&&!(Object.hasOwnProperty.call(e,r)&&Object.propertyIsEnumerable.call(e,r))})(e,o)||(i[o]=c(e,o)&&n.isMergeableObject(r[o])?function(e,r){if(!r.customMerge)return u;var t=r.customMerge(e);return"function"==typeof t?t:u}(o,n)(e[o],r[o],n):t(r[o],n))}),i}(r,i,a):t(i,a)}u.all=function(e,r){if(!Array.isArray(e))throw new Error("first argument should be an array");return e.reduce(function(e,t){return u(e,t,r)},{})};var i=u;return function(e){var r=(e=e||{}).storage||window&&window.localStorage,t=e.key||"vuex";function n(e,r){var t=r.getItem(e);try{return"string"==typeof t?JSON.parse(t):"object"==typeof t?t:void 0}catch(e){}}function o(){return!0}function c(e,r,t){return t.setItem(e,JSON.stringify(r))}function u(e,r){return Array.isArray(r)?r.reduce(function(r,t){return function(e,r,t,n){return!/^(__proto__|constructor|prototype)$/.test(r)&&((r=r.split?r.split("."):r.slice(0)).slice(0,-1).reduce(function(e,r){return e[r]=e[r]||{}},e)[r.pop()]=t),e}(r,t,(n=e,void 0===(n=((o=t).split?o.split("."):o).reduce(function(e,r){return e&&e[r]},n))?void 0:n));var n,o},{}):e}function a(e){return function(r){return e.subscribe(r)}}(e.assertStorage||function(){r.setItem("@@",1),r.removeItem("@@")})(r);var f,s=function(){return(e.getState||n)(t,r)};return e.fetchBeforeUse&&(f=s()),function(n){e.fetchBeforeUse||(f=s()),"object"==typeof f&&null!==f&&(n.replaceState(e.overwrite?f:i(n.state,f,{arrayMerge:e.arrayMerger||function(e,r){return r},clone:!1})),(e.rehydrated||function(){})(n)),(e.subscriber||a)(n)(function(n,i){(e.filter||o)(n)&&(e.setState||c)(t,(e.reducer||u)(i,e.paths),r)})}}});
|
||||
//# sourceMappingURL=vuex-persistedstate.umd.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+111
@@ -0,0 +1,111 @@
|
||||
{
|
||||
"_from": "vuex-persistedstate",
|
||||
"_id": "vuex-persistedstate@4.1.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-3SkEj4NqwM69ikJdFVw6gObeB0NHyspRYMYkR/EbhR0hbvAKyR5gksVhtAfY1UYuWUOCCA0QNGwv9pOwdj+XUQ==",
|
||||
"_location": "/vuex-persistedstate",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "tag",
|
||||
"registry": true,
|
||||
"raw": "vuex-persistedstate",
|
||||
"name": "vuex-persistedstate",
|
||||
"escapedName": "vuex-persistedstate",
|
||||
"rawSpec": "",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "latest"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#USER",
|
||||
"/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/vuex-persistedstate/-/vuex-persistedstate-4.1.0.tgz",
|
||||
"_shasum": "127165f85f5b4534fb3170a5d3a8be9811bd2a53",
|
||||
"_spec": "vuex-persistedstate",
|
||||
"_where": "D:\\work\\云美\\legalAffairs-app",
|
||||
"author": {
|
||||
"name": "Robin van der Vleuten",
|
||||
"email": "robin@webstronauts.co",
|
||||
"url": "robinvdvleuten.nl"
|
||||
},
|
||||
"babel": {
|
||||
"presets": [
|
||||
"@babel/preset-env"
|
||||
]
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/robinvdvleuten/vuex-persistedstate/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"bundlesize": [
|
||||
{
|
||||
"path": "./dist/*.js",
|
||||
"threshold": "800b"
|
||||
}
|
||||
],
|
||||
"dependencies": {
|
||||
"deepmerge": "^4.2.2",
|
||||
"shvl": "^2.0.3"
|
||||
},
|
||||
"deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.",
|
||||
"description": "Persist and rehydrate your Vuex state between page reloads.",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.12.10",
|
||||
"@babel/preset-env": "^7.12.11",
|
||||
"all-contributors-cli": "^6.19.0",
|
||||
"babel-core": "^7.0.0-bridge.0",
|
||||
"babel-jest": "^27.0.2",
|
||||
"bundlesize": "^0.18.1",
|
||||
"dom-storage": "^2.0.2",
|
||||
"eslint": "^7.17.0",
|
||||
"husky": "^7.0.0",
|
||||
"jest": "^27.0.6",
|
||||
"microbundle": "^0.13.0",
|
||||
"npm-run-all": "^4.1.2",
|
||||
"prettier": "^2.2.1",
|
||||
"pretty-quick": "^3.1.0",
|
||||
"rimraf": "^3.0.0",
|
||||
"vue": "^3.0.0",
|
||||
"vuex": "^4.0.0-rc"
|
||||
},
|
||||
"files": [
|
||||
"dist",
|
||||
"src"
|
||||
],
|
||||
"homepage": "https://github.com/robinvdvleuten/vuex-persistedstate#readme",
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-commit": "npm run build && pretty-quick --staged"
|
||||
}
|
||||
},
|
||||
"jest": {
|
||||
"testURL": "http://localhost/"
|
||||
},
|
||||
"keywords": [
|
||||
"vue",
|
||||
"vuex",
|
||||
"plugin"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "dist/vuex-persistedstate.js",
|
||||
"module": "dist/vuex-persistedstate.es.js",
|
||||
"name": "vuex-persistedstate",
|
||||
"peerDependencies": {
|
||||
"vuex": "^3.0 || ^4.0.0-rc"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/robinvdvleuten/vuex-persistedstate.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "rimraf dist && microbundle --external all --name createPersistedState",
|
||||
"prepare": "npm run build",
|
||||
"test": "npm-run-all test:**",
|
||||
"test:jest": "jest --env=jsdom",
|
||||
"test:size": "bundlesize"
|
||||
},
|
||||
"source": "src/index.ts",
|
||||
"types": "dist/index.d.ts",
|
||||
"unpkg": "dist/vuex-persistedstate.umd.js",
|
||||
"version": "4.1.0"
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
import { Store, MutationPayload } from "vuex";
|
||||
import merge from "deepmerge";
|
||||
import * as shvl from "shvl";
|
||||
|
||||
interface Storage {
|
||||
getItem: (key: string) => any;
|
||||
setItem: (key: string, value: any) => void;
|
||||
removeItem: (key: string) => void;
|
||||
}
|
||||
|
||||
interface Options<State> {
|
||||
key?: string;
|
||||
paths?: string[];
|
||||
reducer?: (state: State, paths: string[]) => object;
|
||||
subscriber?: (
|
||||
store: Store<State>
|
||||
) => (handler: (mutation: any, state: State) => void) => void;
|
||||
storage?: Storage;
|
||||
getState?: (key: string, storage: Storage) => any;
|
||||
setState?: (key: string, state: any, storage: Storage) => void;
|
||||
filter?: (mutation: MutationPayload) => boolean;
|
||||
arrayMerger?: (state: any[], saved: any[]) => any;
|
||||
rehydrated?: (store: Store<State>) => void;
|
||||
fetchBeforeUse?: boolean;
|
||||
overwrite?: boolean;
|
||||
assertStorage?: (storage: Storage) => void | Error;
|
||||
}
|
||||
|
||||
export default function <State>(
|
||||
options?: Options<State>
|
||||
): (store: Store<State>) => void {
|
||||
options = options || {};
|
||||
|
||||
const storage = options.storage || (window && window.localStorage);
|
||||
const key = options.key || "vuex";
|
||||
|
||||
function getState(key, storage) {
|
||||
const value = storage.getItem(key);
|
||||
|
||||
try {
|
||||
return (typeof value === "string")
|
||||
? JSON.parse(value) : (typeof value === "object")
|
||||
? value : undefined;
|
||||
} catch (err) {}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function filter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
function setState(key, state, storage) {
|
||||
return storage.setItem(key, JSON.stringify(state));
|
||||
}
|
||||
|
||||
function reducer(state, paths) {
|
||||
return Array.isArray(paths)
|
||||
? paths.reduce(function (substate, path) {
|
||||
return shvl.set(substate, path, shvl.get(state, path));
|
||||
}, {})
|
||||
: state;
|
||||
}
|
||||
|
||||
function subscriber(store) {
|
||||
return function (handler) {
|
||||
return store.subscribe(handler);
|
||||
};
|
||||
}
|
||||
|
||||
const assertStorage =
|
||||
options.assertStorage ||
|
||||
(() => {
|
||||
storage.setItem("@@", 1);
|
||||
storage.removeItem("@@");
|
||||
});
|
||||
|
||||
assertStorage(storage);
|
||||
|
||||
const fetchSavedState = () => (options.getState || getState)(key, storage);
|
||||
|
||||
let savedState;
|
||||
|
||||
if (options.fetchBeforeUse) {
|
||||
savedState = fetchSavedState();
|
||||
}
|
||||
|
||||
return function (store: Store<State>) {
|
||||
if (!options.fetchBeforeUse) {
|
||||
savedState = fetchSavedState();
|
||||
}
|
||||
|
||||
if (typeof savedState === "object" && savedState !== null) {
|
||||
store.replaceState(
|
||||
options.overwrite
|
||||
? savedState
|
||||
: merge(store.state, savedState, {
|
||||
arrayMerge:
|
||||
options.arrayMerger ||
|
||||
function (store, saved) {
|
||||
return saved;
|
||||
},
|
||||
clone: false,
|
||||
})
|
||||
);
|
||||
(options.rehydrated || function () {})(store);
|
||||
}
|
||||
|
||||
(options.subscriber || subscriber)(store)(function (mutation, state) {
|
||||
if ((options.filter || filter)(mutation)) {
|
||||
(options.setState || setState)(
|
||||
key,
|
||||
(options.reducer || reducer)(state, options.paths),
|
||||
storage
|
||||
);
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user