-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetApi.js
45 lines (43 loc) · 1.14 KB
/
getApi.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
const axios = require('axios')
function getApi(url, options, config, except) {
if (url == undefined) {
throw `Err!! You need to gimme a valid url genius!`
}
if (!except) {
if (options == undefined) {
throw `Err!! Gimme an access token genius!`
}
else if (options.access_token == undefined) {
throw `Err!! Gimme an access token genius!`
}
}
const defaultOptions = {
token_type: "Bearer"
}
const final = {
...defaultOptions,
...options
}
return new Promise((resolve, reject) => {
axios.get(url, {
...{
headers: {
Authorization: `${final.token_type} ${final.access_token}`
}
},
...config
})
.then(r => {
resolve(r.data)
})
.catch(r => {
if(r.response == undefined) {
reject(r)
}
else {
reject(r.response.data)
}
})
})
}
module.exports = getApi