From ade7d5365307e38135de9b96350a732b146deb2e Mon Sep 17 00:00:00 2001 From: Alexandr Garbuzov <186095128+alexandr-garbuzov@users.noreply.github.com> Date: Mon, 6 Oct 2025 23:32:47 +0300 Subject: [PATCH] refactor: move cache seconds calculation logic into reusable function (#4532) * refactor: move cache seconds calculation logic into reusable function * dev --------- Co-authored-by: Alexandr --- api/gist.js | 23 ++++++++--------------- api/index.js | 17 +++++++---------- api/pin.js | 23 ++++++++--------------- api/top-langs.js | 17 +++++++---------- api/wakatime.js | 17 +++++++---------- src/common/cache.js | 23 +++++++++++++++++++++++ 6 files changed, 60 insertions(+), 60 deletions(-) create mode 100644 src/common/cache.js diff --git a/api/gist.js b/api/gist.js index f0bc08ad..d5928950 100644 --- a/api/gist.js +++ b/api/gist.js @@ -1,15 +1,11 @@ // @ts-check -import { - clampValue, - CONSTANTS, - renderError, - parseBoolean, -} from "../src/common/utils.js"; +import { CONSTANTS, renderError, parseBoolean } from "../src/common/utils.js"; import { gistWhitelist } from "../src/common/envs.js"; import { isLocaleAvailable } from "../src/translations.js"; import { renderGistCard } from "../src/cards/gist.js"; import { fetchGist } from "../src/fetchers/gist.js"; +import { resolveCacheSeconds } from "../src/common/cache.js"; export default async (req, res) => { const { @@ -60,15 +56,12 @@ export default async (req, res) => { try { const gistData = await fetchGist(id); - - let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.TWO_DAY, 10), - CONSTANTS.TWO_DAY, - CONSTANTS.SIX_DAY, - ); - cacheSeconds = process.env.CACHE_SECONDS - ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds - : cacheSeconds; + const cacheSeconds = resolveCacheSeconds({ + requested: cache_seconds, + def: CONSTANTS.TWO_DAY, + min: CONSTANTS.TWO_DAY, + max: CONSTANTS.SIX_DAY, + }); res.setHeader( "Cache-Control", diff --git a/api/index.js b/api/index.js index 5e0939ab..989e39a6 100644 --- a/api/index.js +++ b/api/index.js @@ -2,9 +2,9 @@ import { renderStatsCard } from "../src/cards/stats.js"; import { blacklist } from "../src/common/blacklist.js"; +import { resolveCacheSeconds } from "../src/common/cache.js"; import { whitelist } from "../src/common/envs.js"; import { - clampValue, CONSTANTS, parseArray, parseBoolean, @@ -103,15 +103,12 @@ export default async (req, res) => { showStats.includes("discussions_answered"), parseInt(commits_year, 10), ); - - let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), - CONSTANTS.TWELVE_HOURS, - CONSTANTS.TWO_DAY, - ); - cacheSeconds = process.env.CACHE_SECONDS - ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds - : cacheSeconds; + const cacheSeconds = resolveCacheSeconds({ + requested: cache_seconds, + def: CONSTANTS.CARD_CACHE_SECONDS, + min: CONSTANTS.TWELVE_HOURS, + max: CONSTANTS.TWO_DAY, + }); res.setHeader( "Cache-Control", diff --git a/api/pin.js b/api/pin.js index 67802a03..10c52f1f 100644 --- a/api/pin.js +++ b/api/pin.js @@ -2,13 +2,9 @@ import { renderRepoCard } from "../src/cards/repo.js"; import { blacklist } from "../src/common/blacklist.js"; +import { resolveCacheSeconds } from "../src/common/cache.js"; import { whitelist } from "../src/common/envs.js"; -import { - clampValue, - CONSTANTS, - parseBoolean, - renderError, -} from "../src/common/utils.js"; +import { CONSTANTS, parseBoolean, renderError } from "../src/common/utils.js"; import { fetchRepo } from "../src/fetchers/repo.js"; import { isLocaleAvailable } from "../src/translations.js"; @@ -80,15 +76,12 @@ export default async (req, res) => { try { const repoData = await fetchRepo(username, repo); - - let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.PIN_CARD_CACHE_SECONDS, 10), - CONSTANTS.ONE_DAY, - CONSTANTS.TEN_DAY, - ); - cacheSeconds = process.env.CACHE_SECONDS - ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds - : cacheSeconds; + const cacheSeconds = resolveCacheSeconds({ + requested: cache_seconds, + def: CONSTANTS.PIN_CARD_CACHE_SECONDS, + min: CONSTANTS.ONE_DAY, + max: CONSTANTS.TEN_DAY, + }); res.setHeader( "Cache-Control", diff --git a/api/top-langs.js b/api/top-langs.js index e701cb16..ce3443ba 100644 --- a/api/top-langs.js +++ b/api/top-langs.js @@ -2,9 +2,9 @@ import { renderTopLanguages } from "../src/cards/top-languages.js"; import { blacklist } from "../src/common/blacklist.js"; +import { resolveCacheSeconds } from "../src/common/cache.js"; import { whitelist } from "../src/common/envs.js"; import { - clampValue, CONSTANTS, parseArray, parseBoolean, @@ -105,15 +105,12 @@ export default async (req, res) => { size_weight, count_weight, ); - - let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.TOP_LANGS_CACHE_SECONDS, 10), - CONSTANTS.TWO_DAY, - CONSTANTS.TEN_DAY, - ); - cacheSeconds = process.env.CACHE_SECONDS - ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds - : cacheSeconds; + const cacheSeconds = resolveCacheSeconds({ + requested: cache_seconds, + def: CONSTANTS.TOP_LANGS_CACHE_SECONDS, + min: CONSTANTS.TWO_DAY, + max: CONSTANTS.TEN_DAY, + }); res.setHeader( "Cache-Control", diff --git a/api/wakatime.js b/api/wakatime.js index 0c5494b4..5aba93ab 100644 --- a/api/wakatime.js +++ b/api/wakatime.js @@ -2,7 +2,6 @@ import { renderWakatimeCard } from "../src/cards/wakatime.js"; import { - clampValue, CONSTANTS, parseArray, parseBoolean, @@ -11,6 +10,7 @@ import { import { whitelist } from "../src/common/envs.js"; import { fetchWakatimeStats } from "../src/fetchers/wakatime.js"; import { isLocaleAvailable } from "../src/translations.js"; +import { resolveCacheSeconds } from "../src/common/cache.js"; export default async (req, res) => { const { @@ -71,15 +71,12 @@ export default async (req, res) => { try { const stats = await fetchWakatimeStats({ username, api_domain }); - - let cacheSeconds = clampValue( - parseInt(cache_seconds || CONSTANTS.CARD_CACHE_SECONDS, 10), - CONSTANTS.SIX_HOURS, - CONSTANTS.TWO_DAY, - ); - cacheSeconds = process.env.CACHE_SECONDS - ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds - : cacheSeconds; + const cacheSeconds = resolveCacheSeconds({ + requested: cache_seconds, + def: CONSTANTS.CARD_CACHE_SECONDS, + min: CONSTANTS.SIX_HOURS, + max: CONSTANTS.TWO_DAY, + }); res.setHeader( "Cache-Control", diff --git a/src/common/cache.js b/src/common/cache.js new file mode 100644 index 00000000..b5fd8ce7 --- /dev/null +++ b/src/common/cache.js @@ -0,0 +1,23 @@ +import { clampValue } from "./utils"; + +/** + * Resolves the cache seconds based on the requested, default, min, and max values. + * + * @param {Object} args The parameters object. + * @param {number} args.requested The requested cache seconds. + * @param {number} args.def The default cache seconds. + * @param {number} args.min The minimum cache seconds. + * @param {number} args.max The maximum cache seconds. + * @returns {number} The resolved cache seconds. + */ +const resolveCacheSeconds = ({ requested, def, min, max }) => { + let cacheSeconds = clampValue(parseInt(requested || def, 10), min, max); + + cacheSeconds = process.env.CACHE_SECONDS + ? parseInt(process.env.CACHE_SECONDS, 10) || cacheSeconds + : cacheSeconds; + + return cacheSeconds; +}; + +export { resolveCacheSeconds };