gh-got
Advanced tools
Comparing version 8.0.1 to 8.1.0
38
index.js
'use strict'; | ||
const got = require('got'); | ||
const getRateLimit = ({headers}) => ({ | ||
limit: parseInt(headers['x-ratelimit-limit'], 10), | ||
remaining: parseInt(headers['x-ratelimit-remaining'], 10), | ||
reset: new Date(parseInt(headers['x-ratelimit-reset'], 10) * 1000) | ||
}); | ||
const create = () => got.create({ | ||
@@ -14,3 +20,5 @@ options: got.mergeOptions(got.defaults.options, { | ||
}), | ||
methods: got.defaults.methods, | ||
handler: (options, next) => { | ||
@@ -21,6 +29,2 @@ if (options.token) { | ||
if (options.method && options.method === 'PUT' && !options.body) { | ||
options.headers['content-length'] = 0; | ||
} | ||
if (options.stream) { | ||
@@ -30,10 +34,22 @@ return next(options); | ||
return next(options).catch(err => { | ||
if (err.response && err.response.body) { | ||
err.name = 'GitHubError'; | ||
err.message = `${err.response.body.message} (${err.statusCode})`; | ||
} | ||
// TODO: Use async/await here when Got supports the `handler` being an async function | ||
return next(options) | ||
.then(response => { // eslint-disable-line promise/prefer-await-to-then | ||
response.rateLimit = getRateLimit(response); | ||
return response; | ||
}) | ||
.catch(error => { | ||
const {response} = error; | ||
throw err; | ||
}); | ||
if (response && response.body) { | ||
error.name = 'GitHubError'; | ||
error.message = `${response.body.message} (${error.statusCode})`; | ||
} | ||
if (response) { | ||
error.rateLimit = getRateLimit(response); | ||
} | ||
throw error; | ||
}); | ||
} | ||
@@ -40,0 +56,0 @@ }); |
{ | ||
"name": "gh-got", | ||
"version": "8.0.1", | ||
"version": "8.1.0", | ||
"description": "Convenience wrapper for `got` to interact with the GitHub API", | ||
@@ -36,10 +36,10 @@ "license": "MIT", | ||
"dependencies": { | ||
"got": "^9.1.0" | ||
"got": "^9.5.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.0.0-beta.7", | ||
"get-stream": "^4.0.0", | ||
"nock": "^9.1.0", | ||
"xo": "*" | ||
"ava": "^1.0.1", | ||
"get-stream": "^4.1.0", | ||
"nock": "^10.0.5", | ||
"xo": "^0.23.0" | ||
} | ||
} |
@@ -64,8 +64,10 @@ # gh-got [![Build Status](https://travis-ci.org/sindresorhus/gh-got.svg?branch=master)](https://travis-ci.org/sindresorhus/gh-got) | ||
Same as [`got`](https://github.com/sindresorhus/got) (including the stream API and aliases), but with some additional options below. | ||
Same API as [`got`](https://github.com/sindresorhus/got), including the stream API and aliases, but with some additional options below. | ||
Errors are improved by using the custom GitHub error messages. Doesn't apply to the stream API. | ||
### token | ||
### `gh-got` specific options | ||
#### token | ||
Type: `string` | ||
@@ -77,3 +79,3 @@ | ||
### baseUrl | ||
#### baseUrl | ||
@@ -87,3 +89,3 @@ Type: `string`<br> | ||
### body | ||
#### body | ||
@@ -95,2 +97,18 @@ Type: `Object` | ||
## Rate limit | ||
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)* | ||
```js | ||
const ghGot = require('gh-got'); | ||
(async () => { | ||
const {rateLimit} = await ghGot('users/sindresorhus'); | ||
console.log(rateLimit); | ||
//=> {limit: 5000, remaining: 4899, reset: [Date 2018-12-31T20:45:20.000Z]} | ||
})(); | ||
``` | ||
## Authorization | ||
@@ -97,0 +115,0 @@ |
6664
48
140
Updatedgot@^9.5.0