we implement a subset of superagent’s API. for the other methods,
for now, we’ll just throw an exception if they’re called. By default,
all methods throw exceptions, and we override some below
Object.keys(superagent.Request.prototype)
.forEach( propName => {
var originalProp = superagent.Request.prototype[propName];
if (typeof originalProp === 'function') {
Request.prototype[propName] = function () {
throw new Error(`${propName}() from superagent's API isn't implemented yet.`);
}
}
});
Request.prototype.agent = function (agent) {
if (typeof agent === 'undefined') {
return this._agent;
}
this._agent = agent;
return this;
}
Request.prototype.method = function (method) {
if (typeof method === 'undefined') {
return this._method;
}
this._method = method;
return this;
}
Request.prototype.urlPath = function (urlPath) {
if (typeof urlPath === 'undefined') {
return this._urlPath;
}
this._urlPath = urlPath;
return this;
}
Request.prototype.query = function (queryParams) {
if (typeof queryParams === 'undefined') {
throw new Error("Request.query does not support retrieving the current query string");
}
this._queryParams.push(queryParams);
return this;
}
Request.prototype.send = function (postParams) {
if (typeof postParams === 'undefined') {
return merge({}, this._postParams || {});
}
if (postParams !== null) {
if (this._postParams === null) {
this._postParams = {};
}
merge(this._postParams, postParams);
}
return this;
}
Request.prototype.set = function (headers) {
if (typeof headers === 'undefined') {
return merge({}, this._headers);
}
merge(this._headers, headers);
return this;
}
Request.prototype.timeout = function (timeout) {
if (typeof timeout === 'undefined') {
return this._timeout;
}
this._timeout = timeout;
return this;
}
Request.prototype.type = function (type) {
if (typeof type === 'undefined') {
return this._type;
}
this._type = type;
return this;
}
Request.prototype.toJSON = function(){
return {
aborted: this.aborted,
cacheWhitelist: this._cacheWhitelist,
headers: this._headers,
method: this._method,
postParams: this._postParams,
queryParams: this._queryParams,
timeout: this._timeout,
type: this._type,
urlPath: this._urlPath,
};
}
Request.prototype.end = function (fn) {
if (!fn || fn.length !== 2) {