2022-09-24 19:20:52 +08:00
|
|
|
import * as dotenv from "dotenv";
|
2022-09-24 20:10:06 +08:00
|
|
|
import { renderStatsCard } from "../src/cards/stats-card.js";
|
|
|
|
import { blacklist } from "../src/common/blacklist.js";
|
2022-09-24 16:20:54 +08:00
|
|
|
import {
|
2020-07-21 00:13:51 +08:00
|
|
|
clampValue,
|
|
|
|
CONSTANTS,
|
2022-09-24 19:20:52 +08:00
|
|
|
parseArray,
|
|
|
|
parseBoolean,
|
|
|
|
renderError,
|
|
|
|
} from "../src/common/utils.js";
|
2022-09-24 20:10:06 +08:00
|
|
|
import { fetchStats } from "../src/fetchers/stats-fetcher.js";
|
2022-09-24 19:20:52 +08:00
|
|
|
import { isLocaleAvailable } from "../src/translations.js";
|
2020-07-09 20:20:15 +08:00
|
|
|
|
2022-09-24 16:20:54 +08:00
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
export default async (req, res) => {
|
2020-07-12 03:01:44 +08:00
|
|
|
const {
|
|
|
|
username,
|
|
|
|
hide,
|
2020-07-18 01:31:16 +08:00
|
|
|
hide_title,
|
2020-07-12 03:01:44 +08:00
|
|
|
hide_border,
|
2022-09-16 18:50:30 +08:00
|
|
|
card_width,
|
2020-07-13 22:11:47 +08:00
|
|
|
hide_rank,
|
2020-07-12 03:01:44 +08:00
|
|
|
show_icons,
|
2020-07-24 22:04:38 +08:00
|
|
|
count_private,
|
2020-07-31 16:07:39 +08:00
|
|
|
include_all_commits,
|
2020-07-12 03:01:44 +08:00
|
|
|
line_height,
|
|
|
|
title_color,
|
|
|
|
icon_color,
|
|
|
|
text_color,
|
2022-09-17 19:32:40 +08:00
|
|
|
text_bold,
|
2020-07-12 15:16:08 +08:00
|
|
|
bg_color,
|
2020-07-19 23:04:41 +08:00
|
|
|
theme,
|
2020-07-21 00:13:51 +08:00
|
|
|
cache_seconds,
|
2022-09-16 19:09:19 +08:00
|
|
|
exclude_repo,
|
2020-09-27 15:40:39 +08:00
|
|
|
custom_title,
|
2020-10-04 16:05:15 +08:00
|
|
|
locale,
|
2020-11-14 21:22:36 +08:00
|
|
|
disable_animations,
|
2021-03-08 00:53:32 +08:00
|
|
|
border_radius,
|
2021-04-28 02:58:44 +08:00
|
|
|
border_color,
|
2020-07-12 03:01:44 +08:00
|
|
|
} = req.query;
|
2020-07-09 18:33:41 +08:00
|
|
|
res.setHeader("Content-Type", "image/svg+xml");
|
2020-07-15 17:29:25 +08:00
|
|
|
|
2020-08-17 16:48:17 +08:00
|
|
|
if (blacklist.includes(username)) {
|
|
|
|
return res.send(renderError("Something went wrong"));
|
|
|
|
}
|
|
|
|
|
2020-10-04 16:05:15 +08:00
|
|
|
if (locale && !isLocaleAvailable(locale)) {
|
|
|
|
return res.send(renderError("Something went wrong", "Language not found"));
|
|
|
|
}
|
|
|
|
|
2020-07-10 20:42:31 +08:00
|
|
|
try {
|
2021-10-10 23:11:40 +08:00
|
|
|
const stats = await fetchStats(
|
2020-07-31 16:07:39 +08:00
|
|
|
username,
|
|
|
|
parseBoolean(count_private),
|
2020-09-25 00:08:14 +08:00
|
|
|
parseBoolean(include_all_commits),
|
2022-09-16 19:09:19 +08:00
|
|
|
parseArray(exclude_repo),
|
2020-07-31 16:07:39 +08:00
|
|
|
);
|
2020-07-10 20:42:31 +08:00
|
|
|
|
2020-08-13 23:48:26 +08:00
|
|
|
const cacheSeconds = clampValue(
|
2022-05-19 03:18:26 +08:00
|
|
|
parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10),
|
|
|
|
CONSTANTS.FOUR_HOURS,
|
2020-09-25 00:08:14 +08:00
|
|
|
CONSTANTS.ONE_DAY,
|
2020-08-13 23:48:26 +08:00
|
|
|
);
|
2020-07-21 00:13:51 +08:00
|
|
|
|
2020-08-13 23:48:26 +08:00
|
|
|
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
|
2020-07-21 00:13:51 +08:00
|
|
|
|
2020-08-13 23:48:26 +08:00
|
|
|
return res.send(
|
|
|
|
renderStatsCard(stats, {
|
|
|
|
hide: parseArray(hide),
|
|
|
|
show_icons: parseBoolean(show_icons),
|
|
|
|
hide_title: parseBoolean(hide_title),
|
|
|
|
hide_border: parseBoolean(hide_border),
|
2022-09-16 18:50:30 +08:00
|
|
|
card_width: parseInt(card_width, 10),
|
2020-08-13 23:48:26 +08:00
|
|
|
hide_rank: parseBoolean(hide_rank),
|
|
|
|
include_all_commits: parseBoolean(include_all_commits),
|
|
|
|
line_height,
|
|
|
|
title_color,
|
|
|
|
icon_color,
|
|
|
|
text_color,
|
2022-09-17 19:32:40 +08:00
|
|
|
text_bold: parseBoolean(text_bold),
|
2020-08-13 23:48:26 +08:00
|
|
|
bg_color,
|
|
|
|
theme,
|
2020-09-27 15:40:39 +08:00
|
|
|
custom_title,
|
2021-03-08 00:53:32 +08:00
|
|
|
border_radius,
|
2021-04-28 02:58:44 +08:00
|
|
|
border_color,
|
2020-10-04 16:05:15 +08:00
|
|
|
locale: locale ? locale.toLowerCase() : null,
|
2020-11-14 21:22:36 +08:00
|
|
|
disable_animations: parseBoolean(disable_animations),
|
2020-09-25 00:08:14 +08:00
|
|
|
}),
|
2020-08-13 23:48:26 +08:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
return res.send(renderError(err.message, err.secondaryMessage));
|
|
|
|
}
|
2020-07-09 18:33:41 +08:00
|
|
|
};
|