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

Add support for Response.error() #1078

Merged
merged 2 commits into from May 3, 2021
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
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"
tkrotoff marked this conversation as resolved.
Show resolved Hide resolved

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()`
tkrotoff marked this conversation as resolved.
Show resolved Hide resolved
- `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
17 changes: 16 additions & 1 deletion src/response.js
Expand Up @@ -21,7 +21,9 @@ export default class Response extends Body {
constructor(body = null, options = {}) {
super(body, options);

const status = options.status || 200;
// 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 +34,7 @@ export default class Response extends Body {
}

this[INTERNALS] = {
type: 'default',
url: options.url,
status,
statusText: options.statusText || '',
Expand All @@ -41,6 +44,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 +86,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 +115,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('');
});
});