/
connect.ts
47 lines (44 loc) · 1.31 KB
/
connect.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 { Incident } from "incident";
import * as api from "./api";
import { Credentials } from "./interfaces/api/api";
import { Context } from "./interfaces/api/context";
import { login } from "./login";
import { requestIo } from "./request-io";
export interface StateContainer {
state: any;
}
export interface ConnectOptions {
credentials?: Credentials;
state?: Context.Json;
verbose?: boolean;
}
/**
* Authenticate the user and create a new API.
*
* @param options
* @returns The Skype API for the provided user
* @throws [[LoginError]]
*/
export async function connect(options: ConnectOptions): Promise<api.Api> {
let apiContext: Context;
if (options.state !== undefined) {
apiContext = Context.fromJson(options.state);
} else if (options.credentials !== undefined) {
apiContext = await login({
io: requestIo,
credentials: options.credentials,
verbose: options.verbose,
});
if (options.verbose) {
console.log("Obtained context trough authentication:");
console.log({
username: apiContext.username,
skypeToken: apiContext.skypeToken,
registrationToken: apiContext.registrationToken,
});
}
} else {
return Promise.reject(new Incident("todo", "Connect must define `credentials`"));
}
return new api.Api(apiContext, requestIo);
}