Skip to content

Commit dfec3da

Browse files
committed
Use axios instead of github-api
1 parent f76efaf commit dfec3da

File tree

10 files changed

+80
-41
lines changed

10 files changed

+80
-41
lines changed

app/backend.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ if (__DEV__) {
2525
lastHash = stats.hash;
2626
console.info(stats.toString({
2727
cached: false,
28-
colors: true
28+
colors: true,
2929
}));
3030

3131
try {
3232
if (httpServer) httpServer.close();
33-
delete require.cache[require.resolve(backendBuildPath)];
33+
Object.keys(require.cache).forEach(key => delete require.cache[key]);
3434
const app = require(backendBuildPath).default;
3535
httpServer = app.listen(proxyPort);
3636
} catch (e) {

app/frontend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ if (__DEV__) {
2121
app.use(webpackDev(compiler, {
2222
stats: {
2323
cached: false,
24-
colors: true
24+
colors: true,
2525
},
2626
}));
2727
app.use(webpackHot(compiler));

environment.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ const {
88

99
GITHUB_CLIENT_ID,
1010
GITHUB_CLIENT_SECRET,
11-
GITHUB_BOT_USERNAME,
12-
GITHUB_BOT_PASSWORD,
13-
GITHUB_ORG = 'algorithm-visualizer',
14-
GITHUB_REPO = 'algorithms',
1511
} = process.env;
1612

1713
const __PROD__ = NODE_ENV === 'production';
@@ -22,12 +18,6 @@ const proxyPort = parseInt(PROXY_PORT);
2218

2319
const githubClientId = GITHUB_CLIENT_ID;
2420
const githubClientSecret = GITHUB_CLIENT_SECRET;
25-
const githubBotAuth = {
26-
username: GITHUB_BOT_USERNAME,
27-
password: GITHUB_BOT_PASSWORD,
28-
};
29-
const githubOrg = GITHUB_ORG;
30-
const githubRepo = GITHUB_REPO;
3121

3222
const buildPath = path.resolve(__dirname, 'build');
3323
const frontendBuildPath = path.resolve(buildPath, 'frontend');
@@ -46,9 +36,6 @@ module.exports = {
4636
proxyPort,
4737
githubClientId,
4838
githubClientSecret,
49-
githubBotAuth,
50-
githubOrg,
51-
githubRepo,
5239
frontendBuildPath,
5340
backendBuildPath,
5441
frontendSrcPath,

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"eslint-plugin-jsx-a11y": "^6.0.3",
6060
"eslint-plugin-react": "^7.7.0",
6161
"express": "^4.15.4",
62-
"github-api": "^3.0.0",
6362
"html-webpack-plugin": "^3.2.0",
6463
"http-proxy-middleware": "^0.18.0",
6564
"imagemin-webpack-plugin": "^2.1.1",

src/backend/apis/index.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Promise from 'bluebird';
2+
import axios from 'axios';
3+
4+
axios.interceptors.response.use(response => {
5+
return response.data;
6+
}, error => {
7+
return Promise.reject(error.response.data);
8+
});
9+
10+
const request = (url, process) => {
11+
const tokens = url.split('/');
12+
const baseURL = /^https?:\/\//i.test(url) ? '' : 'https://api.github.com';
13+
return (...args) => {
14+
return new Promise((resolve, reject) => {
15+
const mappedURL = baseURL + tokens.map((token, i) => token.startsWith(':') ? args.shift() : token).join('/');
16+
return resolve(process(mappedURL, args));
17+
});
18+
};
19+
};
20+
21+
const GET = URL => {
22+
return request(URL, (mappedURL, args) => {
23+
const [params] = args;
24+
return axios.get(mappedURL, { params });
25+
});
26+
};
27+
28+
const DELETE = URL => {
29+
return request(URL, (mappedURL, args) => {
30+
const [params] = args;
31+
return axios.delete(mappedURL, { params });
32+
});
33+
};
34+
35+
const POST = URL => {
36+
return request(URL, (mappedURL, args) => {
37+
const [body, params] = args;
38+
return axios.post(mappedURL, body, { params });
39+
});
40+
};
41+
42+
const PUT = URL => {
43+
return request(URL, (mappedURL, args) => {
44+
const [body, params] = args;
45+
return axios.put(mappedURL, body, { params });
46+
});
47+
};
48+
49+
const PATCH = URL => {
50+
return request(URL, (mappedURL, args) => {
51+
const [body, params] = args;
52+
return axios.patch(mappedURL, body, { params });
53+
});
54+
};
55+
56+
const GitHubApi = {
57+
auth: (client_id, client_secret) => axios.defaults.params = { client_id, client_secret },
58+
listCommits: GET('/repos/:owner/:repo/commits'),
59+
getAccessToken: code => axios.post('https://github.com/login/oauth/access_token', { code }, { headers: { Accept: 'application/json' } }),
60+
};
61+
62+
export {
63+
GitHubApi,
64+
};

src/backend/common/github.js

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

src/backend/controllers/auth.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from 'express';
2-
import axios from 'axios';
3-
import { githubClientId, githubClientSecret } from '/environment';
2+
import { githubClientId } from '/environment';
3+
import { GitHubApi } from '/apis';
44

55
const router = express.Router();
66

@@ -11,14 +11,7 @@ const request = (req, res, next) => {
1111
const response = (req, res, next) => {
1212
const { code } = req.query;
1313

14-
axios.post('https://github.com/login/oauth/access_token', {
15-
client_id: githubClientId,
16-
client_secret: githubClientSecret,
17-
code,
18-
}, {
19-
headers: { Accept: 'application/json' }
20-
}).then(response => {
21-
const { access_token } = response.data;
14+
GitHubApi.getAccessToken(code).then(({ access_token }) => {
2215
res.cookie('access_token', access_token);
2316
res.redirect('/');
2417
}).catch(next);

src/backend/controllers/categories.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'fs';
33
import path from 'path';
44
import { NotFoundError } from '/common/error';
55
import { exec } from 'child_process';
6-
import { repo } from '/common/github';
6+
import { GitHubApi } from '/apis';
77

88
const router = express.Router();
99

@@ -49,16 +49,16 @@ const cacheCategories = () => {
4949
const categories = list(getPath()).map(cacheCategory);
5050

5151
const per_page = 100;
52-
const cacheCommitAuthors = (page = 1, commitAuthors = {}) => repo.listCommits({
52+
const cacheCommitAuthors = (page = 1, commitAuthors = {}) => GitHubApi.listCommits('algorithm-visualizer', 'algorithms', {
5353
per_page,
5454
page,
55-
}).then(({ data }) => {
56-
data.forEach(({ sha, commit, author }) => {
55+
}).then(commits => {
56+
commits.forEach(({ sha, commit, author }) => {
5757
if (!author) return;
5858
const { login, avatar_url } = author;
5959
commitAuthors[sha] = { login, avatar_url };
6060
});
61-
if (data.length < per_page) {
61+
if (commits.length < per_page) {
6262
return commitAuthors;
6363
} else {
6464
return cacheCommitAuthors(page + 1, commitAuthors);

src/backend/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import cookieParser from 'cookie-parser';
44
import bodyParser from 'body-parser';
55
import * as controllers from '/controllers';
66
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error';
7+
import { GitHubApi } from '/apis';
8+
import { githubClientId, githubClientSecret } from '/environment';
79

10+
GitHubApi.auth(githubClientId, githubClientSecret);
811
const app = express();
912
app.use(morgan('tiny'));
1013
app.use(cookieParser());

src/backend/public/libs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

0 commit comments

Comments
 (0)