github-readme-stats/api/top-langs.js

54 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-07-21 19:33:05 +08:00
require("dotenv").config();
const {
renderError,
clampValue,
parseBoolean,
CONSTANTS,
} = require("../src/utils");
2020-07-21 19:33:05 +08:00
const fetchTopLanguages = require("../src/fetchTopLanguages");
const renderTopLanguages = require("../src/renderTopLanguages");
module.exports = async (req, res) => {
const {
username,
2020-07-21 21:07:16 +08:00
hide_langs_below,
hide_title,
2020-07-21 19:33:05 +08:00
card_width,
title_color,
text_color,
bg_color,
theme,
cache_seconds,
} = req.query;
let topLangs;
res.setHeader("Content-Type", "image/svg+xml");
try {
topLangs = await fetchTopLanguages(username);
} catch (err) {
return res.send(renderError(err.message));
}
const cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.THIRTY_MINUTES, 10),
CONSTANTS.THIRTY_MINUTES,
CONSTANTS.ONE_DAY
);
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
res.send(
renderTopLanguages(topLangs, {
theme,
hide_title: parseBoolean(hide_title),
2020-07-21 19:33:05 +08:00
card_width: parseInt(card_width, 10),
2020-07-21 21:07:16 +08:00
hide_langs_below: parseFloat(hide_langs_below, 10),
2020-07-21 19:33:05 +08:00
title_color,
text_color,
bg_color,
theme,
})
);
};