From 866e509d078a1bb0d127231d7c9a34a67cc2da78 Mon Sep 17 00:00:00 2001 From: Zohan Subhash Date: Tue, 17 Jan 2023 15:49:21 +0530 Subject: [PATCH] Create pin.js --- api/wild/pin.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 api/wild/pin.js diff --git a/api/wild/pin.js b/api/wild/pin.js new file mode 100644 index 00000000..c1ccc610 --- /dev/null +++ b/api/wild/pin.js @@ -0,0 +1,81 @@ +import { renderRepoCard } from "../src/cards/repo-card.js"; +import { + clampValue, + CONSTANTS, + parseBoolean, + renderError, +} from "../src/common/utils.js"; + +export default async (req, res) => { + const { + hide_border, + title_color, + icon_color, + text_color, + bg_color, + theme, + cache_seconds, + border_radius, + border_color, + } = req.query; + + res.setHeader("Content-Type", "image/svg+xml"); + + try { + const repoData = { + name: req.query.title, + description: req.query.description, + primaryLanguage: { + name: req.query.footer, + color: `#${req.query.badge}`, + }, + starCount: req.query.stars, + forkCount: req.query.forks, + highlight: req.query.highlight, + }; + + 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; + if (!isNaN(stars) && !isNaN(forks)) { + const isBothOver1K = stars > 1000 && forks > 1000; + const isBothUnder1 = stars < 1 && forks < 1; + if (!cache_seconds && (isBothOver1K || isBothUnder1)) { + cacheSeconds = CONSTANTS.FOUR_HOURS; + } + } + + res.setHeader( + "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, + }), + ); + } 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)); + } +};