From c1a88baad46d6a2c7cb25b3e1fe3729b65079e03 Mon Sep 17 00:00:00 2001 From: Nathan Chu <63111210+nathanchu@users.noreply.github.com> Date: Wed, 27 Jan 2021 02:11:21 -0500 Subject: [PATCH] feat: add support for custom wakatime api domain (#620) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add support for custom wakatime api domain * Update src/fetchers/wakatime-fetcher.js * Update src/fetchers/wakatime-fetcher.js Co-authored-by: 춤추는망고 Co-authored-by: Nathan Chu <63111210+nthnchu@users.noreply.github.com> Co-authored-by: 춤추는망고 --- api/wakatime.js | 3 ++- src/fetchers/wakatime-fetcher.js | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/api/wakatime.js b/api/wakatime.js index 36446bb..44f0984 100644 --- a/api/wakatime.js +++ b/api/wakatime.js @@ -25,6 +25,7 @@ module.exports = async (req, res) => { custom_title, locale, layout, + api_domain, } = req.query; res.setHeader("Content-Type", "image/svg+xml"); @@ -34,7 +35,7 @@ module.exports = async (req, res) => { } try { - const last7Days = await fetchLast7Days({ username }); + const last7Days = await fetchLast7Days({ username, api_domain }); let cacheSeconds = clampValue( parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10), diff --git a/src/fetchers/wakatime-fetcher.js b/src/fetchers/wakatime-fetcher.js index 41ea22f..f6f2b5e 100644 --- a/src/fetchers/wakatime-fetcher.js +++ b/src/fetchers/wakatime-fetcher.js @@ -1,14 +1,16 @@ const axios = require("axios"); -const fetchLast7Days = async ({ username }) => { +const fetchLast7Days = async ({ username, api_domain }) => { try { const { data } = await axios.get( - `https://wakatime.com/api/v1/users/${username}/stats/last_7_days?is_including_today=true`, + `https://${ + api_domain ? api_domain.replace(/[^a-z-.0-9]/gi, "") : "wakatime.com" + }/api/v1/users/${username}/stats/last_7_days?is_including_today=true`, ); return data.data; } catch (err) { - if (err.response.status === 404) { + if (err.response.status < 200 || err.response.status > 299) { throw new Error( "Wakatime user not found, make sure you have a wakatime profile", );