Skip to content

Commit a4d1130

Browse files
committed
refactor(client): move api helpers
1 parent 268699e commit a4d1130

13 files changed

Lines changed: 44 additions & 44 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ type JsonRequestInit = RequestInit & {
1313
json?: JsonValue;
1414
};
1515

16-
export async function requireToken(context: CommandContext): Promise<string> {
16+
export async function requireClientToken(context: CommandContext): Promise<string> {
1717
const token = await loadToken(context.env);
1818
if (!token) {
1919
throw new Error('Not logged in. Run "bee login" first.');
2020
}
2121
return token;
2222
}
2323

24-
export async function requestDeveloperJson(
24+
export async function requestClientJson(
2525
context: CommandContext,
2626
path: string,
2727
init: JsonRequestInit = {}
2828
): Promise<unknown> {
29-
const token = await requireToken(context);
29+
const token = await requireClientToken(context);
3030
const headers = new Headers(init.headers);
3131
headers.set("Authorization", `Bearer ${token}`);
3232

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { CommandContext } from "@/commands/types";
22

3-
type DevUser = {
3+
type ClientUser = {
44
id: number;
55
first_name: string;
66
last_name: string | null;
77
};
88

9-
export async function fetchDeveloperMe(
9+
export async function fetchClientMe(
1010
context: CommandContext,
1111
token: string
12-
): Promise<DevUser> {
12+
): Promise<ClientUser> {
1313
const response = await context.client.fetch("/v1/me", {
1414
method: "GET",
1515
headers: {
@@ -30,7 +30,7 @@ export async function fetchDeveloperMe(
3030
const id = data?.["id"];
3131
const firstName = data?.["first_name"];
3232
if (typeof id !== "number" || typeof firstName !== "string") {
33-
throw new Error("Invalid response from developer API.");
33+
throw new Error("Invalid response from API.");
3434
}
3535

3636
return {

sources/commands/conversations/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Command, CommandContext } from "@/commands/types";
2-
import { printJson, requestDeveloperJson } from "@/commands/developerApi";
2+
import { printJson, requestClientJson } from "@/client/clientApi";
33

44
const USAGE = [
55
"bee conversations list [--limit N] [--cursor <cursor>]",
@@ -50,7 +50,7 @@ async function handleList(
5050

5151
const suffix = params.toString();
5252
const path = suffix ? `/v1/conversations?${suffix}` : "/v1/conversations";
53-
const data = await requestDeveloperJson(context, path, { method: "GET" });
53+
const data = await requestClientJson(context, path, { method: "GET" });
5454
printJson(data);
5555
}
5656

@@ -107,7 +107,7 @@ async function handleGet(
107107
context: CommandContext
108108
): Promise<void> {
109109
const id = parseId(args);
110-
const data = await requestDeveloperJson(context, `/v1/conversations/${id}`, {
110+
const data = await requestClientJson(context, `/v1/conversations/${id}`, {
111111
method: "GET",
112112
});
113113
printJson(data);

sources/commands/daily/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Command, CommandContext } from "@/commands/types";
2-
import { printJson, requestDeveloperJson } from "@/commands/developerApi";
2+
import { printJson, requestClientJson } from "@/client/clientApi";
33

44
const USAGE = [
55
"bee daily list [--limit N]",
@@ -46,7 +46,7 @@ async function handleList(
4646

4747
const suffix = params.toString();
4848
const path = suffix ? `/v1/daily?${suffix}` : "/v1/daily";
49-
const data = await requestDeveloperJson(context, path, { method: "GET" });
49+
const data = await requestClientJson(context, path, { method: "GET" });
5050
printJson(data);
5151
}
5252

@@ -93,7 +93,7 @@ async function handleGet(
9393
context: CommandContext
9494
): Promise<void> {
9595
const id = parseId(args);
96-
const data = await requestDeveloperJson(context, `/v1/daily/${id}`, {
96+
const data = await requestClientJson(context, `/v1/daily/${id}`, {
9797
method: "GET",
9898
});
9999
printJson(data);

sources/commands/facts/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Command, CommandContext } from "@/commands/types";
2-
import { printJson, requestDeveloperJson } from "@/commands/developerApi";
2+
import { printJson, requestClientJson } from "@/client/clientApi";
33

44
const USAGE = [
55
"bee facts list [--limit N] [--cursor <cursor>] [--confirmed <true|false>]",
@@ -66,7 +66,7 @@ async function handleList(
6666

6767
const suffix = params.toString();
6868
const path = suffix ? `/v1/facts?${suffix}` : "/v1/facts";
69-
const data = await requestDeveloperJson(context, path, { method: "GET" });
69+
const data = await requestClientJson(context, path, { method: "GET" });
7070
printJson(data);
7171
}
7272

@@ -137,7 +137,7 @@ async function handleGet(
137137
context: CommandContext
138138
): Promise<void> {
139139
const id = parseId(args);
140-
const data = await requestDeveloperJson(context, `/v1/facts/${id}`, {
140+
const data = await requestClientJson(context, `/v1/facts/${id}`, {
141141
method: "GET",
142142
});
143143
printJson(data);
@@ -167,7 +167,7 @@ async function handleCreate(
167167
context: CommandContext
168168
): Promise<void> {
169169
const options = parseCreateArgs(args);
170-
const data = await requestDeveloperJson(context, "/v1/facts", {
170+
const data = await requestClientJson(context, "/v1/facts", {
171171
method: "POST",
172172
json: { text: options.text },
173173
});
@@ -228,7 +228,7 @@ async function handleUpdate(
228228
body.confirmed = options.confirmed;
229229
}
230230

231-
const data = await requestDeveloperJson(context, `/v1/facts/${options.id}`, {
231+
const data = await requestClientJson(context, `/v1/facts/${options.id}`, {
232232
method: "PUT",
233233
json: body,
234234
});
@@ -311,7 +311,7 @@ async function handleDelete(
311311
context: CommandContext
312312
): Promise<void> {
313313
const id = parseId(args);
314-
const data = await requestDeveloperJson(context, `/v1/facts/${id}`, {
314+
const data = await requestClientJson(context, `/v1/facts/${id}`, {
315315
method: "DELETE",
316316
});
317317
printJson(data);

sources/commands/login/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { emojiHash } from "@/utils/emojiHash";
1111
import { openBrowser } from "@/utils/browser";
1212
import { renderQrCode } from "@/utils/qrCode";
13-
import { fetchDeveloperMe } from "@/commands/auth/developerMe";
13+
import { fetchClientMe } from "@/client/clientMe";
1414

1515
type LoginOptions = {
1616
token?: string;
@@ -63,7 +63,7 @@ async function handleLogin(
6363

6464
token = token.trim();
6565

66-
const user = await fetchDeveloperMe(context, token);
66+
const user = await fetchClientMe(context, token);
6767

6868
await saveToken(context.env, token);
6969

sources/commands/me/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Command } from "@/commands/types";
2-
import { printJson, requestDeveloperJson } from "@/commands/developerApi";
2+
import { printJson, requestClientJson } from "@/client/clientApi";
33

44
const USAGE = "bee me";
55

@@ -12,7 +12,7 @@ export const meCommand: Command = {
1212
throw new Error(`Unexpected arguments: ${args.join(" ")}`);
1313
}
1414

15-
const data = await requestDeveloperJson(context, "/v1/me", {
15+
const data = await requestClientJson(context, "/v1/me", {
1616
method: "GET",
1717
});
1818
printJson(data);

sources/commands/proxy/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Command, CommandContext } from "@/commands/types";
2-
import { requireToken } from "@/commands/developerApi";
2+
import { requireClientToken } from "@/client/clientApi";
33

44
const USAGE = "bee proxy [--port N]";
55
const DEFAULT_PORT = 8787;
@@ -76,7 +76,7 @@ async function startProxy(
7676
context: CommandContext,
7777
options: ProxyOptions
7878
): Promise<void> {
79-
const token = await requireToken(context);
79+
const token = await requireClientToken(context);
8080
const port = await pickPort(options.port);
8181
const hostname = "127.0.0.1";
8282

sources/commands/search/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Command, CommandContext } from "@/commands/types";
2-
import { printJson, requestDeveloperJson } from "@/commands/developerApi";
2+
import { printJson, requestClientJson } from "@/client/clientApi";
33

44
const USAGE =
55
"bee search conversations --query <text> [--limit N] [--cursor <cursor>]";
@@ -46,7 +46,7 @@ async function handleConversations(
4646
body.cursor = options.cursor;
4747
}
4848

49-
const data = await requestDeveloperJson(context, "/v1/search/conversations", {
49+
const data = await requestClientJson(context, "/v1/search/conversations", {
5050
method: "POST",
5151
json: body,
5252
});

sources/commands/status/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Command, CommandContext } from "@/commands/types";
22
import { getEnvironmentConfig } from "@/environment";
33
import { loadToken } from "@/secureStore";
4-
import { fetchDeveloperMe } from "@/commands/auth/developerMe";
4+
import { fetchClientMe } from "@/client/clientMe";
55

66
const USAGE = "bee status";
77

@@ -34,7 +34,7 @@ async function handleStatus(
3434
console.log(`API: ${config.label} (${config.apiUrl})`);
3535
console.log(`Token: ${maskToken(token)}`);
3636

37-
const user = await fetchDeveloperMe(context, token);
37+
const user = await fetchClientMe(context, token);
3838
const name = [user.first_name, user.last_name].filter(Boolean).join(" ");
3939
console.log(`Verified as ${name} (id ${user.id}).`);
4040
}

0 commit comments

Comments
 (0)