src/js/services/ajax/Request.js   A
last analyzed

Size

Lines of Code 194

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 22.81%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
nc 1
dl 0
loc 194
ccs 13
cts 57
cp 0.2281
rs 10
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A Request.js ➔ ??? 0 3 1
1 14
import App from "app";
2
3
const sa = App.libs.Superagent;
4
const EM = App.ServicesContainer.getNewInstance("EventManager");
5
6
/**
7
 * @callback requestCallback
8
 * @param {mixed}   error
9
 * @param {Object}  response
10
 * @param {Object}  reqquest
11
 */
12
13
/**
14
 * @callback successfulRequestCallback
15
 * @param {mixed}   error
16
 * @param {Object}  response
17
 * @param {Object}  reqquest
18
 */
19
20
/**
21
 * Utility class to make XHR requests
22
 * 
23
 * @class Request
24
 */
25
class Request {
26
    constructor() {
27
        this.EM = EM;
28
    }
29
30
    /**
31
     * Register a new event listener to be called when a new request is started
32
     * @param  {requestCallback} cb
33
     * @return {Request}
34
     */
35
    onStart(cb) {
36
        "use strict";
37
38
        this.EM.subscribe("start", cb);
39
        return this;
40
    }
41
42
    /**
43
     * Register a new event listener to be called when an request is completed
44
     * @param  {requestCallback} cb
45
     * @return {Request}
46
     */
47
    onStop(cb) {
48
        "use strict";
49
50
        this.EM.subscribe("stop", cb);
51
        return this;
52
    }
53
54
    /**
55
     * Register a new event listener to be called when an request fails
56
     * @param  {requestCallback} cb
57
     * @return {Request}
58
     */
59
    onError(cb) {
60
        "use strict";
61
62
        this.EM.subscribe("error", cb);
63
        return this;
64
    }
65
66
    /**
67
     * Register a new event listener to be called when an request succeeds
68
     * @param  {successfulRequestCallback} cb
69
     * @return {Request}
70
     */
71
    onSuccess(cb) {
72
        "use strict";
73
74
        this.EM.subscribe("success", cb);
75
        return this;
76
    }
77
78
    /**
79
     * make a xhr
80
     * @param  {String} method get|post|put|delete|del
81
     * @param  {String} url
82
     * @param  {Object} data
83
     * @param  {successfulRequestCallback} onSuccess function to be called if the request succeeds
84
     * @param  {requestCallback} onError   function to be called if the request fails
85
     * @return {null}
86
     */
87
    send(method, url, data, onSuccess, onError) {
88
        "use strict";
89
90
        const req = sa[method](url);
91
92 2
        if (method === "get") {
93
            req.query(data);
94
        } else {
95
            req.send(data);
96
        }
97
98
        this.EM.notify("start", req);
99
100
        req.end((err, res) => {
101
            this.EM.notify("stop", err, res, req);
102
103 2
            if (err) {
104
                this.EM.notify("error", err, res, req);
105
106 2
                if (onError) {
107
                    onError(err, res, req);
108
                }
109
            } else {
110
                this.EM.notify("success", res, req);
111
112 2
                if (onSuccess) {
113
                    onSuccess(res, req);
114
                }
115
            }
116
        });
117
    }
118
119
    /**
120
     * make a xhr with more options
121
     * @param  {{url: string, method: string, onStart: function, onStop: function, onSuccess: function, onError: function, headers: Object, type: string, data: Object}} config
122
     * @param  {String} config.url URL to make the request
123
     * @param  {String} config.method The method. One of: get, post, put, del, delete
124
     * @param  {Function} config.onStart Callback called before send the resquest
125
     * @param  {requestCallback} config.onStop Callback called after complete the request
126
     * @param  {successfulRequestCallback} config.onSuccess Callback for successful request. Called after onStop
127
     * @param  {requestCallback} config.onError Callback for unsuccessful request. Called after onStop
128
     * @param  {Object} config.headers Option headers. Example: {accept: "application/json"}
129
     * @param  {Object} config.data Data to send to the server
130
     */
131
    make(config) {
132
        "use strict";
133
134
        let defaultOptions = {
135
            url: "",
136
            method: "get",
137
            onStart: null,
138
            onStop: null,
139
            onSuccess: null,
140
            onError: null,
141
            headers: {},
142
            type: null,
143
            data: {}
144
        };
145
146
        let options = Object.assign({}, defaultOptions, config);
147
148 2
        if (options.method === "delete") {
149
            options.method = "del";
150
        }
151
152
        let req = sa[options.method](options.url);
153
154 2
        if (options.method === "get") {
155
            req.query(options.data);
156
        } else {
157
            req.send(options.data);
158
        }
159
160 2
        if (options.type) {
161
            req.type(options.type);
162
        }
163
164
        this.EM.notify("start", req);
165 2
        if (typeof options.onStart === "function") {
166
            options.onStart(req);
167
        }
168
169
        // headers
170
        Object.keys(options.headers).map((header) => req.set(header, options.headers[header]));
171
172
        req.end((err, res) => {
173
            this.EM.notify("stop", err, res, req);
174 2
            if (typeof options.onStop === "function") {
175
                options.onStop(err, res, req);
176
            }
177
178 2
            if (err) {
179
                this.EM.notify("error", err, res, req);
180 2
                if (typeof options.onError === "function") {
181
                    options.onError(err, res, req);
182
                }
183
            } else {
184
                this.EM.notify("success", res, req);
185
186 2
                if (typeof options.onSuccess === "function") {
187
                    options.onSuccess(res, req);
188
                }
189
            }
190
        });
191
    }
192
}
193
194
export default Request;
195