@choksheak/react-utils 0.1.6 → 0.1.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/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/sharedQuery.SharedQuery.html +15 -0
- package/docs/classes/sharedState.SharedState.html +8 -0
- package/docs/classes/sharedState.SharedStateConfig.html +5 -5
- package/docs/classes/utils_storage.InMemoryStorageAdapter.html +9 -0
- package/docs/classes/utils_storage.IndexedDbAdapter.html +9 -0
- package/docs/classes/utils_storage.LocalStorageAdapter.html +9 -0
- package/docs/functions/sharedQuery.sharedQuery.html +5 -0
- package/docs/functions/sharedQuery.useQuery.html +4 -0
- package/docs/functions/sharedState.sharedState.html +2 -2
- package/docs/functions/sharedState.useSharedState.html +2 -2
- package/docs/functions/sharedState.useSharedStateSetter.html +2 -2
- package/docs/functions/sharedState.useSharedStateValue.html +2 -2
- package/docs/functions/utils_storage.getStorageAdapter.html +1 -0
- package/docs/functions/utils_stringify.stringifyDeterministicForKeys.html +7 -0
- package/docs/modules/sharedQuery.html +1 -0
- package/docs/modules/sharedState.html +1 -1
- package/docs/modules/utils_storage.html +1 -0
- package/docs/modules/utils_stringify.html +1 -0
- package/docs/modules.html +1 -1
- package/docs/types/sharedQuery.FetchFn.html +1 -0
- package/docs/types/sharedQuery.FetchedData.html +3 -0
- package/docs/types/sharedQuery.QueryStateValue.html +4 -0
- package/docs/types/sharedQuery.SharedQueryOptions.html +1 -0
- package/docs/types/sharedQuery.SharedQueryState.html +3 -0
- package/docs/types/sharedState.SharedStateOptions.html +1 -0
- package/docs/types/utils_storage.StorageAdapter.html +5 -0
- package/docs/types/utils_storage.StorageOptions.html +5 -0
- package/docs/variables/fetcher.fetcher.html +1 -1
- package/docs/variables/fetcher.fetcherWithRetries.html +1 -1
- package/package.json +9 -6
- package/sharedQuery.d.mts +61 -0
- package/sharedQuery.d.ts +58 -0
- package/sharedQuery.js +471 -0
- package/sharedQuery.mjs +444 -0
- package/sharedState.d.mts +21 -16
- package/sharedState.d.ts +19 -15
- package/sharedState.js +159 -88
- package/sharedState.mjs +158 -88
- package/src/sharedQuery.ts +303 -0
- package/src/sharedState.ts +77 -140
- package/src/utils/storage.ts +197 -0
- package/src/utils/stringify.test.ts +38 -0
- package/src/utils/stringify.ts +49 -0
- package/utils/storage.d.mts +46 -0
- package/utils/storage.d.ts +44 -0
- package/utils/storage.js +162 -0
- package/utils/storage.mjs +134 -0
- package/utils/stringify.d.mts +12 -0
- package/utils/stringify.d.ts +10 -0
- package/utils/stringify.js +58 -0
- package/utils/stringify.mjs +33 -0
- package/utils/stringify.test.d.mts +2 -0
- package/utils/stringify.test.d.ts +2 -0
- package/utils/stringify.test.js +64 -0
- package/utils/stringify.test.mjs +81 -0
- package/docs/types/sharedState.SharedState.html +0 -6
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { kvStore } from "@choksheak/ts-utils/kvStore";
|
|
2
|
+
|
|
3
|
+
export type StorageAdapter<T> = {
|
|
4
|
+
save: (key: string, value: T) => Promise<void> | void;
|
|
5
|
+
load: (key: string) => Promise<T | undefined> | T | undefined;
|
|
6
|
+
delete: (key: string) => Promise<void> | void;
|
|
7
|
+
clear: () => Promise<void> | void;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type StorageOptions<T> = {
|
|
11
|
+
// If specified, this will be used and all other fields will be ignored.
|
|
12
|
+
store?: StorageAdapter<T>;
|
|
13
|
+
|
|
14
|
+
// Storage key in local storage for this data.
|
|
15
|
+
localStorageKey?: string;
|
|
16
|
+
|
|
17
|
+
// Storage key in indexed db for this data.
|
|
18
|
+
indexedDbKey?: string;
|
|
19
|
+
|
|
20
|
+
// How long to wait before the value is considered expired.
|
|
21
|
+
storeExpiryMs?: number;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export function getStorageAdapter<T>(
|
|
25
|
+
options: StorageOptions<T> | undefined,
|
|
26
|
+
defaultStoreExpiryMs: number,
|
|
27
|
+
): StorageAdapter<T> | null {
|
|
28
|
+
const { localStorageKey, indexedDbKey, storeExpiryMs, store } = options ?? {};
|
|
29
|
+
|
|
30
|
+
if (store) {
|
|
31
|
+
return store;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const expiryMs = storeExpiryMs || defaultStoreExpiryMs;
|
|
35
|
+
|
|
36
|
+
if (localStorageKey) {
|
|
37
|
+
return new LocalStorageAdapter<T>(localStorageKey, expiryMs);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (indexedDbKey) {
|
|
41
|
+
return new IndexedDbAdapter<T>(indexedDbKey, expiryMs);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/************************************************************************/
|
|
48
|
+
/* Storage adapters */
|
|
49
|
+
/************************************************************************/
|
|
50
|
+
|
|
51
|
+
type ValueWithExpiration<T> = { value: T; expiresAt: number };
|
|
52
|
+
|
|
53
|
+
/** Save state to memory only - not persisted. */
|
|
54
|
+
export class InMemoryStorageAdapter<T> implements StorageAdapter<T> {
|
|
55
|
+
private readonly cache = new Map<string, ValueWithExpiration<T>>();
|
|
56
|
+
|
|
57
|
+
public constructor(
|
|
58
|
+
public readonly keyPrefix: string,
|
|
59
|
+
public readonly expiryMs: number,
|
|
60
|
+
) {}
|
|
61
|
+
|
|
62
|
+
public save(key: string, value: T): void {
|
|
63
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
64
|
+
this.cache.set(fullKey, {
|
|
65
|
+
value,
|
|
66
|
+
expiresAt: Date.now() + this.expiryMs,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public load(key: string): T | undefined {
|
|
71
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
72
|
+
const cached = this.cache.get(fullKey);
|
|
73
|
+
|
|
74
|
+
if (cached && Date.now() < cached.expiresAt) {
|
|
75
|
+
return cached.value;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public delete(key: string): void {
|
|
82
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
83
|
+
this.cache.delete(fullKey);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public clear(): void {
|
|
87
|
+
this.cache.clear();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Save state to local storage. */
|
|
92
|
+
export class LocalStorageAdapter<T> implements StorageAdapter<T> {
|
|
93
|
+
public constructor(
|
|
94
|
+
public readonly keyPrefix: string,
|
|
95
|
+
public readonly expiryMs: number,
|
|
96
|
+
) {}
|
|
97
|
+
|
|
98
|
+
public save(key: string, value: T): void {
|
|
99
|
+
if (typeof window === "undefined") return;
|
|
100
|
+
|
|
101
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
102
|
+
window.localStorage.setItem(
|
|
103
|
+
fullKey,
|
|
104
|
+
JSON.stringify({
|
|
105
|
+
value,
|
|
106
|
+
expiresAt: this.expiryMs ? Date.now() + this.expiryMs : 0,
|
|
107
|
+
} satisfies ValueWithExpiration<T>),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public load(key: string): T | undefined {
|
|
112
|
+
if (typeof window === "undefined") return undefined;
|
|
113
|
+
|
|
114
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
115
|
+
const raw = window.localStorage.getItem(fullKey);
|
|
116
|
+
if (!raw) return undefined;
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
const box = JSON.parse(raw) as ValueWithExpiration<T>;
|
|
120
|
+
if (!box || (box.expiresAt && Date.now() >= box.expiresAt)) {
|
|
121
|
+
window.localStorage.removeItem(fullKey);
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return box.value;
|
|
126
|
+
} catch {
|
|
127
|
+
// Ignore error.
|
|
128
|
+
window.localStorage.removeItem(fullKey);
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public delete(key: string): void {
|
|
134
|
+
if (typeof window === "undefined") return;
|
|
135
|
+
|
|
136
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
137
|
+
window.localStorage.removeItem(fullKey);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public clear(): void {
|
|
141
|
+
if (typeof window === "undefined") return;
|
|
142
|
+
|
|
143
|
+
for (const key of Object.keys(window.localStorage)) {
|
|
144
|
+
if (key.startsWith(this.keyPrefix)) {
|
|
145
|
+
window.localStorage.removeItem(key);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Save state to indexed db. */
|
|
152
|
+
export class IndexedDbAdapter<T> implements StorageAdapter<T> {
|
|
153
|
+
public constructor(
|
|
154
|
+
public readonly keyPrefix: string,
|
|
155
|
+
public readonly expiryMs: number,
|
|
156
|
+
) {}
|
|
157
|
+
|
|
158
|
+
public async save(key: string, value: T): Promise<void> {
|
|
159
|
+
if (typeof window === "undefined") return;
|
|
160
|
+
|
|
161
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
162
|
+
await kvStore.set(fullKey, JSON.stringify(value), this.expiryMs);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public async load(key: string): Promise<T | undefined> {
|
|
166
|
+
if (typeof window === "undefined") return undefined;
|
|
167
|
+
|
|
168
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
169
|
+
const raw = await kvStore.get<string>(fullKey);
|
|
170
|
+
if (raw === undefined) return undefined;
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
return JSON.parse(raw);
|
|
174
|
+
} catch {
|
|
175
|
+
// Ignore error.
|
|
176
|
+
await kvStore.delete(fullKey);
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
public async delete(key: string): Promise<void> {
|
|
182
|
+
if (typeof window === "undefined") return;
|
|
183
|
+
|
|
184
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
185
|
+
await kvStore.delete(fullKey);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
public async clear(): Promise<void> {
|
|
189
|
+
if (typeof window === "undefined") return;
|
|
190
|
+
|
|
191
|
+
await kvStore.forEach(async (k) => {
|
|
192
|
+
if (k.startsWith(this.keyPrefix)) {
|
|
193
|
+
await kvStore.delete(k);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { stringifyDeterministicForKeys } from "./stringify";
|
|
2
|
+
|
|
3
|
+
describe("stringifyDeterministicForKeys", () => {
|
|
4
|
+
test("happy", () => {
|
|
5
|
+
const expected = `{"a":1,"b":2,"c":{"e":1,"f":2}}`;
|
|
6
|
+
|
|
7
|
+
const actual1 = stringifyDeterministicForKeys({
|
|
8
|
+
a: 1,
|
|
9
|
+
b: 2,
|
|
10
|
+
c: { e: 1, f: 2 },
|
|
11
|
+
});
|
|
12
|
+
expect(actual1).toBe(expected);
|
|
13
|
+
|
|
14
|
+
const actual2 = stringifyDeterministicForKeys({
|
|
15
|
+
c: { f: 2, e: 1 },
|
|
16
|
+
b: 2,
|
|
17
|
+
a: 1,
|
|
18
|
+
});
|
|
19
|
+
expect(actual2).toBe(expected);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("circular", () => {
|
|
23
|
+
const circular = { b: {}, a: 1 };
|
|
24
|
+
circular.b = circular;
|
|
25
|
+
|
|
26
|
+
const actual = stringifyDeterministicForKeys(circular);
|
|
27
|
+
|
|
28
|
+
expect(actual).toBe(`{"a":1,"b":"[Circular]"}`);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("function", () => {
|
|
32
|
+
const fn = { b: {}, a: () => {} };
|
|
33
|
+
|
|
34
|
+
expect(() => stringifyDeterministicForKeys(fn)).toThrow(
|
|
35
|
+
`Functions should not be passed into query parameters`,
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON stringify a value of any type, and returns an unsafe string. The string
|
|
3
|
+
* is "unsafe" because you might not be able to restore the original object
|
|
4
|
+
* using the serialized JSON. This is specifically used for query key generation
|
|
5
|
+
* and is not suitable for more general uses.
|
|
6
|
+
*
|
|
7
|
+
* Note: The given value should not contain any functions because functions
|
|
8
|
+
* cannot be serialized and deserialized properly.
|
|
9
|
+
*/
|
|
10
|
+
export function stringifyDeterministicForKeys(params: unknown): string {
|
|
11
|
+
const cache = new Set();
|
|
12
|
+
|
|
13
|
+
return JSON.stringify(params, (_, value) => {
|
|
14
|
+
if (typeof value === "function") {
|
|
15
|
+
throw new Error(`Functions should not be passed into query parameters`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (!value || typeof value !== "object") {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (cache.has(value)) {
|
|
23
|
+
// Circular reference detected! Return a placeholder string.
|
|
24
|
+
return "[Circular]";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
cache.add(value);
|
|
28
|
+
|
|
29
|
+
// Don't crash on circular references, but don't mark the top level object
|
|
30
|
+
// as circular because we want to return as much string as possible.
|
|
31
|
+
if (value !== params) {
|
|
32
|
+
try {
|
|
33
|
+
JSON.stringify(value);
|
|
34
|
+
} catch {
|
|
35
|
+
return "[Unserializable]";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return Object.keys(value)
|
|
40
|
+
.sort()
|
|
41
|
+
.reduce(
|
|
42
|
+
(result: Record<string, unknown>, key: string) => {
|
|
43
|
+
result[key] = value[key];
|
|
44
|
+
return result;
|
|
45
|
+
},
|
|
46
|
+
{} as Record<string, unknown>,
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
type StorageAdapter<T> = {
|
|
2
|
+
save: (key: string, value: T) => Promise<void> | void;
|
|
3
|
+
load: (key: string) => Promise<T | undefined> | T | undefined;
|
|
4
|
+
delete: (key: string) => Promise<void> | void;
|
|
5
|
+
clear: () => Promise<void> | void;
|
|
6
|
+
};
|
|
7
|
+
type StorageOptions<T> = {
|
|
8
|
+
store?: StorageAdapter<T>;
|
|
9
|
+
localStorageKey?: string;
|
|
10
|
+
indexedDbKey?: string;
|
|
11
|
+
storeExpiryMs?: number;
|
|
12
|
+
};
|
|
13
|
+
declare function getStorageAdapter<T>(options: StorageOptions<T> | undefined, defaultStoreExpiryMs: number): StorageAdapter<T> | null;
|
|
14
|
+
/** Save state to memory only - not persisted. */
|
|
15
|
+
declare class InMemoryStorageAdapter<T> implements StorageAdapter<T> {
|
|
16
|
+
readonly keyPrefix: string;
|
|
17
|
+
readonly expiryMs: number;
|
|
18
|
+
private readonly cache;
|
|
19
|
+
constructor(keyPrefix: string, expiryMs: number);
|
|
20
|
+
save(key: string, value: T): void;
|
|
21
|
+
load(key: string): T | undefined;
|
|
22
|
+
delete(key: string): void;
|
|
23
|
+
clear(): void;
|
|
24
|
+
}
|
|
25
|
+
/** Save state to local storage. */
|
|
26
|
+
declare class LocalStorageAdapter<T> implements StorageAdapter<T> {
|
|
27
|
+
readonly keyPrefix: string;
|
|
28
|
+
readonly expiryMs: number;
|
|
29
|
+
constructor(keyPrefix: string, expiryMs: number);
|
|
30
|
+
save(key: string, value: T): void;
|
|
31
|
+
load(key: string): T | undefined;
|
|
32
|
+
delete(key: string): void;
|
|
33
|
+
clear(): void;
|
|
34
|
+
}
|
|
35
|
+
/** Save state to indexed db. */
|
|
36
|
+
declare class IndexedDbAdapter<T> implements StorageAdapter<T> {
|
|
37
|
+
readonly keyPrefix: string;
|
|
38
|
+
readonly expiryMs: number;
|
|
39
|
+
constructor(keyPrefix: string, expiryMs: number);
|
|
40
|
+
save(key: string, value: T): Promise<void>;
|
|
41
|
+
load(key: string): Promise<T | undefined>;
|
|
42
|
+
delete(key: string): Promise<void>;
|
|
43
|
+
clear(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { InMemoryStorageAdapter, IndexedDbAdapter, LocalStorageAdapter, type StorageAdapter, type StorageOptions, getStorageAdapter };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export type StorageAdapter<T> = {
|
|
2
|
+
save: (key: string, value: T) => Promise<void> | void;
|
|
3
|
+
load: (key: string) => Promise<T | undefined> | T | undefined;
|
|
4
|
+
delete: (key: string) => Promise<void> | void;
|
|
5
|
+
clear: () => Promise<void> | void;
|
|
6
|
+
};
|
|
7
|
+
export type StorageOptions<T> = {
|
|
8
|
+
store?: StorageAdapter<T>;
|
|
9
|
+
localStorageKey?: string;
|
|
10
|
+
indexedDbKey?: string;
|
|
11
|
+
storeExpiryMs?: number;
|
|
12
|
+
};
|
|
13
|
+
export declare function getStorageAdapter<T>(options: StorageOptions<T> | undefined, defaultStoreExpiryMs: number): StorageAdapter<T> | null;
|
|
14
|
+
/** Save state to memory only - not persisted. */
|
|
15
|
+
export declare class InMemoryStorageAdapter<T> implements StorageAdapter<T> {
|
|
16
|
+
readonly keyPrefix: string;
|
|
17
|
+
readonly expiryMs: number;
|
|
18
|
+
private readonly cache;
|
|
19
|
+
constructor(keyPrefix: string, expiryMs: number);
|
|
20
|
+
save(key: string, value: T): void;
|
|
21
|
+
load(key: string): T | undefined;
|
|
22
|
+
delete(key: string): void;
|
|
23
|
+
clear(): void;
|
|
24
|
+
}
|
|
25
|
+
/** Save state to local storage. */
|
|
26
|
+
export declare class LocalStorageAdapter<T> implements StorageAdapter<T> {
|
|
27
|
+
readonly keyPrefix: string;
|
|
28
|
+
readonly expiryMs: number;
|
|
29
|
+
constructor(keyPrefix: string, expiryMs: number);
|
|
30
|
+
save(key: string, value: T): void;
|
|
31
|
+
load(key: string): T | undefined;
|
|
32
|
+
delete(key: string): void;
|
|
33
|
+
clear(): void;
|
|
34
|
+
}
|
|
35
|
+
/** Save state to indexed db. */
|
|
36
|
+
export declare class IndexedDbAdapter<T> implements StorageAdapter<T> {
|
|
37
|
+
readonly keyPrefix: string;
|
|
38
|
+
readonly expiryMs: number;
|
|
39
|
+
constructor(keyPrefix: string, expiryMs: number);
|
|
40
|
+
save(key: string, value: T): Promise<void>;
|
|
41
|
+
load(key: string): Promise<T | undefined>;
|
|
42
|
+
delete(key: string): Promise<void>;
|
|
43
|
+
clear(): Promise<void>;
|
|
44
|
+
}
|
package/utils/storage.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
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/utils/storage.ts
|
|
21
|
+
var storage_exports = {};
|
|
22
|
+
__export(storage_exports, {
|
|
23
|
+
InMemoryStorageAdapter: () => InMemoryStorageAdapter,
|
|
24
|
+
IndexedDbAdapter: () => IndexedDbAdapter,
|
|
25
|
+
LocalStorageAdapter: () => LocalStorageAdapter,
|
|
26
|
+
getStorageAdapter: () => getStorageAdapter
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(storage_exports);
|
|
29
|
+
var import_kvStore = require("@choksheak/ts-utils/kvStore");
|
|
30
|
+
function getStorageAdapter(options, defaultStoreExpiryMs) {
|
|
31
|
+
const { localStorageKey, indexedDbKey, storeExpiryMs, store } = options ?? {};
|
|
32
|
+
if (store) {
|
|
33
|
+
return store;
|
|
34
|
+
}
|
|
35
|
+
const expiryMs = storeExpiryMs || defaultStoreExpiryMs;
|
|
36
|
+
if (localStorageKey) {
|
|
37
|
+
return new LocalStorageAdapter(localStorageKey, expiryMs);
|
|
38
|
+
}
|
|
39
|
+
if (indexedDbKey) {
|
|
40
|
+
return new IndexedDbAdapter(indexedDbKey, expiryMs);
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
var InMemoryStorageAdapter = class {
|
|
45
|
+
constructor(keyPrefix, expiryMs) {
|
|
46
|
+
this.keyPrefix = keyPrefix;
|
|
47
|
+
this.expiryMs = expiryMs;
|
|
48
|
+
}
|
|
49
|
+
cache = /* @__PURE__ */ new Map();
|
|
50
|
+
save(key, value) {
|
|
51
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
52
|
+
this.cache.set(fullKey, {
|
|
53
|
+
value,
|
|
54
|
+
expiresAt: Date.now() + this.expiryMs
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
load(key) {
|
|
58
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
59
|
+
const cached = this.cache.get(fullKey);
|
|
60
|
+
if (cached && Date.now() < cached.expiresAt) {
|
|
61
|
+
return cached.value;
|
|
62
|
+
}
|
|
63
|
+
return void 0;
|
|
64
|
+
}
|
|
65
|
+
delete(key) {
|
|
66
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
67
|
+
this.cache.delete(fullKey);
|
|
68
|
+
}
|
|
69
|
+
clear() {
|
|
70
|
+
this.cache.clear();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var LocalStorageAdapter = class {
|
|
74
|
+
constructor(keyPrefix, expiryMs) {
|
|
75
|
+
this.keyPrefix = keyPrefix;
|
|
76
|
+
this.expiryMs = expiryMs;
|
|
77
|
+
}
|
|
78
|
+
save(key, value) {
|
|
79
|
+
if (typeof window === "undefined") return;
|
|
80
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
81
|
+
window.localStorage.setItem(
|
|
82
|
+
fullKey,
|
|
83
|
+
JSON.stringify({
|
|
84
|
+
value,
|
|
85
|
+
expiresAt: this.expiryMs ? Date.now() + this.expiryMs : 0
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
load(key) {
|
|
90
|
+
if (typeof window === "undefined") return void 0;
|
|
91
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
92
|
+
const raw = window.localStorage.getItem(fullKey);
|
|
93
|
+
if (!raw) return void 0;
|
|
94
|
+
try {
|
|
95
|
+
const box = JSON.parse(raw);
|
|
96
|
+
if (!box || box.expiresAt && Date.now() >= box.expiresAt) {
|
|
97
|
+
window.localStorage.removeItem(fullKey);
|
|
98
|
+
return void 0;
|
|
99
|
+
}
|
|
100
|
+
return box.value;
|
|
101
|
+
} catch {
|
|
102
|
+
window.localStorage.removeItem(fullKey);
|
|
103
|
+
return void 0;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
delete(key) {
|
|
107
|
+
if (typeof window === "undefined") return;
|
|
108
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
109
|
+
window.localStorage.removeItem(fullKey);
|
|
110
|
+
}
|
|
111
|
+
clear() {
|
|
112
|
+
if (typeof window === "undefined") return;
|
|
113
|
+
for (const key of Object.keys(window.localStorage)) {
|
|
114
|
+
if (key.startsWith(this.keyPrefix)) {
|
|
115
|
+
window.localStorage.removeItem(key);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
var IndexedDbAdapter = class {
|
|
121
|
+
constructor(keyPrefix, expiryMs) {
|
|
122
|
+
this.keyPrefix = keyPrefix;
|
|
123
|
+
this.expiryMs = expiryMs;
|
|
124
|
+
}
|
|
125
|
+
async save(key, value) {
|
|
126
|
+
if (typeof window === "undefined") return;
|
|
127
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
128
|
+
await import_kvStore.kvStore.set(fullKey, JSON.stringify(value), this.expiryMs);
|
|
129
|
+
}
|
|
130
|
+
async load(key) {
|
|
131
|
+
if (typeof window === "undefined") return void 0;
|
|
132
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
133
|
+
const raw = await import_kvStore.kvStore.get(fullKey);
|
|
134
|
+
if (raw === void 0) return void 0;
|
|
135
|
+
try {
|
|
136
|
+
return JSON.parse(raw);
|
|
137
|
+
} catch {
|
|
138
|
+
await import_kvStore.kvStore.delete(fullKey);
|
|
139
|
+
return void 0;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
async delete(key) {
|
|
143
|
+
if (typeof window === "undefined") return;
|
|
144
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
145
|
+
await import_kvStore.kvStore.delete(fullKey);
|
|
146
|
+
}
|
|
147
|
+
async clear() {
|
|
148
|
+
if (typeof window === "undefined") return;
|
|
149
|
+
await import_kvStore.kvStore.forEach(async (k) => {
|
|
150
|
+
if (k.startsWith(this.keyPrefix)) {
|
|
151
|
+
await import_kvStore.kvStore.delete(k);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
157
|
+
0 && (module.exports = {
|
|
158
|
+
InMemoryStorageAdapter,
|
|
159
|
+
IndexedDbAdapter,
|
|
160
|
+
LocalStorageAdapter,
|
|
161
|
+
getStorageAdapter
|
|
162
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// src/utils/storage.ts
|
|
2
|
+
import { kvStore } from "@choksheak/ts-utils/kvStore";
|
|
3
|
+
function getStorageAdapter(options, defaultStoreExpiryMs) {
|
|
4
|
+
const { localStorageKey, indexedDbKey, storeExpiryMs, store } = options ?? {};
|
|
5
|
+
if (store) {
|
|
6
|
+
return store;
|
|
7
|
+
}
|
|
8
|
+
const expiryMs = storeExpiryMs || defaultStoreExpiryMs;
|
|
9
|
+
if (localStorageKey) {
|
|
10
|
+
return new LocalStorageAdapter(localStorageKey, expiryMs);
|
|
11
|
+
}
|
|
12
|
+
if (indexedDbKey) {
|
|
13
|
+
return new IndexedDbAdapter(indexedDbKey, expiryMs);
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
var InMemoryStorageAdapter = class {
|
|
18
|
+
constructor(keyPrefix, expiryMs) {
|
|
19
|
+
this.keyPrefix = keyPrefix;
|
|
20
|
+
this.expiryMs = expiryMs;
|
|
21
|
+
}
|
|
22
|
+
cache = /* @__PURE__ */ new Map();
|
|
23
|
+
save(key, value) {
|
|
24
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
25
|
+
this.cache.set(fullKey, {
|
|
26
|
+
value,
|
|
27
|
+
expiresAt: Date.now() + this.expiryMs
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
load(key) {
|
|
31
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
32
|
+
const cached = this.cache.get(fullKey);
|
|
33
|
+
if (cached && Date.now() < cached.expiresAt) {
|
|
34
|
+
return cached.value;
|
|
35
|
+
}
|
|
36
|
+
return void 0;
|
|
37
|
+
}
|
|
38
|
+
delete(key) {
|
|
39
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
40
|
+
this.cache.delete(fullKey);
|
|
41
|
+
}
|
|
42
|
+
clear() {
|
|
43
|
+
this.cache.clear();
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
var LocalStorageAdapter = class {
|
|
47
|
+
constructor(keyPrefix, expiryMs) {
|
|
48
|
+
this.keyPrefix = keyPrefix;
|
|
49
|
+
this.expiryMs = expiryMs;
|
|
50
|
+
}
|
|
51
|
+
save(key, value) {
|
|
52
|
+
if (typeof window === "undefined") return;
|
|
53
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
54
|
+
window.localStorage.setItem(
|
|
55
|
+
fullKey,
|
|
56
|
+
JSON.stringify({
|
|
57
|
+
value,
|
|
58
|
+
expiresAt: this.expiryMs ? Date.now() + this.expiryMs : 0
|
|
59
|
+
})
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
load(key) {
|
|
63
|
+
if (typeof window === "undefined") return void 0;
|
|
64
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
65
|
+
const raw = window.localStorage.getItem(fullKey);
|
|
66
|
+
if (!raw) return void 0;
|
|
67
|
+
try {
|
|
68
|
+
const box = JSON.parse(raw);
|
|
69
|
+
if (!box || box.expiresAt && Date.now() >= box.expiresAt) {
|
|
70
|
+
window.localStorage.removeItem(fullKey);
|
|
71
|
+
return void 0;
|
|
72
|
+
}
|
|
73
|
+
return box.value;
|
|
74
|
+
} catch {
|
|
75
|
+
window.localStorage.removeItem(fullKey);
|
|
76
|
+
return void 0;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
delete(key) {
|
|
80
|
+
if (typeof window === "undefined") return;
|
|
81
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
82
|
+
window.localStorage.removeItem(fullKey);
|
|
83
|
+
}
|
|
84
|
+
clear() {
|
|
85
|
+
if (typeof window === "undefined") return;
|
|
86
|
+
for (const key of Object.keys(window.localStorage)) {
|
|
87
|
+
if (key.startsWith(this.keyPrefix)) {
|
|
88
|
+
window.localStorage.removeItem(key);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var IndexedDbAdapter = class {
|
|
94
|
+
constructor(keyPrefix, expiryMs) {
|
|
95
|
+
this.keyPrefix = keyPrefix;
|
|
96
|
+
this.expiryMs = expiryMs;
|
|
97
|
+
}
|
|
98
|
+
async save(key, value) {
|
|
99
|
+
if (typeof window === "undefined") return;
|
|
100
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
101
|
+
await kvStore.set(fullKey, JSON.stringify(value), this.expiryMs);
|
|
102
|
+
}
|
|
103
|
+
async load(key) {
|
|
104
|
+
if (typeof window === "undefined") return void 0;
|
|
105
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
106
|
+
const raw = await kvStore.get(fullKey);
|
|
107
|
+
if (raw === void 0) return void 0;
|
|
108
|
+
try {
|
|
109
|
+
return JSON.parse(raw);
|
|
110
|
+
} catch {
|
|
111
|
+
await kvStore.delete(fullKey);
|
|
112
|
+
return void 0;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async delete(key) {
|
|
116
|
+
if (typeof window === "undefined") return;
|
|
117
|
+
const fullKey = this.keyPrefix + ":" + key;
|
|
118
|
+
await kvStore.delete(fullKey);
|
|
119
|
+
}
|
|
120
|
+
async clear() {
|
|
121
|
+
if (typeof window === "undefined") return;
|
|
122
|
+
await kvStore.forEach(async (k) => {
|
|
123
|
+
if (k.startsWith(this.keyPrefix)) {
|
|
124
|
+
await kvStore.delete(k);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
export {
|
|
130
|
+
InMemoryStorageAdapter,
|
|
131
|
+
IndexedDbAdapter,
|
|
132
|
+
LocalStorageAdapter,
|
|
133
|
+
getStorageAdapter
|
|
134
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON stringify a value of any type, and returns an unsafe string. The string
|
|
3
|
+
* is "unsafe" because you might not be able to restore the original object
|
|
4
|
+
* using the serialized JSON. This is specifically used for query key generation
|
|
5
|
+
* and is not suitable for more general uses.
|
|
6
|
+
*
|
|
7
|
+
* Note: The given value should not contain any functions because functions
|
|
8
|
+
* cannot be serialized and deserialized properly.
|
|
9
|
+
*/
|
|
10
|
+
declare function stringifyDeterministicForKeys(params: unknown): string;
|
|
11
|
+
|
|
12
|
+
export { stringifyDeterministicForKeys };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON stringify a value of any type, and returns an unsafe string. The string
|
|
3
|
+
* is "unsafe" because you might not be able to restore the original object
|
|
4
|
+
* using the serialized JSON. This is specifically used for query key generation
|
|
5
|
+
* and is not suitable for more general uses.
|
|
6
|
+
*
|
|
7
|
+
* Note: The given value should not contain any functions because functions
|
|
8
|
+
* cannot be serialized and deserialized properly.
|
|
9
|
+
*/
|
|
10
|
+
export declare function stringifyDeterministicForKeys(params: unknown): string;
|