aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-03-15 21:06:31 -0400
committerBobby <[email protected]>2022-03-15 21:06:31 -0400
commita4c3fd260d8cdcfd459e9a83db367ed5faea2ca1 (patch)
tree3cb483cde254007967b666e6c4eee950f2c7dbd9 /api
parent92932847a646b31e8bb76fd5beb6c4a33a0fa8d4 (diff)
downloadluciferreeves.github.io-a4c3fd260d8cdcfd459e9a83db367ed5faea2ca1.tar.xz
luciferreeves.github.io-a4c3fd260d8cdcfd459e9a83db367ed5faea2ca1.zip
repository page with pagination + user api + custom cursor and icons
Diffstat (limited to 'api')
-rw-r--r--api/github.js91
1 files changed, 75 insertions, 16 deletions
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;