useImportJob
Helpers to wait for a job the API queued instead of running it inside the request. WeImport uses them so that long imports work the same whether the backend answers synchronously or queues them.
Contract
- The call that starts the work answers with a job handle
{ job_id, status }(typically HTTP 202) instead of the usual result. - The client POSTs to the same URL with
{ ...params, action: 'status', job_id }until the status is terminal. - Each status response is
{ job_id, status, progress?, result?, error? }withstatusone ofqueued,running,completed,failed. - On
completed,resultis exactly the payload the synchronous endpoint would have returned.
Handles and states are read from what useApi().$post returns, i.e. the data of the response.
Usage
ts
const { isImportJob, pollImportJob, ImportJobError } = useImportJob()
const api = useApi()
const res = await api.$post('products/import', body)
const result = isImportJob(res)
? await pollImportJob(api, 'products/import', {
jobId: res.job_id,
onProgress: p => console.log(p ? `${p.done}/${p.total}` : 'waiting…'),
})
: resReturn Value
| Property | Type | Description |
|---|---|---|
isImportJob | (response: any) => response is ImportJobHandle | true when response.job_id is a string |
pollImportJob | <T>(api, url, options) => Promise<T> | Polls until the job ends and resolves with result |
ImportJobError | class | Error thrown by pollImportJob, with a reason |
pollImportJob(api, url, options)
| Argument | Type | Description |
|---|---|---|
api | { $post } | A useApi() instance (only $post is used) |
url | string | Endpoint that started the job |
options.jobId | string | job_id from the handle |
options.params | object | Extra body fields sent with every status call (e.g. the import type). Default {} |
options.onProgress | (progress: { done, total } | null) => void | Called after every status response |
options.signal | AbortSignal | Stops polling |
options.timeout | number | Total wait in ms before giving up. Default 30 minutes |
Behaviour:
- Polls every 1 s for the first 10 s, every 2 s up to 1 minute, then every 5 s.
- Status calls are sent with
notify: false: no toast per attempt, the caller handles errors. - A 4xx on a status call fails immediately (the job is gone or not accessible).
- Network errors and 5xx are retried; more than 3 consecutive failures fail the job.
ImportJobError
ts
class ImportJobError extends Error {
name: 'ImportJobError'
reason: 'failed' | 'timeout' | 'aborted'
}reason | When |
|---|---|
failed | Status failed (message = the job error), a 4xx, or too many consecutive errors |
timeout | timeout elapsed |
aborted | signal aborted |
ts
try {
await pollImportJob(api, url, { jobId, signal: controller.signal })
} catch (e) {
if (e instanceof ImportJobError && e.reason !== 'aborted') {
useNotify().error(e.reason === 'timeout' ? 'The import is taking too long' : e.message)
}
}Types
ts
type ImportJobStatus = 'queued' | 'running' | 'completed' | 'failed'
interface ImportJobHandle {
job_id: string
status: ImportJobStatus
}
interface ImportJobState<T> extends ImportJobHandle {
progress?: { done: number, total: number } | null
result?: T
error?: string
}