node-fetch/src/response.js

67 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-04-05 10:43:12 -07:00
2015-01-27 05:11:26 -08:00
/**
* response.js
*
* Response class provides content decoding
*/
import { STATUS_CODES } from 'http';
import Headers from './headers.js';
import Body, { clone } from './body';
2015-01-27 05:11:26 -08:00
/**
* Response class
*
* @param Stream body Readable stream
* @param Object opts Response options
* @return Void
*/
export default class Response extends Body {
constructor(body = null, opts = {}) {
super(body, opts);
this.url = opts.url;
this.status = opts.status || 200;
this.statusText = opts.statusText || STATUS_CODES[this.status];
this.headers = new Headers(opts.headers);
Object.defineProperty(this, Symbol.toStringTag, {
value: 'Response',
writable: false,
enumerable: false,
configurable: true
});
}
/**
* Convenience property representing if the request ended normally
*/
get ok() {
return this.status >= 200 && this.status < 300;
}
/**
* Clone this response
*
* @return Response
*/
clone() {
return new Response(clone(this), {
url: this.url
, status: this.status
, statusText: this.statusText
, headers: this.headers
, ok: this.ok
});
}
2015-01-27 05:11:26 -08:00
}
Object.defineProperty(Response.prototype, Symbol.toStringTag, {
value: 'ResponsePrototype',
writable: false,
enumerable: false,
configurable: true
});