|
1 | 1 | import FormData, { type Stream } from 'form-data'; |
2 | 2 | import CloudConvert from './CloudConvert'; |
3 | 3 | import { type JobTask } from './JobsResource'; |
4 | | -import axios from 'axios'; |
5 | 4 | import { ReadStream, statSync } from 'fs'; |
6 | 5 |
|
7 | 6 | export type TaskEvent = 'created' | 'updated' | 'finished' | 'failed'; |
@@ -546,64 +545,46 @@ export default class TasksResource { |
546 | 545 | this.cloudConvert = cloudConvert; |
547 | 546 | } |
548 | 547 |
|
549 | | - async get( |
550 | | - id: string, |
551 | | - query: { include: string } | null = null |
552 | | - ): Promise<Task> { |
553 | | - const response = await this.cloudConvert.axios.get(`tasks/${id}`, { |
554 | | - params: query || {} |
555 | | - }); |
556 | | - return response.data.data; |
| 548 | + async get(id: string, query?: { include?: string }): Promise<Task> { |
| 549 | + return await this.cloudConvert.call('GET', `tasks/${id}`, query); |
557 | 550 | } |
558 | 551 |
|
559 | 552 | async wait(id: string): Promise<Task> { |
560 | | - const response = await this.cloudConvert.axios.get(`tasks/${id}`, { |
561 | | - baseURL: this.cloudConvert.useSandbox |
562 | | - ? 'https://sync.api.sandbox.cloudconvert.com/v2/' |
563 | | - : `https://${ |
564 | | - this.cloudConvert.region |
565 | | - ? this.cloudConvert.region + '.' |
566 | | - : '' |
567 | | - }sync.api.cloudconvert.com/v2/` |
568 | | - }); |
569 | | - return response.data.data; |
| 553 | + const baseURL = this.cloudConvert.useSandbox |
| 554 | + ? 'https://sync.api.sandbox.cloudconvert.com/v2/' |
| 555 | + : `https://${ |
| 556 | + this.cloudConvert.region ? this.cloudConvert.region + '.' : '' |
| 557 | + }sync.api.cloudconvert.com/v2/`; |
| 558 | + return await this.cloudConvert.callWithBase( |
| 559 | + baseURL, |
| 560 | + 'GET', |
| 561 | + `tasks/${id}` |
| 562 | + ); |
570 | 563 | } |
571 | 564 |
|
572 | 565 | async cancel(id: string): Promise<Task> { |
573 | | - const response = await this.cloudConvert.axios.post( |
574 | | - `tasks/${id}/cancel` |
575 | | - ); |
576 | | - return response.data.data; |
| 566 | + return await this.cloudConvert.call('POST', `tasks/${id}/cancel`); |
577 | 567 | } |
578 | 568 |
|
579 | | - async all( |
580 | | - query: { |
581 | | - 'filter[job_id]'?: string; |
582 | | - 'filter[status]'?: TaskStatus; |
583 | | - 'filter[operation]'?: Operation['operation']; |
584 | | - per_page?: number; |
585 | | - page?: number; |
586 | | - } | null = null |
587 | | - ): Promise<Task[]> { |
588 | | - const response = await this.cloudConvert.axios.get('tasks', { |
589 | | - params: query || {} |
590 | | - }); |
591 | | - return response.data.data; |
| 569 | + async all(query?: { |
| 570 | + 'filter[job_id]'?: string; |
| 571 | + 'filter[status]'?: TaskStatus; |
| 572 | + 'filter[operation]'?: Operation['operation']; |
| 573 | + per_page?: number; |
| 574 | + page?: number; |
| 575 | + }): Promise<Task[]> { |
| 576 | + return await this.cloudConvert.call('GET', 'tasks', query); |
592 | 577 | } |
593 | 578 |
|
594 | 579 | async create<O extends Operation['operation']>( |
595 | 580 | operation: O, |
596 | | - data: Extract<Operation, { operation: O }>['data'] | null = null |
| 581 | + data?: Extract<Operation, { operation: O }>['data'] |
597 | 582 | ): Promise<Task> { |
598 | | - const response = await this.cloudConvert.axios.post<any>( |
599 | | - operation, |
600 | | - data |
601 | | - ); |
602 | | - return response.data.data; |
| 583 | + return await this.cloudConvert.call('POST', operation, data); |
603 | 584 | } |
604 | 585 |
|
605 | 586 | async delete(id: string): Promise<void> { |
606 | | - await this.cloudConvert.axios.delete(`tasks/${id}`); |
| 587 | + await this.cloudConvert.call('DELETE', `tasks/${id}`); |
607 | 588 | } |
608 | 589 |
|
609 | 590 | async upload( |
@@ -638,16 +619,11 @@ export default class TasksResource { |
638 | 619 | } |
639 | 620 | formData.append('file', stream, fileOptions); |
640 | 621 |
|
641 | | - return await axios.post(task.result.form.url, formData, { |
642 | | - maxContentLength: Infinity, |
643 | | - maxBodyLength: Infinity, |
644 | | - headers: { |
645 | | - ...(formData.hasKnownLength() |
646 | | - ? { 'Content-Length': formData.getLengthSync() } |
647 | | - : {}), |
648 | | - ...formData.getHeaders() |
649 | | - } |
650 | | - }); |
| 622 | + return await this.cloudConvert.call( |
| 623 | + 'POST', |
| 624 | + task.result.form.url, |
| 625 | + formData |
| 626 | + ); |
651 | 627 | } |
652 | 628 |
|
653 | 629 | async subscribeEvent( |
|
0 commit comments