-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.js
51 lines (41 loc) · 1.13 KB
/
utils.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
48
49
50
51
const request = require('@root/request')
module.exports = {
//standard request function
async req(path, params={}) {
let convs = ['snake_case', 'extended']
for (const c of convs) {
if(typeof params[c] !== 'undefined')
params[c] = params[c] ? 1 : 0
}
params = Object.assign({}, this.globalParams, params)
if(this.verbose)
console.log(`Requesting ${path} with params: `, params)
return (await request({
url: `${this.endpoint + path}${Object.keys(params).length > 0 ? `?${new URLSearchParams(params).toString()}` : ''}`,
json: true
})).body
},
//JSON request function
async reqJSON(path, body) {
if(this.verbose)
console.log(`Requesting ${path} with body: `, body)
return (await request({
method: 'POST',
url: this.endpoint + path,
body: body,
json: true
})).body
},
//handle both comma-separated strings, and string arrays, for CSV params
makeCSV(x) {
if(typeof(x) === 'undefined')
return
if(Array.isArray(x))
return x.join(',')
if(typeof(x) === 'string')
return x
},
throwError(method, param) {
return Error(`xivapi-js: Can't use ${method} without providing ${param}.`)
}
}