oto-storage 0.1.1
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/LICENSE +21 -0
- package/README.md +212 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +90 -0
- package/dist/index.mjs +64 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Diomari Madulara
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# Oto Storage
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
A lightweight, Proxy-based wrapper for `localStorage` and `sessionStorage` with full TypeScript type safety.
|
|
7
|
+
|
|
8
|
+
### 📦 Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install oto-storage
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Or with yarn:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
yarn add oto-storage
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Or with pnpm:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pnpm add oto-storage
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### ⚡ The Problem
|
|
27
|
+
|
|
28
|
+
Working with browser storage usually involves repetitive `JSON.parse` and `JSON.stringify` calls, manual key prefixing to avoid collisions, and a total lack of Type Safety.
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
// The old, "clunky" way
|
|
32
|
+
const user = JSON.parse(localStorage.getItem("user_data") || "{}");
|
|
33
|
+
localStorage.setItem("user_data", JSON.stringify({ ...user, theme: "dark" }));
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### ✨ The Solution
|
|
37
|
+
|
|
38
|
+
Oto storage uses the JavaScript Proxy API to let you interact with browser storage as if it were a local object. It handles serialization, prefixing, and type-checking automatically.
|
|
39
|
+
|
|
40
|
+
**Key Features**
|
|
41
|
+
|
|
42
|
+
- Type-Safe: Full autocomplete and build-time error checking.
|
|
43
|
+
|
|
44
|
+
- Zero Boilerplate: No more manual JSON parsing.
|
|
45
|
+
|
|
46
|
+
- Driver Support: Switch between localStorage and sessionStorage with one flag.
|
|
47
|
+
|
|
48
|
+
- Collision Protection: Automatic key prefixing (namespacing).
|
|
49
|
+
|
|
50
|
+
### 🚀 Quick Start
|
|
51
|
+
|
|
52
|
+
**_1. Define your Schema_**
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
interface AppStorage {
|
|
56
|
+
theme: "light" | "dark";
|
|
57
|
+
viewCount: number;
|
|
58
|
+
user: { id: string; name: string } | null;
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
**_2. Initialize_**
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { oto } from "oto-storage";
|
|
66
|
+
|
|
67
|
+
// For localStorage (default)
|
|
68
|
+
const storage = oto<AppStorage>({
|
|
69
|
+
prefix: "myApp-",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// For sessionStorage
|
|
73
|
+
const session = oto<AppStorage>({
|
|
74
|
+
prefix: "myApp-",
|
|
75
|
+
type: "session",
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**_3. Use it like a regular object_**
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
// SETTING: Automatically stringified and saved to 'myApp-theme'
|
|
83
|
+
storage.theme = "dark";
|
|
84
|
+
|
|
85
|
+
// GETTING: Automatically parsed and typed
|
|
86
|
+
if (storage.theme === "dark") {
|
|
87
|
+
console.log("Dark mode active!");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// DELETE: Delete the key-value pair from storage
|
|
91
|
+
delete storage.theme;
|
|
92
|
+
|
|
93
|
+
// CLEAR: Clear entire record
|
|
94
|
+
storage.clearAll();
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### 📖 Comprehensive Examples
|
|
98
|
+
|
|
99
|
+
**Working with Complex Objects**
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
interface UserData {
|
|
103
|
+
id: string;
|
|
104
|
+
name: string;
|
|
105
|
+
preferences: {
|
|
106
|
+
theme: "light" | "dark";
|
|
107
|
+
notifications: boolean;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const storage = oto<{ user: UserData | null }>({ prefix: "app-" });
|
|
112
|
+
|
|
113
|
+
// Store complex objects - automatically serialized
|
|
114
|
+
storage.user = {
|
|
115
|
+
id: "123",
|
|
116
|
+
name: "Alice",
|
|
117
|
+
preferences: {
|
|
118
|
+
theme: "dark",
|
|
119
|
+
notifications: true,
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// Retrieve - fully typed with autocomplete
|
|
124
|
+
console.log(storage.user?.preferences.theme); // "dark"
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**Checking if a Key Exists**
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
const storage = oto<{ token: string | null }>({ prefix: "auth-" });
|
|
131
|
+
|
|
132
|
+
// Use 'in' operator to check if key exists in storage
|
|
133
|
+
if ("token" in storage) {
|
|
134
|
+
console.log("User is authenticated");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Or check directly (returns undefined for non-existent keys)
|
|
138
|
+
if (storage.token) {
|
|
139
|
+
console.log("Token exists:", storage.token);
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**Session Storage**
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
const session = oto<{ temporaryData: string }>({
|
|
147
|
+
prefix: "temp-",
|
|
148
|
+
type: "session", // Uses sessionStorage instead of localStorage
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
session.temporaryData = "This will be cleared when tab closes";
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**Multiple Namespaced Storages**
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
const userPrefs = oto<{ theme: string }>({ prefix: "user-prefs-" });
|
|
158
|
+
const appState = oto<{ sidebarOpen: boolean }>({ prefix: "app-" });
|
|
159
|
+
|
|
160
|
+
userPrefs.theme = "dark";
|
|
161
|
+
appState.sidebarOpen = true;
|
|
162
|
+
|
|
163
|
+
// Keys are stored separately: 'user-prefs-theme' and 'app-sidebarOpen'
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
**Deleting and Clearing**
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
const storage = oto<{ count: number; name: string }>({ prefix: "demo-" });
|
|
170
|
+
|
|
171
|
+
storage.count = 42;
|
|
172
|
+
storage.name = "Test";
|
|
173
|
+
|
|
174
|
+
// Delete single key
|
|
175
|
+
delete storage.count;
|
|
176
|
+
|
|
177
|
+
// Clear all keys with this storage's prefix
|
|
178
|
+
storage.clearAll();
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
**Type Safety at Work**
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
interface AppStorage {
|
|
185
|
+
count: number;
|
|
186
|
+
items: string[];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const storage = oto<AppStorage>({ prefix: "app-" });
|
|
190
|
+
|
|
191
|
+
storage.count = 10; // ✓ Works
|
|
192
|
+
storage.count = "ten"; // ✗ TypeScript error: Type 'string' is not assignable to type 'number'
|
|
193
|
+
|
|
194
|
+
storage.items = ["a", "b"]; // ✓ Works
|
|
195
|
+
storage.items = "abc"; // ✗ TypeScript error: Type 'string' is not assignable to type 'string[]'
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
**Version Export**
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import { oto, version } from "oto-storage";
|
|
202
|
+
|
|
203
|
+
console.log(`Using oto-storage v${version}`);
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### 🛠️ Architecture Decisions
|
|
207
|
+
|
|
208
|
+
**_Why Proxy?_**
|
|
209
|
+
Choosing Proxy API over standard Class-based approach improves Developer Experience (DX). By intercepting `get` and `set` traps, we eliminate the need for `.getItem()` or `.setItem()` methods, making the storage feel "native" to JavaScript.
|
|
210
|
+
|
|
211
|
+
**_Type Safety via Generics_**
|
|
212
|
+
The library uses TypeScript Generics to map the user-provided interface to the Proxy. This ensures that if a developer tries to assign a `string` to a `number` field, the IDE will catch the error before the code even runs.
|
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
oto: () => oto,
|
|
24
|
+
version: () => version
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
function oto(options = {}) {
|
|
28
|
+
const { prefix = "", type = "local" } = options;
|
|
29
|
+
const storage = typeof window !== "undefined" ? type === "session" ? window.sessionStorage : window.localStorage : null;
|
|
30
|
+
const safeStorage = storage || {
|
|
31
|
+
getItem: () => null,
|
|
32
|
+
setItem: () => {
|
|
33
|
+
},
|
|
34
|
+
removeItem: () => {
|
|
35
|
+
},
|
|
36
|
+
clear: () => {
|
|
37
|
+
},
|
|
38
|
+
key: () => null,
|
|
39
|
+
get length() {
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
return new Proxy({}, {
|
|
44
|
+
get(_target, prop) {
|
|
45
|
+
if (prop === "clearAll") {
|
|
46
|
+
return () => {
|
|
47
|
+
const keys = [];
|
|
48
|
+
for (let i = 0; i < safeStorage.length; i++) {
|
|
49
|
+
const key = safeStorage.key(i);
|
|
50
|
+
if (key) keys.push(key);
|
|
51
|
+
}
|
|
52
|
+
keys.forEach((key) => {
|
|
53
|
+
if (key.startsWith(prefix)) safeStorage.removeItem(key);
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const value = safeStorage.getItem(`${prefix}${prop}`);
|
|
58
|
+
if (value === null) return void 0;
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(value);
|
|
61
|
+
} catch {
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
set(_target, prop, value) {
|
|
66
|
+
try {
|
|
67
|
+
const stringValue = JSON.stringify(value);
|
|
68
|
+
safeStorage.setItem(`${prefix}${prop}`, stringValue);
|
|
69
|
+
return true;
|
|
70
|
+
} catch (error) {
|
|
71
|
+
console.error(`TypedProxy: Failed to save ${prop}`, error);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
has(_target, prop) {
|
|
76
|
+
return safeStorage.getItem(`${prefix}${prop}`) !== null;
|
|
77
|
+
},
|
|
78
|
+
deleteProperty(_target, prop) {
|
|
79
|
+
const key = `${prefix}${String(prop)}`;
|
|
80
|
+
safeStorage.removeItem(key);
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
var version = "1.0.0";
|
|
86
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
+
0 && (module.exports = {
|
|
88
|
+
oto,
|
|
89
|
+
version
|
|
90
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function oto(options = {}) {
|
|
3
|
+
const { prefix = "", type = "local" } = options;
|
|
4
|
+
const storage = typeof window !== "undefined" ? type === "session" ? window.sessionStorage : window.localStorage : null;
|
|
5
|
+
const safeStorage = storage || {
|
|
6
|
+
getItem: () => null,
|
|
7
|
+
setItem: () => {
|
|
8
|
+
},
|
|
9
|
+
removeItem: () => {
|
|
10
|
+
},
|
|
11
|
+
clear: () => {
|
|
12
|
+
},
|
|
13
|
+
key: () => null,
|
|
14
|
+
get length() {
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
return new Proxy({}, {
|
|
19
|
+
get(_target, prop) {
|
|
20
|
+
if (prop === "clearAll") {
|
|
21
|
+
return () => {
|
|
22
|
+
const keys = [];
|
|
23
|
+
for (let i = 0; i < safeStorage.length; i++) {
|
|
24
|
+
const key = safeStorage.key(i);
|
|
25
|
+
if (key) keys.push(key);
|
|
26
|
+
}
|
|
27
|
+
keys.forEach((key) => {
|
|
28
|
+
if (key.startsWith(prefix)) safeStorage.removeItem(key);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const value = safeStorage.getItem(`${prefix}${prop}`);
|
|
33
|
+
if (value === null) return void 0;
|
|
34
|
+
try {
|
|
35
|
+
return JSON.parse(value);
|
|
36
|
+
} catch {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
set(_target, prop, value) {
|
|
41
|
+
try {
|
|
42
|
+
const stringValue = JSON.stringify(value);
|
|
43
|
+
safeStorage.setItem(`${prefix}${prop}`, stringValue);
|
|
44
|
+
return true;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error(`TypedProxy: Failed to save ${prop}`, error);
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
has(_target, prop) {
|
|
51
|
+
return safeStorage.getItem(`${prefix}${prop}`) !== null;
|
|
52
|
+
},
|
|
53
|
+
deleteProperty(_target, prop) {
|
|
54
|
+
const key = `${prefix}${String(prop)}`;
|
|
55
|
+
safeStorage.removeItem(key);
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
var version = "1.0.0";
|
|
61
|
+
export {
|
|
62
|
+
oto,
|
|
63
|
+
version
|
|
64
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "oto-storage",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "A lightweight, type-safe wrapper for localStorage and sessionStorage using the Proxy API.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.mjs",
|
|
15
|
+
"require": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"scripts": {
|
|
20
|
+
"dev": "vite",
|
|
21
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
22
|
+
"test": "vitest run",
|
|
23
|
+
"test:watch": "vitest",
|
|
24
|
+
"lint": "tsc --noEmit",
|
|
25
|
+
"preview": "vite preview"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"typescript",
|
|
29
|
+
"localstorage",
|
|
30
|
+
"sessionstorage",
|
|
31
|
+
"proxy",
|
|
32
|
+
"type-safe",
|
|
33
|
+
"dx",
|
|
34
|
+
"storage-wrapper"
|
|
35
|
+
],
|
|
36
|
+
"author": "Diomari Madulara",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/diomari/oto-storage.git"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"typescript": "^5.3.3",
|
|
44
|
+
"vitest": "^1.2.2",
|
|
45
|
+
"jsdom": "^24.0.0",
|
|
46
|
+
"tsup": "^8.0.1",
|
|
47
|
+
"vite": "^5.1.0"
|
|
48
|
+
}
|
|
49
|
+
}
|