-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.js
47 lines (37 loc) · 1.2 KB
/
request.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
41
42
43
44
45
46
47
const fetch = require('node-fetch')
const makeRequest = (method, payload, apiKey) => {
const encodedAuthorizationHeader = new Buffer(`hash_key:${apiKey}`).toString('base64')
const authorizationHeader = `Basic ${encodedAuthorizationHeader}`
let body = undefined
if (method !== 'GET' && payload) {
body = JSON.stringify(payload)
}
return {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8',
'Authorization': authorizationHeader,
},
body,
method,
}
}
const get = (url, payload, apiKey) => fetch(url, makeRequest('GET', payload, apiKey))
.then(response => response.json())
.then(response => {
if (response.errors) { throw response }
return response
})
const post = (url, payload, apiKey) => fetch(url, makeRequest('POST', payload, apiKey))
.then(response => response.json())
.then(response => {
if (response.errors) { throw response }
return response
})
const put = (url, payload, apiKey) => fetch(url, makeRequest('PUT', payload, apiKey))
.then(response => response.json())
.then(response => {
if (response.errors) { throw response }
return response
})
module.exports = { get, post, put }