github-readme-stats/api/index.js

46 lines
1.0 KiB
JavaScript
Raw Normal View History

2020-07-12 01:50:42 +08:00
require("dotenv").config();
2020-07-15 21:02:16 +08:00
const { renderError, parseBoolean } = require("../src/utils");
2020-07-12 00:40:13 +08:00
const fetchStats = require("../src/fetchStats");
const renderStatsCard = require("../src/renderStatsCard");
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,
line_height,
title_color,
icon_color,
text_color,
bg_color,
} = req.query;
2020-07-10 20:42:31 +08:00
let stats;
2020-07-09 18:33:41 +08:00
res.setHeader("Cache-Control", "public, max-age=1800");
2020-07-09 18:33:41 +08:00
res.setHeader("Content-Type", "image/svg+xml");
2020-07-10 20:42:31 +08:00
try {
stats = await fetchStats(username);
} catch (err) {
return res.send(renderError(err.message));
}
res.send(
2020-07-12 00:40:13 +08:00
renderStatsCard(stats, {
hide: JSON.parse(hide || "[]"),
2020-07-15 21:02:16 +08:00
show_icons: parseBoolean(show_icons),
2020-07-18 01:31:16 +08:00
hide_title: parseBoolean(hide_title),
2020-07-15 21:02:16 +08:00
hide_border: parseBoolean(hide_border),
hide_rank: parseBoolean(hide_rank),
line_height,
title_color,
icon_color,
text_color,
bg_color,
})
);
2020-07-09 18:33:41 +08:00
};