github sindresorhus/got v4.0.0
4.0.0

Major release with lots of new stuff! Enjoy!

anigif_enhanced-buzz-14210-1427585395-12

Promise API

got without callback now returns a promise instead of a stream. The stream API now lives under stream property (look at the end of section).

got('todomvc.com')
    .then(function (res) {
        console.log(res.body);
    })
    .catch(function (err) {
        console.error(err.response.body);
    });

In resolve handler you receive a http.response object with body as additional property.

In reject handler we provide an Error object. Unless Error type is RequestError, it will have response property with body property.

This API will be a huge advantage with co and Generators:

var co = require('co');

co(function * () {
    try {
        var response = yield got('todomvc.com');
        console.log(response.body);
    } catch (err) {
        console.error(err);
    }
});

You can go even further with destructuring and await from ES7:

async function () {
    try {
        var {body} = await got('todomvc.com');
        console.log(body);
    } catch (err) {
        console.error(err);
    }
}

Stream support is preserved, but now under the streams property:

got.stream('todomvc.com').pipe(process.stdout);

New Errors

Before 4.0.0 got had one Error class (GotError) with nested stack traces and custom error messages. This was a nice concept, but in production use some edge cases popped out:

  • Error stacks are too long.
  • Relevant information often were in one or two levels deep (eg. error.nested.nested).
  • Errors message are hard to parse for url.

So we decided to review Errors in new version. In 4.0.0 got now have multiple Error classes for different situations: RequestError, ReadError, ParseError, HTTPError and MaxRedirectsError.

In addition to message and code properties from the original error, the new errors types also provide additional properties (host, hostname, path and method) for logging and debugging.

Infinity-agent removed

Custom agent was removed. This returns the ability to configure or replace global Agent, but also returns low maxSockets value in NodeJS 0.10.x. To fix this you can add this lines at the top of your app (not in reusable modules!):

require('http').globalAgent.maxSockets = Infinity;
require('https').globalAgent.maxSockets = Infinity;

Changes

https://github.com/sindresorhus/got/compare/v3.3.1...v4.0.0

latest releases: v11.7.0, v11.6.2, v11.6.1...
6 years ago