Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.5.0 release #630

Merged
merged 4 commits into from May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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.
Expand Down
15 changes: 10 additions & 5 deletions README.md
Expand Up @@ -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]])
Expand All @@ -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

<small>*(spec-compliant)*</small>

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.

<a id="class-headers"></a>
### Class: Headers

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/response.js
Expand Up @@ -40,7 +40,8 @@ export default class Response {
url: opts.url,
status,
statusText: opts.statusText || STATUS_CODES[status],
headers
headers,
counter: opts.counter
};
}

Expand All @@ -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;
}
Expand All @@ -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
});

}
}

Expand All @@ -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 }
Expand Down
18 changes: 16 additions & 2 deletions test/test.js
Expand Up @@ -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',
Expand Down Expand Up @@ -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(() => {
Expand Down