/*! * @license * Copyright Squiz Australia Pty Ltd. All Rights Reserved. */ import { AxiosInstance } from 'axios'; import { handleResponse } from './responseHandler'; /** * Watch and wait for upload and scan to complete * @param apiClient - Axios client for API calls * @param endpoint - API endpoint to poll * @param id - Upload ID to check * @returns Promise that resolves when scan is complete */ export async function watchAndWaitForUploadAndScanComplete( apiClient: AxiosInstance, endpoint: string, id: string, ): Promise { const poll = () => handleResponse(apiClient.get(endpoint + id)); return new Promise((resolve, reject) => { const recurse = () => poll().then(async (req) => { if (req.status == 'Success') { resolve(); } else if (req.status == 'Flagged') { reject(new Error('upload has been flagged as a virus')); } else if (req.status == 'Error') { reject(new Error('there has been an error')); } else { setTimeout(async () => { await recurse(); }, 1000); } }); recurse(); }); }