Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 82bc79e

Browse files
committed
refactor: gitHub api enhance (#553)
* refactor(rename): githubApi -> githubAPI * refactor(github-api): use github origial reame api * refactor(github-api): remove useless jsonp query lib
1 parent e55e195 commit 82bc79e

File tree

13 files changed

+57
-50
lines changed

13 files changed

+57
-50
lines changed

containers/AvatarAdder/logic.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import R from 'ramda'
22
import { useEffect } from 'react'
33

44
import { buildLog } from '@utils'
5-
import { githubApi } from '@services'
5+
import { githubAPI } from '@services'
66

77
/* eslint-disable-next-line */
88
const log = buildLog('L:AvatarAdder')
@@ -14,13 +14,13 @@ export const onSearch = e => {
1414
log('store.searchValue: ', store.searchValue)
1515
store.mark({ searching: true, searchValue: e.target.value })
1616

17-
githubApi
17+
githubAPI
1818
.searchUser(store.searchValue)
1919
.then(res => {
20-
store.mark({ githubUser: githubApi.transformUser(res) })
20+
store.mark({ githubUser: githubAPI.transformUser(res) })
2121
store.mark({ searching: false })
2222
})
23-
.catch(e => store.handleError(githubApi.parseError(e)))
23+
.catch(e => store.handleError(githubAPI.parseError(e)))
2424
}
2525
}
2626

containers/CheatsheetThread/logic.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Prism from 'mastani-codehighlight'
55
import { TYPE, EVENT, ERR, THREAD } from '@constant'
66
import { asyncSuit, buildLog, errRescue, BStore, nilOrEmpty } from '@utils'
77

8-
import { githubApi } from '@services'
8+
import { githubAPI } from '@services'
99
import S from './schema'
1010

1111
/* eslint-disable-next-line */
@@ -45,15 +45,15 @@ export const syncCheetsheetFromGithub = () => {
4545
return store.mark({ showSyncWarning: true })
4646
}
4747

48-
githubApi
48+
githubAPI
4949
.searchCheatsheet(store.curCommunity.raw)
5050
.then(res => {
5151
if (!res || R.startsWith('404', res))
5252
return store.mark({ curView: TYPE.NOT_FOUND })
5353

5454
syncCheatsheet(res)
5555
})
56-
.catch(e => store.handleError(githubApi.parseError(e)))
56+
.catch(e => store.handleError(githubAPI.parseError(e)))
5757
}
5858

5959
export const addContributor = user => {

containers/RepoEditor/logic.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
BStore,
1111
errRescue,
1212
} from '@utils'
13-
import { githubApi } from '@services'
13+
import { githubAPI } from '@services'
1414

1515
import S from './schema'
1616

@@ -41,16 +41,16 @@ export const onGithubSearch = () => {
4141
const { owner, name } = store
4242

4343
store.mark({ searching: true })
44-
githubApi
44+
githubAPI
4545
.searchRepo(owner, name)
4646
.then(res => {
4747
store.mark({
48-
editRepo: githubApi.transformRepo(res),
48+
editRepo: githubAPI.transformRepo(res),
4949
searching: false,
5050
curView: 'show',
5151
})
5252
})
53-
.catch(e => store.handleError(githubApi.parseError(e)))
53+
.catch(e => store.handleError(githubAPI.parseError(e)))
5454
}
5555

5656
/* eslint-disable-next-line */

containers/RepoViewer/logic.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useEffect } from 'react'
33

44
import { TYPE, EVENT, ERR } from '@constant'
55
import { asyncSuit, buildLog, errRescue } from '@utils'
6-
import { githubApi } from '@services'
6+
import { githubAPI } from '@services'
77

88
import S from './schema'
99

@@ -62,12 +62,12 @@ const DataSolver = [
6262
log('should sync repo: ', store.viewingData)
6363
const { id, ownerName, title } = store.viewingData
6464

65-
githubApi
65+
githubAPI
6666
.searchRepo(ownerName, title)
6767
.then(res =>
68-
sr71$.mutate(S.updateRepo, { id, ...githubApi.transformRepo(res) })
68+
sr71$.mutate(S.updateRepo, { id, ...githubAPI.transformRepo(res) })
6969
)
70-
.catch(e => store.handleError(githubApi.parseError(e)))
70+
.catch(e => store.handleError(githubAPI.parseError(e)))
7171
},
7272
},
7373
]

containers/WikiThread/logic.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useEffect } from 'react'
33

44
import { TYPE, EVENT, ERR, THREAD } from '@constant'
55
import { asyncSuit, buildLog, errRescue, BStore, nilOrEmpty } from '@utils'
6-
import { githubApi } from '@services'
6+
import { githubAPI } from '@services'
77

88
import S from './schema'
99

@@ -43,14 +43,14 @@ export const syncWikiFromGithub = () => {
4343
return store.mark({ showSyncWarning: true })
4444
}
4545

46-
githubApi
46+
githubAPI
4747
.searchWiki(store.curCommunity.raw)
4848
.then(res => {
4949
if (!res || R.startsWith('404', res))
5050
return store.mark({ curView: TYPE.NOT_FOUND })
5151
syncWiki(res)
5252
})
53-
.catch(e => store.handleError(githubApi.parseError(e)))
53+
.catch(e => store.handleError(githubAPI.parseError(e)))
5454
}
5555

5656
export const addContributor = user => {

package-docker.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"draft-js-mention-plugin": "3.1.3",
3838
"draft-js-plugins-editor": "2.1.1",
3939
"express": "^4.16.4",
40-
"fetch-jsonp": "^1.1.3",
4140
"glob": "^7.1.2",
4241
"graphql": "14.3.1",
4342
"graphql-request": "^1.6.0",

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
"draft-js-mention-plugin": "3.1.3",
6565
"draft-js-plugins-editor": "2.1.1",
6666
"express": "^4.16.4",
67-
"fetch-jsonp": "^1.1.3",
6867
"glob": "^7.1.2",
6968
"graphql": "14.4.0",
7069
"graphql-request": "^1.6.0",

services/github_api/client.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1-
import fetchJsonp from 'fetch-jsonp'
21
import fetch from 'isomorphic-fetch'
32

43
import { BStore, makeGithubExplore } from '@utils'
54
import { graphqlEndpoint } from './config'
65

76
const token = BStore.get('github_token')
87

9-
const v3EndpointOpt = {
10-
headers: { Authorization: `token ${token}` },
8+
const rawOptions = {
9+
headers: {
10+
Authorization: `token ${token}`,
11+
Accept: 'application/vnd.github.VERSION.raw',
12+
'Content-Type': 'application/vnd.github.VERSION.raw',
13+
},
14+
}
15+
16+
const jsonOptions = {
17+
headers: {
18+
Authorization: `token ${token}`,
19+
Accept: 'application/json',
20+
'Content-Type': 'application/json',
21+
},
1122
}
1223

1324
// graphql client
1425
export const graphqlClient = makeGithubExplore(graphqlEndpoint, token)
15-
// jsonp client
16-
export const restpClient = api =>
17-
fetchJsonp(`${api}`, v3EndpointOpt).then(r => r.json())
18-
// rest client
19-
/* fetch(`${api}`, v3EndpointOpt).then(r => r.json()) */
20-
export const restClient = api => fetch(`${api}`).then(r => r.text())
26+
27+
export const restClient = (api, fmt = 'json') => {
28+
if (fmt === 'json') {
29+
return fetch(`${api}`, jsonOptions).then(r => r.json())
30+
}
31+
32+
return fetch(`${api}`, rawOptions).then(r => r.text())
33+
}

services/github_api/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { searchUserPromise, ransformUser } from './user_search'
77
import { searchWikiPromise } from './wiki_search'
88
import { searchCheatsheeetPromise } from './cheatsheet_search'
99

10-
const githubApi = {
10+
const githubAPI = {
1111
// search repo
1212
searchRepo: (owner, name) => searchRepoPromise(owner, name),
1313
transformRepo: res => transformRepo(res),
@@ -34,4 +34,4 @@ const githubApi = {
3434
},
3535
}
3636

37-
export default githubApi
37+
export default githubAPI

services/github_api/repo_search.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
import R from 'ramda'
22
import { timeout } from 'promise-timeout'
33

4-
import { ISSUE_ADDR } from '@config'
5-
64
import { TIMEOUT_SEC, restEndpoint } from './config'
7-
import { graphqlClient, restpClient } from './client'
5+
import { graphqlClient, restClient } from './client'
86

97
import S from './schema'
108

11-
const baseInfoQuery = (owner, name) =>
12-
graphqlClient.request(S.repository, { owner, name })
9+
const baseInfoQuery = (owner, name) => {
10+
return graphqlClient.request(S.repository, { owner, name })
11+
}
1312

1413
const contributorsQuery = (owner, name) => {
1514
const path = 'contributors?page=1&per_page=8'
1615
const api = `${restEndpoint}/repos/${owner}/${name}/${path}`
1716

18-
return restpClient(`${api}`)
17+
return restClient(`${api}`)
18+
}
19+
20+
const readmeQuery = (owner, name) => {
21+
return restClient(`${restEndpoint}/repos/${owner}/${name}/readme`, 'raw')
1922
}
2023

2124
export const searchRepoPromise = (owner, name) =>
2225
Promise.all([
2326
timeout(baseInfoQuery(owner, name), TIMEOUT_SEC),
2427
timeout(contributorsQuery(owner, name), TIMEOUT_SEC),
28+
timeout(readmeQuery(owner, name), TIMEOUT_SEC),
2529
])
2630

2731
const getRelaseTag = releases => {
@@ -37,7 +41,8 @@ const getLicense = value => {
3741
// transform to match our model
3842
export const transformRepo = res => {
3943
const baseInfoRes = res[0].repository
40-
const contributorsRes = res[1].data
44+
const contributorsRes = res[1]
45+
const readme = res[2]
4146
const contributors = []
4247

4348
R.forEach(user => {
@@ -61,14 +66,9 @@ export const transformRepo = res => {
6166
licenseInfo,
6267
homepageUrl,
6368
releases,
64-
object,
6569
primaryLanguage,
6670
} = baseInfoRes
6771

68-
const readme = object
69-
? object.text
70-
: `同步错误: 目前只同步源仓库中的 README.md 文件,如果源仓库中为 README.MD / readme.md / readme.MD 等格式可能会导致该错误。 如果是其他原因,[恳请提交 issue](${ISSUE_ADDR}/new)`
71-
7272
return {
7373
title: name,
7474
ownerName: owner.login,

0 commit comments

Comments
 (0)