Skip to content

Commit a9b31de

Browse files
rarkinssindresorhus
authored andcommittedAug 23, 2018
Support options.endpoint without trailing slash (#30)
1 parent 9c7597c commit a9b31de

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed
 

‎index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function ghGot(path, opts) {
1212
opts = Object.assign({
1313
json: true,
1414
token: env.GITHUB_TOKEN,
15-
endpoint: env.GITHUB_ENDPOINT ? env.GITHUB_ENDPOINT.replace(/[^/]$/, '$&/') : 'https://api.github.com/'
15+
endpoint: env.GITHUB_ENDPOINT ? env.GITHUB_ENDPOINT : 'https://api.github.com/'
1616
}, opts);
1717

1818
opts.headers = Object.assign({
@@ -29,7 +29,7 @@ function ghGot(path, opts) {
2929
opts.headers['content-length'] = 0;
3030
}
3131

32-
const url = /^https?/.test(path) ? path : opts.endpoint + path;
32+
const url = /^https?/.test(path) ? path : opts.endpoint.replace(/\/$/, '') + '/' + path.replace(/^\//, '');
3333

3434
if (opts.stream) {
3535
return got.stream(url, opts);

‎test.js

+8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ test('accepts options', async t => {
1717
t.is((await m('users/sindresorhus', {})).body.login, 'sindresorhus');
1818
});
1919

20+
test('accepts options.endpoint without trailing slash', async t => {
21+
t.is((await m('users/sindresorhus', {endpoint: 'https://api.github.com'})).body.login, 'sindresorhus');
22+
});
23+
24+
test('dedupes slashes', async t => {
25+
t.is((await m('/users/sindresorhus', {endpoint: 'https://api.github.com/'})).body.login, 'sindresorhus');
26+
});
27+
2028
test.serial('global token option', async t => {
2129
process.env.GITHUB_TOKEN = 'fail';
2230
await t.throws(m('users/sindresorhus'), 'Bad credentials (401)');

0 commit comments

Comments
 (0)
Please sign in to comment.