Skip to content

Commit 5f944dd

Browse files
authored
Update github-card.js
1 parent 68931d5 commit 5f944dd

1 file changed

Lines changed: 74 additions & 70 deletions

File tree

api/github-card.js

Lines changed: 74 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const app = express();
77
app.use(cors());
88

99
const { getGithubData } = require("../utils/githubData")
10-
const { generateErrorSVG, generateSVG } = require("../utils/theSystem")
10+
const { generateErrorSVG, generateSVG, generateSVG2 } = require("../utils/theSystem")
1111
const { wrapSVGText } = require("../utils/allFunction");
1212

1313
// =========================================
@@ -16,95 +16,74 @@ app.get("/", (req, res) => {
1616
res.send("🚀 Server berhasil dijalankan");
1717
});
1818

19-
app.get("/api/github-card", async (req, res) => {
20-
const { username, desc = "Orang yang pengen jadi programmer tapi enggan ngoding", type, no, facebook, twitter } = req.query;
21-
22-
if (!username) {
23-
return SpecialErrorMessage(res, Canvas, `Please enter the username`);
24-
}
25-
26-
19+
app.get("/api/svg-card/", async (req, res) => {
2720

28-
const github = await getGithubData(username);
29-
const descriptionText =
30-
desc && desc !== "No description provided"
31-
? desc
32-
: github.desc || "Pengguna ini belum menulis bio.";
33-
github.desc = descriptionText;
21+
let { type, name, desc, age, study, religion, job, number, email, hobby } = req.query;
3422

35-
36-
const typeNum = parseInt(type);
23+
const typeNum = parseInt(type, 10);
3724
if (isNaN(typeNum)) {
38-
return SpecialErrorMessage(res, Canvas, `Invalid type format, Only 1 - 6 available`);
25+
const errorSvg = generateErrorSVG(
26+
"Error, Type must be a number.\n",
27+
"The form cannot be processed before the type is filled in.",
28+
"Example: /api/svg-card?type=2&more_parameters_here\nParameters: type (1, 2, or 3)",
29+
1150,
30+
160
31+
);
3932
}
4033

41-
if (typeNum === 1) {
42-
if (!no || !facebook || !twitter) {
43-
return SpecialErrorMessage(res, Canvas, `Please enter the Call number, Facebook Account, and Twitter Account`);
44-
}
45-
46-
return typeOneFunction(res, Canvas, github.avatar_url, github.name, github.login, github.desc, twitter, facebook, no, github.totalStars, github.totalRepos, github.locate, github.follower);
34+
if (!type || !name) {
35+
const errorSvg = generateErrorSVG(
36+
"Error, Please fill in Type, and Username.\n",
37+
"The form cannot be processed before the username is filled in.",
38+
"Example: /api/svg-card?type=2&name=YourName&age=YourAge&study=YourStudy&religion=YourReligion&job=YourJob&number=YourNumber&email=YourEmail&hobby=YourHobby\nParameters: name, desc(not mandatory) age(not mandatory), study(not mandatory), religion(not mandatory), job(not mandatory), number(not mandatory), email(not mandatory), hobby(not mandatory)",
39+
1150,
40+
160
41+
);
42+
43+
res.setHeader("Content-Type", "image/svg+xml");
44+
return res.send(errorSvg);
4745
}
4846

49-
if (typeNum === 2) {
50-
return typeTwoFunction(res, Canvas, github.avatar_url, github.name, github.login, github.desc);
51-
}
52-
53-
if (typeNum === 3) {
54-
return typeThreeFunction(res, Canvas, github.avatar_url, github.name);
55-
}
56-
57-
return SpecialErrorMessage(res, Canvas, `Type 1-3 only`);
58-
});
59-
60-
app.get("/api/svg-card/", async (req, res) => {
61-
62-
let { name, desc, age, study, religion, job, number, email, hobby } = req.query;
63-
64-
if (!name || !age || !study || !religion || !job || !number || !email || !hobby) {
65-
const errorSvg = generateErrorSVG(
66-
"Error, Please fill in Username, Age, Study, Religion, Job, Number, Email, and Hobby.\n",
67-
"The form cannot be processed before the username is filled in.",
68-
"Example: /api/svg-card?name=YourName&age=YourAge&study=YourStudy&religion=YourReligion&job=YourJob&number=YourNumber&email=YourEmail&hobby=YourHobby\nParameters: name, desc(not mandatory) age, study, religion, job, number, email, hobby",
69-
1150,
70-
160
71-
);
72-
73-
res.setHeader("Content-Type", "image/svg+xml");
74-
return res.send(errorSvg);
75-
}
76-
77-
/* const github = {};
78-
github.name = "Lemon";
79-
github.bio = "About You And Me";
80-
github.totalStars = 116;
81-
github.totalRepos = 16;
82-
github.totalIssues = 18;
83-
github.location = "Indonesia";
84-
github.totalPullRequests = 17;
85-
github.followers = 47;
86-
*/
87-
88-
const github = await getGithubData(name)
47+
/* const github = {};
48+
github.name = "Lemon";
49+
github.bio = "About You And Me";
50+
github.totalStars = 116;
51+
github.totalRepos = 16;
52+
github.totalIssues = 18;
53+
github.location = "Indonesia";
54+
github.totalPullRequests = 17;
55+
github.followers = 47;
56+
*/
57+
58+
const github = await getGithubData(name).catch(async (err) => {
59+
const errorSvg = generateErrorSVG(
60+
"Error, User not found.\n",
61+
"The form cannot be processed before the username is filled in.",
62+
"-",
63+
1150,
64+
160
65+
)
66+
});
8967

9068
if (!desc) {
9169
desc = github.bio || "No description provided.";
9270
}
9371

9472
// ==========================================
95-
96-
name = github.name.toUpperCase();
97-
desc = `❝ ${desc} ❞`;
98-
desc = wrapSVGText(desc, 50, 17);
9973

10074
const avatarBuffer = await fetch(github.avatar_url)
10175
.then(r => r.arrayBuffer())
10276
.then(buf => Buffer.from(buf));
103-
10477
const avatarBase64 = avatarBuffer.toString("base64");
105-
78+
79+
name = github.name.toUpperCase();
80+
desc = `❝ ${desc} ❞`;
81+
desc = wrapSVGText(desc, 50, 17);
82+
10683
// ==========================================
10784

85+
if (typeNum == 1) {
86+
10887
const svg = await generateSVG(
10988
avatarBase64,
11089
name,
@@ -126,6 +105,30 @@ app.get("/api/svg-card/", async (req, res) => {
126105

127106
res.setHeader("Content-Type", "image/svg+xml");
128107
res.send(svg);
108+
109+
} else if (typeNum == 2) {
110+
111+
const svg2 = await generateSVG2(
112+
avatarBase64,
113+
name,
114+
desc
115+
)
116+
117+
res.setHeader("Content-Type", "image/svg+xml");
118+
res.send(svg2);
119+
} else {
120+
121+
const errorSvg = generateErrorSVG(
122+
"Error, Type must be 1 or 2.\n",
123+
"The form cannot be processed before the type is filled in correctly.",
124+
"Example: /api/svg-card?type=2&more_parameters_here\nParameters: type (1 or 2)",
125+
1150,
126+
160
127+
);
128+
129+
res.setHeader("Content-Type", "image/svg+xml");
130+
res.send(errorSvg);
131+
}
129132
});
130133

131134
module.exports = app;
@@ -162,5 +165,6 @@ module.exports = app;
162165

163166

164167

168+
165169

166170

0 commit comments

Comments
 (0)