forked from ifvictr/nitrotype.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
69 lines (60 loc) · 2.02 KB
/
client.js
File metadata and controls
69 lines (60 loc) · 2.02 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const axios = require('axios')
const cookie = require('cookie')
const qs = require('qs')
const serializeCookies = obj => Object.entries(obj)
.reduce((str, [key, value]) => `${str} ${cookie.serialize(key, value)};`, '')
const BASE_URL = 'https://www.nitrotype.com/api/'
class Client {
constructor(opts) {
if (typeof opts !== 'object') {
throw new Error('`opts` needs to be an object')
}
if (!opts.username) {
throw new Error('`username` property is missing')
}
if (!opts.password) {
throw new Error('`password` property is missing')
}
this.opts = opts
this._cookies = {}
}
_call(method, path, options = {}) {
const uhash = this._cookies.ntuserrem
const params = typeof options.params === 'object' ? options.params : {}
const data = typeof options.data === 'object' ? options.data : {}
return axios({
method,
baseURL: BASE_URL,
url: path,
headers: { Cookie: serializeCookies(this._cookies) },
params: { uhash, ...params },
data: method === "POST" && qs.stringify({
uhash,
...data
})
})
}
login() {
// One-off internal call to save the user's session cookies
return this._call('POST', 'login', {
data: {
username: this.opts.username,
password: this.opts.password
}
})
.then(({ headers }) => {
headers['set-cookie'].forEach(str => {
const cookieObj = cookie.parse(str)
const key = Object.keys(cookieObj)[0]
this._cookies[key] = cookieObj[key]
})
})
}
get(path, options) {
return this._call('GET', path, options).then(res => res.data)
}
post(path, options) {
return this._call('POST', path, options).then(res => res.data)
}
}
module.exports = Client