diff --git a/api/index.js b/api/index.js
index e04b1ad..fae138b 100644
--- a/api/index.js
+++ b/api/index.js
@@ -1,7 +1,10 @@
const axios = require("axios");
+const { renderError, kFormatter } = require("../utils");
require("dotenv").config();
async function fetchStats(username) {
+ if (!username) throw Error("Invalid username");
+
const res = await axios({
url: "https://api.github.com/graphql",
method: "post",
@@ -49,7 +52,11 @@ async function fetchStats(username) {
totalStars: 0,
contributedTo: 0,
};
- if (res.data.error) return stats;
+
+ if (res.data.errors) {
+ console.log(res.data.errors);
+ throw Error("Could not fetch user");
+ }
const user = res.data.data.user;
@@ -67,12 +74,11 @@ async function fetchStats(username) {
}
const createTextNode = (icon, label, value, lheight) => {
+ const classname = icon === "★" && "star-icon";
return `
- ${icon} ${label}:
- ${value}
+ ${icon} ${label}:
+ ${kFormatter(value)}
`;
};
@@ -134,10 +140,15 @@ module.exports = async (req, res) => {
const hide_border = req.query.hide_border;
const show_icons = req.query.show_icons;
const line_height = req.query.line_height;
-
- const stats = await fetchStats(username);
+ let stats;
res.setHeader("Content-Type", "image/svg+xml");
+ try {
+ stats = await fetchStats(username);
+ } catch (err) {
+ return res.send(renderError(err.message));
+ }
+
res.send(
renderSVG(stats, {
hide: JSON.parse(hide || "[]"),
diff --git a/api/pin.js b/api/pin.js
index 6cfade4..424aece 100644
--- a/api/pin.js
+++ b/api/pin.js
@@ -1,12 +1,7 @@
const axios = require("axios");
+const { renderError, kFormatter } = require("../utils");
require("dotenv").config();
-function kFormatter(num) {
- return Math.abs(num) > 999
- ? Math.sign(num) * (Math.abs(num) / 1000).toFixed(1) + "k"
- : Math.sign(num) * Math.abs(num);
-}
-
async function fetchRepo(username, reponame) {
const res = await axios({
url: "https://api.github.com/graphql",
@@ -56,14 +51,26 @@ async function fetchRepo(username, reponame) {
},
});
- if (res.data.error && res.data.error.type !== "NOT_FOUND") return {};
- if (!res.data.data.organization && res.data.data.user)
- return res.data.data.user.repository;
+ const data = res.data.data;
- const isOrg = res.data.data.organization && !res.data.data.user;
- if (isOrg) return res.data.data.organization.repository;
+ console.log(res.data);
+ if (!data.user && !data.organization) {
+ throw new Error("Not found");
+ }
- return res.data.data.user.repository;
+ if (data.organization === null && data.user) {
+ if (!data.user.repository) {
+ throw new Error("User Repository Not found");
+ }
+ return data.user.repository;
+ }
+
+ if (data.user === null && data.organization) {
+ if (!data.organization.repository) {
+ throw new Error("Organization Repository Not found");
+ }
+ return data.organization.repository;
+ }
}
const renderRepoCard = (repo) => {
@@ -72,7 +79,7 @@ const renderRepoCard = (repo) => {
const shiftText = primaryLanguage.name.length > 15 ? 0 : 30;
return `
-