· 5 years ago · Jan 18, 2021, 12:12 PM
1import EventEmitter from "https://deno.land/x/events/mod.ts";
2import HttpManger from "../managers/httpmanager.ts";
3import { ClientOptions } from "../typedefs/ClientOptions.ts";
4
5/**
6 * The Twitter Client, Root point of making request to the API
7 * @param {ClientOptions} [ClientOptions={}] Options for the Client.
8 * @extends {EventEmitter}
9 * @constructor Client
10 */
11class Client extends EventEmitter {
12 http: HttpManger;
13 private Authorization!: string;
14 APIkey: string;
15 APIsecret: string;
16 bearer: string;
17 ping: number;
18 options: ClientOptions;
19 constructor(clientOptions?: ClientOptions) {
20 super();
21 this.options = clientOptions ?? { ws: { version: "1.1" } };
22 if (!clientOptions?.ws?.version)
23 this.options.ws = { ...this.options.ws, version: "1.1" };
24 this.http = new HttpManger(this);
25 this.ping = null!;
26 this.APIkey = null!;
27 this.bearer = null!;
28 this.APIsecret = null!;
29 }
30
31 /**
32 * Logs in to Twitter.
33 * @param {Login} [Authorization] Info to Authorize the Twitter Client.
34 * @param {string} [Authorization.APIkey] The API Key.
35 * @param {string} [Authorization.APIsecret] The API Secret
36 * @param {string} [Authorization.Bearer] The Bearer.
37 * @returns {Boolean} If the login is succesful or not.
38 * @method login
39 */
40 async login({ APIkey, APIsecret, Bearer }: Login): Promise<boolean> {
41 this.APIkey = APIkey;
42 this.APIsecret = APIsecret;
43 this.bearer = Bearer;
44 this.Authorization = `Bearer ${Bearer}`;
45 //this.setProps({ APIkey, APIsecret, Bearer });
46 this.ping = await this.http._getPing();
47 setInterval(async () => (this.ping = await this.http._getPing()), 10000);
48 this.emit("ready");
49 return true;
50 }
51}
52
53/**
54 * @typedef Authorization
55 * @property {string} APIkey The API Key of the bot.
56 * @property {string} APIsecret The Secret Key of the bot.
57 * @property {string} Bearer The Bearer of the bot.
58 */
59interface Login {
60 APIkey: string;
61 APIsecret: string;
62 Bearer: string;
63}
64
65export default Client;
66