@nexus-state/persist 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -19
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +39 -0
- package/dist/vitest.config.js.map +1 -0
- package/package.json +28 -11
- package/.eslintrc.js +0 -10
- package/.turbo/turbo-build.log +0 -5
- package/.turbo/turbo-lint.log +0 -5
- package/index.ts +0 -90
- package/tsconfig.json +0 -19
package/README.md
CHANGED
|
@@ -1,37 +1,62 @@
|
|
|
1
1
|
# @nexus-state/persist
|
|
2
2
|
|
|
3
|
-
Persistence for Nexus State
|
|
3
|
+
> Persistence for Nexus State — LocalStorage, sessionStorage, and custom storage adapters
|
|
4
|
+
>
|
|
5
|
+
> [](https://www.npmjs.com/package/@nexus-state/persist)
|
|
6
|
+
> [](https://www.npmjs.com/package/@nexus-state/persist)
|
|
7
|
+
> [](https://github.com/eustatos/nexus-state/blob/main/LICENSE)
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
[Documentation](https://nexus-state.website.yandexcloud.net/) • [Repository](https://github.com/eustatos/nexus-state)
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
---
|
|
8
12
|
|
|
9
|
-
##
|
|
13
|
+
## 🚀 Quick Start
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { persistAtom } from '@nexus-state/persist';
|
|
17
|
+
import { useAtomValue } from '@nexus-state/react';
|
|
18
|
+
|
|
19
|
+
// Persist atom to localStorage
|
|
20
|
+
const settingsAtom = persistAtom(
|
|
21
|
+
'settings',
|
|
22
|
+
{ theme: 'dark', language: 'en' },
|
|
23
|
+
{ storage: 'localStorage' }
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
// Value is automatically loaded from localStorage
|
|
27
|
+
// and saved on every change
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## 📦 Installation
|
|
10
33
|
|
|
11
34
|
```bash
|
|
12
35
|
npm install @nexus-state/persist
|
|
13
36
|
```
|
|
14
37
|
|
|
15
|
-
|
|
38
|
+
**Required:**
|
|
39
|
+
```bash
|
|
40
|
+
npm install @nexus-state/core
|
|
41
|
+
```
|
|
16
42
|
|
|
17
|
-
|
|
18
|
-
- Support for different persistence strategies
|
|
19
|
-
- Ability to selectively persist parts of the state
|
|
43
|
+
---
|
|
20
44
|
|
|
21
|
-
##
|
|
45
|
+
## 🔗 See Also
|
|
22
46
|
|
|
23
|
-
|
|
24
|
-
|
|
47
|
+
- **Core:** [@nexus-state/core](https://www.npmjs.com/package/@nexus-state/core) — Foundation (atoms, stores)
|
|
48
|
+
- **Framework integration:**
|
|
49
|
+
- [@nexus-state/react](https://www.npmjs.com/package/@nexus-state/react) — React hooks
|
|
50
|
+
- [@nexus-state/vue](https://www.npmjs.com/package/@nexus-state/vue) — Vue composables
|
|
51
|
+
- [@nexus-state/svelte](https://www.npmjs.com/package/@nexus-state/svelte) — Svelte stores
|
|
52
|
+
- **Related:**
|
|
53
|
+
- [@nexus-state/query](https://www.npmjs.com/package/@nexus-state/query) — Data fetching with caching
|
|
54
|
+
- [@nexus-state/form](https://www.npmjs.com/package/@nexus-state/form) — Form management
|
|
25
55
|
|
|
26
|
-
|
|
27
|
-
key: 'my-app-state',
|
|
28
|
-
storage: localStorage
|
|
29
|
-
});
|
|
56
|
+
**Full ecosystem:** [Nexus State Packages](https://www.npmjs.com/org/nexus-state)
|
|
30
57
|
|
|
31
|
-
|
|
32
|
-
store.use(persist);
|
|
33
|
-
```
|
|
58
|
+
---
|
|
34
59
|
|
|
35
|
-
## License
|
|
60
|
+
## 📄 License
|
|
36
61
|
|
|
37
62
|
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vitest.config.d.ts","sourceRoot":"","sources":["../vitest.config.ts"],"names":[],"mappings":";AAEA,wBAoCG"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { defineConfig } from 'vitest/config';
|
|
2
|
+
export default defineConfig({
|
|
3
|
+
test: {
|
|
4
|
+
globals: true,
|
|
5
|
+
environment: 'jsdom',
|
|
6
|
+
coverage: {
|
|
7
|
+
provider: 'v8',
|
|
8
|
+
reporter: ['text', 'json', 'html', 'lcov'],
|
|
9
|
+
include: ['**/*.ts'],
|
|
10
|
+
exclude: [
|
|
11
|
+
'apps/**',
|
|
12
|
+
// Тестовые файлы и директории
|
|
13
|
+
'**/__tests__/**',
|
|
14
|
+
'**/__fixtures__/**',
|
|
15
|
+
'**/__mocks__/**',
|
|
16
|
+
'**/__benchmarks__/**',
|
|
17
|
+
'**/test-utils/**',
|
|
18
|
+
'**/test-utils.ts',
|
|
19
|
+
'**/*.test.ts',
|
|
20
|
+
'**/*.spec.ts',
|
|
21
|
+
'**/*.bench.ts',
|
|
22
|
+
'**/*.test-d.ts',
|
|
23
|
+
// Файлы деклараций и конфигурации
|
|
24
|
+
'**/*.d.ts',
|
|
25
|
+
'**/*.config.ts',
|
|
26
|
+
'**/*.config.js',
|
|
27
|
+
// Скомпилированные файлы и зависимости
|
|
28
|
+
'**/node_modules/**',
|
|
29
|
+
'**/dist/**',
|
|
30
|
+
'**/build/**',
|
|
31
|
+
'**/coverage/**',
|
|
32
|
+
// Прочее
|
|
33
|
+
'**/*.md',
|
|
34
|
+
'**/*.json',
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=vitest.config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vitest.config.js","sourceRoot":"","sources":["../vitest.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAE7C,eAAe,YAAY,CAAC;IAC1B,IAAI,EAAE;QACJ,OAAO,EAAE,IAAI;QACb,WAAW,EAAE,OAAO;QACpB,QAAQ,EAAE;YACR,QAAQ,EAAE,IAAI;YACd,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC;YAC1C,OAAO,EAAE,CAAC,SAAS,CAAC;YACpB,OAAO,EAAE;gBACP,SAAS;gBACT,8BAA8B;gBAC9B,iBAAiB;gBACjB,oBAAoB;gBACpB,iBAAiB;gBACjB,sBAAsB;gBACtB,kBAAkB;gBAClB,kBAAkB;gBAClB,cAAc;gBACd,cAAc;gBACd,eAAe;gBACf,gBAAgB;gBAChB,kCAAkC;gBAClC,WAAW;gBACX,gBAAgB;gBAChB,gBAAgB;gBAChB,uCAAuC;gBACvC,oBAAoB;gBACpB,YAAY;gBACZ,aAAa;gBACb,gBAAgB;gBAChB,SAAS;gBACT,SAAS;gBACT,WAAW;aACZ;SACF;KACF;CACF,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,19 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexus-state/persist",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"main": "index.
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"require": "./dist/index.js"
|
|
11
|
+
}
|
|
10
12
|
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"README.md",
|
|
16
|
+
"CHANGELOG.md",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
11
19
|
"dependencies": {
|
|
12
|
-
"@nexus-state/core": "
|
|
20
|
+
"@nexus-state/core": "0.1.16"
|
|
13
21
|
},
|
|
14
22
|
"devDependencies": {
|
|
23
|
+
"@vitest/coverage-v8": "^3.0.7",
|
|
15
24
|
"typescript": "^5.0.0",
|
|
16
|
-
"vitest": "^0.
|
|
17
|
-
"eslint": "^8.40.0"
|
|
25
|
+
"vitest": "^3.0.7",
|
|
26
|
+
"eslint": "^8.40.0",
|
|
27
|
+
"jsdom": "^26.1.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc",
|
|
31
|
+
"dev": "tsc -w",
|
|
32
|
+
"test": "vitest run src",
|
|
33
|
+
"test:coverage": "vitest run src --coverage",
|
|
34
|
+
"lint": "eslint . --ext .ts"
|
|
18
35
|
}
|
|
19
|
-
}
|
|
36
|
+
}
|
package/.eslintrc.js
DELETED
package/.turbo/turbo-build.log
DELETED
package/.turbo/turbo-lint.log
DELETED
package/index.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
// Persist plugin for nexus-state
|
|
2
|
-
import { Atom, Store } from '@nexus-state/core';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Represents a storage interface for persisting atom values.
|
|
6
|
-
* @typedef {Object} PersistStorage
|
|
7
|
-
* @property {Function} getItem - Function to get an item from storage
|
|
8
|
-
* @property {Function} setItem - Function to set an item in storage
|
|
9
|
-
* @property {Function} removeItem - Function to remove an item from storage
|
|
10
|
-
*/
|
|
11
|
-
export type PersistStorage = {
|
|
12
|
-
getItem: (key: string) => string | null;
|
|
13
|
-
setItem: (key: string, value: string) => void;
|
|
14
|
-
removeItem: (key: string) => void;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Configuration options for the persist plugin.
|
|
19
|
-
* @typedef {Object} PersistConfig
|
|
20
|
-
* @property {string} key - The key to use for storing the atom's value
|
|
21
|
-
* @property {PersistStorage} storage - The storage implementation to use
|
|
22
|
-
* @property {Function} [serialize] - Function to serialize the value (defaults to JSON.stringify)
|
|
23
|
-
* @property {Function} [deserialize] - Function to deserialize the value (defaults to JSON.parse)
|
|
24
|
-
*/
|
|
25
|
-
export type PersistConfig<T> = {
|
|
26
|
-
key: string;
|
|
27
|
-
storage: PersistStorage;
|
|
28
|
-
serialize?: (value: T) => string;
|
|
29
|
-
deserialize?: (value: string) => T;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Plugin to persist atom values to storage.
|
|
34
|
-
* @template T - The type of the atom's value
|
|
35
|
-
* @param {Atom<T>} atom - The atom to persist
|
|
36
|
-
* @param {PersistConfig<T>} config - Configuration options for the plugin
|
|
37
|
-
* @returns {Function} A function that applies the plugin to a store
|
|
38
|
-
* @example
|
|
39
|
-
* const store = createStore([
|
|
40
|
-
* persist(countAtom, {
|
|
41
|
-
* key: 'count',
|
|
42
|
-
* storage: localStorageStorage
|
|
43
|
-
* })
|
|
44
|
-
* ]);
|
|
45
|
-
*/
|
|
46
|
-
export function persist<T>(atom: Atom<T>, config: PersistConfig<T>): (store: Store) => void {
|
|
47
|
-
return (store: Store) => {
|
|
48
|
-
const { key, storage, serialize = JSON.stringify, deserialize = JSON.parse } = config;
|
|
49
|
-
|
|
50
|
-
// Attempt to restore the value from storage
|
|
51
|
-
const savedValue = storage.getItem(key);
|
|
52
|
-
if (savedValue !== null) {
|
|
53
|
-
try {
|
|
54
|
-
const deserializedValue = deserialize(savedValue);
|
|
55
|
-
store.set(atom, deserializedValue);
|
|
56
|
-
} catch (error) {
|
|
57
|
-
console.error(`Failed to deserialize persisted value for atom ${key}:`, error);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Subscribe to atom changes to save to storage
|
|
62
|
-
store.subscribe(atom, () => {
|
|
63
|
-
const value = store.get(atom);
|
|
64
|
-
try {
|
|
65
|
-
const serializedValue = serialize(value);
|
|
66
|
-
storage.setItem(key, serializedValue);
|
|
67
|
-
} catch (error) {
|
|
68
|
-
console.error(`Failed to serialize value for atom ${key}:`, error);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Predefined localStorage storage implementation.
|
|
76
|
-
*/
|
|
77
|
-
export const localStorageStorage: PersistStorage = {
|
|
78
|
-
getItem: (key) => typeof localStorage !== 'undefined' ? localStorage.getItem(key) : null,
|
|
79
|
-
setItem: (key, value) => typeof localStorage !== 'undefined' && localStorage.setItem(key, value),
|
|
80
|
-
removeItem: (key) => typeof localStorage !== 'undefined' && localStorage.removeItem(key),
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Predefined sessionStorage storage implementation.
|
|
85
|
-
*/
|
|
86
|
-
export const sessionStorageStorage: PersistStorage = {
|
|
87
|
-
getItem: (key) => typeof sessionStorage !== 'undefined' ? sessionStorage.getItem(key) : null,
|
|
88
|
-
setItem: (key, value) => typeof sessionStorage !== 'undefined' && sessionStorage.setItem(key, value),
|
|
89
|
-
removeItem: (key) => typeof sessionStorage !== 'undefined' && sessionStorage.removeItem(key),
|
|
90
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"declarationMap": true,
|
|
8
|
-
"sourceMap": true,
|
|
9
|
-
"outDir": "./dist",
|
|
10
|
-
"rootDir": "./",
|
|
11
|
-
"strict": true,
|
|
12
|
-
"esModuleInterop": true,
|
|
13
|
-
"skipLibCheck": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true,
|
|
15
|
-
"resolveJsonModule": true
|
|
16
|
-
},
|
|
17
|
-
"include": ["**/*.ts"],
|
|
18
|
-
"exclude": ["node_modules", "dist"]
|
|
19
|
-
}
|