2016-04-05 10:43:12 -07:00
|
|
|
|
2015-01-27 05:11:26 -08:00
|
|
|
/**
|
|
|
|
* response.js
|
|
|
|
*
|
|
|
|
* Response class provides content decoding
|
|
|
|
*/
|
|
|
|
|
2018-07-22 09:58:03 -07:00
|
|
|
import { STATUS_CODES } from 'http';
|
2016-10-10 11:50:04 -07:00
|
|
|
import Headers from './headers.js';
|
|
|
|
import Body, { clone } from './body';
|
2015-01-27 05:11:26 -08:00
|
|
|
|
2018-01-27 12:28:56 -08:00
|
|
|
const INTERNALS = Symbol('Response internals');
|
|
|
|
|
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
|
|
|
|
2018-01-27 12:28:56 -08:00
|
|
|
const status = opts.status || 200;
|
2017-03-20 09:22:49 -07:00
|
|
|
|
2018-01-27 12:28:56 -08:00
|
|
|
this[INTERNALS] = {
|
|
|
|
url: opts.url,
|
|
|
|
status,
|
|
|
|
statusText: opts.statusText || STATUS_CODES[status],
|
|
|
|
headers: new Headers(opts.headers)
|
|
|
|
};
|
|
|
|
}
|
2016-11-23 14:36:08 -08:00
|
|
|
|
2018-01-27 12:28:56 -08:00
|
|
|
get url() {
|
|
|
|
return this[INTERNALS].url;
|
|
|
|
}
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
return this[INTERNALS].status;
|
2016-10-10 11:50:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience property representing if the request ended normally
|
|
|
|
*/
|
|
|
|
get ok() {
|
2018-01-27 12:28:56 -08:00
|
|
|
return this[INTERNALS].status >= 200 && this[INTERNALS].status < 300;
|
|
|
|
}
|
|
|
|
|
|
|
|
get statusText() {
|
|
|
|
return this[INTERNALS].statusText;
|
|
|
|
}
|
|
|
|
|
|
|
|
get headers() {
|
|
|
|
return this[INTERNALS].headers;
|
2016-10-10 11:50:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone this response
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
clone() {
|
|
|
|
return new Response(clone(this), {
|
2018-03-04 20:29:12 -08:00
|
|
|
url: this.url,
|
|
|
|
status: this.status,
|
|
|
|
statusText: this.statusText,
|
|
|
|
headers: this.headers,
|
|
|
|
ok: this.ok
|
2016-10-10 11:50:04 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
2015-01-27 05:11:26 -08:00
|
|
|
}
|
2016-11-23 14:36:08 -08:00
|
|
|
|
2017-02-26 14:42:09 -08:00
|
|
|
Body.mixIn(Response.prototype);
|
2017-02-26 14:29:40 -08:00
|
|
|
|
2018-01-27 12:28:56 -08:00
|
|
|
Object.defineProperties(Response.prototype, {
|
|
|
|
url: { enumerable: true },
|
|
|
|
status: { enumerable: true },
|
|
|
|
ok: { enumerable: true },
|
|
|
|
statusText: { enumerable: true },
|
|
|
|
headers: { enumerable: true },
|
|
|
|
clone: { enumerable: true }
|
|
|
|
});
|
|
|
|
|
2016-11-23 14:36:08 -08:00
|
|
|
Object.defineProperty(Response.prototype, Symbol.toStringTag, {
|
2018-01-27 12:28:56 -08:00
|
|
|
value: 'Response',
|
2016-11-23 14:36:08 -08:00
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true
|
|
|
|
});
|