Completed
Branch es2015 (569745)
by Luís
02:57
created

Request.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 3
b 0
f 3
1
import App from "app";
2
3
const sa = App.libs.Superagent;
4
const EM = App.ServicesContainer.getNewInstance("EventManager");
5
const _ = App.libs._;
6
7
/**
8
 * @callback requestCallback
9
 * @param {mixed}   error
10
 * @param {Object}  response
11
 * @param {Object}  reqquest
12
 */
13
14
/**
15
 * @callback successfulRequestCallback
16
 * @param {mixed}   error
17
 * @param {Object}  response
18
 * @param {Object}  reqquest
19
 */
20
21
/**
22
 * Utility class to make XHR requests
23
 * 
24
 * @class Request
25
 */
26
class Request {
27
    constructor() {
28
        this.EM = EM;
29
    }
30
31
    /**
32
     * Register a new event listener to be called when a new request is started
33
     * @param  {Function} parameters: req
0 ignored issues
show
Documentation introduced by
The parameter parameters: does not exist. Did you maybe forget to remove this comment?
Loading history...
34
     * @return {Request}
35
     */
36
    onStart(cb) {
37
        "use strict";
38
39
        this.EM.subscribe("start", cb);
40
        return this;
41
    }
42
43
    /**
44
     * Register a new event listener to be called when an request is completed
45
     * @param  {requestCallback} cb
46
     * @return {Request}
47
     */
48
    onStop(cb) {
49
        "use strict";
50
51
        this.EM.subscribe("stop", cb);
52
        return this;
53
    }
54
55
    /**
56
     * Register a new event listener to be called when an request fails
57
     * @param  {requestCallback} cb
58
     * @return {Request}
59
     */
60
    onError(cb) {
61
        "use strict";
62
63
        this.EM.subscribe("error", cb);
64
        return this;
65
    }
66
67
    /**
68
     * Register a new event listener to be called when an request succeeds
69
     * @param  {successfulRequestCallback} cb
70
     * @return {Request}
71
     */
72
    onSuccess(cb) {
73
        "use strict";
74
75
        this.EM.subscribe("success", cb);
76
        return this;
77
    }
78
79
    /**
80
     * make a xhr
81
     * @param  {String} method    get|post|put|delete|del
82
     * @param  {String} url
83
     * @param  {Object} data
84
     * @param  {successfulRequestCallback} onSuccess function to be called if the request succeeds
85
     * @param  {requestCallback} onError   function to be called if the request fails
86
     * @return {null}
87
     */
88
    send(method, url, data, onSuccess, onError) {
89
        "use strict";
90
91
        const req = sa[method](url);
92
93
        if (method === "get") {
94
            req.query(data);
95
        } else {
96
            req.send(data);
97
        }
98
99
        this.EM.notify("start", req);
100
101
        req.end((err, res) => {
102
            this.EM.notify("stop", err, res, req);
103
104
            if (err) {
105
                this.EM.notify("error", err, res, req);
106
107
                if (onError) {
108
                    onError(err, res, req);
109
                }
110
            } else {
111
                this.EM.notify("success", res, req);
112
113
                if (onSuccess) {
114
                    onSuccess(res, req);
115
                }
116
            }
117
        });
118
    }
119
120
    /**
121
     * make a xhr with more options
122
     * @param  {Object} config
123
     * @param  {String} config.url
0 ignored issues
show
Documentation Bug introduced by
The parameter config.url does not exist. Did you maybe mean config instead?
Loading history...
124
     * @param  {String} config.method get|post|put|del|delete
0 ignored issues
show
Documentation Bug introduced by
The parameter config.method does not exist. Did you maybe mean config instead?
Loading history...
125
     * @param  {Function} config.onStart function to call before send the resquest
0 ignored issues
show
Documentation Bug introduced by
The parameter config.onStart does not exist. Did you maybe mean config instead?
Loading history...
126
     * @param  {requestCallback} config.onStop
0 ignored issues
show
Documentation Bug introduced by
The parameter config.onStop does not exist. Did you maybe mean config instead?
Loading history...
127
     * @param  {successfulRequestCallback} config.onSuccess
0 ignored issues
show
Documentation Bug introduced by
The parameter config.onSuccess does not exist. Did you maybe mean config instead?
Loading history...
128
     * @param  {requestCallback} config.onError
0 ignored issues
show
Documentation Bug introduced by
The parameter config.onError does not exist. Did you maybe mean config instead?
Loading history...
129
     * @param  {Object} config.headers
0 ignored issues
show
Documentation Bug introduced by
The parameter config.headers does not exist. Did you maybe mean config instead?
Loading history...
130
     * @param  {Object} config.data
0 ignored issues
show
Documentation Bug introduced by
The parameter config.data does not exist. Did you maybe mean config instead?
Loading history...
131
     */
132
    make(config) {
133
        "use strict";
134
135
        let defaultOptions = {
136
            url: "",
137
            method: "get",
138
            onStart: null,
139
            onStop: null,
140
            onSuccess: null,
141
            onError: null,
142
            headers: {},
143
            data: {}
144
        };
145
146
        let options = _.extend(defaultOptions, config);
147
148
        if (options.method === "delete") {
149
            options.method = "del";
150
        }
151
152
        let req = sa[options.method](options.url).send(options.data);
153
154
        this.EM.notify("start", req);
155
        if (typeof options.onStart === "function") {
156
            options.onStart(req);
157
        }
158
159
        // headers
160
        _.forEach(options.headers, function (value, key) {
161
            req.set(key, value);
162
        });
163
164
        req.end((err, res) => {
165
            this.EM.notify("stop", err, res, req);
166
            if (typeof options.onStop === "function") {
167
                options.onStop(err, res, req);
168
            }
169
170
            if (err) {
171
                this.EM.notify("error", err, res, req);
172
                if (typeof options.onError === "function") {
173
                    options.onError(err, res, req);
174
                }
175
            } else {
176
                this.EM.notify("success", res, req);
177
178
                if (typeof options.onSuccess === "function") {
179
                    options.onSuccess(res, req);
180
                }
181
            }
182
        });
183
    }
184
}
185
186
export default Request;
187