Skip to content

Commit 6c418ab

Browse files
authoredJan 1, 2019
Add .rateLimit property to responses and errors (#34)
1 parent 11e621b commit 6c418ab

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed
 

‎index.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'use strict';
22
const got = require('got');
33

4+
const getRateLimit = ({headers}) => ({
5+
limit: parseInt(headers['x-ratelimit-limit'], 10),
6+
remaining: parseInt(headers['x-ratelimit-remaining'], 10),
7+
reset: new Date(parseInt(headers['x-ratelimit-reset'], 10) * 1000)
8+
});
9+
410
const create = () => got.create({
511
options: got.mergeOptions(got.defaults.options, {
612
json: true,
@@ -23,14 +29,26 @@ const create = () => got.create({
2329
return next(options);
2430
}
2531

26-
return next(options).catch(error => {
27-
if (error.response && error.response.body) {
28-
error.name = 'GitHubError';
29-
error.message = `${error.response.body.message} (${error.statusCode})`;
30-
}
31-
32-
throw error;
33-
});
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+
}
45+
46+
if (response) {
47+
error.rateLimit = getRateLimit(response);
48+
}
49+
50+
throw error;
51+
});
3452
}
3553
});
3654

‎readme.md

+16
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ Type: `Object`
9191
Can be specified as a plain object and will be serialized as JSON with the appropriate headers set.
9292

9393

94+
## Rate limit
95+
96+
Responses and errors have a `.rateLimit` property with info about the current [rate limit](https://developer.github.com/v3/#rate-limiting). *(This is not yet implemented for the stream API)*
97+
98+
```js
99+
const ghGot = require('gh-got');
100+
101+
(async () => {
102+
const {rateLimit} = await ghGot('users/sindresorhus');
103+
104+
console.log(rateLimit);
105+
//=> {limit: 5000, remaining: 4899, reset: [Date 2018-12-31T20:45:20.000Z]}
106+
})();
107+
```
108+
109+
94110
## Authorization
95111

96112
Authorization for GitHub uses the following logic:

‎test.js

+14
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,17 @@ test('custom error', async t => {
7272
message: 'Bad credentials (401)'
7373
});
7474
});
75+
76+
test('.rateLimit response property', async t => {
77+
const {rateLimit} = await ghGot('users/sindresorhus');
78+
t.is(typeof rateLimit.limit, 'number');
79+
t.is(typeof rateLimit.remaining, 'number');
80+
t.true(rateLimit.reset instanceof Date);
81+
});
82+
83+
test('.rateLimit error property', async t => {
84+
const {rateLimit} = await t.throwsAsync(ghGot('users/sindresorhus', {token: 'fail'}));
85+
t.is(typeof rateLimit.limit, 'number');
86+
t.is(typeof rateLimit.remaining, 'number');
87+
t.true(rateLimit.reset instanceof Date);
88+
});

0 commit comments

Comments
 (0)
Please sign in to comment.