x-http-client
Version:
An http client to make it easier to send requests (including JSONP requests) to the server.
83 lines (68 loc) • 2.07 kB
JavaScript
var uuid = require('../shared/uuid');
/**
* The base Reqeust class.
*
* @class
* @param {string} type The type of request, can be `HTTP_REQUEST` or `JSONP_REQUEST`.
* @param {RequestOptions} options The request options.
* @param {RequestSuccessCallback} onsuccess The callback to call on success.
* @param {RequestErrorCallback} onerror The callback to call on error.
*/
function Request(type, options, onsuccess, onerror) {
/**
* If there is an error happend, the `errorCode` is a string reprsengting the type of the error. If there is no
* error, the value of `errorCode` is `null`.
*/
this.errorCode = null;
/**
* The `XMLHttpRequest` we use when sending http request.
*/
this.xhr = null;
/**
* The `HTMLScriptElement` we use when sending JSONP request.
*/
this.script = null;
/**
* Whether the request is finished.
*/
this.finished = false;
/**
* The response JSON data of the JSONP request.
*/
this.responseJSON = null;
/**
* An unique id for this request.
*/
this.requestId = uuid();
/**
* The type of request, can be `HTTP_REQUEST` or `JSONP_REQUEST`.
*/
this.requestType = type;
/**
* The request options.
*/
this.options = options;
/**
* The name of the function that create this request. Can be `send`, `fetch`, `getJOSNP`, `fetchJSONP`. This value
* is set by the libray itself.
*/
this.requestFunctionName = options.requestFunctionName;
/**
* The `CancelController` that used to cancel this request. We never use this property internally, just holding the
* information in case that the user needs.
*/
this.controller = options.controller || null;
/**
* The callback to call on success.
*/
this.onsuccess = onsuccess || null;
/**
* The callback to call on error.
*/
this.onerror = onerror || null;
/**
* Set the request type back.
*/
options.requestType = type;
}
module.exports = Request;