Get rid of Body class

The Body class was inlined in the Request and Response classes.
This patch:
- removes the Body class
- adds Request.postData public property
- adds Response.buffer(), Response.text() and Response.json() methods

Fixes #106.
This commit is contained in:
Andrey Lushnikov
2017-07-27 23:11:24 -07:00
parent 8d7d15fdb1
commit ba37a4f82d
3 changed files with 66 additions and 80 deletions

View File

@@ -137,14 +137,42 @@ class NetworkManager extends EventEmitter {
}
}
class Body {
class Request {
/**
* @param {!Object} payload
*/
constructor(payload) {
this._response = null;
this.url = payload.url;
this.method = payload.method;
this.postData = payload.postData;
this.headers = new Map(Object.entries(payload.headers));
}
/**
* @return {?Response}
*/
response() {
return this._response;
}
}
helper.tracePublicAPI(Request);
class Response {
/**
* @param {?Request} request
* @param {!Object} payload
* @param {function():!Promise<!Buffer>} contentCallback
*/
constructor(contentCallback) {
constructor(request, payload, contentCallback) {
this._request = request;
this._contentCallback = contentCallback;
/** @type {?Promise<!Buffer>} */
this._contentPromise = null;
this.headers = new Map(Object.entries(payload.headers));
this.ok = payload.status >= 200 && payload.status <= 299;
this.status = payload.status;
this.statusText = payload.statusText;
this.url = payload.url;
}
/**
@@ -171,45 +199,6 @@ class Body {
let content = await this.text();
return JSON.parse(content);
}
}
helper.tracePublicAPI(Body);
class Request extends Body {
/**
* @param {!Object} payload
*/
constructor(payload) {
super(() => Promise.resolve(payload.postData || ''));
this._response = null;
this.url = payload.url;
this.method = payload.method;
this.headers = new Map(Object.entries(payload.headers));
}
/**
* @return {?Response}
*/
response() {
return this._response;
}
}
helper.tracePublicAPI(Request);
class Response extends Body {
/**
* @param {?Request} request
* @param {!Object} payload
* @param {function():!Promise<!Buffer>} contentCallback
*/
constructor(request, payload, contentCallback) {
super(contentCallback);
this._request = request;
this.headers = new Map(Object.entries(payload.headers));
this.ok = payload.status >= 200 && payload.status <= 299;
this.status = payload.status;
this.statusText = payload.statusText;
this.url = payload.url;
}
/**
* @return {?Response}