-
Notifications
You must be signed in to change notification settings - Fork 316
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
feat: Add custom theme support #188
base: main
Are you sure you want to change the base?
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,10 +2,21 @@ import {getCommitsLanguageSVGWithThemeName} from '../../src/cards/most-commit-la | |||||||||||||
import {changToNextGitHubToken} from '../utils/github-token-updater'; | ||||||||||||||
import {getErrorMsgCard} from '../utils/error-card'; | ||||||||||||||
import {translateLanguage} from '../../src/utils/translator'; | ||||||||||||||
import {Theme} from '../../src/const/theme'; | ||||||||||||||
import type {VercelRequest, VercelResponse} from '@vercel/node'; | ||||||||||||||
|
||||||||||||||
export default async (req: VercelRequest, res: VercelResponse) => { | ||||||||||||||
let {username, theme = 'default', exclude = ''} = req.query; | ||||||||||||||
let { | ||||||||||||||
username, | ||||||||||||||
theme = 'default', | ||||||||||||||
exclude = '', | ||||||||||||||
title_color = '', | ||||||||||||||
text_color = '', | ||||||||||||||
bg_color = '', | ||||||||||||||
border_color = '', | ||||||||||||||
icon_color = '', | ||||||||||||||
chart_color = '' | ||||||||||||||
} = req.query; | ||||||||||||||
|
||||||||||||||
if (typeof theme !== 'string') { | ||||||||||||||
res.status(400).send('theme must be a string'); | ||||||||||||||
|
@@ -19,16 +30,49 @@ export default async (req: VercelRequest, res: VercelResponse) => { | |||||||||||||
res.status(400).send('exclude must be a string'); | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
if (typeof title_color !== 'string') { | ||||||||||||||
res.status(400).send('title_color must be a string'); | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
if (typeof text_color !== 'string') { | ||||||||||||||
res.status(400).send('text_color must be a string'); | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
if (typeof bg_color !== 'string') { | ||||||||||||||
res.status(400).send('bg_color must be a string'); | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
if (typeof border_color !== 'string') { | ||||||||||||||
res.status(400).send('border_color must be a string'); | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
if (typeof icon_color !== 'string') { | ||||||||||||||
res.status(400).send('icon_color must be a string'); | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
if (typeof chart_color !== 'string') { | ||||||||||||||
res.status(400).send('chart_color must be a string'); | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
let excludeArr = <string[]>[]; | ||||||||||||||
exclude.split(',').forEach(function (val) { | ||||||||||||||
excludeArr.push(translateLanguage(val)); | ||||||||||||||
}); | ||||||||||||||
Comment on lines
58
to
60
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. Consider using an arrow function for the - exclude.split(',').forEach(function (val) {
+ exclude.split(',').forEach(val => { Committable suggestion
Suggested change
ToolsBiome
|
||||||||||||||
let customTheme = new Theme( | ||||||||||||||
title_color, | ||||||||||||||
text_color, | ||||||||||||||
bg_color, | ||||||||||||||
border_color, | ||||||||||||||
-1, // strokeOpacity is not used in custom themes | ||||||||||||||
icon_color, | ||||||||||||||
chart_color | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
try { | ||||||||||||||
let tokenIndex = 0; | ||||||||||||||
while (true) { | ||||||||||||||
try { | ||||||||||||||
const cardSVG = await getCommitsLanguageSVGWithThemeName(username, theme, excludeArr); | ||||||||||||||
const cardSVG = await getCommitsLanguageSVGWithThemeName(username, theme, customTheme, excludeArr); | ||||||||||||||
res.setHeader('Content-Type', 'image/svg+xml'); | ||||||||||||||
res.send(cardSVG); | ||||||||||||||
return; | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,21 @@ import {getReposPerLanguageSVGWithThemeName} from '../../src/cards/repos-per-lan | |
import {changToNextGitHubToken} from '../utils/github-token-updater'; | ||
import {getErrorMsgCard} from '../utils/error-card'; | ||
import {translateLanguage} from '../../src/utils/translator'; | ||
import {Theme} from '../../src/const/theme'; | ||
import type {VercelRequest, VercelResponse} from '@vercel/node'; | ||
|
||
export default async (req: VercelRequest, res: VercelResponse) => { | ||
let {username, theme = 'default', exclude = ''} = req.query; | ||
let { | ||
username, | ||
theme = 'default', | ||
exclude = '', | ||
title_color = '', | ||
text_color = '', | ||
bg_color = '', | ||
border_color = '', | ||
icon_color = '', | ||
chart_color = '' | ||
} = req.query; | ||
|
||
if (typeof theme !== 'string') { | ||
res.status(400).send('theme must be a string'); | ||
|
@@ -19,6 +30,39 @@ export default async (req: VercelRequest, res: VercelResponse) => { | |
res.status(400).send('exclude must be a string'); | ||
return; | ||
} | ||
if (typeof title_color !== 'string') { | ||
res.status(400).send('title_color must be a string'); | ||
return; | ||
} | ||
if (typeof text_color !== 'string') { | ||
res.status(400).send('text_color must be a string'); | ||
return; | ||
} | ||
if (typeof bg_color !== 'string') { | ||
res.status(400).send('bg_color must be a string'); | ||
return; | ||
} | ||
if (typeof border_color !== 'string') { | ||
res.status(400).send('border_color must be a string'); | ||
return; | ||
} | ||
if (typeof icon_color !== 'string') { | ||
res.status(400).send('icon_color must be a string'); | ||
return; | ||
} | ||
if (typeof chart_color !== 'string') { | ||
res.status(400).send('chart_color must be a string'); | ||
return; | ||
Comment on lines
+33
to
+55
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. The repeated pattern of parameter validation can be refactored into a utility function to adhere to DRY principles. + function validateParam(param, paramName) {
+ if (typeof param !== 'string') {
+ res.status(400).send(`${paramName} must be a string`);
+ return false;
+ }
+ return true;
+ }
- if (typeof title_color !== 'string') {
- res.status(400).send('title_color must be a string');
- return;
- }
+ if (!validateParam(title_color, 'title_color')) return;
|
||
} | ||
let customTheme = new Theme( | ||
title_color, | ||
text_color, | ||
bg_color, | ||
border_color, | ||
-1, // strokeOpacity is not used in custom themes | ||
icon_color, | ||
chart_color | ||
); | ||
let excludeArr = <string[]>[]; | ||
exclude.split(',').forEach(function (val) { | ||
excludeArr.push(translateLanguage(val)); | ||
|
@@ -28,7 +72,7 @@ export default async (req: VercelRequest, res: VercelResponse) => { | |
let tokenIndex = 0; | ||
while (true) { | ||
try { | ||
const cardSVG = await getReposPerLanguageSVGWithThemeName(username, theme, excludeArr); | ||
const cardSVG = await getReposPerLanguageSVGWithThemeName(username, theme, customTheme, excludeArr); | ||
res.setHeader('Content-Type', 'image/svg+xml'); | ||
res.send(cardSVG); | ||
return; | ||
|
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.
Refactor the repeated validation code into a utility function to reduce redundancy and enhance maintainability.