提交 dfec3da6 编写于 作者: J Jason Park

Use axios instead of github-api

上级 f76efaf9
......@@ -25,12 +25,12 @@ if (__DEV__) {
lastHash = stats.hash;
console.info(stats.toString({
cached: false,
colors: true
colors: true,
}));
try {
if (httpServer) httpServer.close();
delete require.cache[require.resolve(backendBuildPath)];
Object.keys(require.cache).forEach(key => delete require.cache[key]);
const app = require(backendBuildPath).default;
httpServer = app.listen(proxyPort);
} catch (e) {
......
......@@ -21,7 +21,7 @@ if (__DEV__) {
app.use(webpackDev(compiler, {
stats: {
cached: false,
colors: true
colors: true,
},
}));
app.use(webpackHot(compiler));
......
......@@ -8,10 +8,6 @@ const {
GITHUB_CLIENT_ID,
GITHUB_CLIENT_SECRET,
GITHUB_BOT_USERNAME,
GITHUB_BOT_PASSWORD,
GITHUB_ORG = 'algorithm-visualizer',
GITHUB_REPO = 'algorithms',
} = process.env;
const __PROD__ = NODE_ENV === 'production';
......@@ -22,12 +18,6 @@ const proxyPort = parseInt(PROXY_PORT);
const githubClientId = GITHUB_CLIENT_ID;
const githubClientSecret = GITHUB_CLIENT_SECRET;
const githubBotAuth = {
username: GITHUB_BOT_USERNAME,
password: GITHUB_BOT_PASSWORD,
};
const githubOrg = GITHUB_ORG;
const githubRepo = GITHUB_REPO;
const buildPath = path.resolve(__dirname, 'build');
const frontendBuildPath = path.resolve(buildPath, 'frontend');
......@@ -46,9 +36,6 @@ module.exports = {
proxyPort,
githubClientId,
githubClientSecret,
githubBotAuth,
githubOrg,
githubRepo,
frontendBuildPath,
backendBuildPath,
frontendSrcPath,
......
import Promise from 'bluebird';
import axios from 'axios';
axios.interceptors.response.use(response => {
return response.data;
}, error => {
return Promise.reject(error.response.data);
});
const request = (url, process) => {
const tokens = url.split('/');
const baseURL = /^https?:\/\//i.test(url) ? '' : 'https://api.github.com';
return (...args) => {
return new Promise((resolve, reject) => {
const mappedURL = baseURL + tokens.map((token, i) => token.startsWith(':') ? args.shift() : token).join('/');
return resolve(process(mappedURL, args));
});
};
};
const GET = URL => {
return request(URL, (mappedURL, args) => {
const [params] = args;
return axios.get(mappedURL, { params });
});
};
const DELETE = URL => {
return request(URL, (mappedURL, args) => {
const [params] = args;
return axios.delete(mappedURL, { params });
});
};
const POST = URL => {
return request(URL, (mappedURL, args) => {
const [body, params] = args;
return axios.post(mappedURL, body, { params });
});
};
const PUT = URL => {
return request(URL, (mappedURL, args) => {
const [body, params] = args;
return axios.put(mappedURL, body, { params });
});
};
const PATCH = URL => {
return request(URL, (mappedURL, args) => {
const [body, params] = args;
return axios.patch(mappedURL, body, { params });
});
};
const GitHubApi = {
auth: (client_id, client_secret) => axios.defaults.params = { client_id, client_secret },
listCommits: GET('/repos/:owner/:repo/commits'),
getAccessToken: code => axios.post('https://github.com/login/oauth/access_token', { code }, { headers: { Accept: 'application/json' } }),
};
export {
GitHubApi,
};
\ No newline at end of file
import GitHub from 'github-api';
import { githubBotAuth, githubOrg, githubRepo } from '/environment';
const gh = new GitHub(githubBotAuth);
const repo = gh.getRepo(githubOrg, githubRepo);
export {
repo,
};
\ No newline at end of file
import express from 'express';
import axios from 'axios';
import { githubClientId, githubClientSecret } from '/environment';
import { githubClientId } from '/environment';
import { GitHubApi } from '/apis';
const router = express.Router();
......@@ -11,14 +11,7 @@ const request = (req, res, next) => {
const response = (req, res, next) => {
const { code } = req.query;
axios.post('https://github.com/login/oauth/access_token', {
client_id: githubClientId,
client_secret: githubClientSecret,
code,
}, {
headers: { Accept: 'application/json' }
}).then(response => {
const { access_token } = response.data;
GitHubApi.getAccessToken(code).then(({ access_token }) => {
res.cookie('access_token', access_token);
res.redirect('/');
}).catch(next);
......
......@@ -3,7 +3,7 @@ import fs from 'fs';
import path from 'path';
import { NotFoundError } from '/common/error';
import { exec } from 'child_process';
import { repo } from '/common/github';
import { GitHubApi } from '/apis';
const router = express.Router();
......@@ -49,16 +49,16 @@ const cacheCategories = () => {
const categories = list(getPath()).map(cacheCategory);
const per_page = 100;
const cacheCommitAuthors = (page = 1, commitAuthors = {}) => repo.listCommits({
const cacheCommitAuthors = (page = 1, commitAuthors = {}) => GitHubApi.listCommits('algorithm-visualizer', 'algorithms', {
per_page,
page,
}).then(({ data }) => {
data.forEach(({ sha, commit, author }) => {
}).then(commits => {
commits.forEach(({ sha, commit, author }) => {
if (!author) return;
const { login, avatar_url } = author;
commitAuthors[sha] = { login, avatar_url };
});
if (data.length < per_page) {
if (commits.length < per_page) {
return commitAuthors;
} else {
return cacheCommitAuthors(page + 1, commitAuthors);
......
......@@ -4,7 +4,10 @@ import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import * as controllers from '/controllers';
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error';
import { GitHubApi } from '/apis';
import { githubClientId, githubClientSecret } from '/environment';
GitHubApi.auth(githubClientId, githubClientSecret);
const app = express();
app.use(morgan('tiny'));
app.use(cookieParser());
......
*
!.gitignore
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册