add request utils + remove duplicate codes
This commit is contained in:
parent
c25319ee6c
commit
001062db21
3 changed files with 72 additions and 74 deletions
|
@ -1,18 +1,11 @@
|
|||
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: `
|
||||
const res = await request(`
|
||||
fragment RepoInfo on Repository {
|
||||
name
|
||||
stargazers {
|
||||
|
@ -38,12 +31,9 @@ async function fetchRepo(username, reponame) {
|
|||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
`, {
|
||||
login: username,
|
||||
repo: reponame,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const data = res.data.data;
|
||||
|
|
|
@ -1,17 +1,10 @@
|
|||
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: `
|
||||
const res = await request(`
|
||||
query userInfo($login: String!) {
|
||||
user(login: $login) {
|
||||
name
|
||||
|
@ -36,12 +29,7 @@ async function fetchStats(username) {
|
|||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
variables: {
|
||||
login: username,
|
||||
},
|
||||
},
|
||||
});
|
||||
`, { login: username });
|
||||
|
||||
const stats = {
|
||||
name: "",
|
||||
|
|
22
src/utils.js
22
src/utils.js
|
@ -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">
|
||||
|
@ -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 };
|
||||
|
|
Loading…
Add table
Reference in a new issue