From bf021b06704c0764564ce5f4f0318847643161d3 Mon Sep 17 00:00:00 2001 From: Alexandr Garbuzov <186095128+alexandr-garbuzov@users.noreply.github.com> Date: Wed, 15 Oct 2025 21:51:32 +0300 Subject: [PATCH] fix: resolve several vscode type errors inside fetchers code (#4579) Co-authored-by: Alexandr --- api/status/pat-info.js | 14 ++++------ api/status/up.js | 9 ++----- src/common/http.js | 9 ++----- src/common/retryer.js | 4 +-- src/fetchers/gist.js | 13 +++++---- src/fetchers/repo.js | 9 ++----- src/fetchers/stats.js | 50 +++++++++++++++++------------------ src/fetchers/top-languages.js | 10 +++---- 8 files changed, 47 insertions(+), 71 deletions(-) diff --git a/api/status/pat-info.js b/api/status/pat-info.js index 087abd48..f6d917ce 100644 --- a/api/status/pat-info.js +++ b/api/status/pat-info.js @@ -12,17 +12,12 @@ import { logger, dateDiff } from "../../src/common/utils.js"; export const RATE_LIMIT_SECONDS = 60 * 5; // 1 request per 5 minutes -/** - * @typedef {import('axios').AxiosRequestHeaders} AxiosRequestHeaders Axios request headers. - * @typedef {import('axios').AxiosResponse} AxiosResponse Axios response. - */ - /** * Simple uptime check fetcher for the PATs. * - * @param {AxiosRequestHeaders} variables Fetcher variables. + * @param {any} variables Fetcher variables. * @param {string} token GitHub token. - * @returns {Promise} The response. + * @returns {Promise} The response. */ const uptimeFetcher = (variables, token) => { return request( @@ -47,7 +42,7 @@ const getAllPATs = () => { }; /** - * @typedef {(variables: AxiosRequestHeaders, token: string) => Promise} Fetcher The fetcher function. + * @typedef {(variables: any, token: string) => Promise} Fetcher The fetcher function. * @typedef {{validPATs: string[], expiredPATs: string[], exhaustedPATs: string[], suspendedPATs: string[], errorPATs: string[], details: any}} PATInfo The PAT info. */ @@ -55,10 +50,11 @@ const getAllPATs = () => { * Check whether any of the PATs is expired. * * @param {Fetcher} fetcher The fetcher function. - * @param {AxiosRequestHeaders} variables Fetcher variables. + * @param {any} variables Fetcher variables. * @returns {Promise} The response. */ const getPATInfo = async (fetcher, variables) => { + /** @type {Record} */ const details = {}; const PATs = getAllPATs(); diff --git a/api/status/up.js b/api/status/up.js index 042da140..35964388 100644 --- a/api/status/up.js +++ b/api/status/up.js @@ -13,17 +13,12 @@ import { logger } from "../../src/common/utils.js"; export const RATE_LIMIT_SECONDS = 60 * 5; // 1 request per 5 minutes -/** - * @typedef {import('axios').AxiosRequestHeaders} AxiosRequestHeaders Axios request headers. - * @typedef {import('axios').AxiosResponse} AxiosResponse Axios response. - */ - /** * Simple uptime check fetcher for the PATs. * - * @param {AxiosRequestHeaders} variables Fetcher variables. + * @param {any} variables Fetcher variables. * @param {string} token GitHub token. - * @returns {Promise} The response. + * @returns {Promise} The response. */ const uptimeFetcher = (variables, token) => { return request( diff --git a/src/common/http.js b/src/common/http.js index 0f3270fe..388d1052 100644 --- a/src/common/http.js +++ b/src/common/http.js @@ -1,15 +1,10 @@ import axios from "axios"; -/** - * @typedef {import('axios').AxiosRequestConfig['data']} AxiosRequestConfigData Axios request data. - * @typedef {import('axios').AxiosRequestConfig['headers']} AxiosRequestConfigHeaders Axios request headers. - */ - /** * Send GraphQL request to GitHub API. * - * @param {AxiosRequestConfigData} data Request data. - * @param {AxiosRequestConfigHeaders} headers Request headers. + * @param {import('axios').AxiosRequestConfig['data']} data Request data. + * @param {import('axios').AxiosRequestConfig['headers']} headers Request headers. * @returns {Promise} Request response. */ const request = (data, headers) => { diff --git a/src/common/retryer.js b/src/common/retryer.js index c65d6ede..569108b1 100644 --- a/src/common/retryer.js +++ b/src/common/retryer.js @@ -13,14 +13,14 @@ const RETRIES = process.env.NODE_ENV === "test" ? 7 : PATs; /** * @typedef {import("axios").AxiosResponse} AxiosResponse Axios response. - * @typedef {(variables: object, token: string, retriesForTests?: number) => Promise} FetcherFunction Fetcher function. + * @typedef {(variables: any, token: string, retriesForTests?: number) => Promise} FetcherFunction Fetcher function. */ /** * Try to execute the fetcher function until it succeeds or the max number of retries is reached. * * @param {FetcherFunction} fetcher The fetcher function. - * @param {object} variables Object with arguments to pass to the fetcher function. + * @param {any} variables Object with arguments to pass to the fetcher function. * @param {number} retries How many times to retry. * @returns {Promise} The response from the fetcher function. */ diff --git a/src/fetchers/gist.js b/src/fetchers/gist.js index 635892cf..d9cccc67 100644 --- a/src/fetchers/gist.js +++ b/src/fetchers/gist.js @@ -4,11 +4,6 @@ import { retryer } from "../common/retryer.js"; import { MissingParamError } from "../common/error.js"; import { request } from "../common/http.js"; -/** - * @typedef {import('axios').AxiosRequestHeaders} AxiosRequestHeaders Axios request headers. - * @typedef {import('axios').AxiosResponse} AxiosResponse Axios response. - */ - const QUERY = ` query gistInfo($gistName: String!) { viewer { @@ -36,9 +31,9 @@ query gistInfo($gistName: String!) { /** * Gist data fetcher. * - * @param {AxiosRequestHeaders} variables Fetcher variables. + * @param {object} variables Fetcher variables. * @param {string} token GitHub token. - * @returns {Promise} The response. + * @returns {Promise} The response. */ const fetcher = async (variables, token) => { return await request( @@ -58,7 +53,9 @@ const fetcher = async (variables, token) => { * @returns {string} Primary language. */ const calculatePrimaryLanguage = (files) => { + /** @type {Record} */ const languages = {}; + for (const file of files) { if (file.language) { if (languages[file.language.name]) { @@ -68,12 +65,14 @@ const calculatePrimaryLanguage = (files) => { } } } + let primaryLanguage = Object.keys(languages)[0]; for (const language in languages) { if (languages[language] > languages[primaryLanguage]) { primaryLanguage = language; } } + return primaryLanguage; }; diff --git a/src/fetchers/repo.js b/src/fetchers/repo.js index aa9bc7c1..304aba5f 100644 --- a/src/fetchers/repo.js +++ b/src/fetchers/repo.js @@ -4,17 +4,12 @@ import { MissingParamError } from "../common/error.js"; import { request } from "../common/http.js"; import { retryer } from "../common/retryer.js"; -/** - * @typedef {import('axios').AxiosRequestHeaders} AxiosRequestHeaders Axios request headers. - * @typedef {import('axios').AxiosResponse} AxiosResponse Axios response. - */ - /** * Repo data fetcher. * - * @param {AxiosRequestHeaders} variables Fetcher variables. + * @param {object} variables Fetcher variables. * @param {string} token GitHub token. - * @returns {Promise} The response. + * @returns {Promise} The response. */ const fetcher = (variables, token) => { return request( diff --git a/src/fetchers/stats.js b/src/fetchers/stats.js index 34e558de..29b7b975 100644 --- a/src/fetchers/stats.js +++ b/src/fetchers/stats.js @@ -78,16 +78,12 @@ const GRAPHQL_STATS_QUERY = ` } `; -/** - * @typedef {import('axios').AxiosResponse} AxiosResponse Axios response. - */ - /** * Stats fetcher object. * - * @param {object} variables Fetcher variables. + * @param {object & { after: string | null }} variables Fetcher variables. * @param {string} token GitHub token. - * @returns {Promise} Axios response. + * @returns {Promise} Axios response. */ const fetcher = (variables, token) => { const query = variables.after ? GRAPHQL_REPOS_QUERY : GRAPHQL_STATS_QUERY; @@ -111,7 +107,7 @@ const fetcher = (variables, token) => { * @param {boolean} variables.includeDiscussions Include discussions. * @param {boolean} variables.includeDiscussionsAnswers Include discussions answers. * @param {string|undefined} variables.startTime Time to start the count of total commits. - * @returns {Promise} Axios response. + * @returns {Promise} Axios response. * * @description This function supports multi-page fetching if the 'FETCH_MULTI_PAGE_STARS' environment variable is set to true. */ @@ -162,6 +158,27 @@ const statsFetcher = async ({ return stats; }; +/** + * Fetch total commits using the REST API. + * + * @param {object} variables Fetcher variables. + * @param {string} token GitHub token. + * @returns {Promise} Axios response. + * + * @see https://developer.github.com/v3/search/#search-commits + */ +const fetchTotalCommits = (variables, token) => { + return axios({ + method: "get", + url: `https://api.github.com/search/commits?q=author:${variables.login}`, + headers: { + "Content-Type": "application/json", + Accept: "application/vnd.github.cloak-preview", + Authorization: `token ${token}`, + }, + }); +}; + /** * Fetch all the commits for all the repositories of a given username. * @@ -177,19 +194,6 @@ const totalCommitsFetcher = async (username) => { throw new Error("Invalid username provided."); } - // https://developer.github.com/v3/search/#search-commits - const fetchTotalCommits = (variables, token) => { - return axios({ - method: "get", - url: `https://api.github.com/search/commits?q=author:${variables.login}`, - headers: { - "Content-Type": "application/json", - Accept: "application/vnd.github.cloak-preview", - Authorization: `token ${token}`, - }, - }); - }; - let res; try { res = await retryer(fetchTotalCommits, { login: username }); @@ -208,10 +212,6 @@ const totalCommitsFetcher = async (username) => { return totalCount; }; -/** - * @typedef {import("./types").StatsData} StatsData Stats data. - */ - /** * Fetch stats for a given username. * @@ -222,7 +222,7 @@ const totalCommitsFetcher = async (username) => { * @param {boolean} include_discussions Include discussions. * @param {boolean} include_discussions_answers Include discussions answers. * @param {number|undefined} commits_year Year to count total commits - * @returns {Promise} Stats data. + * @returns {Promise} Stats data. */ const fetchStats = async ( username, diff --git a/src/fetchers/top-languages.js b/src/fetchers/top-languages.js index c0f353eb..601de295 100644 --- a/src/fetchers/top-languages.js +++ b/src/fetchers/top-languages.js @@ -7,17 +7,12 @@ import { CustomError, MissingParamError } from "../common/error.js"; import { wrapTextMultiline } from "../common/fmt.js"; import { request } from "../common/http.js"; -/** - * @typedef {import("axios").AxiosRequestHeaders} AxiosRequestHeaders Axios request headers. - * @typedef {import("axios").AxiosResponse} AxiosResponse Axios response. - */ - /** * Top languages fetcher object. * - * @param {AxiosRequestHeaders} variables Fetcher variables. + * @param {any} variables Fetcher variables. * @param {string} token GitHub token. - * @returns {Promise} Languages fetcher response. + * @returns {Promise} Languages fetcher response. */ const fetcher = (variables, token) => { return request( @@ -97,6 +92,7 @@ const fetchTopLanguages = async ( } let repoNodes = res.data.data.user.repositories.nodes; + /** @type {Record} */ let repoToHide = {}; const allExcludedRepos = [...exclude_repo, ...excludeRepositories];