From 001062db212a0e2c4f4b6d18f767ce19c5966cc3 Mon Sep 17 00:00:00 2001 From: Omid Nikrah Date: Sun, 12 Jul 2020 00:45:58 +0430 Subject: [PATCH 1/2] add request utils + remove duplicate codes --- src/fetchRepo.js | 66 ++++++++++++++++++++--------------------------- src/fetchStats.js | 58 +++++++++++++++++------------------------ src/utils.js | 22 +++++++++++++++- 3 files changed, 72 insertions(+), 74 deletions(-) diff --git a/src/fetchRepo.js b/src/fetchRepo.js index 29434bb..52ab03b 100644 --- a/src/fetchRepo.js +++ b/src/fetchRepo.js @@ -1,49 +1,39 @@ -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 { - name - stargazers { - totalCount - } - description - primaryLanguage { - color - id - name - } - forkCount + const res = await request(` + fragment RepoInfo on Repository { + name + stargazers { + totalCount + } + description + primaryLanguage { + color + id + name + } + forkCount + } + query getRepo($login: String!, $repo: String!) { + user(login: $login) { + repository(name: $repo) { + ...RepoInfo } - query getRepo($login: String!, $repo: String!) { - user(login: $login) { - repository(name: $repo) { - ...RepoInfo - } - } - organization(login: $login) { - repository(name: $repo) { - ...RepoInfo - } - } + } + organization(login: $login) { + repository(name: $repo) { + ...RepoInfo } - `, - variables: { - login: username, - repo: reponame, - }, - }, + } + } + `, { + login: username, + repo: reponame, }); const data = res.data.data; diff --git a/src/fetchStats.js b/src/fetchStats.js index f4a92c7..7a7206a 100644 --- a/src/fetchStats.js +++ b/src/fetchStats.js @@ -1,47 +1,35 @@ -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]) { + const res = await request(` + 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 } - contributionsCollection { - totalCommitContributions - } - pullRequests(first: 100) { - totalCount - } - issues(first: 100) { - totalCount - } - repositories(first: 100) { - nodes { - stargazers { - totalCount - } - } - } } } - `, - variables: { - login: username, - }, - }, - }); + } + } + `, { login: username }); const stats = { name: "", diff --git a/src/utils.js b/src/utils.js index 2b7168e..4926c69 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,3 +1,5 @@ +const axios = require("axios"); + const renderError = (message) => { return ` @@ -25,4 +27,22 @@ function kFormatter(num) { : Math.sign(num) * Math.abs(num); } -module.exports = { renderError, kFormatter, encodeHTML }; +function request(query, variables) { + return new Promise((resolve, reject) => { + axios({ + url: "https://api.github.com/graphql", + method: "post", + headers: { + Authorization: `bearer ${process.env.GITHUB_TOKEN}`, + }, + data: { + query, + variables, + }, + }) + .then((response) => resolve(response)) + .catch((error) => reject(error)); + }); +} + +module.exports = { renderError, kFormatter, encodeHTML, request }; From 208076e7f4866ec230dc661766c1cc06041d5662 Mon Sep 17 00:00:00 2001 From: Omid Nikrah Date: Sun, 12 Jul 2020 12:12:06 +0430 Subject: [PATCH 2/2] update request util --- src/fetchRepo.js | 53 +++++++++++++++++++++++++---------------------- src/fetchStats.js | 47 +++++++++++++++++++++-------------------- src/utils.js | 7 ++----- 3 files changed, 55 insertions(+), 52 deletions(-) diff --git a/src/fetchRepo.js b/src/fetchRepo.js index 52ab03b..c7b7c3e 100644 --- a/src/fetchRepo.js +++ b/src/fetchRepo.js @@ -5,35 +5,38 @@ async function fetchRepo(username, reponame) { throw new Error("Invalid username or reponame"); } - const res = await request(` - fragment RepoInfo on Repository { - name - stargazers { - totalCount - } - description - primaryLanguage { - color - id + const res = await request({ + query: ` + fragment RepoInfo on Repository { name + stargazers { + totalCount + } + description + primaryLanguage { + color + id + name + } + forkCount } - forkCount - } - query getRepo($login: String!, $repo: String!) { - user(login: $login) { - repository(name: $repo) { - ...RepoInfo + query getRepo($login: String!, $repo: String!) { + user(login: $login) { + repository(name: $repo) { + ...RepoInfo + } + } + organization(login: $login) { + repository(name: $repo) { + ...RepoInfo + } } } - organization(login: $login) { - repository(name: $repo) { - ...RepoInfo - } - } - } - `, { - login: username, - repo: reponame, + `, + variables: { + login: username, + repo: reponame, + }, }); const data = res.data.data; diff --git a/src/fetchStats.js b/src/fetchStats.js index 7a7206a..a188ea6 100644 --- a/src/fetchStats.js +++ b/src/fetchStats.js @@ -4,32 +4,35 @@ require("dotenv").config(); async function fetchStats(username) { if (!username) throw Error("Invalid username"); - const res = await request(` - 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 + } } } } } - } - `, { login: username }); + `, + variables: { login: username } + }); const stats = { name: "", diff --git a/src/utils.js b/src/utils.js index 4926c69..d2edebf 100644 --- a/src/utils.js +++ b/src/utils.js @@ -27,7 +27,7 @@ function kFormatter(num) { : Math.sign(num) * Math.abs(num); } -function request(query, variables) { +function request(data) { return new Promise((resolve, reject) => { axios({ url: "https://api.github.com/graphql", @@ -35,10 +35,7 @@ function request(query, variables) { headers: { Authorization: `bearer ${process.env.GITHUB_TOKEN}`, }, - data: { - query, - variables, - }, + data, }) .then((response) => resolve(response)) .catch((error) => reject(error));