Skip to content

Commit cfb82aa

Browse files
committed
Iron out a few more type issues
1 parent 6554faa commit cfb82aa

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

lib/JobsResource.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export interface Job {
1212
created_at: string;
1313
started_at: string | null;
1414
ended_at: string | null;
15-
tasks: Task[];
15+
tasks: JobTask[];
16+
}
17+
type NotPresentWhenInsideJob = 'job_id'
18+
interface JobTask extends Omit<Task, NotPresentWhenInsideJob> {
19+
name: string;
1620
}
1721

1822
export default class JobsResource {
@@ -42,7 +46,7 @@ export default class JobsResource {
4246
}
4347

4448
// See below for an explanation on how this type signature works
45-
async create(data: { tasks: TaskContainer } | null = null): Promise<Job> {
49+
async create(data: JobTemplate | null = null): Promise<Job> {
4650
const response = await this.cloudConvert.axios.post('jobs', data);
4751
return response.data.data;
4852
}
@@ -75,12 +79,15 @@ interface NamedOperation<O> { operation: O }
7579
type OperationByName<O> = Extract<Operation, NamedOperation<O>>
7680
// Given an operation string O, get the operation data for it
7781
type OperationData<O> = OperationByName<O>['data'];
82+
// Add all properties to task that can only occur in tasks that are inside jobs
83+
interface TaskExtras<O> extends NamedOperation<O> { ignore_error?: boolean; }
7884
// Every argument in the tasks object is typed by this (for some operation string O)
79-
type TaskTemplate<O> = NamedOperation<O> & OperationData<O>;
85+
type TaskTemplate<O> = TaskExtras<O> & OperationData<O>;
8086
// Given a union type U of operation strings, turn each operation string into its TaskTemplate
8187
type Distribute<U> = U extends any ? TaskTemplate<U> : never;
8288
// Create a union of all possible tasks
8389
type PossibleOperations = Distribute<PossibleOperationStrings>;
8490
// Allow any number of names, each typed by a possible operation
8591
interface TaskContainer { [name: string]: PossibleOperations; }
86-
// Yay! We can now use this container for our function signature above.
92+
// Add the other properties that are required for job creation
93+
interface JobTemplate { tasks: TaskContainer; tag?: string; }

lib/TasksResource.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ export interface Task {
317317
engine: string;
318318
engine_version: string;
319319
payload: any;
320-
result: any;
320+
result?: { files?: FileResult[]; [key: string]: any; };
321+
}
322+
export interface FileResult {
323+
filename: string;
324+
url?: string;
321325
}
322326

323327
export default class TasksResource {

0 commit comments

Comments
 (0)