src/http.js

Maintainability

76.81

Lines of code

214

Created with Raphaël 2.1.002550751002019-3-122018-12-32018-8-22018-5-92018-4-32018-2-282017-12-212017-11-12017-6-7

2019-10-30
Maintainability: 76.81

Created with Raphaël 2.1.00751502253002019-3-122018-12-32018-8-22018-5-92018-4-32018-2-282017-12-212017-11-12017-6-7

2019-10-30
Lines of Code: 214

Difficulty

17.51

Estimated Errors

0.35

Function weight

By Complexity

Created with Raphaël 2.1.0_gpfHttpSetRequestImplIf2

By SLOC

Created with Raphaël 2.1.0_gpfProcessAlias12
1
/**
2
 * @file HTTP helper
3
 * @since 0.2.1
4
 */
5
/*#ifndef(UMD)*/
6
"use strict";
7
/*global _GPF_HTTP_METHODS*/ // HTTP Methods
8
/*global _gpfHost*/ // Host type
9
/*exported _gpfHttpRequest*/ // HTTP request common implementation
10
/*exported _gpfHttpSetRequestImplIf*/ // Set the http request implementation if the host matches
11
/*#endif*/
12
 
13
/**
14
 * HTTP request settings
15
 *
16
 * @typedef gpf.typedef.httpRequestSettings
17
 * @property {gpf.http.methods} [method=gpf.http.methods.get] HTTP method. Custom verbs (use any string) may be
18
 * supported depending on the host (see {@tutorial COMPATIBILITY}).
19
 * @property {String} url URL to submit the request to
20
 * @property {Object} [headers] Request headers
21
 * @property {String} [data] Request data, valid only for {@link gpf.http.methods.post} and {@link gpf.http.methods.put}
22
 *
23
 * @see gpf.http.request
24
 * @since 0.2.1
25
 */
26
 
27
/**
28
 * HTTP request response
29
 *
30
 * @typedef gpf.typedef.httpRequestResponse
31
 * @property {int} status HTTP status
32
 * @property {Object} headers HTTP response headers
33
 * @property {String} responseText Response Text
34
 *
35
 * @see gpf.http.request
36
 * @since 0.2.1
37
 */
38
 
39
/**
40
 * HTTP request host specific implementation
41
 *
42
 * @type {Function}
43
 * @since 0.2.1
44
 */
45
var _gpfHttpRequestImpl;
46
 
47
/**
48
 * Set the http request implementation if the host matches
49
 *
50
 * @param {String} host host to test, if matching with the current one, the http request implementation is set
51
 * @param {Function} httpRequestImpl http request implementation function
52
 * @return {Function} Previous host specific implementation
53
 * @since 0.2.6
54
 */
55
function _gpfHttpSetRequestImplIf (host, httpRequestImpl) {
56
    var result = _gpfHttpRequestImpl;
57
    if (host === _gpfHost) {
58
        _gpfHttpRequestImpl = httpRequestImpl;
59
    }
60
    return result;
61
}
62
 
63
/**
64
 * HTTP request common implementation
65
 *
66
 * @param {gpf.typedef.httpRequestSettings} request HTTP Request settings
67
 * @return {Promise<gpf.typedef.httpRequestResponse>} Resolved on request completion
68
 * @since 0.2.1
69
 */
70
function _gpfHttpRequest (request) {
71
    return new Promise(function (resolve, reject) {
72
        _gpfHttpRequestImpl(request, resolve, reject);
73
    });
74
}
75
 
76
/**
77
 * Implementation of aliases
78
 *
79
 * @param {String} method HTTP method to apply
80
 * @param {String|gpf.typedef.httpRequestSettings} url Url to send the request to or a request settings object
81
 * @param {*} [data] Data to be sent to the server
82
 * @return {Promise<gpf.typedef.httpRequestResponse>} HTTP request promise
83
 * @since 0.2.1
84
 */
85
function _gpfProcessAlias (method, url, data) {
86
    if (typeof url === "string") {
87
        return _gpfHttpRequest({
88
            method: method,
89
            url: url,
90
            data: data
91
        });
92
    }
93
    return _gpfHttpRequest(Object.assign({
94
        method: method
95
    }, url));
96
}
97
 
98
Object.assign(gpf.http, /** @lends gpf.http */ {
99
 
100
    /**
101
     * HTTP methods enumeration
102
     *
103
     * @enum {String}
104
     * @readonly
105
     * @since 0.2.1
106
     */
107
    methods: {
108
 
109
        /**
110
         * GET
111
         * @since 0.2.1
112
         */
113
        get: _GPF_HTTP_METHODS.GET,
114
 
115
        /**
116
         * POST
117
         * @since 0.2.1
118
         */
119
        post: _GPF_HTTP_METHODS.POST,
120
 
121
        /**
122
         * PUT
123
         * @since 0.2.1
124
         */
125
        put: _GPF_HTTP_METHODS.PUT,
126
 
127
        /**
128
         * OPTIONS
129
         * @since 0.2.1
130
         */
131
        options: _GPF_HTTP_METHODS.OPTIONS,
132
 
133
        /**
134
         * DELETE
135
         * @since 0.2.1
136
         */
137
        "delete": _GPF_HTTP_METHODS.DELETE,
138
 
139
        /**
140
         * HEAD
141
         * @since 0.2.2
142
         */
143
        head: _GPF_HTTP_METHODS.HEAD
144
    },
145
 
146
    /**
147
     * @gpf:sameas _gpfHttpRequest
148
     * @since 0.2.1
149
     */
150
    request: _gpfHttpRequest,
151
 
152
    /**
153
     * HTTP GET request
154
     *
155
     * @method
156
     * @param {String|gpf.typedef.httpRequestSettings} urlOrRequest URL or HTTP Request settings
157
     * @return {Promise<gpf.typedef.httpRequestResponse>} Resolved on request completion
158
     * @since 0.2.1
159
     */
160
    get: _gpfProcessAlias.bind(gpf.http, _GPF_HTTP_METHODS.GET),
161
 
162
    /**
163
     * HTTP POST request
164
     *
165
     * @method
166
     * @param {String|gpf.typedef.httpRequestSettings} urlOrRequest URL or HTTP Request settings
167
     * @param {String} data Data to POST
168
     * @return {Promise<gpf.typedef.httpRequestResponse>} Resolved on request completion
169
     * @since 0.2.1
170
     */
171
    post: _gpfProcessAlias.bind(gpf.http, _GPF_HTTP_METHODS.POST),
172
 
173
    /**
174
     * HTTP PUT request
175
     *
176
     * @method
177
     * @param {String|gpf.typedef.httpRequestSettings} urlOrRequest URL or HTTP Request settings
178
     * @param {String} data Data to PUT
179
     * @return {Promise<gpf.typedef.httpRequestResponse>} Resolved on request completion
180
     * @since 0.2.1
181
     */
182
    put: _gpfProcessAlias.bind(gpf.http, _GPF_HTTP_METHODS.PUT),
183
 
184
    /**
185
     * HTTP OPTIONS request
186
     *
187
     * @method
188
     * @param {String|gpf.typedef.httpRequestSettings} urlOrRequest URL or HTTP Request settings
189
     * @return {Promise<gpf.typedef.httpRequestResponse>} Resolved on request completion
190
     * @since 0.2.1
191
     */
192
    options: _gpfProcessAlias.bind(gpf.http, _GPF_HTTP_METHODS.OPTIONS),
193
 
194
    /**
195
     * HTTP DELETE request
196
     *
197
     * @method
198
     * @param {String|gpf.typedef.httpRequestSettings} urlOrRequest URL or HTTP Request settings
199
     * @return {Promise<gpf.typedef.httpRequestResponse>} Resolved on request completion
200
     * @since 0.2.1
201
     */
202
    "delete": _gpfProcessAlias.bind(gpf.http, _GPF_HTTP_METHODS.DELETE),
203
 
204
    /**
205
     * HTTP HEAD request
206
     *
207
     * @method
208
     * @param {String|gpf.typedef.httpRequestSettings} urlOrRequest URL or HTTP Request settings
209
     * @return {Promise<gpf.typedef.httpRequestResponse>} Resolved on request completion
210
     * @since 0.2.2
211
     */
212
    head: _gpfProcessAlias.bind(gpf.http, _GPF_HTTP_METHODS.HEAD)
213
 
214
});