From 0c2294ec48fa5b84519f8bdd60f4e2672ebd9b06 Mon Sep 17 00:00:00 2001 From: David Frank Date: Wed, 1 May 2019 13:05:32 +0800 Subject: [PATCH] 2.5.0 release (#630) * redirected property * changelog update * readme update * 2.5.0 --- CHANGELOG.md | 8 ++++++++ README.md | 15 ++++++++++----- package.json | 2 +- src/index.js | 3 ++- src/response.js | 12 +++++++++--- test/test.js | 18 ++++++++++++++++-- 6 files changed, 46 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef22c7748..941b6a8d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ Changelog # 2.x release +## v2.5.0 + +- Enhance: `Response` object now includes `redirected` property. +- Enhance: `fetch()` now accepts third-party `Blob` implementation as body. +- Other: disable `package-lock.json` generation as we never commit them. +- Other: dev dependency update. +- Other: readme update. + ## v2.4.1 - Fix: `Blob` import rule for node < 10, as `Readable` isn't a named export. diff --git a/README.md b/README.md index 3e6ff459a..48f4215e4 100644 --- a/README.md +++ b/README.md @@ -381,7 +381,6 @@ The following properties are not implemented in node-fetch at this moment: - `Response.error()` - `Response.redirect()` - `type` -- `redirected` - `trailer` #### new Response([body[, options]]) @@ -401,6 +400,12 @@ Because Node.js does not implement service workers (for which this class was des Convenience property representing if the request ended normally. Will evaluate to true if the response status was greater than or equal to 200 but smaller than 300. +#### response.redirected + +*(spec-compliant)* + +Convenience property representing if the request has been redirected at least once. Will evaluate to true if the internal redirect counter is greater than 0. + ### Class: Headers @@ -510,17 +515,17 @@ An Error thrown when the request is aborted in response to an `AbortSignal`'s `a Thanks to [github/fetch](https://github.com/github/fetch) for providing a solid implementation reference. -`node-fetch` v1 was maintained by [@bitinn](https://github.com/bitinn), v2 is currently maintained by [@TimothyGu](https://github.com/timothygu), v2 readme is written by [@jkantr](https://github.com/jkantr). +`node-fetch` v1 was maintained by [@bitinn](https://github.com/bitinn); v2 was maintained by [@TimothyGu](https://github.com/timothygu), [@bitinn](https://github.com/bitinn) and [@jimmywarting](https://github.com/jimmywarting); v2 readme is written by [@jkantr](https://github.com/jkantr). ## License MIT -[npm-image]: https://img.shields.io/npm/v/node-fetch.svg?style=flat-square +[npm-image]: https://flat.badgen.net/npm/v/node-fetch [npm-url]: https://www.npmjs.com/package/node-fetch -[travis-image]: https://img.shields.io/travis/bitinn/node-fetch.svg?style=flat-square +[travis-image]: https://flat.badgen.net/travis/bitinn/node-fetch [travis-url]: https://travis-ci.org/bitinn/node-fetch -[codecov-image]: https://img.shields.io/codecov/c/github/bitinn/node-fetch.svg?style=flat-square +[codecov-image]: https://flat.badgen.net/codecov/c/github/bitinn/node-fetch/master [codecov-url]: https://codecov.io/gh/bitinn/node-fetch [install-size-image]: https://flat.badgen.net/packagephobia/install/node-fetch [install-size-url]: https://packagephobia.now.sh/result?p=node-fetch diff --git a/package.json b/package.json index 599e16e18..353f79322 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-fetch", - "version": "2.4.1", + "version": "2.5.0", "description": "A light-weight module that brings window.fetch to node.js", "main": "lib/index", "browser": "./browser.js", diff --git a/src/index.js b/src/index.js index b716550a8..907f47275 100644 --- a/src/index.js +++ b/src/index.js @@ -189,7 +189,8 @@ export default function fetch(url, opts) { statusText: res.statusMessage, headers: headers, size: request.size, - timeout: request.timeout + timeout: request.timeout, + counter: request.counter }; // HTTP-network fetch step 12.1.1.3 diff --git a/src/response.js b/src/response.js index f29bfe296..e2ca49c3e 100644 --- a/src/response.js +++ b/src/response.js @@ -40,7 +40,8 @@ export default class Response { url: opts.url, status, statusText: opts.statusText || STATUS_CODES[status], - headers + headers, + counter: opts.counter }; } @@ -59,6 +60,10 @@ export default class Response { return this[INTERNALS].status >= 200 && this[INTERNALS].status < 300; } + get redirected() { + return this[INTERNALS].counter > 0; + } + get statusText() { return this[INTERNALS].statusText; } @@ -78,9 +83,9 @@ export default class Response { status: this.status, statusText: this.statusText, headers: this.headers, - ok: this.ok + ok: this.ok, + redirected: this.redirected }); - } } @@ -90,6 +95,7 @@ Object.defineProperties(Response.prototype, { url: { enumerable: true }, status: { enumerable: true }, ok: { enumerable: true }, + redirected: { enumerable: true }, statusText: { enumerable: true }, headers: { enumerable: true }, clone: { enumerable: true } diff --git a/test/test.js b/test/test.js index e96c85a65..00f45353e 100644 --- a/test/test.js +++ b/test/test.js @@ -489,6 +489,20 @@ describe('node-fetch', () => { }); }); + it('should set redirected property on response when redirect', function() { + const url = `${base}redirect/301`; + return fetch(url).then(res => { + expect(res.redirected).to.be.true; + }); + }); + + it('should not set redirected property on response without redirect', function() { + const url = `${base}hello`; + return fetch(url).then(res => { + expect(res.redirected).to.be.false; + }); + }); + it('should ignore invalid headers', function() { var headers = { 'Invalid-Header ': 'abc\r\n', @@ -2196,12 +2210,12 @@ describe('Response', function () { } for (const toCheck of [ 'body', 'bodyUsed', 'arrayBuffer', 'blob', 'json', 'text', - 'url', 'status', 'ok', 'statusText', 'headers', 'clone' + 'url', 'status', 'ok', 'redirected', 'statusText', 'headers', 'clone' ]) { expect(enumerableProperties).to.contain(toCheck); } for (const toCheck of [ - 'body', 'bodyUsed', 'url', 'status', 'ok', 'statusText', + 'body', 'bodyUsed', 'url', 'status', 'ok', 'redirected', 'statusText', 'headers' ]) { expect(() => {