github-readme-stats/api/top-langs.js
Amir ba7c2f8b55
Support hide_progress for top-langs feature (#2514)
* Add support for hide_progress in top languages feature

* Fix mistake

* Add documents for all languages

* Remove unnecessary value check

* Update top-languages-card.js

* Revert document for all languages except English

* Update documentation

* Update documentation

---------

Co-authored-by: Zohan Subhash <zohan.subhash@gmail.com>
2023-02-16 06:53:11 +05:30

89 lines
2.2 KiB
JavaScript

import { renderTopLanguages } from "../src/cards/top-languages-card.js";
import { blacklist } from "../src/common/blacklist.js";
import {
clampValue,
CONSTANTS,
parseArray,
parseBoolean,
renderError,
} from "../src/common/utils.js";
import { fetchTopLanguages } from "../src/fetchers/top-languages-fetcher.js";
import { isLocaleAvailable } from "../src/translations.js";
export default async (req, res) => {
const {
username,
hide,
hide_title,
hide_border,
card_width,
title_color,
text_color,
bg_color,
theme,
cache_seconds,
layout,
langs_count,
exclude_repo,
custom_title,
locale,
border_radius,
border_color,
disable_animations,
hide_progress,
} = req.query;
res.setHeader("Content-Type", "image/svg+xml");
if (blacklist.includes(username)) {
return res.send(renderError("Something went wrong"));
}
if (locale && !isLocaleAvailable(locale)) {
return res.send(renderError("Something went wrong", "Locale not found"));
}
try {
const topLangs = await fetchTopLanguages(
username,
parseArray(exclude_repo),
);
const cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10),
CONSTANTS.FOUR_HOURS,
CONSTANTS.ONE_DAY,
);
res.setHeader(
"Cache-Control",
`max-age=${
cacheSeconds / 2
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);
return res.send(
renderTopLanguages(topLangs, {
custom_title,
hide_title: parseBoolean(hide_title),
hide_border: parseBoolean(hide_border),
card_width: parseInt(card_width, 10),
hide: parseArray(hide),
title_color,
text_color,
bg_color,
theme,
layout,
langs_count,
border_radius,
border_color,
locale: locale ? locale.toLowerCase() : null,
disable_animations: parseBoolean(disable_animations),
hide_progress: parseBoolean(hide_progress),
}),
);
} catch (err) {
res.setHeader("Cache-Control", `no-cache, no-store, must-revalidate`); // Don't cache error responses.
return res.send(renderError(err.message, err.secondaryMessage));
}
};