File tree 3 files changed +56
-8
lines changed
3 files changed +56
-8
lines changed Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
const got = require ( 'got' ) ;
3
3
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
+
4
10
const create = ( ) => got . create ( {
5
11
options : got . mergeOptions ( got . defaults . options , {
6
12
json : true ,
@@ -23,14 +29,26 @@ const create = () => got.create({
23
29
return next ( options ) ;
24
30
}
25
31
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
+ } ) ;
34
52
}
35
53
} ) ;
36
54
Original file line number Diff line number Diff line change @@ -91,6 +91,22 @@ Type: `Object`
91
91
Can be specified as a plain object and will be serialized as JSON with the appropriate headers set.
92
92
93
93
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
+
94
110
## Authorization
95
111
96
112
Authorization for GitHub uses the following logic:
Original file line number Diff line number Diff line change @@ -72,3 +72,17 @@ test('custom error', async t => {
72
72
message : 'Bad credentials (401)'
73
73
} ) ;
74
74
} ) ;
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments