Skip to content

Commit 6405d34

Browse files
committed
drop axios, use web fetch
1 parent 63c86a8 commit 6405d34

5 files changed

Lines changed: 94 additions & 114 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ const inputFile = fs.createReadStream('./file.pdf');
9292

9393
await cloudConvert.tasks.upload(uploadTask, inputFile, 'file.pdf');
9494
```
95-
> **Note on custom streams**:
96-
The length of the stream needs to be known prior to uploading. The SDK automatically detects the file size of file-based read streams. If you are using a custom stream, you need to pass a `filesize` as fourth parameter to the `upload()` method.
9795

96+
> **Note on custom streams**:
97+
> The length of the stream needs to be known prior to uploading. The SDK automatically detects the file size of file-based read streams. If you are using a custom stream, you need to pass a `filesize` as fourth parameter to the `upload()` method.
9898
9999
## Websocket Events
100100

lib/CloudConvert.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import axios, { type AxiosInstance } from 'axios';
21
import io from 'socket.io-client';
2+
import FormData from 'form-data';
3+
import { version } from '../package.json';
34
import JobsResource, { type JobEventData } from './JobsResource';
5+
import SignedUrlResource from './SignedUrlResource';
46
import TasksResource, {
57
type JobTaskEventData,
68
type TaskEventData
79
} from './TasksResource';
810
import UsersResource from './UsersResource';
911
import WebhooksResource from './WebhooksResource';
10-
import { version } from '../package.json';
11-
import SignedUrlResource from './SignedUrlResource';
1212

1313
export default class CloudConvert {
1414
private socket: SocketIOClient.Socket | undefined;
@@ -18,7 +18,6 @@ export default class CloudConvert {
1818
public readonly useSandbox: boolean;
1919
public readonly region: string | null;
2020

21-
public axios!: AxiosInstance;
2221
public tasks!: TasksResource;
2322
public jobs!: JobsResource;
2423
public users!: UsersResource;
@@ -30,32 +29,49 @@ export default class CloudConvert {
3029
this.useSandbox = useSandbox;
3130
this.region = region;
3231

33-
this.createAxiosInstance();
34-
this.createResources();
35-
}
36-
37-
createAxiosInstance(): void {
38-
this.axios = axios.create({
39-
baseURL: this.useSandbox
40-
? 'https://api.sandbox.cloudconvert.com/v2/'
41-
: `https://${
42-
this.region ? this.region + '.' : ''
43-
}api.cloudconvert.com/v2/`,
44-
headers: {
45-
Authorization: `Bearer ${this.apiKey}`,
46-
'User-Agent': `cloudconvert-node/v${version} (https://github.com/cloudconvert/cloudconvert-node)`
47-
}
48-
});
49-
}
50-
51-
createResources(): void {
5232
this.tasks = new TasksResource(this);
5333
this.jobs = new JobsResource(this);
5434
this.users = new UsersResource(this);
5535
this.webhooks = new WebhooksResource();
5636
this.signedUrls = new SignedUrlResource();
5737
}
5838

39+
async call(
40+
method: 'GET' | 'POST' | 'DELETE',
41+
route: string,
42+
parameters?: FormData | object
43+
) {
44+
const baseURL = this.useSandbox
45+
? 'https://api.sandbox.cloudconvert.com/v2/'
46+
: `https://${
47+
this.region ? this.region + '.' : ''
48+
}api.cloudconvert.com/v2/`;
49+
return await this.callWithBase(baseURL, method, route, parameters);
50+
}
51+
52+
async callWithBase(
53+
baseURL: string,
54+
method: 'GET' | 'POST' | 'DELETE',
55+
route: string,
56+
parameters?: FormData | object
57+
) {
58+
const res = await fetch(new URL(route, baseURL), {
59+
method,
60+
headers: {
61+
Authorization: `Bearer ${this.apiKey}`,
62+
'User-Agent': `cloudconvert-node/v${version} (https://github.com/cloudconvert/cloudconvert-node)`,
63+
...(parameters instanceof FormData
64+
? parameters.getHeaders()
65+
: {})
66+
},
67+
body:
68+
parameters instanceof FormData
69+
? parameters
70+
: JSON.stringify(parameters)
71+
});
72+
return await res.json();
73+
}
74+
5975
subscribe(
6076
channel: string,
6177
event: string,

lib/JobsResource.ts

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,51 +36,40 @@ export default class JobsResource {
3636
this.cloudConvert = cloudConvert;
3737
}
3838

39-
async get(id: string, query = null): Promise<Job> {
40-
const response = await this.cloudConvert.axios.get(`jobs/${id}`, {
41-
params: query || {}
42-
});
43-
return response.data.data;
39+
async get(id: string, query?: object): Promise<Job> {
40+
return await this.cloudConvert.call('GET', `jobs/${id}`, query);
4441
}
4542

4643
async wait(id: string): Promise<Job> {
47-
const response = await this.cloudConvert.axios.get(`jobs/${id}`, {
48-
baseURL: this.cloudConvert.useSandbox
49-
? 'https://sync.api.sandbox.cloudconvert.com/v2/'
50-
: `https://${
51-
this.cloudConvert.region
52-
? this.cloudConvert.region + '.'
53-
: ''
54-
}sync.api.cloudconvert.com/v2/`
55-
});
56-
return response.data.data;
44+
const baseURL = this.cloudConvert.useSandbox
45+
? 'https://sync.api.sandbox.cloudconvert.com/v2/'
46+
: `https://${
47+
this.cloudConvert.region ? this.cloudConvert.region + '.' : ''
48+
}sync.api.cloudconvert.com/v2/`;
49+
return await this.cloudConvert.callWithBase(
50+
baseURL,
51+
'GET',
52+
`jobs/${id}`
53+
);
5754
}
5855

59-
async all(
60-
query: {
61-
'filter[status]'?: JobStatus;
62-
'filter[tag]'?: string;
63-
include?: string;
64-
per_page?: number;
65-
page?: number;
66-
} | null = null
67-
): Promise<Job[]> {
68-
const response = await this.cloudConvert.axios.get('jobs', {
69-
params: query || {}
70-
});
71-
return response.data.data;
56+
async all(query?: {
57+
'filter[status]'?: JobStatus;
58+
'filter[tag]'?: string;
59+
include?: string;
60+
per_page?: number;
61+
page?: number;
62+
}): Promise<Job[]> {
63+
return await this.cloudConvert.call('GET', 'jobs', query);
7264
}
7365

7466
// See below for an explanation on how this type signature works
75-
async create(data: JobTemplate | null = null): Promise<Job> {
76-
const response = await this.cloudConvert.axios.post('jobs', data, {
77-
maxBodyLength: Infinity
78-
});
79-
return response.data.data;
67+
async create(data?: JobTemplate): Promise<Job> {
68+
return await this.cloudConvert.call('POST', 'jobs', data);
8069
}
8170

8271
async delete(id: string): Promise<void> {
83-
await this.cloudConvert.axios.delete(`jobs/${id}`);
72+
await this.cloudConvert.call('DELETE', `jobs/${id}`);
8473
}
8574

8675
async subscribeEvent(

lib/TasksResource.ts

Lines changed: 29 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import FormData, { type Stream } from 'form-data';
22
import CloudConvert from './CloudConvert';
33
import { type JobTask } from './JobsResource';
4-
import axios from 'axios';
54
import { ReadStream, statSync } from 'fs';
65

76
export type TaskEvent = 'created' | 'updated' | 'finished' | 'failed';
@@ -546,64 +545,46 @@ export default class TasksResource {
546545
this.cloudConvert = cloudConvert;
547546
}
548547

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);
557550
}
558551

559552
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+
);
570563
}
571564

572565
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`);
577567
}
578568

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);
592577
}
593578

594579
async create<O extends Operation['operation']>(
595580
operation: O,
596-
data: Extract<Operation, { operation: O }>['data'] | null = null
581+
data?: Extract<Operation, { operation: O }>['data']
597582
): 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);
603584
}
604585

605586
async delete(id: string): Promise<void> {
606-
await this.cloudConvert.axios.delete(`tasks/${id}`);
587+
await this.cloudConvert.call('DELETE', `tasks/${id}`);
607588
}
608589

609590
async upload(
@@ -638,16 +619,11 @@ export default class TasksResource {
638619
}
639620
formData.append('file', stream, fileOptions);
640621

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+
);
651627
}
652628

653629
async subscribeEvent(

lib/UsersResource.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ export default class UsersResource {
1818
}
1919

2020
async me(): Promise<User> {
21-
const response = await this.cloudConvert.axios.get('users/me');
22-
return response.data.data;
21+
return await this.cloudConvert.call('GET', 'users/me');
2322
}
2423

2524
async subscribeJobEvent(

0 commit comments

Comments
 (0)