Skip to content

Commit

Permalink
Add support for Response.error()
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrotoff committed Jan 27, 2021
1 parent 6ee9d31 commit 83c2549
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions @types/index.d.ts
Expand Up @@ -142,6 +142,8 @@ declare class Request extends Body {
clone(): Request;
}

type ResponseType = "basic" | "cors" | "default" | "error" | "opaque" | "opaqueredirect"

declare class Response extends Body {
constructor(body?: BodyInit | null, init?: ResponseInit);

Expand All @@ -150,8 +152,11 @@ declare class Response extends Body {
readonly redirected: boolean;
readonly status: number;
readonly statusText: string;
readonly type: ResponseType;
readonly url: string;
clone(): Response;

static error(): Response;
}

declare class FetchError extends Error {
Expand Down
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -56,6 +56,7 @@
- [new Response([body[, options]])](#new-responsebody-options)
- [response.ok](#responseok)
- [response.redirected](#responseredirected)
- [response.type](#responsetype)
- [Class: Headers](#class-headers)
- [new Headers([init])](#new-headersinit)
- [Interface: Body](#interface-body)
Expand Down Expand Up @@ -587,9 +588,6 @@ An HTTP(S) response. This class implements the [Body](#iface-body) interface.

The following properties are not implemented in node-fetch at this moment:

- `Response.error()`
- `Response.redirect()`
- `type`
- `trailer`

#### new Response([body[, options]])
Expand All @@ -615,6 +613,12 @@ Convenience property representing if the request ended normally. Will evaluate t

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.

#### response.type

<small>_(deviation from spec)_</small>

Convenience property representing the response's type. node-fetch only supports `'default'` and `'error'` and does not make use of [filtered responses](https://fetch.spec.whatwg.org/#concept-filtered-response).

<a id="class-headers"></a>

### Class: Headers
Expand Down
18 changes: 17 additions & 1 deletion src/response.js
Expand Up @@ -21,7 +21,10 @@ export default class Response extends Body {
constructor(body = null, options = {}) {
super(body, options);

const status = options.status || 200;
// TODO Replace with nullish coalescing operator (??) when support for Node.js < 14 is dropped
// eslint-disable-next-line no-eq-null, eqeqeq, no-negated-condition
const status = options.status != null ? options.status : 200;

const headers = new Headers(options.headers);

if (body !== null && !headers.has('Content-Type')) {
Expand All @@ -32,6 +35,7 @@ export default class Response extends Body {
}

this[INTERNALS] = {
type: 'default',
url: options.url,
status,
statusText: options.statusText || '',
Expand All @@ -41,6 +45,10 @@ export default class Response extends Body {
};
}

get type() {
return this[INTERNALS].type;
}

get url() {
return this[INTERNALS].url || '';
}
Expand Down Expand Up @@ -79,6 +87,7 @@ export default class Response extends Body {
*/
clone() {
return new Response(clone(this, this.highWaterMark), {
type: this.type,
url: this.url,
status: this.status,
statusText: this.statusText,
Expand Down Expand Up @@ -107,12 +116,19 @@ export default class Response extends Body {
});
}

static error() {
const response = new Response(null, {status: 0, statusText: ''});
response[INTERNALS].type = 'error';
return response;
}

get [Symbol.toStringTag]() {
return 'Response';
}
}

Object.defineProperties(Response.prototype, {
type: {enumerable: true},
url: {enumerable: true},
status: {enumerable: true},
ok: {enumerable: true},
Expand Down
11 changes: 11 additions & 0 deletions test/response.js
Expand Up @@ -35,6 +35,7 @@ describe('Response', () => {
'blob',
'json',
'text',
'type',
'url',
'status',
'ok',
Expand All @@ -49,6 +50,7 @@ describe('Response', () => {
for (const toCheck of [
'body',
'bodyUsed',
'type',
'url',
'status',
'ok',
Expand Down Expand Up @@ -125,6 +127,7 @@ describe('Response', () => {
});
const cl = res.clone();
expect(cl.headers.get('a')).to.equal('1');
expect(cl.type).to.equal('default');
expect(cl.url).to.equal(base);
expect(cl.status).to.equal(346);
expect(cl.statusText).to.equal('production');
Expand Down Expand Up @@ -205,4 +208,12 @@ describe('Response', () => {
const res = new Response();
expect(res.url).to.equal('');
});

it('should support error() static method', () => {
const res = Response.error();
expect(res).to.be.an.instanceof(Response);
expect(res.type).to.equal('error');
expect(res.status).to.equal(0);
expect(res.statusText).to.equal('');
});
});

0 comments on commit 83c2549

Please sign in to comment.