Skip to content

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 ​

  1. The call that starts the work answers with a job handle { job_id, status } (typically HTTP 202) instead of the usual result.
  2. The client POSTs to the same URL with { ...params, action: 'status', job_id } until the status is terminal.
  3. Each status response is { job_id, status, progress?, result?, error? } with status one of queued, running, completed, failed.
  4. On completed, result is 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…'),
    })
  : res

Return Value ​

PropertyTypeDescription
isImportJob(response: any) => response is ImportJobHandletrue when response.job_id is a string
pollImportJob<T>(api, url, options) => Promise<T>Polls until the job ends and resolves with result
ImportJobErrorclassError thrown by pollImportJob, with a reason

pollImportJob(api, url, options) ​

ArgumentTypeDescription
api{ $post }A useApi() instance (only $post is used)
urlstringEndpoint that started the job
options.jobIdstringjob_id from the handle
options.paramsobjectExtra body fields sent with every status call (e.g. the import type). Default {}
options.onProgress(progress: { done, total } | null) => voidCalled after every status response
options.signalAbortSignalStops polling
options.timeoutnumberTotal 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'
}
reasonWhen
failedStatus failed (message = the job error), a 4xx, or too many consecutive errors
timeouttimeout elapsed
abortedsignal 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
}

Source Code ​