-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter.ts
51 lines (50 loc) · 1.48 KB
/
twitter.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
48
49
50
51
import fetch from "node-fetch";
import { exit } from "../utils.js";
interface TwitterOptions {
token: string;
}
/**
* @class
* @classdesc Twitter Class, which handles all relevant statistical endpoints from the Twitter API
* @param {String} token Twittter Developer Token
*/
export class Twitter {
token : string;
constructor(options: TwitterOptions) {
this.token = options.token;
}
/**
* It makes a request to the url, and if the response is ok, it returns the body data, otherwise it
* throws an error.
* @private
* @async
* @param {String} url - The url you want to send the request to.
* @returns The data from the API.
*/
async req(url: string) {
const res = await fetch(url, {
method:"GET",
headers: {
"Authorization":`Bearer ${this.token}`,
},
});
const body = await res.json();
if(res.ok) {
return body.data;
}
else {
exit(`[Statisfy] ${body.status} ERROR: ${body.error} - ${body.message}`, "red");
}
}
/**
* It takes a Twitter username as a parameter, and returns a JSON object containing the user's
* information.
* @async
* @param {String} user - The username of the user you want to look up.
* @returns The user's information.
*/
async UserLookupByName(user: string) {
const info = await this.req(`https://api.twitter.com/2/users/by/username/${user}?user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld`);
return info;
}
}