Unverified Commit 3e9ce487 authored by Anurag Hazra's avatar Anurag Hazra Committed by GitHub
Browse files

Merge pull request #29 from omidnikrah/master

refactor: added request helper function to reduce code duplication
parents 5f4ada0a 7a7ad717
Loading
Loading
Loading
Loading
+27 −34
Original line number Diff line number Diff line
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: {
  const res = await request({
    query: `
      fragment RepoInfo on Repository {
        name
@@ -43,7 +37,6 @@ async function fetchRepo(username, reponame) {
      login: username,
      repo: reponame,
    },
    },
  });

  const data = res.data.data;
+25 −34
Original line number Diff line number Diff line
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: {
  const res = await request({
    query: `
      query userInfo($login: String!) {
        user(login: $login) {
@@ -37,10 +31,7 @@ async function fetchStats(username) {
        }
      }
    `,
      variables: {
        login: username,
      },
    },
    variables: { login: username }
  });

  const stats = {
+18 −1
Original line number Diff line number Diff line
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">
@@ -31,4 +33,19 @@ function isValidHexColor(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 };