-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-api-url.js
48 lines (45 loc) · 1.32 KB
/
gh-api-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Github Api Urls
Forge url for Github Api
*/
/**
* To understand authentification rate limit policy, please read Github instructions :
* > https://developer.github.com/v3/#increasing-the-unauthenticated-rate-limit-for-oauth-applications
*/
const authRateLimit = {
ghSecret: process.env.GITHUB_SECRET,
ghId: process.env.GITHUB_ID
}
/**
* Add token to url query to increase github rate limit
*
* @param {String} ghId default: authRateLimit.ghId Github user id.
* @param {String} ghSecret default: authRateLimit.ghSecret Github secret token.
* @return {String} url url with query &client_id&client_secret.
*/
const addAuth = (url, { ghId, ghSecret } = authRateLimit) => {
if (!(ghId && ghSecret) || !!url.match(/ghSecret=|ghId=/)) {
return url
}
const querySymbol = url.match(/\?/) ? '&' : '?'
return `${url}${querySymbol}client_id=${ghId}&client_secret=${ghSecret}`
}
/**
* Create the url to extract document from Github.
*
* @param {Object} params - Github params - {localDomain, owner, repo, path, branch}
* @return {String} github-url - The API Github Url.
*/
const toGhUrl = ({ owner, repo, path, branch }) => {
return `https://api.github.com/repos` +
`/${owner}` +
`/${repo}` +
`/contents` +
`${path ? '/' + path : ''}` +
`?ref=${branch}`
}
module.exports = {
// public
addAuth,
toGhUrl
}