-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathclient.js
35 lines (26 loc) · 851 Bytes
/
client.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
const fetch = require('node-fetch')
// const moment = require('moment')
module.exports = serviceName => async (state, apiMethod = 'unknown') => {
// const startTime = moment.now()
const response = await fetch(state.req.url, state.req)
state.res = {
headers: response.headers.raw(),
status: response.status,
}
// const totalTime = moment.now() - startTime
// const tags = {
// api_method: apiMethod,
// method: state.req.method || 'GET',
// response_code: response.status,
// service: serviceName,
// }
state.res.body = await response.text()
const isJSON = (response.headers.get('content-type') || '').includes('application/json')
if (isJSON && state.res.body) {
state.res.body = JSON.parse(state.res.body)
}
if (!response.ok) {
throw new Error(response.statusText)
}
return state
}