2020-09-19 15:23:39 +08:00
|
|
|
require("dotenv").config();
|
|
|
|
const {
|
|
|
|
renderError,
|
|
|
|
parseBoolean,
|
|
|
|
clampValue,
|
|
|
|
CONSTANTS,
|
2020-10-04 16:05:15 +08:00
|
|
|
isLocaleAvailable,
|
2020-09-19 15:23:39 +08:00
|
|
|
} = require("../src/common/utils");
|
|
|
|
const { fetchLast7Days } = require("../src/fetchers/wakatime-fetcher");
|
|
|
|
const wakatimeCard = require("../src/cards/wakatime-card");
|
|
|
|
|
|
|
|
module.exports = async (req, res) => {
|
|
|
|
const {
|
|
|
|
username,
|
|
|
|
title_color,
|
|
|
|
icon_color,
|
|
|
|
hide_border,
|
|
|
|
line_height,
|
|
|
|
text_color,
|
|
|
|
bg_color,
|
|
|
|
theme,
|
|
|
|
cache_seconds,
|
|
|
|
hide_title,
|
|
|
|
hide_progress,
|
2020-09-27 15:40:39 +08:00
|
|
|
custom_title,
|
2020-10-04 16:05:15 +08:00
|
|
|
locale,
|
2020-11-05 02:21:57 +08:00
|
|
|
layout,
|
2020-09-19 15:23:39 +08:00
|
|
|
} = req.query;
|
|
|
|
|
|
|
|
res.setHeader("Content-Type", "image/svg+xml");
|
|
|
|
|
2020-10-04 16:05:15 +08:00
|
|
|
if (locale && !isLocaleAvailable(locale)) {
|
|
|
|
return res.send(renderError("Something went wrong", "Language not found"));
|
|
|
|
}
|
|
|
|
|
2020-09-19 15:23:39 +08:00
|
|
|
try {
|
|
|
|
const last7Days = await fetchLast7Days({ username });
|
|
|
|
|
|
|
|
let cacheSeconds = clampValue(
|
|
|
|
parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10),
|
|
|
|
CONSTANTS.TWO_HOURS,
|
2020-09-25 00:08:14 +08:00
|
|
|
CONSTANTS.ONE_DAY,
|
2020-09-19 15:23:39 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!cache_seconds) {
|
|
|
|
cacheSeconds = CONSTANTS.FOUR_HOURS;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
|
|
|
|
|
|
|
|
return res.send(
|
|
|
|
wakatimeCard(last7Days, {
|
2020-09-27 15:40:39 +08:00
|
|
|
custom_title,
|
2020-09-19 15:23:39 +08:00
|
|
|
hide_title: parseBoolean(hide_title),
|
|
|
|
hide_border: parseBoolean(hide_border),
|
|
|
|
line_height,
|
|
|
|
title_color,
|
|
|
|
icon_color,
|
|
|
|
text_color,
|
|
|
|
bg_color,
|
|
|
|
theme,
|
|
|
|
hide_progress,
|
2020-10-04 16:05:15 +08:00
|
|
|
locale: locale ? locale.toLowerCase() : null,
|
2020-11-05 02:21:57 +08:00
|
|
|
layout,
|
2020-09-25 00:08:14 +08:00
|
|
|
}),
|
2020-09-19 15:23:39 +08:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
return res.send(renderError(err.message, err.secondaryMessage));
|
|
|
|
}
|
|
|
|
};
|