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

jclo / spine / 10734179635

06 Sep 2024 07:14AM UTC coverage: 91.913% (-0.3%) from 92.206%
10734179635

push

github

jclo
Updated the project with @mobilabs/es6kadoo v2.2.1 (improved build scripts).

422 of 488 branches covered (86.48%)

Branch coverage included in aggregate %.

5 of 8 new or added lines in 2 files covered. (62.5%)

11 existing lines in 1 file now uncovered.

3908 of 4223 relevant lines covered (92.54%)

5.6 hits per line

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

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

1✔
32

1✔
33
// -- Vendor Modules
1✔
34

1✔
35

1✔
36
// -- Local Modules
1✔
37

1✔
38

1✔
39
// -- Local Constants
1✔
40

1✔
41

1✔
42
// -- Local Variables
1✔
43

1✔
44

1✔
45
// -- Private functions ----------------------------------------------------
1✔
46

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

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

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

×
69
    case 2:
×
70
      if (typeof arg1 === 'string') {
37✔
71
        if (typeof arg2 === 'object' && arg2.method) {
×
72
          return [arg1, arg2, null, null];
×
73
        }
×
74
        if (typeof arg2 === 'string') {
37!
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];
37!
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];
37✔
116
      }
37✔
117
      if ((typeof arg1 === 'string')
37✔
118
        && typeof arg2 === 'object' && arg2.method
37✔
119
        && typeof arg3 === 'string') {
37✔
120
        return [arg1, arg2, arg3, null];
37✔
121
      }
37✔
122
      return [null, null, null, null];
37!
123

×
124
    default:
×
125
      // > 4
×
126
      if ((typeof arg1 === 'string')
37!
127
        && typeof arg2 === 'object' && arg2.method
37!
128
        && typeof arg3 === 'string'
×
129
        && typeof arg4 === 'function') {
37!
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,
37✔
143
 * @param {Function}        the function to call at the completion,
1✔
144
 * @returns {}              -,
1✔
145
 * @since 0.0.0
1✔
146
 */
1✔
147
// function _oldfetch(url, options, type, callback) /* istanbul ignore next */{
1✔
148
//   fetch(url, options)
1✔
149
//     .then((resp) => {
1✔
150
//       if (resp.ok) {
1✔
151
//         return type === 'json' ? resp.json() : resp.text();
1✔
152
//       }
1✔
153
//       return Promise.reject(resp);
1✔
154
//     })
1✔
155
//     .then((data) => {
1✔
156
//       if (callback) {
1✔
157
//         callback(null, data);
1✔
158
//       } else {
1✔
159
//         /* eslint-disable-next-line no-console */
1✔
160
//         console.log('warning: fetch gets no callback!');
1✔
161
//       }
1✔
162
//     })
1✔
163
//     .catch((err) => {
1✔
164
//       if (callback) {
1✔
165
//         callback(err);
1✔
166
//       } else {
1✔
167
//         /* eslint-disable-next-line no-console */
1✔
168
//         console.log('warning: fetch gets no callback!');
1✔
169
//       }
1✔
170
//     });
1✔
171
// }
1✔
172
function _fetchServer(url, options, callback) /* istanbul ignore next */{
1✔
173
  let status;
1✔
174

1✔
175
  fetch(url, options)
1✔
176
    .then((response) => {
1✔
177
      if (response.ok) {
1✔
178
        return response.text();
1✔
179
      }
37✔
180
      status = response.status;
37✔
181
      return Promise.reject(response);
37✔
182
    })
37✔
183
    .then((data) => {
37✔
184
      callback(null, data);
37✔
185
    })
37✔
186
    .catch((error) => {
37✔
187
      if (error && error.text) {
37!
NEW
188
        error.text()
×
189
          .then((err) => { callback(err || { status, message: 'none!', statusText: 'none!' }); })
37✔
190
        ;
37✔
191
      } else {
37✔
NEW
192
        console.log(error);
×
NEW
193
      }
×
UNCOV
194
    })
×
UNCOV
195
  ;
×
UNCOV
196
}
×
UNCOV
197

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

×
226
    if (err) {
×
227
      callback(err);
×
228
      return;
×
229
    }
×
230

×
231
    if (type === 'json') {
×
232
      let ndata;
×
233
      try {
×
234
        ndata = JSON.parse(data);
×
235
      } catch (e) {
×
236
        ndata = data;
37✔
237
      }
37✔
238
      callback(null, ndata);
×
239
      return;
37✔
240
    }
37✔
241

37✔
242
    callback(null, data);
37✔
243
  });
25✔
244
}
25✔
245

25✔
246

25✔
247
// -- Public Static Methods ------------------------------------------------
25✔
248

2✔
249
const Fetch = {
2✔
250

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

37✔
272

37✔
273
// -- Export
37✔
274
export default Fetch;
37✔
275

37✔
276
/* eslint-enable one-var, semi-style, no-underscore-dangle */
37!
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

© 2025 Coveralls, Inc