-
Notifications
You must be signed in to change notification settings - Fork 2
/
EDCompanionAPI.demo.ts
47 lines (44 loc) · 1.29 KB
/
EDCompanionAPI.demo.ts
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
import { join } from 'path';
import { createInterface } from 'readline';
import { EDCompanionAPI } from '../src/EDCompanionAPI';
import { HTTPClient } from '../src/util/HTTPClient';
import { CookieFileStore } from './CookieFileStore';
function readLineAsync(prompt: string): Promise<string> {
return new Promise<string>(resolve => {
const int = createInterface(process.stdin, process.stderr);
int.question(prompt, response => {
int.close();
resolve(response);
});
});
}
function promiseSequence<T>(generators: (() => Promise<T>)[]): Promise<T[]> {
const results: T[] = [];
let chain = Promise.resolve();
generators.forEach(gen => {
chain = chain.then(() => gen()).then(val => {
results.push(val);
});
});
return chain.then(() => results);
}
const api = new EDCompanionAPI(
new HTTPClient(),
new CookieFileStore(join(__dirname, 'cookies.json')),
{
getLogin() {
return promiseSequence([
() => readLineAsync('E-Mail: '),
() => readLineAsync('Password: '),
]).then(([email, password]) => ({ email, password }));
},
getCode() {
return readLineAsync('Please enter code: ');
},
},
);
api
.getProfile()
.then(val => JSON.stringify(val, null, ' '))
.then(console.log)
.catch(err => console.error(err.stack));