-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
kv.d.ts
54 lines (40 loc) · 2.26 KB
/
kv.d.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
export namespace KV {
type Value = string | ReadableStream | ArrayBuffer;
type WriteOptions = { expiration?: number; expirationTtl?: number };
type ListOptions = { prefix?: string; limit?: number; cursor?: string };
type GetFormat = 'text' | 'json' | 'arrayBuffer' | 'stream';
type GetOptions<T> = { type: T; cacheTtl?: number };
interface KeyList {
keys: Array<{ name: string; expiration?: number }>;
list_complete: boolean;
cursor: string;
}
interface Namespace {
get<T>(key: string, type: 'json'): Promise<T|null>;
get<T>(key: string, options: GetOptions<'json'>): Promise<T|null>;
get<T>(key: string, type: 'stream'): Promise<ReadableStream|null>;
get<T>(key: string, options: GetOptions<'stream'>): Promise<ReadableStream|null>;
get<T>(key: string, type: 'arrayBuffer'): Promise<ArrayBuffer|null>;
get<T>(key: string, options: GetOptions<'arrayBuffer'>): Promise<ArrayBuffer|null>;
get<T>(key: string, type: 'text'): Promise<string|null>;
get<T>(key: string, options: GetOptions<'text'>): Promise<string|null>;
get<T>(key: string, type?: GetFormat): Promise<string|null>; // "text"
get<T>(key: string, options?: GetOptions<GetFormat> ): Promise<string|null>; // "text"
put(key: string, value: Value, options?: WriteOptions): Promise<void>;
list(options?: ListOptions): Promise<KeyList>;
delete(key: string): Promise<void>;
}
}
export declare class Database<Models, Identifiers extends Record<keyof Models, string> = { [P in keyof Models]: string}> {
constructor(binding: KV.Namespace);
get<K extends keyof Models>(type: K, uid: Identifiers[K], format?: KV.GetFormat): Promise<Models[K] | false>;
put<K extends keyof Models>(type: K, uid: Identifiers[K], value: Models[K], toJSON?: boolean, options?: KV.WriteOptions): Promise<boolean>;
del<K extends keyof Models>(type: K, uid: Identifiers[K]): Promise<boolean>;
}
export function read<T>(binding: KV.Namespace, key: string, format?: KV.GetOptions): Promise<T | false>;
export function write<T>(binding: KV.Namespace, key: string, value: T, toJSON?: boolean, options?: KV.WriteOptions): Promise<boolean>;
export function remove(binding: KV.Namespace, key: string): Promise<boolean>;
export function until<X extends string>(
toMake: () => X,
toSearch: (val: X) => Promise<unknown | false>
): Promise<X>;