From 9a29493dad77a1f7cb5fc50aeae848edc7a124d6 Mon Sep 17 00:00:00 2001 From: David Frank Date: Wed, 1 May 2019 11:51:17 +0800 Subject: [PATCH 1/4] changelog update --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef22c7748..8cbbfc20d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ Changelog # 2.x release +## v2.5.0 + +- 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. From 37bb9263574ea104e120c743b0bb08fc1041d869 Mon Sep 17 00:00:00 2001 From: David Frank Date: Wed, 1 May 2019 11:59:08 +0800 Subject: [PATCH 2/4] readme badge update --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3e6ff459a..a1255c1c2 100644 --- a/README.md +++ b/README.md @@ -510,17 +510,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 From 577843cc807c1d552869ad64111900cc47341f9e Mon Sep 17 00:00:00 2001 From: David Frank Date: Wed, 1 May 2019 12:29:20 +0800 Subject: [PATCH 3/4] redirected property --- CHANGELOG.md | 1 + README.md | 7 ++++++- src/index.js | 3 ++- src/response.js | 12 +++++++++--- test/test.js | 18 ++++++++++++++++-- 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cbbfc20d..941b6a8d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Changelog ## 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. diff --git a/README.md b/README.md index a1255c1c2..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 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(() => { From 408ab29237199a987c8dcae41702281af6319e6a Mon Sep 17 00:00:00 2001 From: David Frank Date: Wed, 1 May 2019 12:44:04 +0800 Subject: [PATCH 4/4] 2.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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",