|
| 1 | +import CloudConvert from './CloudConvert'; |
| 2 | +import { Operation, Task, TaskEventData, TaskStatus } from './TasksResource'; |
| 3 | + |
| 4 | +export type JobEvent = 'created' | 'updated' | 'finished' | 'failed'; |
| 5 | +export type JobStatus = 'processing' | 'finished' | 'error'; |
| 6 | +export type JobTaskStatus = Task['status'] | 'queued' |
| 7 | +export interface JobEventData { job: Job } |
| 8 | + |
| 9 | +export interface Job { |
| 10 | + id: string; |
| 11 | + tag: string | null; |
| 12 | + status: TaskStatus; |
| 13 | + created_at: string; |
| 14 | + started_at: string | null; |
| 15 | + ended_at: string | null; |
| 16 | + tasks: JobTask[]; |
| 17 | +} |
| 18 | +type NotPresentWhenInsideJob = 'job_id' | 'status' |
| 19 | +interface JobTask extends Omit<Task, NotPresentWhenInsideJob> { |
| 20 | + name: string; |
| 21 | + status: JobTaskStatus; |
| 22 | +} |
| 23 | + |
| 24 | +export default class JobsResource { |
| 25 | + private readonly cloudConvert: CloudConvert; |
| 26 | + |
| 27 | + constructor(cloudConvert: CloudConvert) { |
| 28 | + this.cloudConvert = cloudConvert; |
| 29 | + } |
| 30 | + |
| 31 | + async get(id: string, query: null = null): Promise<Job> { |
| 32 | + const response = await this.cloudConvert.axios.get('jobs/' + id, { |
| 33 | + params: query || {} |
| 34 | + }); |
| 35 | + return response.data.data; |
| 36 | + } |
| 37 | + |
| 38 | + async wait(id: string): Promise<Job> { |
| 39 | + const response = await this.cloudConvert.axios.get('jobs/' + id + '/wait'); |
| 40 | + return response.data.data; |
| 41 | + } |
| 42 | + |
| 43 | + async all(query: { 'filter[status]'?: JobStatus; 'filter[tag]'?: string; include?: string; per_page?: number; page?: number; } | null = null): Promise<Job[]> { |
| 44 | + const response = await this.cloudConvert.axios.get('jobs', { |
| 45 | + params: query || {} |
| 46 | + }); |
| 47 | + return response.data.data; |
| 48 | + } |
| 49 | + |
| 50 | + // See below for an explanation on how this type signature works |
| 51 | + async create(data: JobTemplate | null = null): Promise<Job> { |
| 52 | + const response = await this.cloudConvert.axios.post('jobs', data); |
| 53 | + return response.data.data; |
| 54 | + } |
| 55 | + |
| 56 | + async delete(id: string): Promise<void> { |
| 57 | + await this.cloudConvert.axios.delete('jobs/' + id); |
| 58 | + } |
| 59 | + |
| 60 | + async subscribeEvent(id: string, event: string, callback: (event: JobEventData) => void): Promise<void> { |
| 61 | + this.cloudConvert.subscribe('private-job.' + id, 'job.' + event, callback); |
| 62 | + } |
| 63 | + |
| 64 | + async subscribeTaskEvent(id: string, event: string, callback: (event: TaskEventData) => void): Promise<void> { |
| 65 | + this.cloudConvert.subscribe('private-job.' + id + '.tasks', 'task.' + event, callback); |
| 66 | + } |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +// We need to map the types from the large Operation union type |
| 71 | +// to the template syntax from the API specs (confer the README) |
| 72 | +// that is used to create a job with a number of tasks. While this |
| 73 | +// is possible to write in just two lines of code, we divide this |
| 74 | +// up in many small steps in order to explain what's happening: |
| 75 | + |
| 76 | +// All possible operation strings ("import/url" etc) |
| 77 | +type PossibleOperationStrings = Operation['operation']; |
| 78 | +// Every argument in the tasks object should be assignable to this (for some operation string O) |
| 79 | +interface NamedOperation<O> { operation: O } |
| 80 | +// Given an operation string O, get the operation for it |
| 81 | +type OperationByName<O> = Extract<Operation, NamedOperation<O>> |
| 82 | +// Given an operation string O, get the operation data for it |
| 83 | +type OperationData<O> = OperationByName<O>['data']; |
| 84 | +// Add all properties to task that can only occur in tasks that are inside jobs |
| 85 | +interface TaskExtras<O> extends NamedOperation<O> { ignore_error?: boolean; } |
| 86 | +// Every argument in the tasks object is typed by this (for some operation string O) |
| 87 | +type TaskTemplate<O> = TaskExtras<O> & OperationData<O>; |
| 88 | +// Given a union type U of operation strings, turn each operation string into its TaskTemplate |
| 89 | +type Distribute<U> = U extends any ? TaskTemplate<U> : never; |
| 90 | +// Create a union of all possible tasks |
| 91 | +type PossibleOperations = Distribute<PossibleOperationStrings>; |
| 92 | +// Allow any number of names, each typed by a possible operation |
| 93 | +interface TaskContainer { [name: string]: PossibleOperations; } |
| 94 | +// Add the other properties that are required for job creation |
| 95 | +interface JobTemplate { tasks: TaskContainer; tag?: string; } |
0 commit comments