safe-vault 1.0.0 → 1.1.2

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.
Files changed (2) hide show
  1. package/package.json +4 -2
  2. package/readme.md +163 -3
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "safe-vault",
3
- "version": "1.0.0",
3
+ "version": "1.1.2",
4
4
  "description": "Type-safe localStorage wrapper with TTL support",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
8
8
  "types": "./dist/index.d.ts",
9
- "files": ["dist"],
9
+ "files": [
10
+ "dist"
11
+ ],
10
12
  "scripts": {
11
13
  "build": "tsup",
12
14
  "dev": "tsup --watch",
package/readme.md CHANGED
@@ -1,7 +1,167 @@
1
- # safe-vault
1
+ # SafeVault
2
2
 
3
- A lightweight, type-safe wrapper around browser storage with TTL expiration.
3
+ A type-safe localStorage wrapper with built-in TTL (Time To Live) support for browser environments.
4
+
5
+ ## Features
6
+
7
+ - 🔒 **Type-safe**: Full TypeScript support with schema definitions
8
+ - ⏰ **TTL Support**: Set automatic expiration times for stored data
9
+ - ðŸ›Ąïļ **Error Handling**: Gracefully handles corrupted data and expired entries
10
+ - ðŸŠķ **Lightweight**: Zero dependencies
11
+ - 🌐 **Storage Agnostic**: Works with both localStorage and sessionStorage
12
+
13
+ ## Installation
4
14
 
5
- ## Install
6
15
  ```bash
7
16
  npm install safe-vault
17
+ ```
18
+
19
+ ```bash
20
+ yarn add safe-vault
21
+ ```
22
+
23
+ ```bash
24
+ pnpm add safe-vault
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### Basic Example
30
+
31
+ ```typescript
32
+ import { SafeVault } from 'safe-vault';
33
+
34
+ // Define your storage schema
35
+ interface MySchema {
36
+ user: {
37
+ id: string;
38
+ name: string;
39
+ };
40
+ token: string;
41
+ preferences: {
42
+ theme: 'light' | 'dark';
43
+ };
44
+ }
45
+
46
+ // Create an instance
47
+ const vault = new SafeVault<MySchema>();
48
+
49
+ // Set values
50
+ vault.set('user', { id: '123', name: 'John Doe' });
51
+ vault.set('token', 'abc123');
52
+
53
+ // Get values
54
+ const user = vault.get('user'); // Typed as { id: string; name: string } | null
55
+ const token = vault.get('token'); // Typed as string | null
56
+
57
+ // Remove values
58
+ vault.remove('token');
59
+
60
+ // Clear all storage
61
+ vault.clear();
62
+ ```
63
+
64
+ ### TTL (Time To Live) Support
65
+
66
+ Set expiration times for your data:
67
+
68
+ ```typescript
69
+ // Expire after 1 hour (3600000 milliseconds)
70
+ vault.set('token', 'abc123', 3600000);
71
+
72
+ // Expire after 5 minutes
73
+ vault.set('sessionData', { temp: true }, 5 * 60 * 1000);
74
+
75
+ // When you try to get expired data, it returns null
76
+ setTimeout(() => {
77
+ const token = vault.get('token'); // null (if expired)
78
+ }, 3600001);
79
+ ```
80
+
81
+ ### Using sessionStorage
82
+
83
+ ```typescript
84
+ const sessionVault = new SafeVault<MySchema>(sessionStorage);
85
+
86
+ sessionVault.set('temporaryData', { foo: 'bar' });
87
+ ```
88
+
89
+ ## API Reference
90
+
91
+ ### Constructor
92
+
93
+ ```typescript
94
+ new SafeVault<Schema>(storage?: Storage)
95
+ ```
96
+
97
+ - `storage`: Optional. Defaults to `localStorage`. Can be `sessionStorage` or any Storage-compatible object.
98
+
99
+ ### Methods
100
+
101
+ #### `set<K>(key: K, value: Schema[K], ttl?: number): void`
102
+
103
+ Store a value with optional TTL.
104
+
105
+ - `key`: The key from your schema
106
+ - `value`: The value to store (must match schema type)
107
+ - `ttl`: Optional. Time to live in milliseconds
108
+
109
+ #### `get<K>(key: K): Schema[K] | null`
110
+
111
+ Retrieve a value. Returns `null` if the key doesn't exist or has expired.
112
+
113
+ - `key`: The key from your schema
114
+ - Returns: The stored value or `null`
115
+
116
+ #### `remove<K>(key: K): void`
117
+
118
+ Remove a specific key from storage.
119
+
120
+ - `key`: The key to remove
121
+
122
+ #### `clear(): void`
123
+
124
+ Clear all items from storage.
125
+
126
+ ## Error Handling
127
+
128
+ SafeVault automatically handles:
129
+
130
+ - **Corrupted JSON**: If stored data can't be parsed, it's removed and `null` is returned
131
+ - **Expired entries**: Automatically removed when accessed
132
+ - **Missing keys**: Returns `null` gracefully
133
+
134
+ ## Browser Support
135
+
136
+ SafeVault requires a browser environment with `localStorage` or `sessionStorage` support. It will throw an error if used in non-browser environments (like Node.js without polyfills).
137
+
138
+ ## TypeScript
139
+
140
+ This package is written in TypeScript and includes type definitions. Your schema provides full autocomplete and type checking:
141
+
142
+ ```typescript
143
+ interface MySchema {
144
+ count: number;
145
+ name: string;
146
+ }
147
+
148
+ const vault = new SafeVault<MySchema>();
149
+
150
+ vault.set('count', 42); // ✅ OK
151
+ vault.set('count', '42'); // ❌ Type error
152
+ vault.set('invalid', 'value'); // ❌ Type error - key not in schema
153
+
154
+ const count = vault.get('count'); // Type: number | null
155
+ ```
156
+
157
+ ## License
158
+
159
+ MIT
160
+
161
+ ## Contributing
162
+
163
+ Contributions are welcome! Please feel free to submit a Pull Request.
164
+
165
+ ## Issues
166
+
167
+ If you find a bug or have a feature request, please open an issue on GitHub.