From a4c3fd260d8cdcfd459e9a83db367ed5faea2ca1 Mon Sep 17 00:00:00 2001 From: Bobby Date: Tue, 15 Mar 2022 21:06:31 -0400 Subject: repository page with pagination + user api + custom cursor and icons --- api/github.js | 91 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 16 deletions(-) (limited to 'api/github.js') diff --git a/api/github.js b/api/github.js index d08663f..318d084 100644 --- a/api/github.js +++ b/api/github.js @@ -1,21 +1,80 @@ -const { Octokit } = require("@octokit/rest"); +const { Octokit: OctokitRest } = require("@octokit/rest"); +const fetch = (...args) => + import("node-fetch").then(({ default: fetch }) => fetch(...args)); + +require("dotenv").config(); class Github { - octokit = null; - username = null; - constructor(username) { - this.octokit = new Octokit(); - this.username = username; - } - async getRepos(page) { - const { data } = await this.octokit.repos.listForUser({ - username: this.username, - type: "all", - sort: "updated", - per_page: 25, - page: page + octokit = null; + username = null; + constructor(username) { + this.octokitRest = new OctokitRest({ + auth: process.env.GITHUB_TOKEN, + }); + this.username = username; + } + async getRepos(page) { + const { data } = await this.octokitRest.repos.listForUser({ + username: this.username, + type: "all", + sort: "updated", + per_page: 10, + page: page, + }); + return data; + } + + async getUserDetails() { + const { data } = await this.octokitRest.users.getByUsername({ + username: this.username, + }); + return data; + } + + async getOneYearUserContributions() { + const body = { + query: `query { + user(login: "${this.username}") { + name + contributionsCollection { + contributionCalendar { + colors + totalContributions + weeks { + contributionDays { + color + contributionCount + date + weekday + } + firstDay + } + } + } + } + }`, + }; + const headers = { + Authorization: `bearer ${process.env.GITHUB_TOKEN}`, + }; + const response = await fetch("https://api.github.com/graphql", { + method: "POST", + body: JSON.stringify(body), + headers: headers, + }); + const data = await response.json(); + const contributionData = []; + data.data.user.contributionsCollection.contributionCalendar.weeks.forEach( + (week) => { + let weeklyContributionCount = 0; + + week.contributionDays.forEach((day) => { + weeklyContributionCount += day.contributionCount; }); - return data; - } + contributionData.push(weeklyContributionCount); + } + ); + return contributionData; + } } exports.Github = Github; -- cgit v1.2.3