mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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:
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user