forked from jsreport/jsreport-azure-functions-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
40 lines (35 loc) · 1.13 KB
/
test.js
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
const axios = require('axios')
const fs = require('fs')
const concat = require('concat-stream')
const AZURE_FUNCTION_TRIGGER_URL = 'https://jsrtest.azurewebsites.net/api/httptrigger1'
async function test() {
console.time('test')
try {
const res = await axios({
url: AZURE_FUNCTION_TRIGGER_URL,
method: 'post',
data: {
template: {
name: 'orders-main',
}
},
responseType: 'stream'
})
res.data.pipe(fs.createWriteStream('out.pdf'))
console.timeEnd('test')
} catch (e) {
console.error(e.message)
if (e.response) {
const responseBuffer = await streamToBuffer(e.response.data)
console.error(responseBuffer.toString())
}
}
}
function streamToBuffer (response) {
return new Promise((resolve, reject) => {
const writeStream = concat((data) => resolve(data))
response.on('error', reject)
response.pipe(writeStream)
})
}
test().catch(console.error)