@zucker-framework/fe-storage 1.0.0 → 1.0.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.
- package/README.md +5 -0
- package/dist/index.d.mts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +42 -9
- package/dist/index.mjs +40 -8
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Frontend storage
|
|
2
|
+
|
|
3
|
+
`LocalStore` provides the shared browser storage and authentication-key contract, including best-effort handling of unavailable storage.
|
|
4
|
+
|
|
5
|
+
`downloadBlob(blob, fileName, { revokeDelayMs? }?)` triggers a caller-requested browser download. It owns the temporary anchor and object URL, cleaning both after the click, including click failures. URL cleanup is immediate by default; a caller with an existing delayed-revocation requirement can provide that delay. Applications retain the filename, MIME type and when to download. This helper does not perform HTTP requests and throws outside a browser.
|
package/dist/index.d.mts
CHANGED
|
@@ -12,6 +12,7 @@ declare const REFRESH_TOKEN_KEY = "REFRESH_TOKEN";
|
|
|
12
12
|
* All methods are static so the class can be used without instantiation.
|
|
13
13
|
*/
|
|
14
14
|
declare class LocalStore {
|
|
15
|
+
private static attempt;
|
|
15
16
|
/** Store a value under the given key. Non-string values are JSON-serialized. */
|
|
16
17
|
static set(key: string, data: unknown): void;
|
|
17
18
|
/** Retrieve and JSON-parse a value by key. Returns null if the key does not exist. */
|
|
@@ -35,9 +36,14 @@ declare class LocalStore {
|
|
|
35
36
|
/** Retrieve the stored user profile object. */
|
|
36
37
|
static getUserInfo<T = Record<string, unknown>>(): T | null;
|
|
37
38
|
/** Persist a user profile object. */
|
|
38
|
-
static setUserInfo(userInfo:
|
|
39
|
+
static setUserInfo<TUser extends object>(userInfo: TUser): void;
|
|
39
40
|
/** Remove all auth-related keys (token, refresh token, user info). */
|
|
40
41
|
static clearAuth(): void;
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
/** Trigger an explicitly requested browser download and release its temporary DOM/URL resources. */
|
|
45
|
+
declare function downloadBlob(blob: Blob, fileName: string, options?: {
|
|
46
|
+
revokeDelayMs?: number;
|
|
47
|
+
}): void;
|
|
48
|
+
|
|
49
|
+
export { LocalStore, REFRESH_TOKEN_KEY, TOKEN_KEY, USER_KEY, downloadBlob };
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ declare const REFRESH_TOKEN_KEY = "REFRESH_TOKEN";
|
|
|
12
12
|
* All methods are static so the class can be used without instantiation.
|
|
13
13
|
*/
|
|
14
14
|
declare class LocalStore {
|
|
15
|
+
private static attempt;
|
|
15
16
|
/** Store a value under the given key. Non-string values are JSON-serialized. */
|
|
16
17
|
static set(key: string, data: unknown): void;
|
|
17
18
|
/** Retrieve and JSON-parse a value by key. Returns null if the key does not exist. */
|
|
@@ -35,9 +36,14 @@ declare class LocalStore {
|
|
|
35
36
|
/** Retrieve the stored user profile object. */
|
|
36
37
|
static getUserInfo<T = Record<string, unknown>>(): T | null;
|
|
37
38
|
/** Persist a user profile object. */
|
|
38
|
-
static setUserInfo(userInfo:
|
|
39
|
+
static setUserInfo<TUser extends object>(userInfo: TUser): void;
|
|
39
40
|
/** Remove all auth-related keys (token, refresh token, user info). */
|
|
40
41
|
static clearAuth(): void;
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
/** Trigger an explicitly requested browser download and release its temporary DOM/URL resources. */
|
|
45
|
+
declare function downloadBlob(blob: Blob, fileName: string, options?: {
|
|
46
|
+
revokeDelayMs?: number;
|
|
47
|
+
}): void;
|
|
48
|
+
|
|
49
|
+
export { LocalStore, REFRESH_TOKEN_KEY, TOKEN_KEY, USER_KEY, downloadBlob };
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,8 @@ __export(index_exports, {
|
|
|
23
23
|
LocalStore: () => LocalStore,
|
|
24
24
|
REFRESH_TOKEN_KEY: () => REFRESH_TOKEN_KEY,
|
|
25
25
|
TOKEN_KEY: () => TOKEN_KEY,
|
|
26
|
-
USER_KEY: () => USER_KEY
|
|
26
|
+
USER_KEY: () => USER_KEY,
|
|
27
|
+
downloadBlob: () => downloadBlob
|
|
27
28
|
});
|
|
28
29
|
module.exports = __toCommonJS(index_exports);
|
|
29
30
|
|
|
@@ -34,14 +35,23 @@ var REFRESH_TOKEN_KEY = "REFRESH_TOKEN";
|
|
|
34
35
|
|
|
35
36
|
// src/local-store.ts
|
|
36
37
|
var LocalStore = class {
|
|
38
|
+
static attempt(operation, fallback) {
|
|
39
|
+
try {
|
|
40
|
+
return operation();
|
|
41
|
+
} catch {
|
|
42
|
+
return fallback;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
37
45
|
/** Store a value under the given key. Non-string values are JSON-serialized. */
|
|
38
46
|
static set(key, data) {
|
|
39
|
-
|
|
40
|
-
|
|
47
|
+
this.attempt(() => {
|
|
48
|
+
const value = typeof data === "string" ? data : JSON.stringify(data);
|
|
49
|
+
localStorage.setItem(key, value);
|
|
50
|
+
}, void 0);
|
|
41
51
|
}
|
|
42
52
|
/** Retrieve and JSON-parse a value by key. Returns null if the key does not exist. */
|
|
43
53
|
static get(key) {
|
|
44
|
-
const dataStr = localStorage.getItem(key);
|
|
54
|
+
const dataStr = this.attempt(() => localStorage.getItem(key), null);
|
|
45
55
|
if (!dataStr) return null;
|
|
46
56
|
try {
|
|
47
57
|
return JSON.parse(dataStr);
|
|
@@ -51,19 +61,19 @@ var LocalStore = class {
|
|
|
51
61
|
}
|
|
52
62
|
/** Remove a single key from localStorage. */
|
|
53
63
|
static remove(key) {
|
|
54
|
-
localStorage.removeItem(key);
|
|
64
|
+
this.attempt(() => localStorage.removeItem(key), void 0);
|
|
55
65
|
}
|
|
56
66
|
/** Clear all keys from localStorage. */
|
|
57
67
|
static removeAll() {
|
|
58
|
-
localStorage.clear();
|
|
68
|
+
this.attempt(() => localStorage.clear(), void 0);
|
|
59
69
|
}
|
|
60
70
|
/** Check whether a key exists in localStorage. */
|
|
61
71
|
static has(key) {
|
|
62
|
-
return localStorage.getItem(key) !== null;
|
|
72
|
+
return this.attempt(() => localStorage.getItem(key) !== null, false);
|
|
63
73
|
}
|
|
64
74
|
/** Return all keys currently stored in localStorage. */
|
|
65
75
|
static keys() {
|
|
66
|
-
return Object.keys(localStorage);
|
|
76
|
+
return this.attempt(() => Object.keys(localStorage), []);
|
|
67
77
|
}
|
|
68
78
|
/** Retrieve the stored access token. */
|
|
69
79
|
static getToken() {
|
|
@@ -96,10 +106,33 @@ var LocalStore = class {
|
|
|
96
106
|
this.remove(USER_KEY);
|
|
97
107
|
}
|
|
98
108
|
};
|
|
109
|
+
|
|
110
|
+
// src/download-blob.ts
|
|
111
|
+
function downloadBlob(blob, fileName, options = {}) {
|
|
112
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
113
|
+
throw new Error("downloadBlob requires a browser");
|
|
114
|
+
}
|
|
115
|
+
const url = window.URL.createObjectURL(blob);
|
|
116
|
+
const link = document.createElement("a");
|
|
117
|
+
try {
|
|
118
|
+
link.href = url;
|
|
119
|
+
link.download = fileName;
|
|
120
|
+
document.body.appendChild(link);
|
|
121
|
+
link.click();
|
|
122
|
+
} finally {
|
|
123
|
+
link.remove();
|
|
124
|
+
if (options.revokeDelayMs && options.revokeDelayMs > 0) {
|
|
125
|
+
setTimeout(() => window.URL.revokeObjectURL(url), options.revokeDelayMs);
|
|
126
|
+
} else {
|
|
127
|
+
window.URL.revokeObjectURL(url);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
99
131
|
// Annotate the CommonJS export names for ESM import in node:
|
|
100
132
|
0 && (module.exports = {
|
|
101
133
|
LocalStore,
|
|
102
134
|
REFRESH_TOKEN_KEY,
|
|
103
135
|
TOKEN_KEY,
|
|
104
|
-
USER_KEY
|
|
136
|
+
USER_KEY,
|
|
137
|
+
downloadBlob
|
|
105
138
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -5,14 +5,23 @@ var REFRESH_TOKEN_KEY = "REFRESH_TOKEN";
|
|
|
5
5
|
|
|
6
6
|
// src/local-store.ts
|
|
7
7
|
var LocalStore = class {
|
|
8
|
+
static attempt(operation, fallback) {
|
|
9
|
+
try {
|
|
10
|
+
return operation();
|
|
11
|
+
} catch {
|
|
12
|
+
return fallback;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
8
15
|
/** Store a value under the given key. Non-string values are JSON-serialized. */
|
|
9
16
|
static set(key, data) {
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
this.attempt(() => {
|
|
18
|
+
const value = typeof data === "string" ? data : JSON.stringify(data);
|
|
19
|
+
localStorage.setItem(key, value);
|
|
20
|
+
}, void 0);
|
|
12
21
|
}
|
|
13
22
|
/** Retrieve and JSON-parse a value by key. Returns null if the key does not exist. */
|
|
14
23
|
static get(key) {
|
|
15
|
-
const dataStr = localStorage.getItem(key);
|
|
24
|
+
const dataStr = this.attempt(() => localStorage.getItem(key), null);
|
|
16
25
|
if (!dataStr) return null;
|
|
17
26
|
try {
|
|
18
27
|
return JSON.parse(dataStr);
|
|
@@ -22,19 +31,19 @@ var LocalStore = class {
|
|
|
22
31
|
}
|
|
23
32
|
/** Remove a single key from localStorage. */
|
|
24
33
|
static remove(key) {
|
|
25
|
-
localStorage.removeItem(key);
|
|
34
|
+
this.attempt(() => localStorage.removeItem(key), void 0);
|
|
26
35
|
}
|
|
27
36
|
/** Clear all keys from localStorage. */
|
|
28
37
|
static removeAll() {
|
|
29
|
-
localStorage.clear();
|
|
38
|
+
this.attempt(() => localStorage.clear(), void 0);
|
|
30
39
|
}
|
|
31
40
|
/** Check whether a key exists in localStorage. */
|
|
32
41
|
static has(key) {
|
|
33
|
-
return localStorage.getItem(key) !== null;
|
|
42
|
+
return this.attempt(() => localStorage.getItem(key) !== null, false);
|
|
34
43
|
}
|
|
35
44
|
/** Return all keys currently stored in localStorage. */
|
|
36
45
|
static keys() {
|
|
37
|
-
return Object.keys(localStorage);
|
|
46
|
+
return this.attempt(() => Object.keys(localStorage), []);
|
|
38
47
|
}
|
|
39
48
|
/** Retrieve the stored access token. */
|
|
40
49
|
static getToken() {
|
|
@@ -67,9 +76,32 @@ var LocalStore = class {
|
|
|
67
76
|
this.remove(USER_KEY);
|
|
68
77
|
}
|
|
69
78
|
};
|
|
79
|
+
|
|
80
|
+
// src/download-blob.ts
|
|
81
|
+
function downloadBlob(blob, fileName, options = {}) {
|
|
82
|
+
if (typeof window === "undefined" || typeof document === "undefined") {
|
|
83
|
+
throw new Error("downloadBlob requires a browser");
|
|
84
|
+
}
|
|
85
|
+
const url = window.URL.createObjectURL(blob);
|
|
86
|
+
const link = document.createElement("a");
|
|
87
|
+
try {
|
|
88
|
+
link.href = url;
|
|
89
|
+
link.download = fileName;
|
|
90
|
+
document.body.appendChild(link);
|
|
91
|
+
link.click();
|
|
92
|
+
} finally {
|
|
93
|
+
link.remove();
|
|
94
|
+
if (options.revokeDelayMs && options.revokeDelayMs > 0) {
|
|
95
|
+
setTimeout(() => window.URL.revokeObjectURL(url), options.revokeDelayMs);
|
|
96
|
+
} else {
|
|
97
|
+
window.URL.revokeObjectURL(url);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
70
101
|
export {
|
|
71
102
|
LocalStore,
|
|
72
103
|
REFRESH_TOKEN_KEY,
|
|
73
104
|
TOKEN_KEY,
|
|
74
|
-
USER_KEY
|
|
105
|
+
USER_KEY,
|
|
106
|
+
downloadBlob
|
|
75
107
|
};
|