Skip to content
This repository was archived by the owner on Sep 12, 2019. It is now read-only.

Commit 58f6772

Browse files
authored
Merge pull request #30 from netlify/addSafeJoin
extract safeJoin functionality out into a library
2 parents 4d24b04 + 5fb6949 commit 58f6772

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"inquirer": "^6.2.2",
1717
"netlify": "ssh+git://[email protected]:netlify/js-client-private",
1818
"netlify-rules-proxy": "git+ssh://[email protected]/netlify/netlify-rules-proxy.git",
19+
"safe-join": "^0.1.1",
1920
"node-fetch": "^2.3.0",
2021
"opn": "^5.5.0",
2122
"static-dev-server": "^1.0.0",

src/utils/readRepoURL.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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");
34

45
// supported repo host types
5-
const GITHUB = Symbol('GITHUB')
6+
const GITHUB = Symbol("GITHUB");
67
// const BITBUCKET = Symbol('BITBUCKET')
78
// const GITLAB = Symbol('GITLAB')
89

@@ -11,52 +12,52 @@ const GITHUB = Symbol('GITHUB')
1112
* and returns https://api.github.com/repos/netlify-labs/all-the-functions/contents/functions/9-using-middleware
1213
*/
1314
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;
1924
}
2025

2126
async function getRepoURLContents(repoHost, owner_and_repo, contents_path) {
2227
// naive joining strategy for now
2328
if (repoHost === GITHUB) {
2429
// https://developer.github.com/v3/repos/contents/#get-contents
2530
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",
2734
contents_path
28-
)
35+
);
2936
return fetch(APIURL)
3037
.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+
);
3241
} else {
33-
throw new Error('unsupported host ', repoHost)
42+
throw new Error("unsupported host ", repoHost);
3443
}
3544
}
3645

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-
4646
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");
4849
// other validation logic here
49-
return GITHUB
50+
return GITHUB;
5051
}
5152
function parseRepoURL(repoHost, URL) {
5253
// naive splitting strategy for now
5354
if (repoHost === GITHUB) {
5455
// 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];
5758
} else {
58-
throw new Error('unsupported host ', repoHost)
59+
throw new Error("unsupported host ", repoHost);
5960
}
6061
}
6162

62-
module.exports = readRepoURL
63+
module.exports = readRepoURL;

0 commit comments

Comments
 (0)