diff --git a/backend/api-renamed/status/pat-info.js b/backend/api-renamed/status/pat-info.js index 1b007970..46aae023 100644 --- a/backend/api-renamed/status/pat-info.js +++ b/backend/api-renamed/status/pat-info.js @@ -12,17 +12,12 @@ import { logger, dateDiff } from "../../src/common/utils.js"; export const RATE_LIMIT_SECONDS = 60 * 3; // 1 request per 3 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/backend/api-renamed/status/up.js b/backend/api-renamed/status/up.js index 57f450aa..6bbc1ab4 100644 --- a/backend/api-renamed/status/up.js +++ b/backend/api-renamed/status/up.js @@ -13,17 +13,12 @@ import { logger } from "../../src/common/utils.js"; export const RATE_LIMIT_SECONDS = 60 * 3; // 1 request per 3 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/backend/src/common/http.js b/backend/src/common/http.js index 0f3270fe..388d1052 100644 --- a/backend/src/common/http.js +++ b/backend/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/backend/src/common/retryer.js b/backend/src/common/retryer.js index 45831337..ab01bda7 100644 --- a/backend/src/common/retryer.js +++ b/backend/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/backend/src/fetchers/gist.js b/backend/src/fetchers/gist.js index 635892cf..d9cccc67 100644 --- a/backend/src/fetchers/gist.js +++ b/backend/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/backend/src/fetchers/repo.js b/backend/src/fetchers/repo.js index 1434b39e..6d45f4e7 100644 --- a/backend/src/fetchers/repo.js +++ b/backend/src/fetchers/repo.js @@ -5,17 +5,12 @@ import { request } from "../common/http.js"; import { retryer } from "../common/retryer.js"; import { fetchRepoUserStats } from "./stats.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/backend/src/fetchers/stats.js b/backend/src/fetchers/stats.js index 88941588..1f54bdb3 100644 --- a/backend/src/fetchers/stats.js +++ b/backend/src/fetchers/stats.js @@ -82,16 +82,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; @@ -116,7 +112,7 @@ const fetcher = (variables, token) => { * @param {boolean} variables.includeDiscussionsAnswers Include discussions answers. * @param {string|undefined} variables.startTime Time to start the count of total commits. * @param {string[]} ownerAffiliations The owner affiliations to filter by. Default: OWNER. - * @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. */ @@ -169,6 +165,35 @@ 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 fetchTotalItems = (variables, token) => { + return axios({ + method: "get", + url: + `https://api.github.com/search/` + + type + + `?per_page=1&q=` + + buildSearchFilter(variables.repo, variables.owner).replaceAll( + " ", + "+", + ) + + filter, + 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. * @@ -184,27 +209,6 @@ const totalItemsFetcher = async (username, repo, owner, type, filter) => { throw new Error("Invalid username provided."); } - // https://developer.github.com/v3/search/#search-commits - const fetchTotalItems = (variables, token) => { - return axios({ - method: "get", - url: - `https://api.github.com/search/` + - type + - `?per_page=1&q=` + - buildSearchFilter(variables.repo, variables.owner).replaceAll( - " ", - "+", - ) + - filter, - headers: { - "Content-Type": "application/json", - Accept: "application/vnd.github.cloak-preview", - Authorization: `token ${token}`, - }, - }); - }; - let res; try { res = await retryer(fetchTotalItems, { @@ -287,10 +291,6 @@ const fetchRepoUserStats = async ( return stats; }; -/** - * @typedef {import("./types").StatsData} StatsData Stats data. - */ - /** * Fetch stats for a given username. * @@ -303,7 +303,7 @@ const fetchRepoUserStats = async ( * @param {string[]} ownerAffiliations Owner affiliations. Default: OWNER. * @param {number|undefined} commits_year Year to count total commits * @param {string[]} ownerAffiliations Owner affiliations. Default: OWNER. - * @returns {Promise} Stats data. + * @returns {Promise} Stats data. */ const fetchStats = async ( username, diff --git a/backend/src/fetchers/top-languages.js b/backend/src/fetchers/top-languages.js index 680232e4..33a7c977 100644 --- a/backend/src/fetchers/top-languages.js +++ b/backend/src/fetchers/top-languages.js @@ -10,17 +10,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( @@ -103,6 +98,7 @@ const fetchTopLanguages = async ( } let repoNodes = res.data.data.user.repositories.nodes; + /** @type {Record} */ let repoToHide = {}; const allExcludedRepos = [...exclude_repo, ...excludeRepositories];