Skip to content

Commit 3b80aa4

Browse files
Merge pull request #27 from BunnyWay/database-shell-custom-fetch-client
use custom database shell fetch client
2 parents 8feef14 + 53148d7 commit 3b80aa4

5 files changed

Lines changed: 36 additions & 9 deletions

File tree

.changeset/cyan-cameras-beam.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@bunny.net/database-shell": patch
3+
"@bunny.net/cli": patch
4+
---
5+
6+
use custom fetch client for database shell

packages/cli/src/commands/db/shell.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ export const dbShellCommand = defineCommand<{
175175
verbose,
176176
apiKey,
177177
}) => {
178-
const { createClient } = await import("@libsql/client/web");
179-
const { startShell, executeQuery, executeFile } = await import("@bunny.net/database-shell");
178+
const { createShellClient, startShell, executeQuery, executeFile } = await import("@bunny.net/database-shell");
180179

181180
// If database-id doesn't look like a database ID, treat it as the query
182181
let databaseId = databaseIdArg;
@@ -204,7 +203,7 @@ export const dbShellCommand = defineCommand<{
204203
verbose,
205204
);
206205

207-
const client = createClient({ url, authToken: token });
206+
const client = createShellClient({ url, authToken: token });
208207
const log = shellLogger();
209208

210209
// Non-interactive: execute and exit

packages/database-shell/src/cli.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env bun
22

3-
import { createClient } from "@libsql/client/web";
4-
import { startShell, executeQuery, executeFile, PRINT_MODES } from "./index.ts";
3+
import { createShellClient, startShell, executeQuery, executeFile, PRINT_MODES } from "./index.ts";
54
import type { PrintMode } from "./types.ts";
65

76
function printUsage() {
@@ -74,10 +73,7 @@ async function main() {
7473
process.exit(help ? 0 : 1);
7574
}
7675

77-
const client = createClient({
78-
url,
79-
authToken: token,
80-
});
76+
const client = createShellClient({ url, authToken: token });
8177

8278
const options = { mode, masked: !unmask, timing };
8379

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createClient, type Client } from "@libsql/client/web";
2+
import pkg from "../package.json";
3+
4+
const USER_AGENT = `${pkg.name}/${pkg.version}`;
5+
6+
/**
7+
* Create a libSQL client with a custom `User-Agent` header
8+
* so requests from the CLI/shell can be identified server-side.
9+
*/
10+
export function createShellClient(opts: {
11+
url: string;
12+
authToken?: string;
13+
}): Client {
14+
return createClient({
15+
...opts,
16+
fetch: (input: string | Request | URL, init?: RequestInit) => {
17+
const headers = new Headers(init?.headers);
18+
if (input instanceof Request) {
19+
input.headers.forEach((v, k) => headers.set(k, v));
20+
}
21+
headers.set("User-Agent", USER_AGENT);
22+
return fetch(input, { ...init, headers });
23+
},
24+
});
25+
}

packages/database-shell/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Public API
2+
export { createShellClient } from "./client.ts";
23
export { startShell, executeQuery, executeFile } from "./shell.ts";
34

45
// Types

0 commit comments

Comments
 (0)