Skip to content

Commit 71c668b

Browse files
author
Miqueas Martinez
committed
Minor changes in the TS example
1 parent df94787 commit 71c668b

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

Github.ts

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import yargs from "https://deno.land/x/[email protected]/deno.ts";
22

33
const print = console.log;
4-
const BASE_URL = "https://api.github.com/users/"
4+
const BASE_URL = "https://api.github.com/users/";
55

66
class GthUserItem {
77
count: number;
@@ -15,27 +15,25 @@ class GthUserItem {
1515

1616
class GthUser {
1717
username: string;
18-
name!: string;
19-
bio!: string;
20-
link!: string;
21-
repos!: GthUserItem;
22-
gists!: GthUserItem;
23-
followers!: GthUserItem;
24-
following!: GthUserItem;
18+
name?: string;
19+
bio?: string;
20+
link?: string;
21+
repos?: GthUserItem;
22+
gists?: GthUserItem;
23+
followers?: GthUserItem;
24+
following?: GthUserItem;
2525

2626
constructor(username: string) {
2727
this.username = username;
2828
}
2929

30-
static async init(user: string): Promise<GthUser> {
30+
static async create(user: string): Promise<GthUser> {
3131
let self = new GthUser(user);
32-
let url = BASE_URL + user;
33-
32+
let url = BASE_URL + self.username;
3433
let res = await fetch(url);
3534

36-
if (!res.ok) {
35+
if (!res.ok)
3736
throw new Error(`can't fetch url '${url}'`);
38-
}
3937

4038
let obj = await res.json();
4139

@@ -55,31 +53,30 @@ class GthUser {
5553
let url = `${BASE_URL + this.username}/${thing}`
5654
let res = await fetch(url);
5755

58-
if (!res.ok) {
56+
if (!res.ok)
5957
throw new Error(`can't fetch url '${url}'`);
60-
}
6158

6259
let arr = await res.json();
6360

6461
switch (thing) {
6562
case "repos":
6663
for (const val of arr)
67-
this.repos.arr.push(val.name);
64+
this.repos?.arr.push(val.name);
6865
break;
6966

7067
case "gists":
7168
for (const val of arr)
72-
this.gists.arr.push(val.description);
69+
this.gists?.arr.push(val.description);
7370
break;
7471

7572
case "followers":
7673
for (const val of arr)
77-
this.followers.arr.push(val.login);
74+
this.followers?.arr.push(val.login);
7875
break;
7976

8077
case "following":
8178
for (const val of arr)
82-
this.following.arr.push(val.login);
79+
this.following?.arr.push(val.login);
8380
break;
8481

8582
default:
@@ -92,18 +89,27 @@ let opts = yargs(Deno.args)
9289
.version(false)
9390
.usage("$0 Github.ts [options...] <usernames...>")
9491
.option("repos", {
95-
alias: "r", type: "boolean", describe: "Show user repos"
92+
alias: "r",
93+
type: "boolean",
94+
describe: "Show user repos"
9695
})
9796
.option("gists", {
98-
alias: "g", type: "boolean", describe: "Show user gists"
97+
alias: "g",
98+
type: "boolean",
99+
describe: "Show user gists"
99100
})
100101
.option("followers", {
101-
alias: "f", type: "boolean", describe: "Show user followers"
102+
alias: "f",
103+
type: "boolean",
104+
describe: "Show user followers"
102105
})
103106
.option("following", {
104-
alias: "F", type: "boolean", describe: "Show user following"
107+
alias: "F",
108+
type: "boolean",
109+
describe: "Show user following"
105110
})
106111
.help("help", "Show this help")
112+
.alias("h", "help")
107113
.parse();
108114

109115
if (opts._.length <= 0) {
@@ -112,42 +118,46 @@ if (opts._.length <= 0) {
112118
}
113119

114120
for (const val of opts._) {
115-
let user = await GthUser.init(val);
121+
let user = await GthUser.create(val);
116122

117123
print(`Name: ${user.name}`);
118124
print(`Bio: ${user.bio}`);
119125
print(`Link: ${user.link}`);
120126

121-
print(`Public repos: ${user.repos.count}`);
127+
print(`Public repos: ${user.repos?.count}`);
122128
if (opts.r) {
123129
await user.fetch("repos");
124130

125-
for (const idx in user.repos.arr)
126-
print(` | ${parseInt(idx) + 1} ${user.repos.arr[idx]}`);
131+
if (user.repos)
132+
for (const idx in user.repos.arr)
133+
print(` | ${parseInt(idx) + 1} ${user.repos.arr[parseInt(idx)]}`);
127134
}
128135

129-
print(`Public gists: ${user.gists.count}`);
136+
print(`Public gists: ${user.gists?.count}`);
130137
if (opts.g) {
131138
await user.fetch("gists");
132139

133-
for (const idx in user.gists.arr)
134-
print(` | ${parseInt(idx) + 1} ${user.gists.arr[idx]}`);
140+
if (user.gists)
141+
for (const idx in user.gists.arr)
142+
print(` | ${parseInt(idx) + 1} ${user.gists.arr[parseInt(idx)]}`);
135143
}
136144

137-
print(`Public followers: ${user.followers.count}`);
145+
print(`Public followers: ${user.followers?.count}`);
138146
if (opts.followers) {
139147
await user.fetch("followers");
140148

141-
for (const val of user.followers.arr)
142-
print(` | @${val}`);
149+
if (user.followers)
150+
for (const val of user.followers.arr)
151+
print(` | @${val}`);
143152
}
144153

145-
print(`Public following: ${user.following.count}`);
154+
print(`Public following: ${user.following?.count}`);
146155
if (opts.following) {
147156
await user.fetch("following");
148157

149-
for (const val of user.following.arr)
150-
print(` | @${val}`);
158+
if (user.following)
159+
for (const val of user.following.arr)
160+
print(` | @${val}`);
151161
}
152162

153163
print();

0 commit comments

Comments
 (0)