Skip to content

Commit 9f14644

Browse files
committed
refactor(service): use octokit
1 parent 5996462 commit 9f14644

File tree

5 files changed

+48
-72
lines changed

5 files changed

+48
-72
lines changed

public/js/chart.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/* eslint-disable no-useless-escape */
22
/* eslint-disable no-undef */
33
/* eslint-disable no-unused-vars */
4-
const { MAX_REPOS_PER_CHART } = require('../../src/config');
5-
64
const sum = (array) => {
75
if (array.length === 0) {
86
return 0;
@@ -58,7 +56,7 @@ const chart = ({ limit, singleColor }) => {
5856
}
5957
const labels = keys.slice(0, limit);
6058
const data = values.slice(0, limit);
61-
if (keys.length > MAX_REPOS_PER_CHART) {
59+
if (keys.length > 20) {
6260
const valueOthers = sum(values.slice(limit, keys.length));
6361
labels.push('Others');
6462
data.push(valueOthers);
@@ -135,7 +133,7 @@ const chart = ({ limit, singleColor }) => {
135133
const toggleColor = () => {
136134
window.chart.destroy();
137135
// eslint-disable-next-line no-undef
138-
singleColor = chart({ limit: MAX_REPOS_PER_CHART, singleColor: !singleColor });
136+
singleColor = chart({ limit: 20, singleColor: !singleColor });
139137
const btn = document.getElementById('btn-toggle');
140138

141139
btn.classList.toggle('active');

src/api/controller.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,31 @@ const axios = require('axios');
55

66
// config related variables
77
const {
8-
GITHUB_API_URL,
9-
CLIENT_ID_AND_CLIENT_SECRET_MISSING_ERROR_MESSAGE,
108
REPOS_PER_PAGE,
119
MAX_REPOS_PER_CHART,
1210
RANDOM_STATEMENTS,
13-
MISTERY_STATEMENTS
11+
MISTERY_STATEMENTS,
12+
TOKEN_MISSING_ERROR_MESSAGE
1413
} = require('../config');
1514

1615
// load utils
1716
const { emojiGenerator } = require('../utils/emoji-generator');
1817
const { getColorsForLanguages } =require('../utils/language-colors-generator');
1918

2019

21-
const client_id = process.env.CLIENT_ID || false;
22-
const client_secret = process.env.CLIENT_SECRET || false;
23-
if (!(client_id && client_secret)) {
24-
throw CLIENT_ID_AND_CLIENT_SECRET_MISSING_ERROR_MESSAGE;
20+
const token = process.env.TOKEN || false;
21+
if (!token) {
22+
throw TOKEN_MISSING_ERROR_MESSAGE;
2523
}
2624

27-
// load service
28-
const GithubService = require('./service');
29-
const githubService = new GithubService(GITHUB_API_URL, client_id, client_secret);
25+
// initialize service
26+
const OctokitService = require('./octokit');
27+
const octokitService = new OctokitService(token);
3028

3129
const reqRepos = async (username, numberOfPages) => {
3230
const requests = [];
3331
for (let i = 1; i <= numberOfPages; i++) {
34-
requests.push(await githubService.getReposForUser(username, i));
32+
requests.push(await octokitService.getReposForUser(username, i));
3533
}
3634
return requests;
3735
};
@@ -63,9 +61,9 @@ exports.index = async (req, res) => {
6361
return;
6462
}
6563

66-
await githubService.getUser(username)
64+
await octokitService.getUser(username)
6765
.then(async (user) => {
68-
const userData = user.data;
66+
const userData = user;
6967
const numberOfRepos = userData.public_repos;
7068
const fetchedRenderValue = {
7169
avatar: userData.avatar_url,

src/api/octokit.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const { Octokit } = require('octokit');
4+
const { REPOS_PER_PAGE } = require('../config');
5+
6+
class OctokitService {
7+
constructor(token) {
8+
this.token = token,
9+
this.octokit = new Octokit({
10+
auth: this.token
11+
});
12+
}
13+
14+
async getUser(username) {
15+
try {
16+
const { data } = await this.octokit.request(`GET /users/${username}`);
17+
return data;
18+
} catch (e) {
19+
throw e;
20+
}
21+
}
22+
23+
async getReposForUser(username, page) {
24+
try {
25+
const data = await this.octokit.request(`GET /users/${username}/repos?per_page=${REPOS_PER_PAGE}&page=${page}`);
26+
return data;
27+
} catch (e) {
28+
throw e;
29+
}
30+
}
31+
}
32+
33+
module.exports = OctokitService;

src/api/service.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/config.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
const GITHUB_API_URL = 'https://api.github.com';
2-
31
const REPOS_PER_PAGE = 100;
42
const MAX_REPOS_PER_CHART = 20;
53

64
const ENV_MISSING_ERROR_MESSAGE = 'Missing ENV environment variables';
7-
const CLIENT_ID_AND_CLIENT_SECRET_MISSING_ERROR_MESSAGE = 'Missing CLIENT_ID and CLIENT_SECRET environment variables';
5+
const TOKEN_MISSING_ERROR_MESSAGE = 'Missing token';
86

97
const RANDOM_STATEMENTS = [
108
'You\'ve got skills on',
@@ -22,11 +20,10 @@ const MISTERY_STATEMENTS = [
2220
];
2321

2422
module.exports = {
25-
GITHUB_API_URL,
2623
REPOS_PER_PAGE,
2724
MAX_REPOS_PER_CHART,
2825
ENV_MISSING_ERROR_MESSAGE,
29-
CLIENT_ID_AND_CLIENT_SECRET_MISSING_ERROR_MESSAGE,
26+
TOKEN_MISSING_ERROR_MESSAGE,
3027
RANDOM_STATEMENTS,
3128
MISTERY_STATEMENTS
3229
};

0 commit comments

Comments
 (0)