fix: resolve several vscode type errors inside fetchers code (#4579)

Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
This commit is contained in:
Alexandr Garbuzov
2025-10-15 21:51:32 +03:00
committed by GitHub
co-authored by Alexandr
parent 2df35521d7
commit bf021b0670
8 changed files with 47 additions and 71 deletions
+5 -9
View File
@@ -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<AxiosResponse>} The response.
* @returns {Promise<import('axios').AxiosResponse>} The response.
*/
const uptimeFetcher = (variables, token) => {
return request(
@@ -47,7 +42,7 @@ const getAllPATs = () => {
};
/**
* @typedef {(variables: AxiosRequestHeaders, token: string) => Promise<AxiosResponse>} Fetcher The fetcher function.
* @typedef {(variables: any, token: string) => Promise<import('axios').AxiosResponse>} 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<PATInfo>} The response.
*/
const getPATInfo = async (fetcher, variables) => {
/** @type {Record<string, any>} */
const details = {};
const PATs = getAllPATs();
+2 -7
View File
@@ -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<AxiosResponse>} The response.
* @returns {Promise<import('axios').AxiosResponse>} The response.
*/
const uptimeFetcher = (variables, token) => {
return request(
+2 -7
View File
@@ -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<any>} Request response.
*/
const request = (data, headers) => {
+2 -2
View File
@@ -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<AxiosResponse>} FetcherFunction Fetcher function.
* @typedef {(variables: any, token: string, retriesForTests?: number) => Promise<AxiosResponse>} 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<any>} The response from the fetcher function.
*/
+6 -7
View File
@@ -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<AxiosResponse>} The response.
* @returns {Promise<import('axios').AxiosResponse>} 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<string, number>} */
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;
};
+2 -7
View File
@@ -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<AxiosResponse>} The response.
* @returns {Promise<import('axios').AxiosResponse>} The response.
*/
const fetcher = (variables, token) => {
return request(
+25 -25
View File
@@ -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<AxiosResponse>} Axios response.
* @returns {Promise<import('axios').AxiosResponse>} 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<AxiosResponse>} Axios response.
* @returns {Promise<import('axios').AxiosResponse>} 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<import('axios').AxiosResponse>} 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<StatsData>} Stats data.
* @returns {Promise<import("./types").StatsData>} Stats data.
*/
const fetchStats = async (
username,
+3 -7
View File
@@ -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<AxiosResponse>} Languages fetcher response.
* @returns {Promise<import("axios").AxiosResponse>} 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<string, boolean>} */
let repoToHide = {};
const allExcludedRepos = [...exclude_repo, ...excludeRepositories];