Skip to content

Commit

Permalink
😆 把state抽离出去 >_<#@!
Browse files Browse the repository at this point in the history
  • Loading branch information
shalldie committed May 9, 2018
1 parent 4106624 commit d3be6a6
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 136 deletions.
59 changes: 21 additions & 38 deletions src/git-comment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import state from './state';
import { appendQuery, getQuery } from "./lib/utils";
import http from "./lib/http";
import * as github from './lib/github';

const STORAGE_TOKEN_KEY = 'STORAGE_TOKEN_KEY';

Expand All @@ -11,26 +12,15 @@ class GitComment {
*/
config(options) {
const { client_id, client_secret } = options;
this.client_id = client_id;
this.client_secret = client_secret;
state.client_id = client_id;
state.client_secret = client_secret;
this.init();
}

//#region fields

client_id = ''

client_secret = ''

access_token = ''

/**
* 是否登陆
*
* @memberof GitHub
*/
ifLogin = false

state = state

//#endregion

//#region private methods
Expand Down Expand Up @@ -67,16 +57,12 @@ class GitComment {
* @memberof GitComment
*/
_getToken(code) {
http.post('https://github.com/login/oauth/access_token', {
client_id: this.client_id,
client_secret: this.client_secret,
code
}, true)
.then(body => {
let token = getQuery(body, 'access_token');
let replaceUrl = getQuery(window.location.search, 'state');
replaceUrl = decodeURIComponent(replaceUrl);

github.getToken(state.client_id, state.client_secret, code)
.then(token => {
this._updateToken(token);
let replaceUrl = getQuery(window.location.search, 'state');
replaceUrl = decodeURIComponent(replaceUrl);
window.history.replaceState(null, null, replaceUrl);
})
.catch(err => console.log(err));
Expand All @@ -90,14 +76,14 @@ class GitComment {
*/
_updateToken(token) {
if (token && token.length) {
this.ifLogin = true;
this.access_token = token;
state.ifLogin = true;
state.access_token = token;
}
else {
this.ifLogin = false;
this.access_token = '';
state.ifLogin = false;
state.access_token = '';
}
window.localStorage.setItem(STORAGE_TOKEN_KEY, this.access_token);
window.localStorage.setItem(STORAGE_TOKEN_KEY, state.access_token);
}

//#endregion
Expand All @@ -110,14 +96,11 @@ class GitComment {
* @memberof GitHub
*/
login() {
let url = 'https://github.com/login/oauth/authorize';
url = appendQuery(url, {
client_id: this.client_id,
redirect_uri: window.location.href,
scope: 'public_repo',
state: window.location.href
});
window.location.href = url;
github.toAuthorize(state.client_id);
}

getUserInfo() {
github.getAuthUser().then(body => console.log(body));
}

//#endregion
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ import gitComment from './git-comment';

gitComment.config(auth);

if (!gitComment.ifLogin) {
if (!gitComment.state.ifLogin) {
// gitComment.login();
}
else {
gitComment.getUserInfo();
}

window.gitComment = gitComment;
console.log(gitComment);
126 changes: 67 additions & 59 deletions src/lib/github.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,73 @@
import { appendQuery } from './utils';

class GitHub {

constructor(options) {
const { client_id, client_secret } = options;
this.client_id = client_id;
this.client_secret = client_secret;
}

client_id = ''

client_secret = ''

/**
* 是否登陆
*
* @memberof GitHub
*/
ifLogin = false

/**
* 去github获取权限
*
* @param {string} client_id
* @memberof GitHub
*/
login(client_id) {
let url = 'https://github.com/login/oauth/authorize';
url = appendQuery(url, {
client_id,
redirect_uri: window.location.href,
scope: 'public_repo'
});
window.location.href = url;
}
import http from './http';
import { getQuery, appendQuery } from "./utils";
import state from '../state';

/**
* 找到第一个符合的issue
*
* @export
* @param {string} owner 仓库所有者
* @param {string} repo 仓库名称
* @param {string} labels labels 用逗号分隔
* @returns {any>}
*/
export function getFirstIssue(owner, repo, labels) {
return http.get(`/repos/${owner}/${repo}/issues`, {
creator: owner,
labels
}).then(body => JSON.parse(body)[0]);
}

const github = {
/**
* 是否登陆
*/
ifLogin: false,
/**
*
*
* @export
* @param {string} owner
* @param {string} repo
* @param {number} number
* @returns
*/
export function getIssue(owner, repo, number) {
return http.get(`/repos/${owner}/${repo}/issues/${number}`)
.then(body => JSON.parse(body));
}

token: '',
/**
* 获取token
*
* @export
* @param {string} client_id
* @param {string} client_secret
* @param {string} code
* @returns {Promise<string>}
*/
export function getToken(client_id, client_secret, code) {
return http
.post('https://github.com/login/oauth/access_token', {
client_id: state.client_id,
client_secret: state.client_secret,
code
}, true)
.then(body => getQuery(body, 'access_token'));
}

/**
* 去github获取权限
*
* @param {string} client_id
*/
login(client_id) {
let url = 'https://github.com/login/oauth/authorize';
url = appendQuery(url, {
client_id,
redirect_uri: window.location.href,
scope: 'public_repo'
});
window.location.href = url;
console.log(url);
}
};
/**
* 跳转去认证
*
* @export
* @param {string} client_id
*/
export function toAuthorize(client_id) {
let url = 'https://github.com/login/oauth/authorize';
url = appendQuery(url, {
client_id: client_id,
redirect_uri: window.location.href,
scope: 'public_repo',
state: window.location.href
});
window.location.href = url;
}

export default github;
export function getAuthUser() {
return http.get('/user');
}
6 changes: 3 additions & 3 deletions src/lib/http.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Deferred from './Deferred';
import gitComment from '../git-comment';
import state from '../state';
import { stringifyQuery, appendQuery } from "./utils";

const baseUrl = 'https://api.github.com';
Expand Down Expand Up @@ -41,8 +41,8 @@ function ajax(method, url, data = {}, proxy = false) {

xh.open(method, url, true);
// xh.setRequestHeader('Accept', 'application/vnd.github.symmetra-preview+json');
if (gitComment.access_token) {
xh.setRequestHeader('Authorization', `token ${gitComment.access_token}`);
if (state.access_token) {
xh.setRequestHeader('Authorization', `token ${state.access_token}`);
}
if (method === 'POST') {
xh.setRequestHeader('Content-Type', 'application/json');
Expand Down
35 changes: 0 additions & 35 deletions src/lib/issue.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* 当前状态和全局数据
*/
export default {
client_id: '',

client_secret: '',

access_token: '',

/**
* 是否登录
*/
ifLogin: false
};

0 comments on commit d3be6a6

Please sign in to comment.