-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkv.ts
229 lines (206 loc) · 6.14 KB
/
kv.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* SPDX-FileCopyrightText: 2022-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import { baseUrl, createFetch, HttpMethod, type Credentials } from "./fetch.js";
// #region TypeScript
type Options = {
accountId: string;
} & Credentials;
type FindOptions = {
/**
* Page number of paginated results
* @minimum 1
* @default 1
*/
page?: number;
/**
* Maximum number of results per page
* @minimum 5
* @maximum 100
* @default 20
*/
per_page?: number;
/**
* Field to order results by
*/
order?: "id" | "title";
/**
* Direction to order namespaces
*/
direction?: "asc" | "desc";
};
export type Namespace = {
id: string;
title: string;
supports_url_encoding: boolean;
};
export type Key = {
name: string;
expiration: number;
metadata: Record<string, string | number>;
};
type KeysOptions = {
/**
* A string prefix used to filter down which keys will be returned. Exact
* matches and any key names that begin with the prefix will be returned.
*/
prefix?: string;
/**
* The number of keys to return. The cursor attribute may be used to iterate
* over the next batch of keys if there are more than the limit.
* @minimum 10
* @maximum 1000
* @example 1000
*/
first?: number;
/**
* Opaque token indicating the position from which to continue when requesting
* the next set of records if the amount of list results was limited by the
* limit parameter. A valid value for the cursor can be obtained from the
* cursors object in the result_info structure.
* @example "6Ck1la0VxJ0djhidm1MdX2FyDGxLKVeeHZZmORS_8XeSuhz9SjIJRaSa2lnsF01tQOHrfTGAP3R5X1Kv5iVUuMbNKhWNAXHOl6ePB0TUL8nw"
*/
after?: string;
};
type SetOptions = {
expires?: number;
expiresTtl?: number;
/**
* @default JSON.stringify
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
encode?: ((value: any) => any) | false;
/**
* @default "text/plain; charset=utf-8"
*/
contentType?: string;
};
type GetOptions = {
/**
* @default JSON.parse
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
decode?: ((value: ArrayBuffer) => any) | false;
};
// #endregion
const { GET, PUT, POST, DELETE } = HttpMethod;
/**
* Cloudflare KV Storage client
*/
export function kv(options: Options) {
const { accountId, ...credentials } = options;
const url = `${baseUrl}/accounts/${accountId}/storage/kv/namespaces`;
if (!accountId) throw new TypeError(`options.accountId`);
if ("accessToken" in credentials) {
if (!credentials.accessToken) throw new TypeError(`options.accessToken`);
} else {
if (!credentials.authKey) throw new TypeError(`options.authKey`);
if (!credentials.authEmail) throw new TypeError(`options.authEmail`);
}
return {
/**
* List Namespaces
* @see https://api.cloudflare.com/#workers-kv-namespace-list-namespaces
*/
find: createFetch((params?: FindOptions) => ({
method: GET,
url,
searchParams: params,
credentials,
})).query<Namespace>(),
/**
* Create a Namespace
* @see https://api.cloudflare.com/#workers-kv-namespace-properties
*/
create: createFetch((name: string) => ({
method: POST,
url,
body: JSON.stringify({ title: name }),
credentials,
})).response<Namespace>(),
/**
* Rename a Namespace
* @see https://api.cloudflare.com/#workers-kv-namespace-rename-a-namespace
*/
update: createFetch((id: string, title: string) => ({
method: PUT,
url: `${url}/${id}`,
body: JSON.stringify({ title }),
credentials,
})).response<null>(),
/**
* Remove a Namespace
* @see https://api.cloudflare.com/#workers-kv-namespace-remove-a-namespace
*/
delete: createFetch((id: string) => ({
method: DELETE,
url: `${url}/${id}`,
credentials,
})).response<null>(),
namespace(id: string) {
const namespaceUrl = `${url}/${id}`;
const namespaceId = id;
return {
keys: createFetch((params?: KeysOptions) => ({
method: GET,
url: `${namespaceUrl}/keys`,
searchParams: {
prefix: params?.prefix,
limit: params?.first,
cursor: params?.after,
},
credentials,
})).query<Key>(),
/**
* Read key-value pair
* @see https://api.cloudflare.com/#workers-kv-namespace-read-key-value-pair
*/
get: createFetch((key: string, options?: GetOptions) => ({
method: GET,
url: `${url}/${namespaceId}/values/${key}`,
credentials,
type:
options?.decode === false
? "text"
: options?.decode
? "binary"
: "json",
notFoundResponse: undefined,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
})).response<any>() as <T = any>(
key: string,
options?: GetOptions
) => Promise<T>,
/**
* Write key-value pair
* @see https://api.cloudflare.com/#workers-kv-namespace-write-key-value-pair
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
set: createFetch((key: string, value: any, options?: SetOptions) => ({
method: PUT,
url: `${namespaceUrl}/values/${key}`,
searchParams: {
expiration: options?.expires,
expiration_ttl: options?.expiresTtl,
},
body:
options?.encode === false ||
(typeof options?.encode === undefined && typeof value === "string")
? value
: JSON.stringify(value),
contentType: options?.contentType ?? "text/plain; charset=utf-8",
credentials,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
})).response<undefined>() as <T = any>(
key: string,
value: T,
options?: SetOptions
) => Promise<undefined>,
delete: createFetch((id: string) => ({
method: DELETE,
url: `${url}/${namespaceId}/values/${id}`,
credentials,
})).response<void>(),
};
},
};
}