forked from zenmoney/ZenPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbank.js
More file actions
32 lines (26 loc) · 1.21 KB
/
Copy pathbank.js
File metadata and controls
32 lines (26 loc) · 1.21 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
import { fetchJson } from '../../common/network'
import { InvalidLoginOrPasswordError } from '../../errors'
const baseUrl = 'https://raw.githubusercontent.com/zenmoney/ZenPlugins/master/src/plugins/example/public/'
async function callGate (url, options, predicate = () => true) {
const response = await fetchJson(baseUrl + url, options || {})
if (predicate) {
validateResponse(response, response => response.body && !response.body.error && predicate(response))
}
return response
}
function validateResponse (response, predicate) {
console.assert(!predicate || predicate(response), 'non-successful response')
}
export async function login (login, password) {
// It happens on server side
if (login !== 'example' || password !== 'example') {
throw new InvalidLoginOrPasswordError()
}
return (await callGate('auth.json', null, response => response.body.access_token)).body.access_token
}
export async function fetchAccounts (token) {
return (await callGate('accounts.json', null, response => Array.isArray(response.body))).body
}
export async function fetchTransactions (token, fromDate, toDate = null) {
return (await callGate('transactions.json', null, response => Array.isArray(response.body))).body
}