-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference-client.ts
More file actions
56 lines (45 loc) · 1.52 KB
/
Copy pathreference-client.ts
File metadata and controls
56 lines (45 loc) · 1.52 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
// Reference http-sql v0.1 client, ~40 lines.
//
// Uses the platform `fetch`. No dependencies.
interface Statement { sql: string; params?: unknown[]; }
interface Result {
columns: string[];
rows: unknown[][];
rowsAffected: number;
lastInsertId?: string | number | null;
}
interface BatchResult { results: Result[]; }
interface HttpSqlError {
code: string;
message: string;
statementIndex?: number;
}
export class HttpSqlClient {
constructor(private endpoint: string, private token: string) {}
async execute(sql: string, params: unknown[] = []): Promise<Result> {
return this.send({ sql, params }) as Promise<Result>;
}
async batch(statements: Statement[], atomic = false): Promise<BatchResult> {
return this.send({ batch: statements, atomic }) as Promise<BatchResult>;
}
private async send(body: unknown): Promise<unknown> {
const res = await fetch(this.endpoint, {
method: "POST",
headers: {
"content-type": "application/json",
"authorization": `Bearer ${this.token}`,
"x-http-sql-accept-version": "0.1",
},
body: JSON.stringify(body),
});
const json = await res.json().catch(() => ({}));
if (!res.ok || (typeof json === "object" && json !== null && "error" in json)) {
const err = (json as { error?: HttpSqlError }).error ?? {
code: "internal_error",
message: `HTTP ${res.status}`,
};
throw Object.assign(new Error(err.message), { code: err.code, statementIndex: err.statementIndex });
}
return json;
}
}