add request utils + remove duplicate codes

This commit is contained in:
Omid Nikrah 2020-07-12 00:45:58 +04:30
parent c25319ee6c
commit 001062db21
3 changed files with 72 additions and 74 deletions

View File

@ -1,49 +1,39 @@
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", fragment RepoInfo on Repository {
method: "post", name
headers: { stargazers {
Authorization: `bearer ${process.env.GITHUB_TOKEN}`, totalCount
}, }
data: { description
query: ` primaryLanguage {
fragment RepoInfo on Repository { color
name id
stargazers { name
totalCount }
} forkCount
description }
primaryLanguage { query getRepo($login: String!, $repo: String!) {
color user(login: $login) {
id repository(name: $repo) {
name ...RepoInfo
}
forkCount
} }
query getRepo($login: String!, $repo: String!) { }
user(login: $login) { organization(login: $login) {
repository(name: $repo) { repository(name: $repo) {
...RepoInfo ...RepoInfo
}
}
organization(login: $login) {
repository(name: $repo) {
...RepoInfo
}
}
} }
`, }
variables: { }
login: username, `, {
repo: reponame, login: username,
}, repo: reponame,
},
}); });
const data = res.data.data; const data = res.data.data;

View File

@ -1,47 +1,35 @@
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 userInfo($login: String!) {
method: "post", user(login: $login) {
headers: { name
Authorization: `bearer ${process.env.GITHUB_TOKEN}`, repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) {
}, totalCount
data: { }
query: ` contributionsCollection {
query userInfo($login: String!) { totalCommitContributions
user(login: $login) { }
name pullRequests(first: 100) {
repositoriesContributedTo(first: 100, contributionTypes: [COMMIT, ISSUE, PULL_REQUEST, REPOSITORY]) { totalCount
}
issues(first: 100) {
totalCount
}
repositories(first: 100) {
nodes {
stargazers {
totalCount totalCount
} }
contributionsCollection {
totalCommitContributions
}
pullRequests(first: 100) {
totalCount
}
issues(first: 100) {
totalCount
}
repositories(first: 100) {
nodes {
stargazers {
totalCount
}
}
}
} }
} }
`, }
variables: { }
login: username, `, { login: username });
},
},
});
const stats = { const stats = {
name: "", name: "",

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">
@ -25,4 +27,22 @@ function kFormatter(num) {
: Math.sign(num) * Math.abs(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 };