-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMono.js
104 lines (92 loc) · 2.62 KB
/
Mono.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const axios = require('axios')
class Mono {
token = null;
constructor(token) {
if (!token) throw new Error('No mono token');
this.axios_instance = axios.create({
baseURL: 'https://api.monobank.ua/',
headers: {
'X-Token': token
}
});
}
async api(adress) {
try {
return await this.axios_instance(adress)
} catch(err) {
if (err.response && err.response.status === 429) {
console.log('too many reqests will try again in 90s')
return setTimeout(() => this.api(adress), 90000)
}
if (err.response) {
console.log(err.response.status, err.response.data)
}
}
}
async post(adress, body) {
try {
return await this.axios_instance.post(adress, body)
} catch(err) {
if (err.response && err.response.status === 429) {
console.log('too many reqests will try again in 90s')
return setTimeout(() => this.post(adress, body), 90000)
}
if (err.response) {
console.log(err.response.status, err.response.data)
}
}
}
/**
* Validates monobank token
*
* @returns { boolean } Result of validation
*/
async validate() {
let response;
try {
response = await this.api('/personal/client-info')
} catch(err) {
return false;
}
if (response.data.clientId) {
return true
} else {
return false;
}
}
/**
* Get list of accounts
*
* @returns { Array } List of account objects
*/
async getAccounts() {
const response = await this.api('/personal/client-info')
if (!response.data.accounts) return null;
return response.data.accounts;
}
/**
* Get statement
*
* @returns { Array } Statement
* @param { string } account Account id
* @param { number } from Time from
* @param { number } to Time to
*/
async getStatement(account, from, to) {
const response = await this.api(`/personal/statement/${account}/${from}${to ? `/${to}` : ''}`)
return response.data
}
/**
* Set Webhook adress
*
* @param { string } url
*/
async setWebhook(url) {
console.log('mono webhook set as ' + url)
const response = await this.post(`/personal/webhook`, {
"webHookUrl": url
})
return response.data
}
}
module.exports = Mono