refactor(browser): share download request helper

This commit is contained in:
Peter Steinberger
2026-02-18 18:54:27 +00:00
parent 2863661bcc
commit 9362e0f9a9

View File

@@ -88,6 +88,23 @@ export type BrowserDownloadPayload = {
path: string; path: string;
}; };
type BrowserDownloadResult = { ok: true; targetId: string; download: BrowserDownloadPayload };
async function postDownloadRequest(
baseUrl: string | undefined,
route: "/wait/download" | "/download",
body: Record<string, unknown>,
profile?: string,
): Promise<BrowserDownloadResult> {
const q = buildProfileQuery(profile);
return await fetchBrowserJson<BrowserDownloadResult>(withBaseUrl(baseUrl, `${route}${q}`), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
timeoutMs: 20000,
});
}
export async function browserNavigate( export async function browserNavigate(
baseUrl: string | undefined, baseUrl: string | undefined,
opts: { opts: {
@@ -165,22 +182,17 @@ export async function browserWaitForDownload(
timeoutMs?: number; timeoutMs?: number;
profile?: string; profile?: string;
}, },
): Promise<{ ok: true; targetId: string; download: BrowserDownloadPayload }> { ): Promise<BrowserDownloadResult> {
const q = buildProfileQuery(opts.profile); return await postDownloadRequest(
return await fetchBrowserJson<{ baseUrl,
ok: true; "/wait/download",
targetId: string; {
download: BrowserDownloadPayload;
}>(withBaseUrl(baseUrl, `/wait/download${q}`), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
targetId: opts.targetId, targetId: opts.targetId,
path: opts.path, path: opts.path,
timeoutMs: opts.timeoutMs, timeoutMs: opts.timeoutMs,
}), },
timeoutMs: 20000, opts.profile,
}); );
} }
export async function browserDownload( export async function browserDownload(
@@ -192,23 +204,18 @@ export async function browserDownload(
timeoutMs?: number; timeoutMs?: number;
profile?: string; profile?: string;
}, },
): Promise<{ ok: true; targetId: string; download: BrowserDownloadPayload }> { ): Promise<BrowserDownloadResult> {
const q = buildProfileQuery(opts.profile); return await postDownloadRequest(
return await fetchBrowserJson<{ baseUrl,
ok: true; "/download",
targetId: string; {
download: BrowserDownloadPayload;
}>(withBaseUrl(baseUrl, `/download${q}`), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
targetId: opts.targetId, targetId: opts.targetId,
ref: opts.ref, ref: opts.ref,
path: opts.path, path: opts.path,
timeoutMs: opts.timeoutMs, timeoutMs: opts.timeoutMs,
}), },
timeoutMs: 20000, opts.profile,
}); );
} }
export async function browserAct( export async function browserAct(