UNPKG

2.06 kBJavaScriptView Raw
1var uuid = require('../shared/uuid');
2
3/**
4 * The base Reqeust class.
5 *
6 * @class
7 * @param {string} type The type of request, can be `HTTP_REQUEST` or `JSONP_REQUEST`.
8 * @param {RequestOptions} options The request options.
9 * @param {RequestSuccessCallback} onsuccess The callback to call on success.
10 * @param {RequestErrorCallback} onerror The callback to call on error.
11 */
12function Request(type, options, onsuccess, onerror) {
13 /**
14 * If there is an error happend, the `error` is a string reprsengting the type of the error. If there is no
15 * error, the value of `error` is `null`.
16 */
17 this.error = null;
18
19 /**
20 * The `XMLHttpRequest` we use when sending http request.
21 */
22 this.xhr = null;
23
24 /**
25 * The `HTMLScriptElement` we use when sending JSONP request.
26 */
27 this.script = null;
28
29 /**
30 * Whether the request is finished.
31 */
32 this.finished = false;
33
34 /**
35 * The response JSON data of the JSONP request.
36 */
37 this.responseJSON = null;
38
39 /**
40 * An unique id for this request.
41 */
42 this.requestId = uuid();
43
44 /**
45 * The type of request, can be `HTTP_REQUEST` or `JSONP_REQUEST`.
46 */
47 this.requestType = type;
48
49 /**
50 * The request options.
51 */
52 this.options = options;
53
54 /**
55 * The name of the function that create this request. Can be `send`, `fetch`, `getJOSNP`, `fetchJSONP`. This value
56 * is set by the libray itself.
57 */
58 this.requestFunctionName = options.requestFunctionName;
59
60 /**
61 * The `CancelController` that used to cancel this request. We never use this property internally, just holding the
62 * information in case that the user needs.
63 */
64 this.controller = options.controller || null;
65
66 /**
67 * The callback to call on success.
68 */
69 this.onsuccess = onsuccess || null;
70
71 /**
72 * The callback to call on error.
73 */
74 this.onerror = onerror || null;
75
76 /**
77 * Set the request type back.
78 */
79 options.requestType = type;
80}
81
82module.exports = Request;