/** * @file pollResponse.ts * @description A generic helper function to poll an endpoint until a specific condition is met or an error occurs. * * Usage: * ```typescript * import { pollResponse } from './pollResponse'; * * interface MyJobStatus { * id: string; * status: 'pending' | 'processing' | 'finished' | 'error'; * result?: any; * errorMessage?: string; * } * * async function checkJob(jobId: string): Promise { * const url = `https://api.example.com/jobs/${jobId}`; * * try { * const finalStatus = await pollResponse({ * url, * isCompleteCondition: (response) => response.status === 'finished', * isErrorCondition: (response) => response.status === 'error', * intervalMs: 2000, // Poll every 2 seconds * maxRetries: 30, // Try up to 30 times (1 minute total) * errorMessageContext: `Job ${jobId}` // For clearer error messages * }); * console.log('Job finished successfully:', finalStatus.result); * return finalStatus; * } catch (error) { * console.error('Failed to get job status:', error); * throw error; * } * } * ``` */ /** * Defines the parameters for the pollResponse function. * @template TResponse The expected type of the JSON response from the URL. */ export interface PollResponseParams { /** The URL to poll. */ url: string; /** * A callback function that takes the response data and returns `true` if the polling * should be considered complete, `false` otherwise. * @param response The JSON response from the URL. * @returns `true` if the condition for completion is met, `false` otherwise. */ isCompleteCondition: (response: TResponse) => boolean; /** * (Optional) A callback function that takes the response data and returns `true` * if the job/process has definitively failed or encountered an unrecoverable error. * If this condition is met, polling will stop immediately and the promise will be rejected. * @param response The JSON response from the URL. * @returns `true` if an error condition is met, `false` otherwise. */ isErrorCondition?: (response: TResponse) => boolean; /** The interval in milliseconds between polling attempts. Defaults to 1000ms (1 second). */ intervalMs?: number; /** The maximum number of polling attempts. Defaults to 60. */ maxRetries?: number; /** (Optional) A string to provide context in error messages (e.g., "Job ID X", "Process Y"). */ errorMessageContext?: string; } /** * Polls an endpoint until a specific condition is met, an error condition is met, or the maximum retries are exhausted. * @template TResponse The expected type of the JSON response from the URL. * @param params Parameters for polling, including the URL, completion condition, and retry logic. * @returns A promise that resolves with the successful response data when the completion condition is met. * @throws An error if the error condition is met, polling times out, a network error occurs, or the response is not valid JSON. */ export declare function pollResponse({ url, isCompleteCondition, isErrorCondition, intervalMs, maxRetries, errorMessageContext, }: PollResponseParams): Promise;