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