github-readme-stats/api/index.js

76 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-07-12 01:50:42 +08:00
require("dotenv").config();
const {
renderError,
parseBoolean,
parseArray,
clampValue,
CONSTANTS,
} = require("../src/common/utils");
const fetchStats = require("../src/fetchers/stats-fetcher");
const renderStatsCard = require("../src/cards/stats-card");
2020-08-17 16:48:17 +08:00
const blacklist = require("../src/common/blacklist");
2020-07-09 18:33:41 +08:00
module.exports = async (req, res) => {
const {
username,
hide,
2020-07-18 01:31:16 +08:00
hide_title,
hide_border,
2020-07-13 22:11:47 +08:00
hide_rank,
show_icons,
count_private,
include_all_commits,
line_height,
title_color,
icon_color,
text_color,
bg_color,
theme,
cache_seconds,
custom_title,
} = req.query;
2020-07-10 20:42:31 +08:00
let stats;
2020-07-09 18:33:41 +08:00
res.setHeader("Content-Type", "image/svg+xml");
2020-08-17 16:48:17 +08:00
if (blacklist.includes(username)) {
return res.send(renderError("Something went wrong"));
}
2020-07-10 20:42:31 +08:00
try {
stats = await fetchStats(
username,
parseBoolean(count_private),
parseBoolean(include_all_commits),
);
2020-07-10 20:42:31 +08:00
2020-08-13 23:48:26 +08:00
const cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10),
CONSTANTS.TWO_HOURS,
CONSTANTS.ONE_DAY,
2020-08-13 23:48:26 +08:00
);
2020-08-13 23:48:26 +08:00
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
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),
hide_rank: parseBoolean(hide_rank),
include_all_commits: parseBoolean(include_all_commits),
line_height,
title_color,
icon_color,
text_color,
bg_color,
theme,
custom_title,
}),
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
};