Merge pull request #29 from omidnikrah/master

refactor: added request helper function to reduce code duplication
This commit is contained in:
Anurag Hazra 2020-07-12 14:37:20 +05:30 committed by GitHub
commit 3e9ce48777
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 70 deletions

View file

@ -1,48 +1,41 @@
const axios = require("axios"); const { request } = require("./utils");
async function fetchRepo(username, reponame) { async function fetchRepo(username, reponame) {
if (!username || !reponame) { if (!username || !reponame) {
throw new Error("Invalid username or reponame"); throw new Error("Invalid username or reponame");
} }
const res = await axios({ const res = await request({
url: "https://api.github.com/graphql", query: `
method: "post", fragment RepoInfo on Repository {
headers: { name
Authorization: `bearer ${process.env.GITHUB_TOKEN}`, stargazers {
}, totalCount
data: { }
query: ` description
fragment RepoInfo on Repository { primaryLanguage {
color
id
name name
stargazers {
totalCount
}
description
primaryLanguage {
color
id
name
}
forkCount
} }
query getRepo($login: String!, $repo: String!) { forkCount
user(login: $login) { }
repository(name: $repo) { query getRepo($login: String!, $repo: String!) {
...RepoInfo user(login: $login) {
} repository(name: $repo) {
} ...RepoInfo
organization(login: $login) {
repository(name: $repo) {
...RepoInfo
}
} }
} }
`, organization(login: $login) {
variables: { repository(name: $repo) {
login: username, ...RepoInfo
repo: reponame, }
}, }
}
`,
variables: {
login: username,
repo: reponame,
}, },
}); });

View file

@ -1,46 +1,37 @@
const axios = require("axios"); const { request } = require("./utils");
require("dotenv").config(); require("dotenv").config();
async function fetchStats(username) { async function fetchStats(username) {
if (!username) throw Error("Invalid username"); if (!username) throw Error("Invalid username");
const res = await axios({ const res = await request({
url: "https://api.github.com/graphql", query: `
method: "post", query userInfo($login: String!) {
headers: { user(login: $login) {
Authorization: `bearer ${process.env.GITHUB_TOKEN}`, name
}, repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
data: { totalCount
query: ` }
query userInfo($login: String!) { contributionsCollection {
user(login: $login) { totalCommitContributions
name }
repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) { pullRequests(first: 100) {
totalCount totalCount
} }
contributionsCollection { issues(first: 100) {
totalCommitContributions totalCount
} }
pullRequests(first: 100) { repositories(first: 100) {
totalCount nodes {
} stargazers {
issues(first: 100) { totalCount
totalCount
}
repositories(first: 100) {
nodes {
stargazers {
totalCount
}
} }
} }
} }
} }
`, }
variables: { `,
login: username, variables: { login: username }
},
},
}); });
const stats = { const stats = {

View file

@ -1,3 +1,5 @@
const axios = require("axios");
const renderError = (message) => { const renderError = (message) => {
return ` return `
<svg width="495" height="100" viewBox="0 0 495 100" fill="none" xmlns="http://www.w3.org/2000/svg"> <svg width="495" height="100" viewBox="0 0 495 100" fill="none" xmlns="http://www.w3.org/2000/svg">
@ -31,4 +33,19 @@ function isValidHexColor(hexColor) {
).test(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 };