forked from zenmoney/ZenPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
44 lines (36 loc) · 1.77 KB
/
api.ts
File metadata and controls
44 lines (36 loc) · 1.77 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
import { fetchJson, FetchOptions, FetchResponse } from '../../common/network'
import { InvalidLoginOrPasswordError } from '../../errors'
import get from '../../types/get'
import { Product } from './converters'
const baseUrl = 'https://raw.githubusercontent.com/zenmoney/ZenPlugins/master/src/plugins/example/public/'
export type Auth = string
export interface Preferences {
login: string
password: string
}
async function fetchApi (url: string, options?: FetchOptions, predicate?: (x: FetchResponse) => boolean): Promise<FetchResponse> {
const response = await fetchJson(baseUrl + url, options ?? {})
if (predicate) {
validateResponse(response, response => !get(response.body, 'error') && predicate(response))
}
return response
}
function validateResponse (response: FetchResponse, predicate?: (x: FetchResponse) => boolean): void {
console.assert(!predicate || predicate(response), 'non-successful response')
}
export async function login ({ login, password }: Preferences, auth?: Auth): Promise<Auth> {
if (auth != null) {
return auth
}
// It happens on server side
if (login !== 'example' || password !== 'example') {
throw new InvalidLoginOrPasswordError()
}
return get((await fetchApi('auth.json', undefined, response => !!get(response.body, 'access_token'))).body, 'access_token') as Auth
}
export async function fetchAccounts (auth: Auth): Promise<unknown[]> {
return (await fetchApi('accounts.json', undefined, response => Array.isArray(response.body))).body as unknown[]
}
export async function fetchTransactions (auth: Auth, { id, transactionNode }: Product, fromDate: Date, toDate?: Date): Promise<unknown[]> {
return (await fetchApi(`transactions_${transactionNode}${id}.json`, undefined, response => Array.isArray(response.body))).body as unknown[]
}