• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jclo / spine / 17

pending completion
17

push

travis-ci-com

jclo
Improved the fetch method to comply with http/2 and the suppression of statusText.

327 of 500 branches covered (65.4%)

Branch coverage included in aggregate %.

18 of 18 new or added lines in 1 file covered. (100.0%)

500 of 661 relevant lines covered (75.64%)

16.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.26
/src/sync/fetch.js
1
/** ************************************************************************
2
 *
3
 * Retrieves data from the server.
4
 * (this file is just a copy and paste of the file './src/private/fetch.js'
5
 * extracted from PicoQ v1.0.1 with one exception: fetch does not return
6
 * a promise)
7
 *
8
 * fetch.js is just a literal object that contains a set of functions.
9
 * It can't be instantiated.
10
 *
11
 * Private Functions:
12
 *  . _getArgs                    returns the named fetch arguments,
13
 *  . _fetchServer                fetches data on the server,
14
 *  .  _fetch                     formats returned value,
15
 *
16
 *
17
 * Public Static Methods:
18
 *  . fetch                       fetches data on the server,
19
 *
20
 *
21
 *
22
 * @namespace    -
23
 * @dependencies none
24
 * @exports      -
25
 * @author       -
26
 * @since        0.0.0
27
 * @version      -
28
 * ********************************************************************** */
29
/* global */
30
/* eslint-disable one-var, semi-style, no-underscore-dangle */
31

32

33
// -- Vendor Modules
34

35

36
// -- Local Modules
37

38

39
// -- Local Constants
40

41

42
// -- Local Variables
43

44

45
// -- Private functions ----------------------------------------------------
46

47
/**
48
 * Returns the named fetch arguments.
49
 *
50
 * @function (...args)
51
 * @private
52
 * @param {...}             the optional arguments [url, options, type, callback],
53
 * @returns {}              -,
54
 * @since 0.0.0
55
 */
56
function _getArgs(...args) /* istanbul ignore next */{
57
  const [arg1, arg2, arg3, arg4] = args;
58

59
  switch (args.length) {
60
    case 0:
61
      return [null, null, null, null];
62

63
    case 1:
64
      if (typeof arg1 === 'string') {
65
        return [arg1, {}, null, null];
66
      }
67
      return [null, null, null, null];
68

69
    case 2:
70
      if (typeof arg1 === 'string') {
71
        if (typeof arg2 === 'object' && arg2.method) {
72
          return [arg1, arg2, null, null];
73
        }
74
        if (typeof arg2 === 'string') {
75
          return [arg1, {}, arg2, null];
76
        }
77
        if (typeof arg2 === 'function') {
78
          return [arg1, {}, null, arg2];
79
        }
80
        return [arg1, {}, null, null];
81
      }
82
      return [null, null, null, null];
83

84
    case 3:
85
      if (typeof arg1 === 'string') {
86
        if (typeof arg2 === 'object' && arg2.method) {
87
          if (typeof arg3 === 'string') {
88
            return [arg1, arg2, arg3, null];
89
          }
90
          if (typeof arg3 === 'function') {
91
            return [arg1, arg2, null, arg3];
92
          }
93
          return [arg1, arg2, null, null];
94
        }
95

96
        if (typeof arg2 === 'string') {
97
          if (typeof arg3 === 'function') {
98
            return [arg1, {}, arg2, arg3];
99
          }
100
          return [arg1, {}, arg2, null];
101
        }
102

103
        if (typeof arg3 === 'function') {
104
          return [arg1, {}, null, arg3];
105
        }
106
        return [arg1, {}, null, null];
107
      }
108
      return [null, null, null, null];
109

110
    case 4:
111
      if ((typeof arg1 === 'string')
112
        && typeof arg2 === 'object' && arg2.method
113
        && typeof arg3 === 'string'
114
        && typeof arg4 === 'function') {
115
        return [arg1, arg2, arg3, arg4];
116
      }
117
      if ((typeof arg1 === 'string')
118
        && typeof arg2 === 'object' && arg2.method
119
        && typeof arg3 === 'string') {
120
        return [arg1, arg2, arg3, null];
121
      }
122
      return [null, null, null, null];
123

124
    default:
125
      // > 4
126
      if ((typeof arg1 === 'string')
127
        && typeof arg2 === 'object' && arg2.method
128
        && typeof arg3 === 'string'
129
        && typeof arg4 === 'function') {
130
        return [arg1, arg2, arg3, arg4];
131
      }
132
      return [null, null, null, null];
133
  }
134
}
135

136
/**
137
 * Fetches data on the server.
138
 *
139
 * @function (arg1, arg2, arg3)
140
 * @public
141
 * @param {String}          the server url & api,
142
 * @param {Object}          the fetch parameters,
143
 * @param {Function}        the function to call at the completion,
144
 * @returns {}              -,
145
 * @since 0.0.0
146
 */
147
// function _oldfetch(url, options, type, callback) /* istanbul ignore next */{
148
//   fetch(url, options)
149
//     .then((resp) => {
150
//       if (resp.ok) {
151
//         return type === 'json' ? resp.json() : resp.text();
152
//       }
153
//       return Promise.reject(resp);
154
//     })
155
//     .then((data) => {
156
//       if (callback) {
157
//         callback(null, data);
158
//       } else {
159
//         /* eslint-disable-next-line no-console */
160
//         console.log('warning: fetch gets no callback!');
161
//       }
162
//     })
163
//     .catch((err) => {
164
//       if (callback) {
165
//         callback(err);
166
//       } else {
167
//         /* eslint-disable-next-line no-console */
168
//         console.log('warning: fetch gets no callback!');
169
//       }
170
//     });
171
// }
172
function _fetchServer(url, options, callback) /* istanbul ignore next */{
173
  let status;
174

175
  fetch(url, options)
176
    .then((response) => {
177
      if (response.ok) {
178
        return response.text();
179
      }
180
      status = response.status;
181
      return Promise.reject(response);
182
    })
183
    .then((data) => {
184
      callback(null, data);
185
    })
186
    .catch((error) => {
187
      error.text()
188
        .then((err) => { callback(err || { status, message: 'none!', statusText: 'none!' }); })
189
      ;
190
    })
191
  ;
192
}
193

194
/**
195
 * Fetches data.
196
 *
197
 * @function (arg1, arg2, arg3, arg4)
198
 * @public
199
 * @param {String}          the server url & api,
200
 * @param {Object}          the fetch parameters,
201
 * @param {String}          the returned format (string or json),
202
 * @param {Function}        the function to call at the completion,
203
 * @returns {}              -,
204
 * @since 0.0.0
205
 */
206
function _fetch(url, options, type, callback) {
207
  _fetchServer(url, options, (err, data) => {
37✔
208
    if (err && type === 'json') {
37!
209
      let nerr;
210
      try {
×
211
        nerr = JSON.parse(err);
×
212
        // statusText is to keep compatibility with previous versions.
213
        // (to be removed by the end of 2023)
214
        nerr.statusText = nerr.message;
×
215
      } catch (e) {
216
        nerr = { status: '40x', message: err, statusText: err };
×
217
      }
218
      callback(nerr);
×
219
      return;
×
220
    }
221

222
    if (err) {
37!
223
      callback(err);
×
224
      return;
×
225
    }
226

227
    if (type === 'json') {
37✔
228
      let ndata;
229
      try {
25✔
230
        ndata = JSON.parse(data);
25✔
231
      } catch (e) {
232
        ndata = data;
2✔
233
      }
234
      callback(null, ndata);
25✔
235
      return;
22✔
236
    }
237

238
    callback(null, data);
12✔
239
  });
240
}
241

242

243
// -- Public Static Methods ------------------------------------------------
244

245
const Fetch = {
1✔
246

247
  /**
248
   * Fetches data on the server.
249
   *
250
   * @method (arg1, [arg2], [arg3], [arg4])
251
   * @public
252
   * @param {String}        the server url,
253
   * @param {Object}        the fetch options,
254
   * @param {String}        the type of file (json or text),
255
   * @param {String}        the function to call at the completion,
256
   * @returns {Object}      returns this,
257
   * @since 0.0.0
258
   */
259
  fetch(...args) /* istanbul ignore next */{
260
    const [url, options, type, callback] = _getArgs(...args);
261
    _fetch(url, options, type || 'json', (err, data) => {
262
      if (callback) callback(err, data);
263
    });
264
    return this;
265
  },
266
};
267

268

269
// -- Export
270
export default Fetch;
271

272
/* eslint-enable one-var, semi-style, no-underscore-dangle */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc