-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathinitClient.ts
24 lines (18 loc) · 890 Bytes
/
initClient.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
import 'dotenv-defaults/config';
import {APIClient} from '../APIClient';
export async function initClient(useLive: boolean = false): Promise<APIClient> {
const logout = async (): Promise<void> => {
console.info('Logging out...');
return client.rest.login.logout();
};
process.once('SIGINT', logout);
process.once('SIGTERM', logout);
const apiKey = useLive ? process.env.IG_LIVE_API_KEY : process.env.IG_DEMO_API_KEY;
const username = useLive ? process.env.IG_LIVE_USERNAME : process.env.IG_DEMO_USERNAME;
const password = useLive ? process.env.IG_LIVE_PASSWORD : process.env.IG_DEMO_PASSWORD;
const baseUrl = useLive ? APIClient.URL_LIVE : APIClient.URL_DEMO;
const client = new APIClient(baseUrl, apiKey!);
const session = await client.rest.login.login(username!, password!);
console.info(`Your client ID is "${session.clientId}".`);
return client;
}