Skip to content

Commit

Permalink
add request utils + remove duplicate codes
Browse files Browse the repository at this point in the history
  • Loading branch information
omidnikrah committed Jul 11, 2020
1 parent c25319e commit 001062d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 74 deletions.
66 changes: 28 additions & 38 deletions src/fetchRepo.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
58 changes: 23 additions & 35 deletions src/fetchStats.js
Original file line number Diff line number Diff line change
@@ -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]) {
totalCount
}
contributionsCollection {
totalCommitContributions
}
pullRequests(first: 100) {
totalCount
}
issues(first: 100) {
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
}
repositories(first: 100) {
nodes {
stargazers {
totalCount
}
}
}
}
}
`,
variables: {
login: username,
},
},
});
}
}
`, { login: username });

const stats = {
name: "",
Expand Down
22 changes: 21 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const axios = require("axios");

const renderError = (message) => {
return `
<svg width="495" height="100" viewBox="0 0 495 100" fill="none" xmlns="http://www.w3.org/2000/svg">
Expand Down Expand Up @@ -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 };

0 comments on commit 001062d

Please sign in to comment.