@ahoo-wang/fetcher-storage 1.6.6 → 1.6.8
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 +126 -0
- package/README.zh-CN.md +125 -0
- package/package.json +10 -15
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# @ahoo-wang/fetcher-storage
|
|
2
|
+
|
|
3
|
+
A lightweight, cross-environment storage library with change event listening capabilities. Provides consistent API for
|
|
4
|
+
browser localStorage/sessionStorage and in-memory storage with change notifications.
|
|
5
|
+
|
|
6
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-storage)
|
|
7
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-storage)
|
|
8
|
+
[](https://github.com/Ahoo-Wang/fetcher/blob/master/LICENSE)
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- 🌐 Cross-environment support (Browser & Node.js)
|
|
13
|
+
- 📦 Ultra-lightweight (~1KB gzip)
|
|
14
|
+
- 🔔 Storage change event listening
|
|
15
|
+
- 🔄 Automatic environment detection
|
|
16
|
+
- 🛠️ Key-based storage with caching
|
|
17
|
+
- 🔧 Custom serialization support
|
|
18
|
+
- 📝 TypeScript support
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @ahoo-wang/fetcher-storage
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Basic Usage
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { createListenableStorage } from '@ahoo-wang/fetcher-storage';
|
|
32
|
+
|
|
33
|
+
// Automatically selects the appropriate storage implementation
|
|
34
|
+
const storage = createListenableStorage();
|
|
35
|
+
|
|
36
|
+
// Use like regular Storage API
|
|
37
|
+
storage.setItem('key', 'value');
|
|
38
|
+
const value = storage.getItem('key');
|
|
39
|
+
|
|
40
|
+
// Listen for storage changes
|
|
41
|
+
const removeListener = storage.addListener((event) => {
|
|
42
|
+
console.log('Storage changed:', event);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Remove listener when no longer needed
|
|
46
|
+
removeListener();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Key-based Storage with Caching
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { KeyStorage } from '@ahoo-wang/fetcher-storage';
|
|
53
|
+
|
|
54
|
+
// Create a storage for a specific key
|
|
55
|
+
const userStorage = new KeyStorage<{ name: string, age: number }>({
|
|
56
|
+
key: 'user'
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Set and get values
|
|
60
|
+
userStorage.set({ name: 'John', age: 30 });
|
|
61
|
+
const user = userStorage.get(); // {name: 'John', age: 30}
|
|
62
|
+
|
|
63
|
+
// Listen for changes to this specific key
|
|
64
|
+
const removeListener = userStorage.addListener((event) => {
|
|
65
|
+
console.log('User changed:', event.newValue);
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Custom Serialization
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { KeyStorage, JsonSerializer } from '@ahoo-wang/fetcher-storage';
|
|
73
|
+
|
|
74
|
+
const jsonStorage = new KeyStorage<any>({
|
|
75
|
+
key: 'data',
|
|
76
|
+
serializer: new JsonSerializer()
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
jsonStorage.set({ message: 'Hello World' });
|
|
80
|
+
const data = jsonStorage.get(); // {message: 'Hello World'}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Environment-specific Storage
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { BrowserListenableStorage, InMemoryListenableStorage } from '@ahoo-wang/fetcher-storage';
|
|
87
|
+
|
|
88
|
+
// Browser storage (wraps localStorage or sessionStorage)
|
|
89
|
+
const browserStorage = new BrowserListenableStorage(localStorage);
|
|
90
|
+
|
|
91
|
+
// In-memory storage (works in any environment)
|
|
92
|
+
const memoryStorage = new InMemoryListenableStorage();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## API
|
|
96
|
+
|
|
97
|
+
### createListenableStorage()
|
|
98
|
+
|
|
99
|
+
Factory function that automatically returns the appropriate storage implementation based on the environment:
|
|
100
|
+
|
|
101
|
+
- Browser environment: `BrowserListenableStorage` wrapping `localStorage`
|
|
102
|
+
- Non-browser environment: `InMemoryListenableStorage`
|
|
103
|
+
|
|
104
|
+
### ListenableStorage
|
|
105
|
+
|
|
106
|
+
Extends the native `Storage` interface with event listening capabilities:
|
|
107
|
+
|
|
108
|
+
- `addListener(listener: StorageListener): RemoveStorageListener`
|
|
109
|
+
- All standard `Storage` methods (`getItem`, `setItem`, `removeItem`, etc.)
|
|
110
|
+
|
|
111
|
+
### KeyStorage
|
|
112
|
+
|
|
113
|
+
A storage wrapper for managing a single value associated with a specific key:
|
|
114
|
+
|
|
115
|
+
- Automatic caching with cache invalidation
|
|
116
|
+
- Key-specific event listening
|
|
117
|
+
- Custom serialization support
|
|
118
|
+
|
|
119
|
+
### Serializers
|
|
120
|
+
|
|
121
|
+
- `JsonSerializer`: Serializes values to/from JSON strings
|
|
122
|
+
- `IdentitySerializer`: Passes values through without modification
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
[Apache 2.0](https://github.com/Ahoo-Wang/fetcher/blob/master/LICENSE)
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# @ahoo-wang/fetcher-storage
|
|
2
|
+
|
|
3
|
+
一个轻量级的跨环境存储库,具有变更事件监听功能。为浏览器 localStorage/sessionStorage 和内存存储提供一致的 API,并支持变更通知。
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-storage)
|
|
6
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-storage)
|
|
7
|
+
[](https://github.com/Ahoo-Wang/fetcher/blob/master/LICENSE)
|
|
8
|
+
|
|
9
|
+
## 特性
|
|
10
|
+
|
|
11
|
+
- 🌐 跨环境支持(浏览器和 Node.js)
|
|
12
|
+
- 📦 超轻量级(~1KB gzip)
|
|
13
|
+
- 🔔 存储变更事件监听
|
|
14
|
+
- 🔄 自动环境检测
|
|
15
|
+
- 🛠️ 基于键的存储和缓存
|
|
16
|
+
- 🔧 自定义序列化支持
|
|
17
|
+
- 📝 TypeScript 支持
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @ahoo-wang/fetcher-storage
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 使用方法
|
|
26
|
+
|
|
27
|
+
### 基本用法
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { createListenableStorage } from '@ahoo-wang/fetcher-storage';
|
|
31
|
+
|
|
32
|
+
// 自动选择合适的存储实现
|
|
33
|
+
const storage = createListenableStorage();
|
|
34
|
+
|
|
35
|
+
// 像使用常规 Storage API 一样使用
|
|
36
|
+
storage.setItem('key', 'value');
|
|
37
|
+
const value = storage.getItem('key');
|
|
38
|
+
|
|
39
|
+
// 监听存储变更
|
|
40
|
+
const removeListener = storage.addListener((event) => {
|
|
41
|
+
console.log('存储变更:', event);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// 不再需要时移除监听器
|
|
45
|
+
removeListener();
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 基于键的存储和缓存
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { KeyStorage } from '@ahoo-wang/fetcher-storage';
|
|
52
|
+
|
|
53
|
+
// 为特定键创建存储
|
|
54
|
+
const userStorage = new KeyStorage<{ name: string, age: number }>({
|
|
55
|
+
key: 'user'
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// 设置和获取值
|
|
59
|
+
userStorage.set({ name: 'John', age: 30 });
|
|
60
|
+
const user = userStorage.get(); // {name: 'John', age: 30}
|
|
61
|
+
|
|
62
|
+
// 监听此特定键的变更
|
|
63
|
+
const removeListener = userStorage.addListener((event) => {
|
|
64
|
+
console.log('用户变更:', event.newValue);
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 自定义序列化
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { KeyStorage, JsonSerializer } from '@ahoo-wang/fetcher-storage';
|
|
72
|
+
|
|
73
|
+
const jsonStorage = new KeyStorage<any>({
|
|
74
|
+
key: 'data',
|
|
75
|
+
serializer: new JsonSerializer()
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
jsonStorage.set({ message: 'Hello World' });
|
|
79
|
+
const data = jsonStorage.get(); // {message: 'Hello World'}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 环境特定的存储
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { BrowserListenableStorage, InMemoryListenableStorage } from '@ahoo-wang/fetcher-storage';
|
|
86
|
+
|
|
87
|
+
// 浏览器存储(包装 localStorage 或 sessionStorage)
|
|
88
|
+
const browserStorage = new BrowserListenableStorage(localStorage);
|
|
89
|
+
|
|
90
|
+
// 内存存储(在任何环境中都能工作)
|
|
91
|
+
const memoryStorage = new InMemoryListenableStorage();
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## API
|
|
95
|
+
|
|
96
|
+
### createListenableStorage()
|
|
97
|
+
|
|
98
|
+
工厂函数,根据环境自动返回合适的存储实现:
|
|
99
|
+
|
|
100
|
+
- 浏览器环境:包装 localStorage 的 `BrowserListenableStorage`
|
|
101
|
+
- 非浏览器环境:`InMemoryListenableStorage`
|
|
102
|
+
|
|
103
|
+
### ListenableStorage
|
|
104
|
+
|
|
105
|
+
扩展了原生 `Storage` 接口,增加了事件监听功能:
|
|
106
|
+
|
|
107
|
+
- `addListener(listener: StorageListener): RemoveStorageListener`
|
|
108
|
+
- 所有标准 `Storage` 方法(`getItem`、`setItem`、`removeItem` 等)
|
|
109
|
+
|
|
110
|
+
### KeyStorage
|
|
111
|
+
|
|
112
|
+
用于管理与特定键关联的单个值的存储包装器:
|
|
113
|
+
|
|
114
|
+
- 自动缓存和缓存失效
|
|
115
|
+
- 键特定的事件监听
|
|
116
|
+
- 自定义序列化支持
|
|
117
|
+
|
|
118
|
+
### 序列化器
|
|
119
|
+
|
|
120
|
+
- `JsonSerializer`:将值序列化为 JSON 字符串
|
|
121
|
+
- `IdentitySerializer`:不进行修改直接传递值
|
|
122
|
+
|
|
123
|
+
## 许可证
|
|
124
|
+
|
|
125
|
+
[Apache 2.0](https://github.com/Ahoo-Wang/fetcher/blob/master/LICENSE)
|
package/package.json
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher-storage",
|
|
3
|
-
"version": "1.6.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.6.8",
|
|
4
|
+
"description": "A lightweight, cross-environment storage library with change event listening capabilities. Provides consistent API for browser localStorage/sessionStorage and in-memory storage with change notifications.",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"typescript"
|
|
13
|
-
"interceptor",
|
|
14
|
-
"llm",
|
|
15
|
-
"streaming",
|
|
16
|
-
"sse",
|
|
17
|
-
"server-sent-events"
|
|
6
|
+
"storage",
|
|
7
|
+
"localStorage",
|
|
8
|
+
"sessionStorage",
|
|
9
|
+
"memory-storage",
|
|
10
|
+
"event-listener",
|
|
11
|
+
"cross-environment",
|
|
12
|
+
"typescript"
|
|
18
13
|
],
|
|
19
14
|
"author": "Ahoo-Wang",
|
|
20
15
|
"license": "Apache-2.0",
|
|
21
|
-
"homepage": "https://github.com/Ahoo-Wang/fetcher/tree/master/packages/
|
|
16
|
+
"homepage": "https://github.com/Ahoo-Wang/fetcher/tree/master/packages/storage",
|
|
22
17
|
"repository": {
|
|
23
18
|
"type": "git",
|
|
24
19
|
"url": "https://github.com/Ahoo-Wang/fetcher.git",
|