refactor(cli): dedupe browser debug and download opts

This commit is contained in:
Peter Steinberger
2026-02-18 23:33:01 +00:00
parent 0048af4e2d
commit 3f621d13ff
2 changed files with 36 additions and 16 deletions

View File

@@ -22,6 +22,13 @@ export function registerBrowserFilesAndDownloadsCommands(
browser: Command,
parentOpts: (cmd: Command) => BrowserParentOpts,
) {
const resolveTimeoutAndTarget = (opts: { timeoutMs?: unknown; targetId?: unknown }) => {
const timeoutMs = Number.isFinite(opts.timeoutMs) ? Number(opts.timeoutMs) : undefined;
const targetId =
typeof opts.targetId === "string" ? opts.targetId.trim() || undefined : undefined;
return { timeoutMs, targetId };
};
const runDownloadCommand = async (
cmd: Command,
opts: { timeoutMs?: unknown; targetId?: unknown },
@@ -29,7 +36,7 @@ export function registerBrowserFilesAndDownloadsCommands(
) => {
const { parent, profile } = resolveBrowserActionContext(cmd, parentOpts);
try {
const timeoutMs = Number.isFinite(opts.timeoutMs) ? Number(opts.timeoutMs) : undefined;
const { timeoutMs, targetId } = resolveTimeoutAndTarget(opts);
const result = await callBrowserRequest<{ download: { path: string } }>(
parent,
{
@@ -38,8 +45,7 @@ export function registerBrowserFilesAndDownloadsCommands(
query: profile ? { profile } : undefined,
body: {
...request.body,
targetId:
typeof opts.targetId === "string" ? opts.targetId.trim() || undefined : undefined,
targetId,
timeoutMs,
},
},
@@ -76,7 +82,7 @@ export function registerBrowserFilesAndDownloadsCommands(
const { parent, profile } = resolveBrowserActionContext(cmd, parentOpts);
try {
const normalizedPaths = normalizeUploadPaths(paths);
const timeoutMs = Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : undefined;
const { timeoutMs, targetId } = resolveTimeoutAndTarget(opts);
const result = await callBrowserRequest<{ download: { path: string } }>(
parent,
{
@@ -88,7 +94,7 @@ export function registerBrowserFilesAndDownloadsCommands(
ref: opts.ref?.trim() || undefined,
inputRef: opts.inputRef?.trim() || undefined,
element: opts.element?.trim() || undefined,
targetId: opts.targetId?.trim() || undefined,
targetId,
timeoutMs,
},
},
@@ -172,7 +178,7 @@ export function registerBrowserFilesAndDownloadsCommands(
return;
}
try {
const timeoutMs = Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : undefined;
const { timeoutMs, targetId } = resolveTimeoutAndTarget(opts);
const result = await callBrowserRequest(
parent,
{
@@ -182,7 +188,7 @@ export function registerBrowserFilesAndDownloadsCommands(
body: {
accept,
promptText: opts.prompt?.trim() || undefined,
targetId: opts.targetId?.trim() || undefined,
targetId,
timeoutMs,
},
},

View File

@@ -12,6 +12,20 @@ function runBrowserDebug(action: () => Promise<void>) {
});
}
function resolveDebugQuery(params: {
targetId?: unknown;
clear?: unknown;
profile?: string;
filter?: unknown;
}) {
return {
targetId: typeof params.targetId === "string" ? params.targetId.trim() || undefined : undefined,
filter: typeof params.filter === "string" ? params.filter.trim() || undefined : undefined,
clear: Boolean(params.clear),
profile: params.profile,
};
}
export function registerBrowserDebugCommands(
browser: Command,
parentOpts: (cmd: Command) => BrowserParentOpts,
@@ -62,11 +76,11 @@ export function registerBrowserDebugCommands(
{
method: "GET",
path: "/errors",
query: {
targetId: opts.targetId?.trim() || undefined,
clear: Boolean(opts.clear),
query: resolveDebugQuery({
targetId: opts.targetId,
clear: opts.clear,
profile,
},
}),
},
{ timeoutMs: 20000 },
);
@@ -110,12 +124,12 @@ export function registerBrowserDebugCommands(
{
method: "GET",
path: "/requests",
query: {
targetId: opts.targetId?.trim() || undefined,
filter: opts.filter?.trim() || undefined,
clear: Boolean(opts.clear),
query: resolveDebugQuery({
targetId: opts.targetId,
filter: opts.filter,
clear: opts.clear,
profile,
},
}),
},
{ timeoutMs: 20000 },
);