Skip to content

Commit 0c2294e

Browse files
authoredMay 1, 2019
2.5.0 release (#630)
* redirected property * changelog update * readme update * 2.5.0
1 parent 0fc414c commit 0c2294e

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed
 

‎CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ Changelog
55

66
# 2.x release
77

8+
## v2.5.0
9+
10+
- Enhance: `Response` object now includes `redirected` property.
11+
- Enhance: `fetch()` now accepts third-party `Blob` implementation as body.
12+
- Other: disable `package-lock.json` generation as we never commit them.
13+
- Other: dev dependency update.
14+
- Other: readme update.
15+
816
## v2.4.1
917

1018
- Fix: `Blob` import rule for node < 10, as `Readable` isn't a named export.

‎README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ The following properties are not implemented in node-fetch at this moment:
381381
- `Response.error()`
382382
- `Response.redirect()`
383383
- `type`
384-
- `redirected`
385384
- `trailer`
386385

387386
#### new Response([body[, options]])
@@ -401,6 +400,12 @@ Because Node.js does not implement service workers (for which this class was des
401400

402401
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.
403402

403+
#### response.redirected
404+
405+
<small>*(spec-compliant)*</small>
406+
407+
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.
408+
404409
<a id="class-headers"></a>
405410
### Class: Headers
406411

@@ -510,17 +515,17 @@ An Error thrown when the request is aborted in response to an `AbortSignal`'s `a
510515

511516
Thanks to [github/fetch](https://github.com/github/fetch) for providing a solid implementation reference.
512517

513-
`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).
518+
`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).
514519

515520
## License
516521

517522
MIT
518523

519-
[npm-image]: https://img.shields.io/npm/v/node-fetch.svg?style=flat-square
524+
[npm-image]: https://flat.badgen.net/npm/v/node-fetch
520525
[npm-url]: https://www.npmjs.com/package/node-fetch
521-
[travis-image]: https://img.shields.io/travis/bitinn/node-fetch.svg?style=flat-square
526+
[travis-image]: https://flat.badgen.net/travis/bitinn/node-fetch
522527
[travis-url]: https://travis-ci.org/bitinn/node-fetch
523-
[codecov-image]: https://img.shields.io/codecov/c/github/bitinn/node-fetch.svg?style=flat-square
528+
[codecov-image]: https://flat.badgen.net/codecov/c/github/bitinn/node-fetch/master
524529
[codecov-url]: https://codecov.io/gh/bitinn/node-fetch
525530
[install-size-image]: https://flat.badgen.net/packagephobia/install/node-fetch
526531
[install-size-url]: https://packagephobia.now.sh/result?p=node-fetch

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-fetch",
3-
"version": "2.4.1",
3+
"version": "2.5.0",
44
"description": "A light-weight module that brings window.fetch to node.js",
55
"main": "lib/index",
66
"browser": "./browser.js",

‎src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ export default function fetch(url, opts) {
189189
statusText: res.statusMessage,
190190
headers: headers,
191191
size: request.size,
192-
timeout: request.timeout
192+
timeout: request.timeout,
193+
counter: request.counter
193194
};
194195

195196
// HTTP-network fetch step 12.1.1.3

‎src/response.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export default class Response {
4040
url: opts.url,
4141
status,
4242
statusText: opts.statusText || STATUS_CODES[status],
43-
headers
43+
headers,
44+
counter: opts.counter
4445
};
4546
}
4647

@@ -59,6 +60,10 @@ export default class Response {
5960
return this[INTERNALS].status >= 200 && this[INTERNALS].status < 300;
6061
}
6162

63+
get redirected() {
64+
return this[INTERNALS].counter > 0;
65+
}
66+
6267
get statusText() {
6368
return this[INTERNALS].statusText;
6469
}
@@ -78,9 +83,9 @@ export default class Response {
7883
status: this.status,
7984
statusText: this.statusText,
8085
headers: this.headers,
81-
ok: this.ok
86+
ok: this.ok,
87+
redirected: this.redirected
8288
});
83-
8489
}
8590
}
8691

@@ -90,6 +95,7 @@ Object.defineProperties(Response.prototype, {
9095
url: { enumerable: true },
9196
status: { enumerable: true },
9297
ok: { enumerable: true },
98+
redirected: { enumerable: true },
9399
statusText: { enumerable: true },
94100
headers: { enumerable: true },
95101
clone: { enumerable: true }

‎test/test.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,20 @@ describe('node-fetch', () => {
489489
});
490490
});
491491

492+
it('should set redirected property on response when redirect', function() {
493+
const url = `${base}redirect/301`;
494+
return fetch(url).then(res => {
495+
expect(res.redirected).to.be.true;
496+
});
497+
});
498+
499+
it('should not set redirected property on response without redirect', function() {
500+
const url = `${base}hello`;
501+
return fetch(url).then(res => {
502+
expect(res.redirected).to.be.false;
503+
});
504+
});
505+
492506
it('should ignore invalid headers', function() {
493507
var headers = {
494508
'Invalid-Header ': 'abc\r\n',
@@ -2196,12 +2210,12 @@ describe('Response', function () {
21962210
}
21972211
for (const toCheck of [
21982212
'body', 'bodyUsed', 'arrayBuffer', 'blob', 'json', 'text',
2199-
'url', 'status', 'ok', 'statusText', 'headers', 'clone'
2213+
'url', 'status', 'ok', 'redirected', 'statusText', 'headers', 'clone'
22002214
]) {
22012215
expect(enumerableProperties).to.contain(toCheck);
22022216
}
22032217
for (const toCheck of [
2204-
'body', 'bodyUsed', 'url', 'status', 'ok', 'statusText',
2218+
'body', 'bodyUsed', 'url', 'status', 'ok', 'redirected', 'statusText',
22052219
'headers'
22062220
]) {
22072221
expect(() => {

0 commit comments

Comments
 (0)
Please sign in to comment.