Skip to content

Commit

Permalink
[bbrt] Include credentials and API key when fetching boards (#4035)
Browse files Browse the repository at this point in the history
  • Loading branch information
aomarks authored Dec 16, 2024
1 parent 51fa2c5 commit a15e79b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 8 additions & 3 deletions packages/bbrt/src/breadboard/breadboard-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ const BOARDS_CACHE_KEY = "bbrt-boards-v1";

export class BreadboardServer {
readonly #baseUrl: string;
readonly #apiKey: string | undefined;

constructor(url: string) {
constructor(url: string, apiKey?: string) {
this.#baseUrl = url;
this.#apiKey = apiKey;
}

get url() {
Expand All @@ -35,7 +37,7 @@ export class BreadboardServer {
return resultify(() => JSON.parse(cached) as BreadboardBoardListing[]);
}
const url = new URL("/boards", this.#baseUrl);
const response = await resultify(fetch(url));
const response = await resultify(fetch(url, { credentials: "include" }));
if (!response.ok) {
return response;
}
Expand All @@ -57,7 +59,10 @@ export class BreadboardServer {

async board(boardPath: string): Promise<Result<GraphDescriptor>> {
const url = new URL(`/boards/${boardPath}`, this.#baseUrl);
const response = await resultify(fetch(url));
if (this.#apiKey) {
url.searchParams.set("API_KEY", this.#apiKey);
}
const response = await resultify(fetch(url, { credentials: "include" }));
if (!response.ok) {
return response;
}
Expand Down
9 changes: 5 additions & 4 deletions packages/bbrt/src/breadboard/indexed-db-servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Result } from "../util/result.js";
import { resultify } from "../util/resultify.js";
import { BreadboardServer } from "./breadboard-server.js";

type BoardServerEntry = { url: string };
type BoardServerEntry = { url: string; user?: { apiKey?: string } };

export async function readBoardServersFromIndexedDB(): Promise<
Result<BreadboardServer[]>
Expand Down Expand Up @@ -36,8 +36,9 @@ export async function readBoardServersFromIndexedDB(): Promise<
return {
ok: true,
value: entries.value
.map(({ url }) => url)
.filter((url) => url.startsWith("http") || url.startsWith("https"))
.map((url) => new BreadboardServer(url)),
.filter(
({ url }) => url.startsWith("http://") || url.startsWith("https://")
)
.map(({ url, user }) => new BreadboardServer(url, user?.apiKey)),
};
}

0 comments on commit a15e79b

Please sign in to comment.