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

thelinmichael / spotify-web-api-node / #191

07 Dec 2024 08:20PM UTC coverage: 98.291% (+0.4%) from 97.866%
#191

push

travis-ci

Zaptyp
Fix tests to create coveralls

131 of 145 branches covered (90.34%)

345 of 351 relevant lines covered (98.29%)

36.4 hits per line

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

97.1
/src/http-manager.js
1
'use strict';
2

3
var {
4
  TimeoutError,
5
  WebapiError,
6
  WebapiRegularError,
7
  WebapiAuthenticationError,
8
  WebapiPlayerError
9
} = require('./response-error');
2✔
10

11
var HttpManager = {};
2✔
12

13
/* Create superagent options from the base request */
14
var _getParametersFromRequest = function (request) {
2✔
15
  var options = {};
144✔
16

17
  if (request.getQueryParameters()) {
144✔
18
    options.query = new URLSearchParams(
79✔
19
      request.getQueryParameters()
20
    ).toString();
21
  }
22

23
  if (
144✔
24
    request.getHeaders() &&
227✔
25
    request.getHeaders()['Content-Type'] === 'application/json'
26
  ) {
27
    options.data = JSON.stringify(request.getBodyParameters());
27✔
28
  } else if (request.getBodyParameters()) {
117✔
29
    options.data = request.getBodyParameters();
5✔
30
  }
31

32
  if (request.getHeaders()) {
144✔
33
    options.headers = request.getHeaders();
83✔
34
  }
35

36
  if (request.getTimeout()) {
144✔
37
    options.timeout = request.getTimeout();
1✔
38
  }
39

40
  return options;
144✔
41
};
42

43
/**
44
 * @param {Response} response
45
 */
46
var _toError = async function (response) {
2✔
47
  let body
48
  let response2
49
  try {
7✔
50
    response2 = response.clone();
7✔
51
    body = await response.json();
7✔
52
  } catch (e) {
53
    body = await response2.text();
×
54
  }
55

56
  if (
7✔
57
    typeof body === 'object' &&
17✔
58
    typeof body.error === 'object' &&
59
    typeof body.error.reason === 'string'
60
  ) {
61
    return new WebapiPlayerError(body, response.headers, response.status);
1✔
62
  }
63

64
  if (typeof body === 'object' && typeof body.error === 'object') {
6✔
65
    return new WebapiRegularError(body, response.headers, response.status);
3✔
66
  }
67

68
  if (typeof body === 'object' && typeof body.error === 'string') {
3✔
69
    return new WebapiAuthenticationError(
2✔
70
      body,
71
      response.headers,
72
      response.status
73
    );
74
  }
75

76
  /* Other type of error, or unhandled Web API error format */
77
  return new WebapiError(body, response.headers, response.status, body);
1✔
78
};
79

80
/* Make the request to the Web API */
81
HttpManager._makeRequest = function (method, options, uri, callback) {
2✔
82
  if (typeof callback !== 'function') {
144!
83
    throw new TypeError('Expected callback to be a function');
×
84
  }
85

86
  const headers = new Headers(options.headers || {});
144✔
87
  let serializationMethod = JSON.stringify;
144✔
88

89
  if (headers.get('Content-Type') === 'application/x-www-form-urlencoded') {
144✔
90
    serializationMethod = d => new URLSearchParams(d);
4✔
91
  }
92

93
  const controller = new AbortController();
144✔
94
  let timeoutId;
95

96
  if (options.timeout) {
144✔
97
    timeoutId = setTimeout(() => {
1✔
98
      controller.abort();
1✔
99
    }, options.timeout);
100
  }
101

102
  let body = options.data;
144✔
103

104
  if (body && typeof body !== 'string') {
144✔
105
    body = serializationMethod(body);
4✔
106
  }
107

108
  const fullUri = uri + (options.query ? '?' + options.query : '');
144✔
109

110
  fetch(fullUri, {
144✔
111
    method,
112
    headers,
113
    body,
114
    signal: controller.signal
115
  })
116
    .then(async resp => {
117
      clearTimeout(timeoutId);
142✔
118

119
      if (!resp.ok) {
142✔
120
        return callback(await _toError(resp));
7✔
121
      }
122

123
      return callback(null, {
135✔
124
        body: await resp.json().catch(() => null),
39✔
125
        headers: resp.headers,
126
        statusCode: resp.status
127
      });
128
    })
129
    .catch(err => {
130
      clearTimeout(timeoutId);
2✔
131

132
      if (controller.signal.aborted) {
2✔
133
        return callback(
1✔
134
          new TimeoutError(`request took longer than ${options.timeout}ms`)
135
        );
136
      }
137

138
      return callback(err);
1✔
139
    });
140
};
141

142
/**
143
 * Make a HTTP GET request.
144
 * @param {BaseRequest} The request.
145
 * @param {Function} The callback function.
146
 */
147
HttpManager.get = function (request, callback) {
2✔
148
  var options = _getParametersFromRequest(request);
90✔
149
  var method = 'GET';
90✔
150

151
  HttpManager._makeRequest(method, options, request.getURI(), callback);
90✔
152
};
153

154
/**
155
 * Make a HTTP POST request.
156
 * @param {BaseRequest} The request.
157
 * @param {Function} The callback function.
158
 */
159
HttpManager.post = function (request, callback) {
2✔
160
  var options = _getParametersFromRequest(request);
15✔
161
  var method = 'POST';
15✔
162

163
  HttpManager._makeRequest(method, options, request.getURI(), callback);
15✔
164
};
165

166
/**
167
 * Make a HTTP DELETE request.
168
 * @param {BaseRequest} The request.
169
 * @param {Function} The callback function.
170
 */
171
HttpManager.del = function (request, callback) {
2✔
172
  var options = _getParametersFromRequest(request);
11✔
173
  var method = 'DELETE';
11✔
174

175
  HttpManager._makeRequest(method, options, request.getURI(), callback);
11✔
176
};
177

178
/**
179
 * Make a HTTP PUT request.
180
 * @param {BaseRequest} The request.
181
 * @param {Function} The callback function.
182
 */
183
HttpManager.put = function (request, callback) {
2✔
184
  var options = _getParametersFromRequest(request);
28✔
185
  var method = 'PUT';
28✔
186

187
  HttpManager._makeRequest(method, options, request.getURI(), callback);
28✔
188
};
189

190
module.exports = HttpManager;
2✔
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