( fileName: string, fileSize: number, domain: string = DEFAULT_DOMAIN )
| 30 | * @throws Error if the request fails |
| 31 | */ |
| 32 | export async function initImport( |
| 33 | fileName: string, |
| 34 | fileSize: number, |
| 35 | domain: string = DEFAULT_DOMAIN |
| 36 | ): Promise<InitImportResponse> { |
| 37 | const apiEndpoint = getApiEndpoint(domain) |
| 38 | const url = `${apiEndpoint}/v1/import/init` |
| 39 | |
| 40 | debug(`Initializing import at ${url}`) |
| 41 | |
| 42 | const response = await fetch(url, { |
| 43 | method: 'POST', |
| 44 | headers: { |
| 45 | 'Content-Type': 'application/json', |
| 46 | }, |
| 47 | body: JSON.stringify({ |
| 48 | fileName, |
| 49 | fileSize, |
| 50 | }), |
| 51 | signal: AbortSignal.timeout(30_000), |
| 52 | }) |
| 53 | |
| 54 | if (!response.ok) { |
| 55 | const responseBody = await response.text() |
| 56 | debug(`Init import failed - Status: ${response.status}, Body: ${responseBody}`) |
| 57 | const message = parseApiErrorMessage(responseBody, 'Failed to initialize import') |
| 58 | throw new ImportError(message, response.status) |
| 59 | } |
| 60 | |
| 61 | return (await response.json()) as InitImportResponse |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Uploads a file to the presigned S3 URL. |
no test coverage detected