• 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

85.06
/src/components/model/private/fetch.js
1
/** ************************************************************************
2
 *
3
 * Implements the fetch method.
4
 *
5
 * fetch.js is just a literal object that contains a set of functions. It
6
 * can't be intantiated.
7
 *
8
 * Private Functions:
9
 *  . _getArgs4FetchAndDelete     decodes the fetch and delete arguments,
10
 *  . _getArgs4Save               decodes the save arguments,
11
 *  . _fetch                      retrieves one object from the server,
12
 *  . _save                       sends one object to the server,
13
 *  . _delete                     removes a model from the server,
14
 *
15
 *
16
 * Public Static Methods:
17
 *  . fetch                       retrieves one object from the server,
18
 *  . save                        sends one object to the server,
19
 *  . delete                      removes a model from the server,
20
 *
21
 *
22
 *
23
 * @namespace    -
24
 * @dependencies none
25
 * @exports      -
26
 * @author       -
27
 * @since        0.0.0
28
 * @version      -
29
 * ********************************************************************** */
30
/* global */
31
/* eslint-disable one-var, semi-style, no-underscore-dangle */
32

33

34
// -- Vendor Modules
35

36

37
// -- Local Modules
38
import _ from '../../../libs/_';
39
import F from '../../../sync/main';
40
import U from '../../../utils/util1';
41

42

43
// -- Local Constants
44

45

46
// -- Local Variables
47

48

49
// -- Private Functions ----------------------------------------------------
50

51
/**
52
 * Decodes the delete arguments.
53
 *
54
 * @function ([arg1], [arg2])
55
 * @private
56
 * @param {Object}          the options,
57
 * @param {Function}        the function to call at the completion
58
 * @returns {Array}         returns an object with options, callback,
59
 * @since 0.0.0
60
 */
61
function _getArgs4FetchAndDelete(...args) {
62
  const [arg1, arg2] = args;
18✔
63
  switch (args.length) {
18✔
64
    case 0:
65
      return [{}, null];
4✔
66

67
    case 1:
68
      if (_.isLiteralObject(arg1)) {
6✔
69
        return [arg1, null];
2✔
70
      }
71
      if (_.isFunction(arg1)) {
4✔
72
        return [{}, arg1];
3✔
73
      }
74
      return [{}, null];
1✔
75

76
    default:
77
      if (_.isLiteralObject(arg1) && _.isFunction(arg2)) {
8✔
78
        return [arg1, arg2];
5✔
79
      }
80
      if (_.isLiteralObject(arg1)) {
3✔
81
        return [arg1, null];
1✔
82
      }
83
      if (_.isFunction(arg1)) {
2✔
84
        return [{}, arg1];
1✔
85
      }
86
      return [{}, null];
1✔
87
  }
88
}
89

90
/**
91
 * Decodes the save arguments.
92
 *
93
 * @function ([arg1], [arg2], [arg3])
94
 * @private
95
 * @param {Object}          the modified model properties,
96
 * @param {Object}          the options,
97
 * @param {Function}        the function to call at the completion
98
 * @returns {Array}         returns an object with properties, options, callback,
99
 * @since 0.0.0
100
 */
101
function _getArgs4Save(...args) {
102
  const [arg1, arg2, arg3] = args;
14✔
103
  switch (args.length) {
14✔
104
    case 0:
105
      return [null, null, null];
1✔
106

107
    case 1:
108
      if (_.isLiteralObject(arg1)) {
1!
109
        return [arg1, {}, null];
×
110
      }
111
      return [null, null, null];
1✔
112

113
    case 2:
114
      if (_.isLiteralObject(arg1) && _.isLiteralObject(arg2)) {
3✔
115
        return [arg1, arg2, null];
2✔
116
      }
117
      if (_.isLiteralObject(arg1) && _.isFunction(arg2)) {
1!
118
        return [arg1, {}, arg2];
×
119
      }
120
      if (_.isLiteralObject(arg1)) {
1!
121
        return [arg1, {}, null];
×
122
      }
123
      return [null, null, null];
1✔
124

125
    default:
126
      if (_.isLiteralObject(arg1) && _.isLiteralObject(arg2) && _.isFunction(arg3)) {
9✔
127
        return [arg1, arg2, arg3];
5✔
128
      }
129
      if (_.isLiteralObject(arg1) && _.isLiteralObject(arg2)) {
4✔
130
        return [arg1, arg2, null];
3✔
131
      }
132
      if (_.isLiteralObject(arg1) && _.isFunction(arg2)) {
1!
133
        return [arg1, {}, arg2];
×
134
      }
135
      if (_.isLiteralObject(arg1)) {
1!
136
        return [arg1, {}, null];
×
137
      }
138
      return [null, null, null];
1✔
139
  }
140
}
141

142
/**
143
 * Retrieves one object from the server.
144
 *
145
 * @function (arg1, arg2, arg3, arg4)
146
 * @private
147
 * @param {Object}          the model object,
148
 * @param {String}          the server url,
149
 * @param {Object}          the fetch options,
150
 * @param {Function}        the function to call at the completion,
151
 * @returns {}              -,
152
 * @since 0.0.0
153
 */
154
/* eslint-disable no-param-reassign */
155
function _fetch(model, url, ...args) {
156
  const [opts, callback] = _getArgs4FetchAndDelete(...args);
10✔
157

158
  const options = {
10✔
159
    method: 'GET',
160
    headers: {
161
      Accept: 'application/json',
162
      'Content-Type': 'application/json',
163
    },
164
  };
165

166
  let nurl;
167
  if (opts.params || opts.query) {
10✔
168
    nurl = U.getUrl(url, opts);
1✔
169
  } else {
170
    nurl = model._attributes.id
9✔
171
      ? `${url}/${model._attributes.id}`
172
      : `${url}/1`;
173
  }
174

175
  const type = opts.type === 'text' ? 'text' : 'json';
10!
176
  F.fetch(nurl, options, type, (err, data) => {
10✔
177
    if (err) {
10!
178
      if (callback) callback(err);
×
179
      return;
×
180
    }
181
    model._attributes = model._parse(data, opts);
10✔
182
    if (!opts.silent) model.fire('load', model._attributes);
10✔
183
    if (callback) {
7✔
184
      callback(null, model._attributes);
3✔
185
    }
186
  });
187
}
188
/* eslint-enable no-param-reassign */
189

190
/**
191
 * Sends one object to the server.
192
 *
193
 * @function (arg1, arg2, arg3, [arg4], [arg5])
194
 * @private
195
 * @param {Object}          the model object,
196
 * @param {String}          the server url,
197
 * @param {Object}          the updated attributes,
198
 * @param {Object}          the options,
199
 * @param {Function}        the function to call at the completion,
200
 * @returns {}              -,
201
 * @since 0.0.0
202
 */
203
function _save(model, url, ...args) {
204
  const [changed, opts, callback] = _getArgs4Save(...args);
14✔
205
  if (!changed) return;
14✔
206

207
  const options = {
10✔
208
    method: 'POST',
209
    headers: {
210
      Accept: 'application/json',
211
      'Content-Type': 'application/json',
212
    },
213
    body: JSON.stringify(changed),
214
  };
215

216
  const type = opts.type === 'text' ? 'text' : 'json';
10✔
217
  F.fetch(url, options, type, (err, res) => {
10✔
218
    if (err) {
10!
219
      if (callback) callback(err);
×
220
    } else {
221
      if (!opts.silent) model.fire('save', res);
10✔
222
      if (callback) callback(err, res);
10✔
223
    }
224
  });
225
}
226

227
/**
228
 * Removes a model from the server.
229
 *
230
 * @function (arg1, arg2, [arg3])
231
 * @public
232
 * @param {Object}          the model object,
233
 * @param {String}          the server url,
234
 * @param {Function}        the function to call at the completion,
235
 * @returns {}              -,
236
 * @since 0.0.0
237
 */
238
function _delete(model, url, ...args) {
239
  const [opts, callback] = _getArgs4FetchAndDelete(...args)
8✔
240
      , id                  = model.get('id')
8✔
241
      ;
242

243
  if (!id) {
8✔
244
    if (callback) callback(null, 'This model has no id. Thus, it does not exist on the server!');
2✔
245
    return;
2✔
246
  }
247

248
  if (!url) {
6✔
249
    if (callback) callback(null, 'This model has no url!');
2✔
250
    return;
2✔
251
  }
252

253
  const options = {
4✔
254
    method: 'DELETE',
255
    headers: {
256
      Accept: 'application/json',
257
      'Content-Type': 'application/json',
258
    },
259
  };
260

261
  const type = opts.type === 'text' ? 'text' : 'json';
4✔
262
  F.fetch(`${url}/${id}`, options, type, (err, res) => {
4✔
263
    if (err) {
4!
264
      if (callback) callback(err);
×
265
    } else {
266
      if (!opts.silent) model.fire('delete', res);
4✔
267
      if (callback) callback(err, res);
4✔
268
    }
269
  });
270
}
271

272

273
// -- Public Static Methods ------------------------------------------------
274

275
const Util = {
1✔
276

277
  /**
278
   * Retrieves one object from the server.
279
   *
280
   * @method (arg1, arg2, [arg3], [arg4)
281
   * @public
282
   * @param {Object}          the model object,
283
   * @param {String}          the server url,
284
   * @param {Object}          the fetch options,
285
   * @param {Function}        the function to call at the completion,
286
   * @returns {Object}        returns this,
287
   * @since 0.0.0
288
   */
289
  fetch(model, url, ...args) {
290
    _fetch(model, url, ...args);
10✔
291
    return this;
10✔
292
  },
293

294
  /**
295
   * Sends one object to the server.
296
   *
297
   * @method (arg1, arg2, arg3, [arg4], [arg5])
298
   * @public
299
   * @param {Object}        the model object,
300
   * @param {String}        the server url,
301
   * @param {Object}        the updated attributes,
302
   * @param {Object}        the options,
303
   * @param {Function}      the function to call at the completion,
304
   * @returns {Object}      returns this,
305
   * @since 0.0.0
306
   */
307
  save(model, url, ...args) {
308
    _save(model, url, ...args);
14✔
309
    return this;
14✔
310
  },
311

312
  /**
313
   * Removes a model from the server.
314
   *
315
   * @method (arg1, arg2, [arg3], [arg4])
316
   * @public
317
   * @param {Object}          the model object,
318
   * @param {String}          the server url,
319
   * @param {Object}          the options,
320
   * @param {Function}        the function to call at the completion,
321
   * @returns {Object}        returns this,
322
   * @since 0.0.0
323
   */
324
  delete(model, url, ...args) {
325
    _delete(model, url, ...args);
8✔
326
    return this;
8✔
327
  },
328
};
329

330

331
// -- Export
332
export default Util;
333

334
/* 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