From d11b459486faa34affc1e2c54d8039e2573e1cf1 Mon Sep 17 00:00:00 2001 From: Oleg Gulevskyy <43781031+OlegGulevskyy@users.noreply.github.com> Date: Thu, 29 Dec 2022 00:51:49 +0100 Subject: [PATCH] refactor crowdin init to not throw without key (#2231) --- website/src/api/crowdin.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/website/src/api/crowdin.js b/website/src/api/crowdin.js index bfcd86f92..2f9cd5fb6 100644 --- a/website/src/api/crowdin.js +++ b/website/src/api/crowdin.js @@ -1,14 +1,33 @@ const crowdin = require("@crowdin/crowdin-api-client"); +const personalToken = process.env.CROWDIN_PERSONAL_TOKEN; +const projectId = 531392; // initialization of crowdin client -const { translationStatusApi } = new crowdin.default({ - token: process.env.CROWDIN_PERSONAL_TOKEN, -}); +const initClient = async () => { + if (!personalToken) { + console.warn( + "No crowding personal token, some features might not work as expected" + ); + return; + } + + return new crowdin.default({ + token: personalToken, + }); +}; + +const client = initClient() || {}; async function getTranslationProgress() { let translationProgress = {}; + const { translationStatusApi } = client; - await translationStatusApi.getProjectProgress(531392).then((res) => { + // do nothing if client failed to init + if (!translationStatusApi) { + return translationProgress; + } + + await translationStatusApi.getProjectProgress(projectId).then((res) => { for (const item of res.data) { translationProgress[item.data.languageId] = item.data.approvalProgress; }