-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
37 lines (33 loc) · 979 Bytes
/
api.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
const unirest = require('unirest');
const assert = require('assert')
const join = require("url-join")
module.exports = (config = {}) => {
assert(config.baseURL, 'requires config.baseURL')
return {
post: (endpoint, params = {}) => {
assert(endpoint, 'endpoint required')
return new Promise((resolve, reject) => {
const url = join(config.baseURL, endpoint)
unirest.post(url)
.type('json')
.send(params)
.end(({ error, body }) => {
if (error) reject(error)
else resolve(body)
});
})
},
get: (endpoint, params = {}) => {
return new Promise((resolve, reject) => {
assert(endpoint, 'endpoint required')
const url = join(config.baseURL, endpoint)
unirest.get(url)
.query(params)
.end(({ error, body }) => {
if (error) reject(error)
else resolve(body)
});
})
},
}
}