diff --git a/src/__tests__/async-task.test.ts b/src/__tests__/async-task.test.ts index 578dee6..b78c878 100644 --- a/src/__tests__/async-task.test.ts +++ b/src/__tests__/async-task.test.ts @@ -1,5 +1,5 @@ import type { SearchParams, Task, TaskRequest, TaskStatus } from 'linkup-sdk'; -import { pollTask, runAsyncTaskFlow } from '../commands/async-task'; +import { pollTask, runTaskFlow } from '../commands/async-task'; import { captureConsole } from './helpers/capture'; import { makeTask } from './helpers/fixtures'; @@ -72,7 +72,7 @@ describe('pollTask', () => { }); }); -describe('runAsyncTaskFlow', () => { +describe('runTaskFlow', () => { type TestResponse = { answer: string }; beforeEach(() => { @@ -113,7 +113,7 @@ describe('runAsyncTaskFlow', () => { const runSync = jest.fn().mockResolvedValue({ answer: 'done' }); const { errorSpy, logSpy } = captureConsole(); - await runAsyncTaskFlow({ + await runTaskFlow({ buildRequest, client, formatSync, @@ -137,7 +137,7 @@ describe('runAsyncTaskFlow', () => { const runSync = jest.fn(); const { logSpy } = captureConsole(); - await runAsyncTaskFlow({ + await runTaskFlow({ async: true, buildRequest, client, @@ -158,7 +158,7 @@ describe('runAsyncTaskFlow', () => { }); const { logSpy } = captureConsole(); - await runAsyncTaskFlow({ + await runTaskFlow({ async: true, buildRequest, client, @@ -184,7 +184,7 @@ describe('runAsyncTaskFlow', () => { }); const { logSpy } = captureConsole(); - await runAsyncTaskFlow({ + await runTaskFlow({ async: true, buildRequest, client, @@ -207,7 +207,7 @@ describe('runAsyncTaskFlow', () => { }); const { errorSpy, logSpy } = captureConsole(); - await runAsyncTaskFlow({ + await runTaskFlow({ async: true, buildRequest, client, diff --git a/src/commands/async-task.ts b/src/commands/async-task.ts index ed43407..7979c58 100644 --- a/src/commands/async-task.ts +++ b/src/commands/async-task.ts @@ -120,7 +120,7 @@ export async function waitForTask( } } -export async function runAsyncTaskFlow( +export async function runTaskFlow( options: AsyncTaskFlowOptions, ): Promise { if (options.wait && !options.async) { diff --git a/src/commands/fetch.ts b/src/commands/fetch.ts index c8c3ef4..70f466a 100644 --- a/src/commands/fetch.ts +++ b/src/commands/fetch.ts @@ -4,7 +4,7 @@ import { resolveGlobals } from '../client'; import { exitWithError, formatErrorLine } from '../output/errors'; import { formatFetch } from '../output/fetch'; import { formatTaskErrorLine } from '../output/task-errors'; -import { createPollIntervalOption, createTimeoutOption, runAsyncTaskFlow } from './async-task'; +import { createPollIntervalOption, createTimeoutOption, runTaskFlow } from './async-task'; type FetchCommandOptions = { renderJs?: boolean; @@ -47,7 +47,7 @@ async function runFetch( const { client, json } = resolveGlobals(command); const params = buildFetchParams(url, options); - await runAsyncTaskFlow({ + await runTaskFlow({ async: options.async, buildRequest: buildFetchTaskRequest, client, diff --git a/src/commands/research.ts b/src/commands/research.ts index bbe09da..fd86827 100644 --- a/src/commands/research.ts +++ b/src/commands/research.ts @@ -183,6 +183,23 @@ function printResearchResult(result: PollTaskResult, json: boolean printLines(lines); } +async function waitForResearchAndPrint( + client: ReturnType['client'], + id: string, + options: { pollInterval: number; timeout: number }, + json: boolean, +): Promise { + const result = await waitForTask({ + getTask: taskId => client.getResearch(taskId), + id, + json, + label: 'Researching...', + pollIntervalSeconds: options.pollInterval, + timeoutSeconds: options.timeout, + }); + printResearchResult(result, json); +} + async function runResearchSubmit( queryParts: string[], options: ResearchCommandOptions, @@ -210,15 +227,7 @@ async function runResearchSubmit( const task = await client.research(params); if (options.wait) { - const result = await waitForTask({ - getTask: id => client.getResearch(id), - id: task.id, - json, - label: 'Researching...', - pollIntervalSeconds: options.pollInterval, - timeoutSeconds: options.timeout, - }); - printResearchResult(result, json); + await waitForResearchAndPrint(client, task.id, options, json); return; } @@ -238,15 +247,7 @@ async function runResearchGet( try { if (options.wait) { - const result = await waitForTask({ - getTask: taskId => client.getResearch(taskId), - id, - json, - label: 'Researching...', - pollIntervalSeconds: options.pollInterval, - timeoutSeconds: options.timeout, - }); - printResearchResult(result, json); + await waitForResearchAndPrint(client, id, options, json); return; } diff --git a/src/commands/search.ts b/src/commands/search.ts index f686c52..5f0fa40 100644 --- a/src/commands/search.ts +++ b/src/commands/search.ts @@ -4,7 +4,7 @@ import { resolveGlobals } from '../client'; import { exitWithError, formatErrorLine } from '../output/errors'; import { formatSearch } from '../output/search'; import { formatTaskErrorLine } from '../output/task-errors'; -import { createPollIntervalOption, createTimeoutOption, runAsyncTaskFlow } from './async-task'; +import { createPollIntervalOption, createTimeoutOption, runTaskFlow } from './async-task'; import { parseDateOption, parseDomainList, parsePositiveInt } from './option-parsers'; import { queryUsageLines, resolveQueryOrExit } from './query-input'; import { @@ -177,7 +177,7 @@ async function runSearch( console.error(warning); } - await runAsyncTaskFlow({ + await runTaskFlow({ async: options.async, buildRequest: buildSearchTaskRequest, client,