Skip to content

Commit 402f07d

Browse files
szmarczaksindresorhus
authored andcommittedAug 23, 2018
Update to Got 9, rename endpoint option, and require Node.js 8 (#29)
Fixes #24 Fixes #25
1 parent a73bca8 commit 402f07d

File tree

5 files changed

+47
-74
lines changed

5 files changed

+47
-74
lines changed
 

‎.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
language: node_js
22
node_js:
3+
- '10'
34
- '8'
4-
- '6'
5-
- '4'

‎index.js

+31-56
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,43 @@
11
'use strict';
22
const got = require('got');
3-
const isPlainObj = require('is-plain-obj');
43

5-
function ghGot(path, opts) {
6-
if (typeof path !== 'string') {
7-
return Promise.reject(new TypeError(`Expected \`path\` to be a string, got ${typeof path}`));
8-
}
9-
10-
const env = process.env;
11-
12-
opts = Object.assign({
4+
const create = () => got.create({
5+
options: got.mergeOptions(got.defaults.options, {
136
json: true,
14-
token: env.GITHUB_TOKEN,
15-
endpoint: env.GITHUB_ENDPOINT ? env.GITHUB_ENDPOINT : 'https://api.github.com/'
16-
}, opts);
17-
18-
opts.headers = Object.assign({
19-
accept: 'application/vnd.github.v3+json',
20-
'user-agent': 'https://github.com/sindresorhus/gh-got'
21-
}, opts.headers);
22-
23-
if (opts.token) {
24-
opts.headers.authorization = `token ${opts.token}`;
25-
}
26-
27-
// https://developer.github.com/v3/#http-verbs
28-
if (opts.method && opts.method.toLowerCase() === 'put' && !opts.body) {
29-
opts.headers['content-length'] = 0;
30-
}
31-
32-
const url = /^https?/.test(path) ? path : opts.endpoint.replace(/\/$/, '') + '/' + path.replace(/^\//, '');
7+
token: process.env.GITHUB_TOKEN,
8+
baseUrl: process.env.GITHUB_ENDPOINT || 'https://api.github.com',
9+
headers: {
10+
accept: 'application/vnd.github.v3+json',
11+
'user-agent': 'https://github.com/sindresorhus/gh-got'
12+
}
13+
}),
14+
methods: got.defaults.methods,
15+
handler: (options, next) => {
16+
if (options.token) {
17+
options.headers.authorization = `token ${options.token}`;
18+
}
3319

34-
if (opts.stream) {
35-
return got.stream(url, opts);
36-
}
20+
if (options.method && options.method === 'PUT' && !options.body) {
21+
options.headers['content-length'] = 0;
22+
}
3723

38-
return got(url, opts).catch(err => {
39-
if (err.response && isPlainObj(err.response.body)) {
40-
err.name = 'GitHubError';
41-
err.message = `${err.response.body.message} (${err.statusCode})`;
24+
if (options.stream) {
25+
return next(options);
4226
}
4327

44-
throw err;
45-
});
46-
}
28+
return next(options).catch(err => {
29+
if (err.response && err.response.body) {
30+
err.name = 'GitHubError';
31+
err.message = `${err.response.body.message} (${err.statusCode})`;
32+
}
4733

48-
const helpers = [
49-
'get',
50-
'post',
51-
'put',
52-
'patch',
53-
'head',
54-
'delete'
55-
];
34+
throw err;
35+
});
36+
}
37+
});
5638

57-
ghGot.stream = (url, opts) => ghGot(url, Object.assign({}, opts, {
58-
json: false,
59-
stream: true
60-
}));
39+
module.exports = create();
6140

62-
for (const x of helpers) {
63-
const method = x.toUpperCase();
64-
ghGot[x] = (url, opts) => ghGot(url, Object.assign({}, opts, {method}));
65-
ghGot.stream[x] = (url, opts) => ghGot.stream(url, Object.assign({}, opts, {method}));
41+
if (process.env.NODE_ENV === 'test') {
42+
module.exports.recreate = create;
6643
}
67-
68-
module.exports = ghGot;

‎package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=4"
13+
"node": ">=8"
1414
},
1515
"scripts": {
1616
"test": "xo && ava"
@@ -33,12 +33,11 @@
3333
"utility"
3434
],
3535
"dependencies": {
36-
"got": "^8.0.0",
37-
"is-plain-obj": "^1.1.0"
36+
"got": "^9.1.0"
3837
},
3938
"devDependencies": {
40-
"ava": "*",
41-
"get-stream": "^3.0.0",
39+
"ava": "^1.0.0-beta.7",
40+
"get-stream": "^4.0.0",
4241
"nock": "^9.1.0",
4342
"xo": "*"
4443
}

‎readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ GitHub [access token](https://github.com/settings/tokens/new).
7373

7474
Can be set globally with the `GITHUB_TOKEN` environment variable.
7575

76-
### endpoint
76+
### baseUrl
7777

7878
Type: `string`<br>
7979
Default: `https://api.github.com/`

‎test.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ test('dedupes slashes', async t => {
2727

2828
test.serial('global token option', async t => {
2929
process.env.GITHUB_TOKEN = 'fail';
30-
await t.throws(m('users/sindresorhus'), 'Bad credentials (401)');
30+
await t.throwsAsync(m.recreate()('users/sindresorhus'), 'Bad credentials (401)');
3131
process.env.GITHUB_TOKEN = token;
3232
});
3333

3434
test('token option', async t => {
35-
await t.throws(m('users/sindresorhus', {token: 'fail'}), 'Bad credentials (401)');
35+
await t.throwsAsync(m('users/sindresorhus', {token: 'fail'}), 'Bad credentials (401)');
3636
});
3737

3838
test.serial('global endpoint option', async t => {
3939
process.env.GITHUB_ENDPOINT = 'fail';
40-
await t.throws(m('users/sindresorhus', {retries: 1}), /ENOTFOUND/);
40+
await t.throwsAsync(m.recreate()('users/sindresorhus', {retries: 1}), 'Invalid URL: fail/');
4141
delete process.env.GITHUB_ENDPOINT;
4242
});
4343

4444
test.serial('endpoint option', async t => {
4545
process.env.GITHUB_ENDPOINT = 'https://api.github.com/';
46-
await t.throws(m('users/sindresorhus', {
47-
endpoint: 'fail',
46+
await t.throwsAsync(m.recreate()('users/sindresorhus', {
47+
baseUrl: 'fail',
4848
retries: 1
49-
}), /ENOTFOUND/);
49+
}), 'Invalid URL: fail/');
5050
delete process.env.GITHUB_ENDPOINT;
5151
});
5252

@@ -56,18 +56,18 @@ test('stream interface', async t => {
5656
});
5757

5858
test('json body', async t => {
59-
const endpoint = 'http://mock-endpoint';
59+
const baseUrl = 'http://mock-endpoint';
6060
const body = {test: [1, 3, 3, 7]};
6161
const reply = {ok: true};
6262

63-
const scope = nock(endpoint).post('/test', body).reply(200, reply);
63+
const scope = nock(baseUrl).post('/test', body).reply(200, reply);
6464

65-
t.deepEqual((await m('/test', {endpoint, body})).body, reply);
65+
t.deepEqual((await m('/test', {baseUrl, body})).body, reply);
6666
t.truthy(scope.isDone());
6767
});
6868

6969
test('custom error', async t => {
70-
const err = await t.throws(m('users/sindresorhus', {token: 'fail'}));
70+
const err = await t.throwsAsync(m('users/sindresorhus', {token: 'fail'}));
7171
t.is(err.name, 'GitHubError');
7272
t.is(err.message, 'Bad credentials (401)');
7373
});

0 commit comments

Comments
 (0)
Please sign in to comment.