@nice-code/util 0.2.7 → 0.2.9
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/build/index.js +8 -184
- package/build/types/storage_adapter/StorageAdapter.d.ts +8 -1
- package/build/types/storage_adapter/specific/browser/browser_storage.d.ts +2 -2
- package/build/types/storage_adapter/specific/durable_object/durable_object_storage.d.ts +1 -1
- package/build/types/storage_adapter/specific/memory/memory_storage.d.ts +2 -2
- package/package.json +1 -1
package/build/index.js
CHANGED
|
@@ -1,184 +1,8 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class StorageAdapter {
|
|
10
|
-
implementation;
|
|
11
|
-
constructor(implementation) {
|
|
12
|
-
this.implementation = implementation;
|
|
13
|
-
}
|
|
14
|
-
async removeItem(key) {
|
|
15
|
-
await this.implementation.removeItem(key);
|
|
16
|
-
}
|
|
17
|
-
async setJson(key, value) {
|
|
18
|
-
if (this.implementation.type === "string" /* string */) {
|
|
19
|
-
await this.implementation.setItem(key, JSON.stringify(value));
|
|
20
|
-
} else {
|
|
21
|
-
await this.implementation.setItem(key, value);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
async getJson(key) {
|
|
25
|
-
if (this.implementation.type === "string" /* string */) {
|
|
26
|
-
const val = await this.implementation.getItem(key);
|
|
27
|
-
if (val == null || val === "undefined" || val === "null") {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
return JSON.parse(val);
|
|
31
|
-
} else {
|
|
32
|
-
const val = await this.implementation.getItem(key);
|
|
33
|
-
if (val == null || val === "undefined" || val === "null") {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
return val;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
async getJsonOrDef(key, defVal) {
|
|
40
|
-
if (this.implementation.type === "string" /* string */) {
|
|
41
|
-
const val2 = await this.implementation.getItem(key);
|
|
42
|
-
if (val2 == null || val2 === "undefined" || val2 === "null") {
|
|
43
|
-
return defVal;
|
|
44
|
-
}
|
|
45
|
-
return JSON.parse(val2);
|
|
46
|
-
}
|
|
47
|
-
const val = await this.implementation.getItem(key);
|
|
48
|
-
if (val == null || val === "undefined" || val === "null") {
|
|
49
|
-
return defVal;
|
|
50
|
-
}
|
|
51
|
-
return val;
|
|
52
|
-
}
|
|
53
|
-
createJsonGetterSetter(key) {
|
|
54
|
-
return {
|
|
55
|
-
get: () => this.getJson(key),
|
|
56
|
-
set: (value) => this.setJson(key, value)
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
// src/storage_adapter/typed_storage/createTypedStorage.ts
|
|
61
|
-
function createTypedStorage({
|
|
62
|
-
storageAdapter
|
|
63
|
-
}) {
|
|
64
|
-
const getJson = async (key) => {
|
|
65
|
-
return storageAdapter.getJson(key);
|
|
66
|
-
};
|
|
67
|
-
const getJsonOrDef = async (key, defVal) => {
|
|
68
|
-
return await storageAdapter.getJson(key) ?? defVal;
|
|
69
|
-
};
|
|
70
|
-
const setJson = async (key, val) => {
|
|
71
|
-
return storageAdapter.setJson(key, val);
|
|
72
|
-
};
|
|
73
|
-
const removeItem = async (key) => {
|
|
74
|
-
await storageAdapter.removeItem(key);
|
|
75
|
-
};
|
|
76
|
-
return {
|
|
77
|
-
getJson,
|
|
78
|
-
getJsonOrDef,
|
|
79
|
-
setJson,
|
|
80
|
-
removeItem
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// src/storage_adapter/specific/browser/browser_storage.ts
|
|
85
|
-
function createWebLocalStorageMethods(_localStorage) {
|
|
86
|
-
return {
|
|
87
|
-
type: "string" /* string */,
|
|
88
|
-
getItem: async (key) => _localStorage.getItem(key),
|
|
89
|
-
setItem: async (key, value) => {
|
|
90
|
-
_localStorage.setItem(key, value);
|
|
91
|
-
},
|
|
92
|
-
removeItem: async (key) => {
|
|
93
|
-
_localStorage.removeItem(key);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
function createTypedWebLocalStorage(_localStorage) {
|
|
98
|
-
return createTypedStorage({
|
|
99
|
-
storageAdapter: new StorageAdapter(createWebLocalStorageMethods(_localStorage))
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
function createWebSessionStorageMethods(_sessionStorage) {
|
|
103
|
-
return {
|
|
104
|
-
type: "string" /* string */,
|
|
105
|
-
getItem: async (key) => _sessionStorage.getItem(key),
|
|
106
|
-
setItem: async (key, value) => {
|
|
107
|
-
_sessionStorage.setItem(key, value);
|
|
108
|
-
},
|
|
109
|
-
removeItem: async (key) => {
|
|
110
|
-
_sessionStorage.removeItem(key);
|
|
111
|
-
}
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
function createTypedWebSessionStorage(_sessionStorage) {
|
|
115
|
-
return createTypedStorage({
|
|
116
|
-
storageAdapter: new StorageAdapter(createWebSessionStorageMethods(_sessionStorage))
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
// src/storage_adapter/specific/durable_object/durable_object_storage.ts
|
|
120
|
-
function createDurableObjectStorageMethods(durableObjectStorage) {
|
|
121
|
-
return {
|
|
122
|
-
type: "json" /* json */,
|
|
123
|
-
getItem: (key) => durableObjectStorage.get(key),
|
|
124
|
-
setItem: (key, value) => durableObjectStorage.put(key, value),
|
|
125
|
-
removeItem: async (key) => {
|
|
126
|
-
await durableObjectStorage.delete(key);
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
function createDurableObjectTypedStorage(durableObjectStorage) {
|
|
131
|
-
return createTypedStorage({
|
|
132
|
-
storageAdapter: new StorageAdapter(createDurableObjectStorageMethods(durableObjectStorage))
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
// src/storage_adapter/specific/memory/memory_storage.ts
|
|
136
|
-
function createMemoryStorageMethods_string(memoryStorageMap = new Map) {
|
|
137
|
-
return {
|
|
138
|
-
type: "string" /* string */,
|
|
139
|
-
getItem: async (key) => memoryStorageMap.get(key) ?? null,
|
|
140
|
-
setItem: async (key, value) => {
|
|
141
|
-
memoryStorageMap.set(key, value);
|
|
142
|
-
},
|
|
143
|
-
removeItem: async (key) => {
|
|
144
|
-
memoryStorageMap.delete(key);
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
function createTypedMemoryStorage_string(memoryStorageMap = new Map) {
|
|
149
|
-
return createTypedStorage({
|
|
150
|
-
storageAdapter: new StorageAdapter(createMemoryStorageMethods_string(memoryStorageMap))
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
function createMemoryStorageMethods_json(memoryStorageMap = new Map) {
|
|
154
|
-
return {
|
|
155
|
-
type: "json" /* json */,
|
|
156
|
-
getItem: async (key) => memoryStorageMap.get(key),
|
|
157
|
-
setItem: async (key, value) => {
|
|
158
|
-
memoryStorageMap.set(key, value);
|
|
159
|
-
},
|
|
160
|
-
removeItem: async (key) => {
|
|
161
|
-
memoryStorageMap.delete(key);
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
function createTypedMemoryStorage_json(memoryStorageMap = new Map) {
|
|
166
|
-
return createTypedStorage({
|
|
167
|
-
storageAdapter: new StorageAdapter(createMemoryStorageMethods_json(memoryStorageMap))
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
export {
|
|
171
|
-
createWebSessionStorageMethods,
|
|
172
|
-
createWebLocalStorageMethods,
|
|
173
|
-
createTypedWebSessionStorage,
|
|
174
|
-
createTypedWebLocalStorage,
|
|
175
|
-
createTypedStorage,
|
|
176
|
-
createTypedMemoryStorage_string,
|
|
177
|
-
createTypedMemoryStorage_json,
|
|
178
|
-
createMemoryStorageMethods_string,
|
|
179
|
-
createMemoryStorageMethods_json,
|
|
180
|
-
createDurableObjectTypedStorage,
|
|
181
|
-
createDurableObjectStorageMethods,
|
|
182
|
-
StorageAdapter,
|
|
183
|
-
EStorageAdapterType
|
|
184
|
-
};
|
|
1
|
+
// src/index.ts
|
|
2
|
+
export * from "./storage_adapter/StorageAdapter";
|
|
3
|
+
export * from "./storage_adapter/specific/browser/browser_storage";
|
|
4
|
+
export * from "./storage_adapter/specific/durable_object/durable_object_storage";
|
|
5
|
+
export * from "./storage_adapter/specific/memory/memory_storage";
|
|
6
|
+
export * from "./storage_adapter/storage_adapter.types";
|
|
7
|
+
export * from "./storage_adapter/typed_storage/createTypedStorage";
|
|
8
|
+
export * from "./typescript/special_typescript_types";
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { type IStorageKeyGetterAndSetter, type TStorageAdapterMethods } from "./storage_adapter.types";
|
|
2
|
+
interface IStorageAdapterConstructor {
|
|
3
|
+
methods: TStorageAdapterMethods;
|
|
4
|
+
keyPrefix?: string;
|
|
5
|
+
}
|
|
2
6
|
export declare class StorageAdapter {
|
|
3
7
|
private implementation;
|
|
4
|
-
|
|
8
|
+
readonly keyPrefix: string;
|
|
9
|
+
constructor({ methods, keyPrefix }: IStorageAdapterConstructor);
|
|
10
|
+
private getPrefixedKey;
|
|
5
11
|
removeItem(key: string): Promise<void>;
|
|
6
12
|
setJson(key: string, value: any): Promise<void>;
|
|
7
13
|
getJson<T>(key: string): Promise<T | undefined>;
|
|
8
14
|
getJsonOrDef<T>(key: string, defVal: T): Promise<T>;
|
|
9
15
|
createJsonGetterSetter<T>(key: string): IStorageKeyGetterAndSetter<T>;
|
|
10
16
|
}
|
|
17
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type IStorageAdapterMethods_String } from "../../storage_adapter.types";
|
|
2
2
|
import { type ITypedStorage } from "../../typed_storage/createTypedStorage";
|
|
3
3
|
export declare function createWebLocalStorageMethods(_localStorage: typeof localStorage): IStorageAdapterMethods_String;
|
|
4
|
-
export declare function createTypedWebLocalStorage<T extends Record<string, any>>(_localStorage: typeof localStorage): ITypedStorage<T>;
|
|
4
|
+
export declare function createTypedWebLocalStorage<T extends Record<string, any>>(_localStorage: typeof localStorage, keyPrefix?: string): ITypedStorage<T>;
|
|
5
5
|
export declare function createWebSessionStorageMethods(_sessionStorage: typeof sessionStorage): IStorageAdapterMethods_String;
|
|
6
|
-
export declare function createTypedWebSessionStorage<T extends Record<string, any>>(_sessionStorage: typeof sessionStorage): ITypedStorage<T>;
|
|
6
|
+
export declare function createTypedWebSessionStorage<T extends Record<string, any>>(_sessionStorage: typeof sessionStorage, keyPrefix?: string): ITypedStorage<T>;
|
|
@@ -2,4 +2,4 @@ import { type IStorageAdapterMethods_Json } from "../../storage_adapter.types";
|
|
|
2
2
|
import { type ITypedStorage } from "../../typed_storage/createTypedStorage";
|
|
3
3
|
import type { IDurableObjectStorage } from "./durable_object_storage.types";
|
|
4
4
|
export declare function createDurableObjectStorageMethods(durableObjectStorage: IDurableObjectStorage): IStorageAdapterMethods_Json;
|
|
5
|
-
export declare function createDurableObjectTypedStorage<T extends Record<string, any>>(durableObjectStorage: IDurableObjectStorage): ITypedStorage<T>;
|
|
5
|
+
export declare function createDurableObjectTypedStorage<T extends Record<string, any>>(durableObjectStorage: IDurableObjectStorage, keyPrefix?: string): ITypedStorage<T>;
|
|
@@ -3,6 +3,6 @@ import { type ITypedStorage } from "../../typed_storage/createTypedStorage";
|
|
|
3
3
|
export type TMemoryStorage_string = Map<string, string>;
|
|
4
4
|
export type TMemoryStorage_json = Map<string, any>;
|
|
5
5
|
export declare function createMemoryStorageMethods_string(memoryStorageMap?: TMemoryStorage_string): IStorageAdapterMethods_String;
|
|
6
|
-
export declare function createTypedMemoryStorage_string<T extends Record<string, any>>(memoryStorageMap?: TMemoryStorage_string): ITypedStorage<T>;
|
|
6
|
+
export declare function createTypedMemoryStorage_string<T extends Record<string, any>>(memoryStorageMap?: TMemoryStorage_string, keyPrefix?: string): ITypedStorage<T>;
|
|
7
7
|
export declare function createMemoryStorageMethods_json(memoryStorageMap?: TMemoryStorage_json): IStorageAdapterMethods_Json;
|
|
8
|
-
export declare function createTypedMemoryStorage_json<T extends Record<string, any>>(memoryStorageMap?: TMemoryStorage_json): ITypedStorage<T>;
|
|
8
|
+
export declare function createTypedMemoryStorage_json<T extends Record<string, any>>(memoryStorageMap?: TMemoryStorage_json, keyPrefix?: string): ITypedStorage<T>;
|