-
-
Notifications
You must be signed in to change notification settings - Fork 23.9k
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
base: master
Are you sure you want to change the base?
GRS as an npm module! #2473
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 |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
npm test | ||
npx lint-staged | ||
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 { | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Think this should be |
||
"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)); | ||
|
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.
Kept showing errors when I tried commiting. @rickstaa any idea why that is?
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.
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 😅.
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 line should stay since we want the tests to run when people try to commit. 🤔