@nice-code/util 0.2.5
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 +1 -0
- package/build/index.js +145 -0
- package/build/types/index.d.ts +6 -0
- package/build/types/storage_adapter/StorageAdapter.d.ts +10 -0
- package/build/types/storage_adapter/specific/browser/browser_storage.d.ts +6 -0
- package/build/types/storage_adapter/specific/durable_object/durable_object_storage.d.ts +5 -0
- package/build/types/storage_adapter/specific/durable_object/durable_object_storage.types.d.ts +18 -0
- package/build/types/storage_adapter/storage_adapter.types.d.ts +21 -0
- package/build/types/storage_adapter/typed_storage/createTypedStorage.d.ts +13 -0
- package/build/types/typescript/special_typescript_types.d.ts +1 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# @nice-code/utils
|
package/build/index.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// src/storage_adapter/storage_adapter.types.ts
|
|
2
|
+
var EStorageAdapterType;
|
|
3
|
+
((EStorageAdapterType2) => {
|
|
4
|
+
EStorageAdapterType2["string"] = "string";
|
|
5
|
+
EStorageAdapterType2["json"] = "json";
|
|
6
|
+
})(EStorageAdapterType ||= {});
|
|
7
|
+
|
|
8
|
+
// src/storage_adapter/StorageAdapter.ts
|
|
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
|
+
export {
|
|
136
|
+
createWebSessionStorageMethods,
|
|
137
|
+
createWebLocalStorageMethods,
|
|
138
|
+
createTypedWebSessionStorage,
|
|
139
|
+
createTypedWebLocalStorage,
|
|
140
|
+
createTypedStorage,
|
|
141
|
+
createDurableObjectTypedStorage,
|
|
142
|
+
createDurableObjectStorageMethods,
|
|
143
|
+
StorageAdapter,
|
|
144
|
+
EStorageAdapterType
|
|
145
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from "./storage_adapter/StorageAdapter";
|
|
2
|
+
export * from "./storage_adapter/specific/browser/browser_storage";
|
|
3
|
+
export * from "./storage_adapter/specific/durable_object/durable_object_storage";
|
|
4
|
+
export * from "./storage_adapter/storage_adapter.types";
|
|
5
|
+
export * from "./storage_adapter/typed_storage/createTypedStorage";
|
|
6
|
+
export * from "./typescript/special_typescript_types";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type IStorageKeyGetterAndSetter, type TStorageAdapterMethods } from "./storage_adapter.types";
|
|
2
|
+
export declare class StorageAdapter {
|
|
3
|
+
private implementation;
|
|
4
|
+
constructor(implementation: TStorageAdapterMethods);
|
|
5
|
+
removeItem(key: string): Promise<void>;
|
|
6
|
+
setJson(key: string, value: any): Promise<void>;
|
|
7
|
+
getJson<T>(key: string): Promise<T | undefined>;
|
|
8
|
+
getJsonOrDef<T>(key: string, defVal: T): Promise<T>;
|
|
9
|
+
createJsonGetterSetter<T>(key: string): IStorageKeyGetterAndSetter<T>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type IStorageAdapterMethods_String } from "../../storage_adapter.types";
|
|
2
|
+
import { type ITypedStorage } from "../../typed_storage/createTypedStorage";
|
|
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>;
|
|
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>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type IStorageAdapterMethods_Json } from "../../storage_adapter.types";
|
|
2
|
+
import { type ITypedStorage } from "../../typed_storage/createTypedStorage";
|
|
3
|
+
import type { IDurableObjectStorage } from "./durable_object_storage.types";
|
|
4
|
+
export declare function createDurableObjectStorageMethods(durableObjectStorage: IDurableObjectStorage): IStorageAdapterMethods_Json;
|
|
5
|
+
export declare function createDurableObjectTypedStorage<T extends Record<string, any>>(durableObjectStorage: IDurableObjectStorage): ITypedStorage<T>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface DurableObjectGetOptions {
|
|
2
|
+
allowConcurrency?: boolean;
|
|
3
|
+
noCache?: boolean;
|
|
4
|
+
}
|
|
5
|
+
interface DurableObjectPutOptions {
|
|
6
|
+
allowConcurrency?: boolean;
|
|
7
|
+
allowUnconfirmed?: boolean;
|
|
8
|
+
noCache?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface IDurableObjectStorage {
|
|
11
|
+
get<T = unknown>(key: string, options?: DurableObjectGetOptions): Promise<T | undefined>;
|
|
12
|
+
get<T = unknown>(keys: string[], options?: DurableObjectGetOptions): Promise<Map<string, T>>;
|
|
13
|
+
put<T>(key: string, value: T, options?: DurableObjectPutOptions): Promise<void>;
|
|
14
|
+
put<T>(entries: Record<string, T>, options?: DurableObjectPutOptions): Promise<void>;
|
|
15
|
+
delete(key: string, options?: DurableObjectPutOptions): Promise<boolean>;
|
|
16
|
+
delete(keys: string[], options?: DurableObjectPutOptions): Promise<number>;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare enum EStorageAdapterType {
|
|
2
|
+
string = "string",
|
|
3
|
+
json = "json"
|
|
4
|
+
}
|
|
5
|
+
export interface IStorageAdapterMethods_String {
|
|
6
|
+
type: EStorageAdapterType.string;
|
|
7
|
+
setItem: (key: string, value: string) => Promise<void>;
|
|
8
|
+
getItem: (key: string) => Promise<string | null | undefined>;
|
|
9
|
+
removeItem: (key: string) => Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export interface IStorageAdapterMethods_Json {
|
|
12
|
+
type: EStorageAdapterType.json;
|
|
13
|
+
setItem: <T>(key: string, value: T) => Promise<void>;
|
|
14
|
+
getItem: <T>(key: string) => Promise<T | null | undefined>;
|
|
15
|
+
removeItem: (key: string) => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export type TStorageAdapterMethods = IStorageAdapterMethods_String | IStorageAdapterMethods_Json;
|
|
18
|
+
export interface IStorageKeyGetterAndSetter<T> {
|
|
19
|
+
get: () => Promise<T | undefined>;
|
|
20
|
+
set: (value: T) => Promise<void>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { StringKeys } from "../../typescript/special_typescript_types";
|
|
2
|
+
import type { StorageAdapter } from "../StorageAdapter";
|
|
3
|
+
export interface ITypedStorage<T extends Record<string, any>> {
|
|
4
|
+
getJson<K extends StringKeys<T>>(key: K): Promise<T[K] | undefined>;
|
|
5
|
+
getJsonOrDef<K extends StringKeys<T>>(key: K, defVal: T[K]): Promise<T[K]>;
|
|
6
|
+
setJson<K extends StringKeys<T>>(key: K, val: T[K]): Promise<void>;
|
|
7
|
+
removeItem<K extends StringKeys<T>>(key: K): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
interface ITypedStorage_Create_Input {
|
|
10
|
+
storageAdapter: StorageAdapter;
|
|
11
|
+
}
|
|
12
|
+
export declare function createTypedStorage<T extends Record<string, any>>({ storageAdapter, }: ITypedStorage_Create_Input): ITypedStorage<T>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type StringKeys<T> = keyof T & string;
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nice-code/util",
|
|
3
|
+
"version": "0.2.5",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./build/types/index.d.ts",
|
|
9
|
+
"import": "./build/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"build",
|
|
14
|
+
"package.json",
|
|
15
|
+
"README.md"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"type-check": "bunx tsc --noEmit",
|
|
22
|
+
"type-check-watch": "bunx tsc --noEmit --watch",
|
|
23
|
+
"clean-build": "bunx rimraf build",
|
|
24
|
+
"vitest": "vitest --typecheck",
|
|
25
|
+
"vitest-agent": "vitest --typecheck --reporter=agent",
|
|
26
|
+
"build": "bun run clean-build && bun run build.ts && bun run build-types",
|
|
27
|
+
"build-watch": "bun run clean-build && bun run build.ts --watch && bun run build-types --watch",
|
|
28
|
+
"build-types": "tsc --project tsconfig.build.json"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"nanoid": "^5.1.9"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@tanstack/react-query": "^5.100.3",
|
|
36
|
+
"react": ">=18",
|
|
37
|
+
"valibot": "^1.3.1",
|
|
38
|
+
"wrangler": "4.94.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"wrangler": {
|
|
42
|
+
"optional": true
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|