-
Notifications
You must be signed in to change notification settings - Fork 2
/
ua.js
40 lines (36 loc) · 1.23 KB
/
ua.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 stream = require('stream')
const url = require('url')
const logger = require('prolific.logger').create('compassion.colleague')
const axios = require('axios')
const httpAdapter = require('axios/lib/adapters/http')
module.exports = {
json: async (location, path, body) => {
const resolved = url.resolve(location, path)
try {
if (body == null) {
return (await axios.get(resolved)).data
} else {
return (await axios.post(resolved, body)).data
}
} catch (error) {
logger.error('ua', { url: resolved, stack: error.stack })
return null
}
},
// See: // https://github.com/andrewstart/axios-streaming/blob/master/axios.js
stream: async (location, path, body) => {
const resolved = url.resolve(location, path)
try {
return (await axios.post(resolved, body, {
responseType: 'stream',
adapter: httpAdapter
})).data
} catch (error) {
console.log(error.stack)
logger.error('ua', { url: resolved, stack: error.stack })
}
const empty = new stream.PassThrough
empty.end()
return empty
}
}