-
Notifications
You must be signed in to change notification settings - Fork 0
feat: combined stats card with streaks and languages #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // @ts-check | ||
|
|
||
| import { renderCombinedCard } from "../src/cards/combined.js"; | ||
| import { guardAccess } from "../src/common/access.js"; | ||
| import { | ||
| CACHE_TTL, | ||
| resolveCacheSeconds, | ||
| setCacheHeaders, | ||
| setErrorCacheHeaders, | ||
| } from "../src/common/cache.js"; | ||
| import { | ||
| MissingParamError, | ||
| retrieveSecondaryMessage, | ||
| } from "../src/common/error.js"; | ||
| import { parseArray, parseBoolean } from "../src/common/ops.js"; | ||
| import { renderError } from "../src/common/render.js"; | ||
| import { fetchOverview } from "../src/fetchers/overview.js"; | ||
| import { fetchStreak } from "../src/fetchers/streak.js"; | ||
| import { fetchTopLanguages } from "../src/fetchers/top-languages.js"; | ||
|
|
||
| // @ts-ignore | ||
| export default async (req, res) => { | ||
| const { | ||
| username, | ||
| hide_border, | ||
| card_width, | ||
| title_color, | ||
| icon_color, | ||
| text_color, | ||
| bg_color, | ||
| theme, | ||
| cache_seconds, | ||
| custom_title, | ||
| border_radius, | ||
| border_color, | ||
| disable_animations, | ||
| langs_count, | ||
| exclude_repo, | ||
| hide, | ||
| } = req.query; | ||
| res.setHeader("Content-Type", "image/svg+xml"); | ||
|
|
||
| const access = guardAccess({ | ||
| res, | ||
| id: username, | ||
| type: "username", | ||
| colors: { | ||
| title_color, | ||
| text_color, | ||
| bg_color, | ||
| border_color, | ||
| theme, | ||
| }, | ||
| }); | ||
| if (!access.isPassed) { | ||
| return access.result; | ||
| } | ||
|
|
||
| try { | ||
| // Fetch all three data sources in parallel. | ||
| const [overview, streak, langs] = await Promise.all([ | ||
| fetchOverview(username), | ||
| fetchStreak(username), | ||
| fetchTopLanguages(username, parseArray(exclude_repo)), | ||
| ]); | ||
|
|
||
| const cacheSeconds = resolveCacheSeconds({ | ||
| requested: parseInt(cache_seconds, 10), | ||
| def: CACHE_TTL.STATS_CARD.DEFAULT, | ||
| min: CACHE_TTL.STATS_CARD.MIN, | ||
| max: CACHE_TTL.STATS_CARD.MAX, | ||
| }); | ||
|
|
||
| setCacheHeaders(res, cacheSeconds); | ||
|
|
||
| return res.send( | ||
| renderCombinedCard( | ||
| { overview, streak, langs }, | ||
| { | ||
| hide_border: parseBoolean(hide_border), | ||
| card_width: parseInt(card_width, 10), | ||
| title_color, | ||
| icon_color, | ||
| text_color, | ||
| bg_color, | ||
| theme, | ||
| custom_title, | ||
| border_radius, | ||
| border_color, | ||
| disable_animations: parseBoolean(disable_animations), | ||
| langs_count: parseInt(langs_count, 10) || 8, | ||
| hide: parseArray(hide), | ||
| }, | ||
|
Comment on lines
+90
to
+93
|
||
| ), | ||
| ); | ||
|
Comment on lines
+59
to
+95
|
||
| } catch (err) { | ||
| setErrorCacheHeaders(res); | ||
| if (err instanceof Error) { | ||
| return res.send( | ||
| renderError({ | ||
| message: err.message, | ||
| secondaryMessage: retrieveSecondaryMessage(err), | ||
| renderOptions: { | ||
| title_color, | ||
| text_color, | ||
| bg_color, | ||
| border_color, | ||
| theme, | ||
| show_repo_link: !(err instanceof MissingParamError), | ||
| }, | ||
| }), | ||
| ); | ||
| } | ||
| return res.send( | ||
| renderError({ | ||
| message: "An unknown error occurred", | ||
| renderOptions: { | ||
| title_color, | ||
| text_color, | ||
| bg_color, | ||
| border_color, | ||
| theme, | ||
| }, | ||
| }), | ||
| ); | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This endpoint renders top-languages data but resolves cache seconds using
CACHE_TTL.STATS_CARDbounds (min 12h). That can significantly increase GitHub GraphQL load compared to/api/top-langs(min 2d), increasing rate-limit/latency risk for a more expensive fetcher. Consider usingCACHE_TTL.TOP_LANGS_CARDbounds (or at leastmin/maxaligned to top-langs) for/api/combined.