safe-vault 1.0.0
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/dist/index.cjs +80 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/package.json +23 -0
- package/readme.md +7 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
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
|
+
SafeVault: () => SafeVault
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var SafeVault = class {
|
|
27
|
+
storage;
|
|
28
|
+
constructor(storage = localStorage) {
|
|
29
|
+
if (typeof window === "undefined") {
|
|
30
|
+
throw new Error("SafeVault can only be used in browser environments");
|
|
31
|
+
}
|
|
32
|
+
this.storage = storage;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Set a value with optional TTL (in milliseconds)
|
|
36
|
+
*/
|
|
37
|
+
set(key, value, ttl) {
|
|
38
|
+
const expiry = typeof ttl === "number" ? Date.now() + ttl : null;
|
|
39
|
+
const data = {
|
|
40
|
+
value,
|
|
41
|
+
expiry
|
|
42
|
+
};
|
|
43
|
+
this.storage.setItem(key, JSON.stringify(data));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get a value. Returns null if expired or not found.
|
|
47
|
+
*/
|
|
48
|
+
get(key) {
|
|
49
|
+
const item = this.storage.getItem(key);
|
|
50
|
+
if (!item) return null;
|
|
51
|
+
try {
|
|
52
|
+
const data = JSON.parse(item);
|
|
53
|
+
if (data.expiry !== null && Date.now() > data.expiry) {
|
|
54
|
+
this.storage.removeItem(key);
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
return data.value;
|
|
58
|
+
} catch {
|
|
59
|
+
this.storage.removeItem(key);
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Remove a single key
|
|
65
|
+
*/
|
|
66
|
+
remove(key) {
|
|
67
|
+
this.storage.removeItem(key);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Clear all storage
|
|
71
|
+
*/
|
|
72
|
+
clear() {
|
|
73
|
+
this.storage.clear();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
77
|
+
0 && (module.exports = {
|
|
78
|
+
SafeVault
|
|
79
|
+
});
|
|
80
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["interface StorageValue<T> {\r\n value: T;\r\n expiry: number | null;\r\n}\r\n\r\nexport class SafeVault<Schema extends Record<string, any>> {\r\n private storage: Storage;\r\n\r\n constructor(storage: Storage = localStorage) {\r\n if (typeof window === \"undefined\") {\r\n throw new Error(\"SafeVault can only be used in browser environments\");\r\n }\r\n this.storage = storage;\r\n }\r\n\r\n /**\r\n * Set a value with optional TTL (in milliseconds)\r\n */\r\n set<K extends keyof Schema>(\r\n key: K,\r\n value: Schema[K],\r\n ttl?: number\r\n ): void {\r\n const expiry = typeof ttl === \"number\" ? Date.now() + ttl : null;\r\n\r\n const data: StorageValue<Schema[K]> = {\r\n value,\r\n expiry,\r\n };\r\n\r\n this.storage.setItem(key as string, JSON.stringify(data));\r\n }\r\n\r\n /**\r\n * Get a value. Returns null if expired or not found.\r\n */\r\n get<K extends keyof Schema>(key: K): Schema[K] | null {\r\n const item = this.storage.getItem(key as string);\r\n if (!item) return null;\r\n\r\n try {\r\n const data: StorageValue<Schema[K]> = JSON.parse(item);\r\n\r\n if (data.expiry !== null && Date.now() > data.expiry) {\r\n this.storage.removeItem(key as string);\r\n return null;\r\n }\r\n\r\n return data.value;\r\n } catch {\r\n // corrupted JSON\r\n this.storage.removeItem(key as string);\r\n return null;\r\n }\r\n }\r\n\r\n /**\r\n * Remove a single key\r\n */\r\n remove<K extends keyof Schema>(key: K): void {\r\n this.storage.removeItem(key as string);\r\n }\r\n\r\n /**\r\n * Clear all storage\r\n */\r\n clear(): void {\r\n this.storage.clear();\r\n }\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKO,IAAM,YAAN,MAAoD;AAAA,EACjD;AAAA,EAER,YAAY,UAAmB,cAAc;AAC3C,QAAI,OAAO,WAAW,aAAa;AACjC,YAAM,IAAI,MAAM,oDAAoD;AAAA,IACtE;AACA,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA,EAKA,IACE,KACA,OACA,KACM;AACN,UAAM,SAAS,OAAO,QAAQ,WAAW,KAAK,IAAI,IAAI,MAAM;AAE5D,UAAM,OAAgC;AAAA,MACpC;AAAA,MACA;AAAA,IACF;AAEA,SAAK,QAAQ,QAAQ,KAAe,KAAK,UAAU,IAAI,CAAC;AAAA,EAC1D;AAAA;AAAA;AAAA;AAAA,EAKA,IAA4B,KAA0B;AACpD,UAAM,OAAO,KAAK,QAAQ,QAAQ,GAAa;AAC/C,QAAI,CAAC,KAAM,QAAO;AAElB,QAAI;AACF,YAAM,OAAgC,KAAK,MAAM,IAAI;AAErD,UAAI,KAAK,WAAW,QAAQ,KAAK,IAAI,IAAI,KAAK,QAAQ;AACpD,aAAK,QAAQ,WAAW,GAAa;AACrC,eAAO;AAAA,MACT;AAEA,aAAO,KAAK;AAAA,IACd,QAAQ;AAEN,WAAK,QAAQ,WAAW,GAAa;AACrC,aAAO;AAAA,IACT;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,OAA+B,KAAc;AAC3C,SAAK,QAAQ,WAAW,GAAa;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,QAAc;AACZ,SAAK,QAAQ,MAAM;AAAA,EACrB;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
declare class SafeVault<Schema extends Record<string, any>> {
|
|
2
|
+
private storage;
|
|
3
|
+
constructor(storage?: Storage);
|
|
4
|
+
/**
|
|
5
|
+
* Set a value with optional TTL (in milliseconds)
|
|
6
|
+
*/
|
|
7
|
+
set<K extends keyof Schema>(key: K, value: Schema[K], ttl?: number): void;
|
|
8
|
+
/**
|
|
9
|
+
* Get a value. Returns null if expired or not found.
|
|
10
|
+
*/
|
|
11
|
+
get<K extends keyof Schema>(key: K): Schema[K] | null;
|
|
12
|
+
/**
|
|
13
|
+
* Remove a single key
|
|
14
|
+
*/
|
|
15
|
+
remove<K extends keyof Schema>(key: K): void;
|
|
16
|
+
/**
|
|
17
|
+
* Clear all storage
|
|
18
|
+
*/
|
|
19
|
+
clear(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { SafeVault };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
declare class SafeVault<Schema extends Record<string, any>> {
|
|
2
|
+
private storage;
|
|
3
|
+
constructor(storage?: Storage);
|
|
4
|
+
/**
|
|
5
|
+
* Set a value with optional TTL (in milliseconds)
|
|
6
|
+
*/
|
|
7
|
+
set<K extends keyof Schema>(key: K, value: Schema[K], ttl?: number): void;
|
|
8
|
+
/**
|
|
9
|
+
* Get a value. Returns null if expired or not found.
|
|
10
|
+
*/
|
|
11
|
+
get<K extends keyof Schema>(key: K): Schema[K] | null;
|
|
12
|
+
/**
|
|
13
|
+
* Remove a single key
|
|
14
|
+
*/
|
|
15
|
+
remove<K extends keyof Schema>(key: K): void;
|
|
16
|
+
/**
|
|
17
|
+
* Clear all storage
|
|
18
|
+
*/
|
|
19
|
+
clear(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { SafeVault };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var SafeVault = class {
|
|
3
|
+
storage;
|
|
4
|
+
constructor(storage = localStorage) {
|
|
5
|
+
if (typeof window === "undefined") {
|
|
6
|
+
throw new Error("SafeVault can only be used in browser environments");
|
|
7
|
+
}
|
|
8
|
+
this.storage = storage;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Set a value with optional TTL (in milliseconds)
|
|
12
|
+
*/
|
|
13
|
+
set(key, value, ttl) {
|
|
14
|
+
const expiry = typeof ttl === "number" ? Date.now() + ttl : null;
|
|
15
|
+
const data = {
|
|
16
|
+
value,
|
|
17
|
+
expiry
|
|
18
|
+
};
|
|
19
|
+
this.storage.setItem(key, JSON.stringify(data));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get a value. Returns null if expired or not found.
|
|
23
|
+
*/
|
|
24
|
+
get(key) {
|
|
25
|
+
const item = this.storage.getItem(key);
|
|
26
|
+
if (!item) return null;
|
|
27
|
+
try {
|
|
28
|
+
const data = JSON.parse(item);
|
|
29
|
+
if (data.expiry !== null && Date.now() > data.expiry) {
|
|
30
|
+
this.storage.removeItem(key);
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return data.value;
|
|
34
|
+
} catch {
|
|
35
|
+
this.storage.removeItem(key);
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Remove a single key
|
|
41
|
+
*/
|
|
42
|
+
remove(key) {
|
|
43
|
+
this.storage.removeItem(key);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Clear all storage
|
|
47
|
+
*/
|
|
48
|
+
clear() {
|
|
49
|
+
this.storage.clear();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
export {
|
|
53
|
+
SafeVault
|
|
54
|
+
};
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["interface StorageValue<T> {\r\n value: T;\r\n expiry: number | null;\r\n}\r\n\r\nexport class SafeVault<Schema extends Record<string, any>> {\r\n private storage: Storage;\r\n\r\n constructor(storage: Storage = localStorage) {\r\n if (typeof window === \"undefined\") {\r\n throw new Error(\"SafeVault can only be used in browser environments\");\r\n }\r\n this.storage = storage;\r\n }\r\n\r\n /**\r\n * Set a value with optional TTL (in milliseconds)\r\n */\r\n set<K extends keyof Schema>(\r\n key: K,\r\n value: Schema[K],\r\n ttl?: number\r\n ): void {\r\n const expiry = typeof ttl === \"number\" ? Date.now() + ttl : null;\r\n\r\n const data: StorageValue<Schema[K]> = {\r\n value,\r\n expiry,\r\n };\r\n\r\n this.storage.setItem(key as string, JSON.stringify(data));\r\n }\r\n\r\n /**\r\n * Get a value. Returns null if expired or not found.\r\n */\r\n get<K extends keyof Schema>(key: K): Schema[K] | null {\r\n const item = this.storage.getItem(key as string);\r\n if (!item) return null;\r\n\r\n try {\r\n const data: StorageValue<Schema[K]> = JSON.parse(item);\r\n\r\n if (data.expiry !== null && Date.now() > data.expiry) {\r\n this.storage.removeItem(key as string);\r\n return null;\r\n }\r\n\r\n return data.value;\r\n } catch {\r\n // corrupted JSON\r\n this.storage.removeItem(key as string);\r\n return null;\r\n }\r\n }\r\n\r\n /**\r\n * Remove a single key\r\n */\r\n remove<K extends keyof Schema>(key: K): void {\r\n this.storage.removeItem(key as string);\r\n }\r\n\r\n /**\r\n * Clear all storage\r\n */\r\n clear(): void {\r\n this.storage.clear();\r\n }\r\n}\r\n"],"mappings":";AAKO,IAAM,YAAN,MAAoD;AAAA,EACjD;AAAA,EAER,YAAY,UAAmB,cAAc;AAC3C,QAAI,OAAO,WAAW,aAAa;AACjC,YAAM,IAAI,MAAM,oDAAoD;AAAA,IACtE;AACA,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA,EAKA,IACE,KACA,OACA,KACM;AACN,UAAM,SAAS,OAAO,QAAQ,WAAW,KAAK,IAAI,IAAI,MAAM;AAE5D,UAAM,OAAgC;AAAA,MACpC;AAAA,MACA;AAAA,IACF;AAEA,SAAK,QAAQ,QAAQ,KAAe,KAAK,UAAU,IAAI,CAAC;AAAA,EAC1D;AAAA;AAAA;AAAA;AAAA,EAKA,IAA4B,KAA0B;AACpD,UAAM,OAAO,KAAK,QAAQ,QAAQ,GAAa;AAC/C,QAAI,CAAC,KAAM,QAAO;AAElB,QAAI;AACF,YAAM,OAAgC,KAAK,MAAM,IAAI;AAErD,UAAI,KAAK,WAAW,QAAQ,KAAK,IAAI,IAAI,KAAK,QAAQ;AACpD,aAAK,QAAQ,WAAW,GAAa;AACrC,eAAO;AAAA,MACT;AAEA,aAAO,KAAK;AAAA,IACd,QAAQ;AAEN,WAAK,QAAQ,WAAW,GAAa;AACrC,aAAO;AAAA,IACT;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,OAA+B,KAAc;AAC3C,SAAK,QAAQ,WAAW,GAAa;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,QAAc;AACZ,SAAK,QAAQ,MAAM;AAAA,EACrB;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "safe-vault",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Type-safe localStorage wrapper with TTL support",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"files": ["dist"],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup",
|
|
12
|
+
"dev": "tsup --watch",
|
|
13
|
+
"lint": "tsc --noEmit"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"localStorage",
|
|
17
|
+
"storage",
|
|
18
|
+
"ttl",
|
|
19
|
+
"typescript",
|
|
20
|
+
"cache"
|
|
21
|
+
],
|
|
22
|
+
"license": "MIT"
|
|
23
|
+
}
|