Skip to content

Commit e17687a

Browse files
szmarczaksindresorhus
andauthoredFeb 17, 2020
Update to Got 10 (#38)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 1e062bd commit e17687a

File tree

4 files changed

+91
-60
lines changed

4 files changed

+91
-60
lines changed
 

‎index.js

+57-37
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,74 @@
11
'use strict';
22
const got = require('got');
33

4-
const getRateLimit = ({headers}) => ({
4+
const getRateLimit = headers => ({
55
limit: parseInt(headers['x-ratelimit-limit'], 10),
66
remaining: parseInt(headers['x-ratelimit-remaining'], 10),
77
reset: new Date(parseInt(headers['x-ratelimit-reset'], 10) * 1000)
88
});
99

10-
const create = () => got.create({
11-
options: got.mergeOptions(got.defaults.options, {
12-
json: true,
13-
token: process.env.GITHUB_TOKEN,
14-
baseUrl: process.env.GITHUB_ENDPOINT || 'https://api.github.com',
15-
headers: {
16-
accept: 'application/vnd.github.v3+json',
17-
'user-agent': 'https://github.com/sindresorhus/gh-got'
18-
}
19-
}),
10+
const create = () => got.extend({
11+
prefixUrl: process.env.GITHUB_ENDPOINT || 'https://api.github.com',
12+
headers: {
13+
accept: 'application/vnd.github.v3+json',
14+
'user-agent': 'https://github.com/sindresorhus/gh-got'
15+
},
16+
responseType: 'json',
17+
token: process.env.GITHUB_TOKEN,
18+
handlers: [
19+
(options, next) => {
20+
// Authorization
21+
if (options.token && !options.headers.authorization) {
22+
options.headers.authorization = `token ${options.token}`;
23+
}
2024

21-
methods: got.defaults.methods,
25+
// `options.body` -> `options.json`
26+
options.json = options.body;
27+
delete options.body;
2228

23-
handler: (options, next) => {
24-
if (options.token) {
25-
options.headers.authorization = options.headers.authorization || `token ${options.token}`;
26-
}
29+
// Don't touch streams
30+
if (options.isStream) {
31+
return next(options);
32+
}
2733

28-
if (options.stream) {
29-
return next(options);
30-
}
34+
// Magic begins
35+
return (async () => {
36+
try {
37+
const response = await next(options);
3138

32-
// TODO: Use async/await here when Got supports the `handler` being an async function
33-
return next(options)
34-
.then(response => { // eslint-disable-line promise/prefer-await-to-then
35-
response.rateLimit = getRateLimit(response);
36-
return response;
37-
})
38-
.catch(error => {
39-
const {response} = error;
40-
41-
if (response && response.body) {
42-
error.name = 'GitHubError';
43-
error.message = `${response.body.message} (${error.statusCode})`;
44-
}
39+
// Rate limit for the Response object
40+
response.rateLimit = getRateLimit(response.headers);
4541

46-
if (response) {
47-
error.rateLimit = getRateLimit(response);
48-
}
42+
return response;
43+
} catch (error) {
44+
const {response} = error;
4945

50-
throw error;
51-
});
46+
// Nicer errors
47+
if (response && response.body) {
48+
error.name = 'GitHubError';
49+
error.message = `${response.body.message} (${error.response.statusCode})`;
50+
}
51+
52+
// Rate limit for errors
53+
if (response) {
54+
error.rateLimit = getRateLimit(response.headers);
55+
}
56+
57+
throw error;
58+
}
59+
})();
60+
}
61+
],
62+
hooks: {
63+
init: [
64+
options => {
65+
// TODO: This should be fixed in Got
66+
// Remove leading slashes
67+
if (typeof options.url === 'string' && options.url.startsWith('/')) {
68+
options.url = options.url.slice(1);
69+
}
70+
}
71+
]
5272
}
5373
});
5474

‎package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"author": {
88
"name": "Sindre Sorhus",
99
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
10+
"url": "https://sindresorhus.com"
1111
},
1212
"engines": {
1313
"node": ">=8"
@@ -33,12 +33,12 @@
3333
"utility"
3434
],
3535
"dependencies": {
36-
"got": "^9.5.0"
36+
"got": "^10.5.7"
3737
},
3838
"devDependencies": {
39-
"ava": "^1.0.1",
40-
"get-stream": "^4.1.0",
41-
"nock": "^10.0.5",
42-
"xo": "^0.23.0"
39+
"ava": "^3.0.2",
40+
"get-stream": "^5.1.0",
41+
"nock": "^11.7.2",
42+
"xo": "^0.25.3"
4343
}
4444
}

‎readme.md

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

7676
Can be set globally with the `GITHUB_TOKEN` environment variable.
7777

78-
#### baseUrl
78+
#### prefixUrl
7979

8080
Type: `string`<br>
8181
Default: `https://api.github.com/`

‎test.js

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import test from 'ava';
2-
import nock from 'nock';
3-
import getStream from 'get-stream';
4-
import ghGot from '.';
1+
const test = require('ava');
2+
const nock = require('nock');
3+
const getStream = require('get-stream');
4+
const ghGot = require('.');
55

66
const token = process.env.GITHUB_TOKEN;
77

@@ -10,43 +10,54 @@ test('default', async t => {
1010
});
1111

1212
test('full path', async t => {
13-
t.is((await ghGot('https://api.github.com/users/sindresorhus')).body.login, 'sindresorhus');
13+
t.is((await ghGot('https://api.github.com/users/sindresorhus', {prefixUrl: ''})).body.login, 'sindresorhus');
1414
});
1515

1616
test('accepts options', async t => {
1717
t.is((await ghGot('users/sindresorhus', {})).body.login, 'sindresorhus');
1818
});
1919

20-
test('accepts options.endpoint without trailing slash', async t => {
21-
t.is((await ghGot('users/sindresorhus', {endpoint: 'https://api.github.com'})).body.login, 'sindresorhus');
20+
test('accepts options.prefixUrl without trailing slash', async t => {
21+
t.is((await ghGot('users/sindresorhus', {prefixUrl: 'https://api.github.com'})).body.login, 'sindresorhus');
2222
});
2323

2424
test('dedupes slashes', async t => {
25-
t.is((await ghGot('/users/sindresorhus', {endpoint: 'https://api.github.com/'})).body.login, 'sindresorhus');
25+
t.is((await ghGot('/users/sindresorhus', {prefixUrl: 'https://api.github.com/'})).body.login, 'sindresorhus');
2626
});
2727

2828
test.serial('global token option', async t => {
2929
process.env.GITHUB_TOKEN = 'fail';
30-
await t.throwsAsync(ghGot.recreate()('users/sindresorhus'), 'Bad credentials (401)');
30+
await t.throwsAsync(
31+
ghGot.recreate()('users/sindresorhus'),
32+
{
33+
message: 'Bad credentials (401)'
34+
}
35+
);
3136
process.env.GITHUB_TOKEN = token;
3237
});
3338

3439
test('token option', async t => {
35-
await t.throwsAsync(ghGot('users/sindresorhus', {token: 'fail'}), 'Bad credentials (401)');
40+
await t.throwsAsync(ghGot('users/sindresorhus', {token: 'fail'}), {
41+
message: 'Bad credentials (401)'
42+
});
3643
});
3744

3845
test.serial('global endpoint option', async t => {
3946
process.env.GITHUB_ENDPOINT = 'fail';
40-
await t.throwsAsync(ghGot.recreate()('users/sindresorhus', {retries: 1}), 'Invalid URL: fail/');
47+
await t.throwsAsync(ghGot.recreate()('users/sindresorhus', {retries: 1}), {
48+
message: 'Invalid URL: fail/users/sindresorhus'
49+
});
4150
delete process.env.GITHUB_ENDPOINT;
4251
});
4352

4453
test.serial('endpoint option', async t => {
4554
process.env.GITHUB_ENDPOINT = 'https://api.github.com/';
4655
await t.throwsAsync(ghGot.recreate()('users/sindresorhus', {
47-
baseUrl: 'fail',
56+
prefixUrl: 'fail',
4857
retries: 1
49-
}), 'Invalid URL: fail/');
58+
}), {
59+
message: 'Invalid URL: fail/users/sindresorhus'
60+
});
5061
delete process.env.GITHUB_ENDPOINT;
5162
});
5263

@@ -56,13 +67,13 @@ test('stream interface', async t => {
5667
});
5768

5869
test('json body', async t => {
59-
const baseUrl = 'http://mock-endpoint';
70+
const prefixUrl = 'http://mock-endpoint';
6071
const body = {test: [1, 3, 3, 7]};
6172
const reply = {ok: true};
6273

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

65-
t.deepEqual((await ghGot('/test', {baseUrl, body})).body, reply);
76+
t.deepEqual((await ghGot.post('test', {prefixUrl, body})).body, reply);
6677
t.truthy(scope.isDone());
6778
});
6879

0 commit comments

Comments
 (0)
Please sign in to comment.