2016-04-05 10:43:12 -07:00
|
|
|
|
2015-01-27 05:11:26 -08:00
|
|
|
/**
|
|
|
|
* response.js
|
|
|
|
*
|
|
|
|
* Response class provides content decoding
|
|
|
|
*/
|
|
|
|
|
2016-10-10 11:50:04 -07:00
|
|
|
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
|
|
|
|
*/
|
2017-02-26 14:29:40 -08:00
|
|
|
export default class Response {
|
2016-10-08 19:40:56 -07:00
|
|
|
constructor(body = null, opts = {}) {
|
2017-02-26 14:29:40 -08:00
|
|
|
Body.call(this, body, opts);
|
2016-10-10 11:50:04 -07:00
|
|
|
|
|
|
|
this.url = opts.url;
|
|
|
|
this.status = opts.status || 200;
|
|
|
|
this.statusText = opts.statusText || STATUS_CODES[this.status];
|
|
|
|
this.headers = new Headers(opts.headers);
|
2016-11-23 14:36:08 -08:00
|
|
|
|
|
|
|
Object.defineProperty(this, Symbol.toStringTag, {
|
|
|
|
value: 'Response',
|
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true
|
|
|
|
});
|
2016-10-10 11:50:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
2016-11-23 14:36:08 -08:00
|
|
|
|
2017-02-26 14:29:40 -08:00
|
|
|
for (const name of Object.getOwnPropertyNames(Body.prototype)) {
|
|
|
|
if (!(name in Response.prototype)) {
|
|
|
|
const desc = Object.getOwnPropertyDescriptor(Body.prototype, name);
|
|
|
|
Object.defineProperty(Response.prototype, name, desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:36:08 -08:00
|
|
|
Object.defineProperty(Response.prototype, Symbol.toStringTag, {
|
|
|
|
value: 'ResponsePrototype',
|
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true
|
|
|
|
});
|