-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadvanced-manual-control.js
More file actions
84 lines (74 loc) · 2.4 KB
/
Copy pathadvanced-manual-control.js
File metadata and controls
84 lines (74 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Advanced example with manual control
*
* This example shows how to manually upload files, create tasks,
* and download results for full control over the conversion process.
*/
const { ConversionToolsClient } = require('conversiontools');
const apiToken = process.env.CONVERSION_TOOLS_API_TOKEN || 'your-api-token-here';
async function main() {
const client = new ConversionToolsClient({
apiToken,
});
try {
// Step 1: Upload file manually
console.log('1. Uploading file...');
const fileId = await client.files.upload('./data.xml', {
onProgress: (progress) => {
console.log(` Upload progress: ${progress.percent}%`);
},
});
console.log(` ✓ File uploaded: ${fileId}`);
// Step 2: Get file info
console.log('\n2. Getting file info...');
const fileInfo = await client.files.getInfo(fileId);
console.log(` Name: ${fileInfo.name}`);
console.log(` Size: ${fileInfo.size} bytes`);
// Step 3: Create conversion task
console.log('\n3. Creating conversion task...');
const task = await client.createTask({
type: 'convert.xml_to_excel',
options: {
file_id: fileId,
excel_format: 'xlsx',
},
});
console.log(` ✓ Task created: ${task.id}`);
// Step 4: Wait for completion with status updates
console.log('\n4. Waiting for conversion to complete...');
await task.wait({
onProgress: (status) => {
console.log(
` Status: ${status.status}, Progress: ${status.conversionProgress}%`
);
},
});
console.log(' ✓ Conversion complete!');
// Step 5: Download result
console.log('\n5. Downloading result...');
const outputPath = await task.downloadTo('./result.xlsx');
console.log(` ✓ Downloaded to: ${outputPath}`);
// Step 6: Get rate limits
const limits = client.getRateLimits();
if (limits) {
console.log('\n6. Rate limits:');
if (limits.daily) {
console.log(
` Daily: ${limits.daily.remaining}/${limits.daily.limit} remaining`
);
}
if (limits.monthly) {
console.log(
` Monthly: ${limits.monthly.remaining}/${limits.monthly.limit} remaining`
);
}
}
} catch (error) {
console.error('✗ Error:', error.message);
if (error.status) {
console.error(` Status: ${error.status}`);
}
process.exit(1);
}
}
main();