Skip to content

Commit df6615f

Browse files
author
Miqueas Martinez
committed
Added initial TypeScript code
1 parent 51e1eef commit df6615f

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

Github.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import yargs from "https://deno.land/x/[email protected]/deno.ts";
2+
3+
const print = console.log;
4+
const BASE_URL = "https://api.github.com/users/"
5+
6+
class GthUserItem {
7+
count: number;
8+
arr: string[];
9+
10+
constructor(n: number) {
11+
this.count = n;
12+
this.arr = [];
13+
}
14+
}
15+
16+
class GthUser {
17+
username: string;
18+
name!: string;
19+
bio!: string;
20+
link!: string;
21+
repos!: GthUserItem;
22+
gists!: GthUserItem;
23+
followers!: GthUserItem;
24+
following!: GthUserItem;
25+
26+
constructor(username: string) {
27+
this.username = username;
28+
}
29+
30+
static async init(user: string): Promise<GthUser> {
31+
let self = new GthUser(user);
32+
let url = BASE_URL + user;
33+
34+
let res = await fetch(url);
35+
36+
if (!res.ok) {
37+
throw new Error(`can't fetch url '${url}'`);
38+
}
39+
40+
let obj = await res.json();
41+
42+
self.name = obj.name;
43+
self.bio = obj.bio;
44+
self.link = obj.html_url;
45+
46+
self.repos = new GthUserItem(obj.public_repos);
47+
self.gists = new GthUserItem(obj.public_gists);
48+
self.followers = new GthUserItem(obj.followers);
49+
self.following = new GthUserItem(obj.following);
50+
51+
return self;
52+
}
53+
54+
async fetch(thing: string) {
55+
let url = `${BASE_URL + this.username}/${thing}`
56+
let res = await fetch(url);
57+
58+
if (!res.ok) {
59+
throw new Error(`can't fetch url '${url}'`);
60+
}
61+
62+
let arr = await res.json();
63+
64+
switch (thing) {
65+
case "repos":
66+
for (const val of arr)
67+
this.repos.arr.push(val.name);
68+
break;
69+
70+
case "gists":
71+
for (const val of arr)
72+
this.gists.arr.push(val.description);
73+
break;
74+
75+
case "followers":
76+
for (const val of arr)
77+
this.followers.arr.push(val.login);
78+
break;
79+
80+
case "following":
81+
for (const val of arr)
82+
this.following.arr.push(val.login);
83+
break;
84+
85+
default:
86+
throw new Error(`unsupported endpoint: ${thing}`);
87+
}
88+
}
89+
}
90+
91+
let opts = yargs(Deno.args)
92+
.version(false)
93+
.usage("$0 Github.ts [options...] <usernames...>")
94+
.option("repos", {
95+
alias: "r", type: "boolean", describe: "Show user repos"
96+
})
97+
.option("gists", {
98+
alias: "g", type: "boolean", describe: "Show user gists"
99+
})
100+
.option("followers", {
101+
alias: "f", type: "boolean", describe: "Show user followers"
102+
})
103+
.option("following", {
104+
alias: "F", type: "boolean", describe: "Show user following"
105+
})
106+
.help("help", "Show this help")
107+
.parse();
108+
109+
if (opts._.length <= 0) {
110+
print("Nothing to do.");
111+
Deno.exit(1);
112+
}
113+
114+
for (const val of opts._) {
115+
let user = await GthUser.init(val);
116+
117+
print(`Name: ${user.name}`);
118+
print(`Bio: ${user.bio}`);
119+
print(`Link: ${user.link}`);
120+
121+
print();
122+
}

0 commit comments

Comments
 (0)