-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.ts
More file actions
289 lines (278 loc) · 10 KB
/
client.ts
File metadata and controls
289 lines (278 loc) · 10 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import type { ApiResponse, ApiErrorResponse } from "@keyflare/shared";
import { hc } from "hono/client";
import { getApiUrl, readApiKey } from "../config.js";
import { makeDebug, redact } from "../debug.js";
const debug = makeDebug("api");
export class KeyflareApiError extends Error {
constructor(
public readonly code: string,
message: string,
public readonly status: number
) {
super(message);
this.name = "KeyflareApiError";
}
}
function baseUrl(): string {
const url = getApiUrl().replace(/\/$/, "") + "/";
debug("base URL resolved: %s", url);
return url;
}
interface KeyflareRpcClient {
bootstrap: {
$get: () => Promise<Response>;
$post: () => Promise<Response>;
};
keys: {
$get: () => Promise<Response>;
$post: (opts: { json: object }) => Promise<Response>;
":prefix": {
$delete: (opts: { param: { prefix: string } }) => Promise<Response>;
$put: (opts: { param: { prefix: string }; json: object }) => Promise<Response>;
};
};
projects: {
$get: () => Promise<Response>;
$post: (opts: { json: { name: string } }) => Promise<Response>;
":name": { $delete: (opts: { param: { name: string } }) => Promise<Response> };
":project": {
environments: {
$get: (opts: { param: { project: string } }) => Promise<Response>;
$post: (opts: { param: { project: string }; json: { name: string } }) => Promise<Response>;
":environment": {
$delete: (opts: { param: { project: string; environment: string } }) => Promise<Response>;
secrets: {
$get: (opts: { param: { project: string; environment: string } }) => Promise<Response>;
$put: (opts: {
param: { project: string; environment: string };
json: { secrets: Record<string, string> };
}) => Promise<Response>;
$patch: (opts: {
param: { project: string; environment: string };
json: object;
}) => Promise<Response>;
};
};
};
};
};
}
function client(apiKey?: string): KeyflareRpcClient {
const key = apiKey ?? readApiKey();
debug("creating rpc client with apiKey=%s", redact(key));
return hc(baseUrl(), {
headers: key ? { Authorization: `Bearer ${key}` } : {},
}) as unknown as KeyflareRpcClient;
}
async function unwrap<T>(resPromise: Promise<Response>): Promise<T> {
const res = await resPromise;
debug("response status=%d", res.status);
const json = (await res.json()) as ApiResponse<T>;
if (!res.ok || !("ok" in json) || !json.ok) {
const err = (json as ApiErrorResponse).error;
throw new KeyflareApiError(
err?.code ?? "UNKNOWN",
err?.message ?? "Request failed",
res.status
);
}
return (json as { ok: true; data: T }).data;
}
export const api = {
get: <T>(path: string, apiKey?: string): Promise<T> => {
debug("GET %s", path);
const c = client(apiKey);
if (path === "/bootstrap") return unwrap<T>(c.bootstrap.$get());
if (path === "/keys") return unwrap<T>(c.keys.$get());
if (path === "/projects") return unwrap<T>(c.projects.$get());
// For /auth/verify and other unregistered paths, fall through to the generic fetch below
const projectEnvsMatch = path.match(/^\/projects\/([^/]+)\/environments$/);
if (projectEnvsMatch) {
const project = decodeURIComponent(projectEnvsMatch[1]);
return unwrap<T>(c.projects[":project"].environments.$get({ param: { project } }));
}
const secretsMatch = path.match(
/^\/projects\/([^/]+)\/environments\/([^/]+)\/secrets$/
);
if (secretsMatch) {
const project = decodeURIComponent(secretsMatch[1]);
const environment = decodeURIComponent(secretsMatch[2]);
return unwrap<T>(
c.projects[":project"].environments[":environment"].secrets.$get({
param: { project, environment },
})
);
}
return fetch(baseUrl().replace(/\/$/, "") + path, {
method: "GET",
headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
}).then((r) =>
r.json().then((j) => {
const json = j as { ok?: boolean; data?: T; error?: { code: string; message: string } };
if (!r.ok || !json.ok)
throw new KeyflareApiError(
json.error?.code ?? "UNKNOWN",
json.error?.message ?? "Request failed",
r.status
);
return json.data as T;
})
);
},
post: <T>(path: string, body?: unknown, apiKey?: string): Promise<T> => {
debug("POST %s bodyKeys=%o", path, body && typeof body === "object" ? Object.keys(body as Record<string, unknown>) : []);
const c = client(apiKey);
if (path === "/bootstrap") return unwrap<T>(c.bootstrap.$post());
if (path === "/keys")
return unwrap<T>(c.keys.$post({ json: (body ?? {}) as object }));
if (path === "/projects")
return unwrap<T>(c.projects.$post({ json: (body ?? {}) as { name: string } }));
const projectEnvsMatch = path.match(/^\/projects\/([^/]+)\/environments$/);
if (projectEnvsMatch) {
const project = decodeURIComponent(projectEnvsMatch[1]);
return unwrap<T>(
c.projects[":project"].environments.$post({
param: { project },
json: (body ?? {}) as { name: string },
})
);
}
return fetch(baseUrl().replace(/\/$/, "") + path, {
method: "POST",
headers: {
"Content-Type": "application/json",
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
},
body: body ? JSON.stringify(body) : undefined,
}).then((r) =>
r.json().then((j) => {
const json = j as { ok?: boolean; data?: T; error?: { code: string; message: string } };
if (!r.ok || !json.ok)
throw new KeyflareApiError(
json.error?.code ?? "UNKNOWN",
json.error?.message ?? "Request failed",
r.status
);
return json.data as T;
})
);
},
put: <T>(path: string, body: unknown, apiKey?: string): Promise<T> => {
debug("PUT %s bodyType=%s", path, typeof body);
const c = client(apiKey);
const keysPrefixMatch = path.match(/^\/keys\/([^/]+)$/);
if (keysPrefixMatch) {
const prefix = keysPrefixMatch[1];
return unwrap<T>(
c.keys[":prefix"].$put({ param: { prefix }, json: body as object })
);
}
const secretsMatch = path.match(
/^\/projects\/([^/]+)\/environments\/([^/]+)\/secrets$/
);
if (secretsMatch) {
const project = decodeURIComponent(secretsMatch[1]);
const environment = decodeURIComponent(secretsMatch[2]);
return unwrap<T>(
c.projects[":project"].environments[":environment"].secrets.$put({
param: { project, environment },
json: body as { secrets: Record<string, string> },
})
);
}
return fetch(baseUrl().replace(/\/$/, "") + path, {
method: "PUT",
headers: {
"Content-Type": "application/json",
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
},
body: JSON.stringify(body),
}).then((r) =>
r.json().then((j) => {
const json = j as { ok?: boolean; data?: T; error?: { code: string; message: string } };
if (!r.ok || !json.ok)
throw new KeyflareApiError(
json.error?.code ?? "UNKNOWN",
json.error?.message ?? "Request failed",
r.status
);
return json.data as T;
})
);
},
patch: <T>(path: string, body: unknown, apiKey?: string): Promise<T> => {
debug("PATCH %s bodyType=%s", path, typeof body);
const c = client(apiKey);
const secretsMatch = path.match(
/^\/projects\/([^/]+)\/environments\/([^/]+)\/secrets$/
);
if (secretsMatch) {
const project = decodeURIComponent(secretsMatch[1]);
const environment = decodeURIComponent(secretsMatch[2]);
return unwrap<T>(
c.projects[":project"].environments[":environment"].secrets.$patch({
param: { project, environment },
json: body as object,
})
);
}
return fetch(baseUrl().replace(/\/$/, "") + path, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
},
body: JSON.stringify(body),
}).then((r) =>
r.json().then((j) => {
const json = j as { ok?: boolean; data?: T; error?: { code: string; message: string } };
if (!r.ok || !json.ok)
throw new KeyflareApiError(
json.error?.code ?? "UNKNOWN",
json.error?.message ?? "Request failed",
r.status
);
return json.data as T;
})
);
},
delete: <T>(path: string, apiKey?: string): Promise<T> => {
debug("DELETE %s", path);
const c = client(apiKey);
const keysPrefixMatch = path.match(/^\/keys\/([^/]+)$/);
if (keysPrefixMatch) {
const prefix = keysPrefixMatch[1];
return unwrap<T>(c.keys[":prefix"].$delete({ param: { prefix } }));
}
const projectMatch = path.match(/^\/projects\/([^/]+)$/);
if (projectMatch) {
const name = decodeURIComponent(projectMatch[1]);
return unwrap<T>(c.projects[":name"].$delete({ param: { name } }));
}
const envMatch = path.match(/^\/projects\/([^/]+)\/environments\/([^/]+)$/);
if (envMatch) {
const project = decodeURIComponent(envMatch[1]);
const environment = decodeURIComponent(envMatch[2]);
return unwrap<T>(
c.projects[":project"].environments[":environment"].$delete({
param: { project, environment },
})
);
}
return fetch(baseUrl().replace(/\/$/, "") + path, {
method: "DELETE",
headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : {},
}).then((r) =>
r.json().then((j) => {
const json = j as { ok?: boolean; data?: T; error?: { code: string; message: string } };
if (!r.ok || !json.ok)
throw new KeyflareApiError(
json.error?.code ?? "UNKNOWN",
json.error?.message ?? "Request failed",
r.status
);
return json.data as T;
})
);
},
};