Skip to content
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

GRS as an npm module! #2473

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm test
npx lint-staged
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept showing errors when I tried commiting. @rickstaa any idea why that is?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what causes the error. You need to update the tests a bit to let them work with your pull request. You can debug these tests with the https://github.com/jest-community/vscode-jest plugin. Sadly, I don't have time to look at it for the next three months 😅.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should stay since we want the tests to run when people try to commit. 🤔

79 changes: 29 additions & 50 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { renderStatsCard } from "../src/cards/stats-card.js";
import { blacklist } from "../src/common/blacklist.js";
import {
clampValue,
CONSTANTS,
parseArray,
parseBoolean,
renderError,
} from "../src/common/utils.js";
import { fetchStats } from "../src/fetchers/stats-fetcher.js";
import { isLocaleAvailable } from "../src/translations.js";
import generateStatsCard from "../src/generators/stats-card.js";
import { renderError } from "../src/common/utils.js";

export default async (req, res) => {
const {
Expand Down Expand Up @@ -39,22 +30,7 @@ export default async (req, res) => {
} = 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", "Language not found"));
}

try {
const stats = await fetchStats(
username,
parseBoolean(count_private),
parseBoolean(include_all_commits),
parseArray(exclude_repo),
);

const cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10),
CONSTANTS.FOUR_HOURS,
Expand All @@ -68,30 +44,33 @@ export default async (req, res) => {
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);

return res.send(
renderStatsCard(stats, {
hide: parseArray(hide),
show_icons: parseBoolean(show_icons),
hide_title: parseBoolean(hide_title),
hide_border: parseBoolean(hide_border),
card_width: parseInt(card_width, 10),
hide_rank: parseBoolean(hide_rank),
include_all_commits: parseBoolean(include_all_commits),
line_height,
title_color,
ring_color,
icon_color,
text_color,
text_bold: parseBoolean(text_bold),
bg_color,
theme,
custom_title,
border_radius,
border_color,
locale: locale ? locale.toLowerCase() : null,
disable_animations: parseBoolean(disable_animations),
}),
);
const card = await generateStatsCard({
username,
hide,
hide_title,
hide_border,
card_width,
hide_rank,
show_icons,
count_private,
include_all_commits,
line_height,
title_color,
ring_color,
icon_color,
text_color,
text_bold,
bg_color,
theme,
exclude_repo,
custom_title,
locale,
disable_animations,
border_radius,
border_color,
});

return res.send(card);
} 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));
Expand Down
65 changes: 18 additions & 47 deletions api/pin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { renderRepoCard } from "../src/cards/repo-card.js";
import { blacklist } from "../src/common/blacklist.js";
import {
clampValue,
CONSTANTS,
parseBoolean,
renderError,
} from "../src/common/utils.js";
import { fetchRepo } from "../src/fetchers/repo-fetcher.js";
import { isLocaleAvailable } from "../src/translations.js";
import generateRepoCard from "../src/generators/repo.js";
import { renderError } from "../src/common/utils.js";

export default async (req, res) => {
const {
Expand All @@ -28,57 +20,36 @@ export default async (req, res) => {

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", "Language not found"));
}

try {
const repoData = await fetchRepo(username, repo);

let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10),
CONSTANTS.FOUR_HOURS,
CONSTANTS.ONE_DAY,
);

/*
if star count & fork count is over 1k then we are kFormating the text
and if both are zero we are not showing the stats
so we can just make the cache longer, since there is no need to frequent updates
*/
const stars = repoData.starCount;
const forks = repoData.forkCount;
const isBothOver1K = stars > 1000 && forks > 1000;
const isBothUnder1 = stars < 1 && forks < 1;
if (!cache_seconds && (isBothOver1K || isBothUnder1)) {
cacheSeconds = CONSTANTS.FOUR_HOURS;
}

res.setHeader(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be very effective as the default cache is anyways FOUR_HOURS. Let me know if I should add it back.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be very effective as the default cache is anyways FOUR_HOURS. Let me know if I should add it back.

Think this should be EIGHT_HOURS now that we increased the default to FOUR_HOURS.

"Cache-Control",
`max-age=${
cacheSeconds / 2
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);

return res.send(
renderRepoCard(repoData, {
hide_border: parseBoolean(hide_border),
title_color,
icon_color,
text_color,
bg_color,
theme,
border_radius,
border_color,
show_owner: parseBoolean(show_owner),
locale: locale ? locale.toLowerCase() : null,
}),
);
const card = await generateRepoCard({
username,
repo,
hide_border,
title_color,
icon_color,
text_color,
bg_color,
theme,
show_owner,
locale,
border_radius,
border_color,
});

return res.send(card);
} 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));
Expand Down
66 changes: 23 additions & 43 deletions api/top-langs.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
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";
import generateTopLanguagesCard from "../src/generators/top-langs.js";
import { renderError } from "../src/common/utils.js";

export default async (req, res) => {
const {
Expand All @@ -33,20 +24,7 @@ export default async (req, res) => {
} = 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,
Expand All @@ -60,25 +38,27 @@ export default async (req, res) => {
}, 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),
}),
);
const card = await generateTopLanguagesCard({
username,
hide,
hide_title,
hide_border,
card_width,
title_color,
text_color,
bg_color,
theme,
layout,
langs_count,
exclude_repo,
custom_title,
locale,
border_radius,
border_color,
disable_animations,
});

return res.send(card);
} 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));
Expand Down
61 changes: 25 additions & 36 deletions api/wakatime.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { renderWakatimeCard } from "../src/cards/wakatime-card.js";
import {
clampValue,
CONSTANTS,
parseArray,
parseBoolean,
renderError,
} from "../src/common/utils.js";
import { fetchWakatimeStats } from "../src/fetchers/wakatime-fetcher.js";
import { isLocaleAvailable } from "../src/translations.js";
import generateWakatimeCard from "../src/generators/wakatime.js";
import { renderError } from "../src/common/utils.js";

export default async (req, res) => {
const {
Expand Down Expand Up @@ -35,13 +27,7 @@ export default async (req, res) => {

res.setHeader("Content-Type", "image/svg+xml");

if (locale && !isLocaleAvailable(locale)) {
return res.send(renderError("Something went wrong", "Language not found"));
}

try {
const stats = await fetchWakatimeStats({ username, api_domain, range });

let cacheSeconds = clampValue(
parseInt(cache_seconds || CONSTANTS.FOUR_HOURS, 10),
CONSTANTS.FOUR_HOURS,
Expand All @@ -59,26 +45,29 @@ export default async (req, res) => {
}, s-maxage=${cacheSeconds}, stale-while-revalidate=${CONSTANTS.ONE_DAY}`,
);

return res.send(
renderWakatimeCard(stats, {
custom_title,
hide_title: parseBoolean(hide_title),
hide_border: parseBoolean(hide_border),
hide: parseArray(hide),
line_height,
title_color,
icon_color,
text_color,
bg_color,
theme,
hide_progress,
border_radius,
border_color,
locale: locale ? locale.toLowerCase() : null,
layout,
langs_count,
}),
);
const card = await generateWakatimeCard({
username,
title_color,
icon_color,
hide_border,
line_height,
text_color,
bg_color,
theme,
hide_title,
hide_progress,
custom_title,
locale,
layout,
langs_count,
hide,
api_domain,
range,
border_radius,
border_color,
});

return res.send(card);
} 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));
Expand Down
Loading