diff --git a/src/fetchRepo.js b/src/fetchRepo.js index 29434bb..c7b7c3e 100644 --- a/src/fetchRepo.js +++ b/src/fetchRepo.js @@ -1,48 +1,41 @@ -const axios = require("axios"); +const { request } = require("./utils"); async function fetchRepo(username, reponame) { if (!username || !reponame) { throw new Error("Invalid username or reponame"); } - const res = await axios({ - url: "https://api.github.com/graphql", - method: "post", - headers: { - Authorization: `bearer ${process.env.GITHUB_TOKEN}`, - }, - data: { - query: ` - fragment RepoInfo on Repository { + const res = await request({ + query: ` + fragment RepoInfo on Repository { + name + stargazers { + totalCount + } + description + primaryLanguage { + color + id name - stargazers { - totalCount - } - description - primaryLanguage { - color - id - name - } - forkCount } - query getRepo($login: String!, $repo: String!) { - user(login: $login) { - repository(name: $repo) { - ...RepoInfo - } - } - organization(login: $login) { - repository(name: $repo) { - ...RepoInfo - } + forkCount + } + query getRepo($login: String!, $repo: String!) { + user(login: $login) { + repository(name: $repo) { + ...RepoInfo } } - `, - variables: { - login: username, - repo: reponame, - }, + organization(login: $login) { + repository(name: $repo) { + ...RepoInfo + } + } + } + `, + variables: { + login: username, + repo: reponame, }, }); diff --git a/src/fetchStats.js b/src/fetchStats.js index f4a92c7..a188ea6 100644 --- a/src/fetchStats.js +++ b/src/fetchStats.js @@ -1,46 +1,37 @@ -const axios = require("axios"); +const { request } = require("./utils"); require("dotenv").config(); async function fetchStats(username) { if (!username) throw Error("Invalid username"); - const res = await axios({ - url: "https://api.github.com/graphql", - method: "post", - headers: { - Authorization: `bearer ${process.env.GITHUB_TOKEN}`, - }, - data: { - query: ` - query userInfo($login: String!) { - user(login: $login) { - name - repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) { - totalCount - } - contributionsCollection { - totalCommitContributions - } - pullRequests(first: 100) { - totalCount - } - issues(first: 100) { - totalCount - } - repositories(first: 100) { - nodes { - stargazers { - totalCount - } + const res = await request({ + query: ` + query userInfo($login: String!) { + user(login: $login) { + name + repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) { + totalCount + } + contributionsCollection { + totalCommitContributions + } + pullRequests(first: 100) { + totalCount + } + issues(first: 100) { + totalCount + } + repositories(first: 100) { + nodes { + stargazers { + totalCount } } } } - `, - variables: { - login: username, - }, - }, + } + `, + variables: { login: username } }); const stats = { diff --git a/src/utils.js b/src/utils.js index 6cd2b50..470a9e8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,3 +1,5 @@ +const axios = require("axios"); + const renderError = (message) => { return ` @@ -31,4 +33,19 @@ function isValidHexColor(hexColor) { ).test(hexColor); } -module.exports = { renderError, kFormatter, encodeHTML, isValidHexColor }; +function request(data) { + return new Promise((resolve, reject) => { + axios({ + url: "https://api.github.com/graphql", + method: "post", + headers: { + Authorization: `bearer ${process.env.GITHUB_TOKEN}`, + }, + data, + }) + .then((response) => resolve(response)) + .catch((error) => reject(error)); + }); +} + +module.exports = { renderError, kFormatter, encodeHTML, isValidHexColor, request };