forked from unjs/unstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
128 lines (121 loc) · 4.01 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
export type StorageValue = null | string | number | boolean | object;
export type WatchEvent = "update" | "remove";
export type WatchCallback = (event: WatchEvent, key: string) => any;
type MaybePromise<T> = T | Promise<T>;
type MaybeDefined<T> = T extends any ? T : any;
export type Unwatch = () => MaybePromise<void>;
export interface StorageMeta {
atime?: Date;
mtime?: Date;
[key: string]: StorageValue | Date | undefined;
}
export type TransactionOptions = Record<string, any>;
export interface Driver {
name?: string;
options?: any;
hasItem: (key: string, opts: TransactionOptions) => MaybePromise<boolean>;
getItem: (
key: string,
opts?: TransactionOptions
) => MaybePromise<StorageValue>;
/** @experimental */
getItems?: (
items: { key: string; options?: TransactionOptions }[],
commonOptions?: TransactionOptions
) => MaybePromise<{ key: string; value: StorageValue }[]>;
/** @experimental */
getItemRaw?: (key: string, opts: TransactionOptions) => MaybePromise<unknown>;
setItem?: (
key: string,
value: string,
opts: TransactionOptions
) => MaybePromise<void>;
/** @experimental */
setItems?: (
items: { key: string; value: string; options?: TransactionOptions }[],
commonOptions?: TransactionOptions
) => MaybePromise<void>;
/** @experimental */
setItemRaw?: (
key: string,
value: any,
opts: TransactionOptions
) => MaybePromise<void>;
removeItem?: (key: string, opts: TransactionOptions) => MaybePromise<void>;
getMeta?: (
key: string,
opts: TransactionOptions
) => MaybePromise<StorageMeta | null>;
getKeys: (base: string, opts: TransactionOptions) => MaybePromise<string[]>;
clear?: (base: string, opts: TransactionOptions) => MaybePromise<void>;
dispose?: () => MaybePromise<void>;
watch?: (callback: WatchCallback) => MaybePromise<Unwatch>;
}
export interface Storage<T extends StorageValue = StorageValue> {
// Item
hasItem: (key: string, opts?: TransactionOptions) => Promise<boolean>;
getItem: <U extends T>(
key: string,
opts?: TransactionOptions
) => Promise<U | null>;
/** @experimental */
getItems: (
items: (string | { key: string; options?: TransactionOptions })[],
commonOptions?: TransactionOptions
) => Promise<{ key: string; value: StorageValue }[]>;
/** @experimental See https://github.com/unjs/unstorage/issues/142 */
getItemRaw: <T = any>(
key: string,
opts?: TransactionOptions
) => Promise<MaybeDefined<T> | null>;
setItem: <U extends T>(
key: string,
value: U,
opts?: TransactionOptions
) => Promise<void>;
/** @experimental */
setItems: (
items: { key: string; value: string; options?: TransactionOptions }[],
commonOptions?: TransactionOptions
) => Promise<void>;
/** @experimental See https://github.com/unjs/unstorage/issues/142 */
setItemRaw: <T = any>(
key: string,
value: MaybeDefined<T>,
opts?: TransactionOptions
) => Promise<void>;
removeItem: (
key: string,
opts?:
| (TransactionOptions & { removeMeta?: boolean })
| boolean /* legacy: removeMeta */
) => Promise<void>;
// Meta
getMeta: (
key: string,
opts?:
| (TransactionOptions & { nativeOnly?: boolean })
| boolean /* legacy: nativeOnly */
) => MaybePromise<StorageMeta>;
setMeta: (
key: string,
value: StorageMeta,
opts?: TransactionOptions
) => Promise<void>;
removeMeta: (key: string, opts?: TransactionOptions) => Promise<void>;
// Keys
getKeys: (base?: string, opts?: TransactionOptions) => Promise<string[]>;
// Utils
clear: (base?: string, opts?: TransactionOptions) => Promise<void>;
dispose: () => Promise<void>;
watch: (callback: WatchCallback) => Promise<Unwatch>;
unwatch: () => Promise<void>;
// Mount
mount: (base: string, driver: Driver) => Storage;
unmount: (base: string, dispose?: boolean) => Promise<void>;
getMount: (key?: string) => { base: string; driver: Driver };
getMounts: (
base?: string,
options?: { parents?: boolean }
) => { base: string; driver: Driver }[];
}