1
- const url = require ( " url" ) ;
2
- const fetch = require ( " node-fetch" ) ;
3
- const safeJoin = require ( " safe-join" ) ;
1
+ const url = require ( ' url' )
2
+ const fetch = require ( ' node-fetch' )
3
+ const { safeJoin } = require ( ' safe-join' )
4
4
5
5
// supported repo host types
6
- const GITHUB = Symbol ( " GITHUB" ) ;
6
+ const GITHUB = Symbol ( ' GITHUB' )
7
7
// const BITBUCKET = Symbol('BITBUCKET')
8
8
// const GITLAB = Symbol('GITLAB')
9
9
@@ -12,52 +12,40 @@ const GITHUB = Symbol("GITHUB");
12
12
* and returns https://api.github.com/repos/netlify-labs/all-the-functions/contents/functions/9-using-middleware
13
13
*/
14
14
async function readRepoURL ( _url ) {
15
- const URL = url . parse ( _url ) ;
16
- const repoHost = validateRepoURL ( URL ) ;
17
- const [ owner_and_repo , contents_path ] = parseRepoURL ( repoHost , URL ) ;
18
- const folderContents = await getRepoURLContents (
19
- repoHost ,
20
- owner_and_repo ,
21
- contents_path
22
- ) ;
23
- return folderContents ;
15
+ const URL = url . parse ( _url )
16
+ const repoHost = validateRepoURL ( URL )
17
+ const [ owner_and_repo , contents_path ] = parseRepoURL ( repoHost , URL )
18
+ const folderContents = await getRepoURLContents ( repoHost , owner_and_repo , contents_path )
19
+ return folderContents
24
20
}
25
21
26
22
async function getRepoURLContents ( repoHost , owner_and_repo , contents_path ) {
27
23
// naive joining strategy for now
28
24
if ( repoHost === GITHUB ) {
29
25
// https://developer.github.com/v3/repos/contents/#get-contents
30
- const APIURL = safeJoin (
31
- "https://api.github.com/repos" ,
32
- owner_and_repo ,
33
- "contents" ,
34
- contents_path
35
- ) ;
26
+ const APIURL = safeJoin ( 'https://api.github.com/repos' , owner_and_repo , 'contents' , contents_path )
36
27
return fetch ( APIURL )
37
28
. then ( x => x . json ( ) )
38
- . catch ( err =>
39
- console . error ( "Error occurred while fetching " , APIURL , err )
40
- ) ;
29
+ . catch ( err => console . error ( 'Error occurred while fetching ' , APIURL , err ) )
41
30
} else {
42
- throw new Error ( " unsupported host " , repoHost ) ;
31
+ throw new Error ( ' unsupported host ' , repoHost )
43
32
}
44
33
}
45
34
46
35
function validateRepoURL ( URL ) {
47
- if ( URL . host !== "github.com" )
48
- throw new Error ( "only github repos are supported for now" ) ;
36
+ if ( URL . host !== 'github.com' ) throw new Error ( 'only github repos are supported for now' )
49
37
// other validation logic here
50
- return GITHUB ;
38
+ return GITHUB
51
39
}
52
40
function parseRepoURL ( repoHost , URL ) {
53
41
// naive splitting strategy for now
54
42
if ( repoHost === GITHUB ) {
55
43
// https://developer.github.com/v3/repos/contents/#get-contents
56
- const [ owner_and_repo , contents_path ] = URL . path . split ( " /tree/master" ) ; // what if it's not master? note that our contents retrieval may assume it is master
57
- return [ owner_and_repo , contents_path ] ;
44
+ const [ owner_and_repo , contents_path ] = URL . path . split ( ' /tree/master' ) // what if it's not master? note that our contents retrieval may assume it is master
45
+ return [ owner_and_repo , contents_path ]
58
46
} else {
59
- throw new Error ( " unsupported host " , repoHost ) ;
47
+ throw new Error ( ' unsupported host ' , repoHost )
60
48
}
61
49
}
62
50
63
- module . exports = readRepoURL ;
51
+ module . exports = readRepoURL
0 commit comments