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

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

07 Dec 2024 10:03PM UTC coverage: 96.978% (-1.3%) from 98.291%
#194

push

travis-ci

Zaptyp
Deprecated some functions (https://developer.spotify.com/blog/2024-11-27-changes-to-the-web-api)

132 of 147 branches covered (89.8%)

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

7 existing lines in 2 files now uncovered.

353 of 364 relevant lines covered (96.98%)

35.13 hits per line

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

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

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

12
var HttpManager = {};
2✔
13

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

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

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

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

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

41
  return options;
144✔
42
};
43

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

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

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

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

77
  if (response.status === 429) {
1!
UNCOV
78
    return new WebapiRatelimitError(body, response.headers, response.status);
×
79
  }
80

81
  /* Other type of error, or unhandled Web API error format */
82
  return new WebapiError(body, response.headers, response.status, body);
1✔
83
};
84

85
/* Make the request to the Web API */
86
HttpManager._makeRequest = function (method, options, uri, callback) {
2✔
87
  if (typeof callback !== 'function') {
144!
UNCOV
88
    throw new TypeError('Expected callback to be a function');
×
89
  }
90

91
  const headers = new Headers(options.headers || {});
144✔
92
  let serializationMethod = JSON.stringify;
144✔
93

94
  if (headers.get('Content-Type') === 'application/x-www-form-urlencoded') {
144✔
95
    serializationMethod = d => new URLSearchParams(d);
4✔
96
  }
97

98
  const controller = new AbortController();
144✔
99
  let timeoutId;
100

101
  if (options.timeout) {
144✔
102
    timeoutId = setTimeout(() => {
1✔
103
      controller.abort();
1✔
104
    }, options.timeout);
105
  }
106

107
  let body = options.data;
144✔
108

109
  if (body && typeof body !== 'string') {
144✔
110
    body = serializationMethod(body);
4✔
111
  }
112

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

115
  fetch(fullUri, {
144✔
116
    method,
117
    headers,
118
    body,
119
    signal: controller.signal
120
  })
121
    .then(async resp => {
122
      clearTimeout(timeoutId);
142✔
123

124
      if (!resp.ok) {
142✔
125
        return callback(await _toError(resp));
7✔
126
      }
127

128
      return callback(null, {
135✔
129
        body: await resp.json().catch(() => null),
39✔
130
        headers: resp.headers,
131
        statusCode: resp.status
132
      });
133
    })
134
    .catch(err => {
135
      clearTimeout(timeoutId);
2✔
136

137
      if (controller.signal.aborted) {
2✔
138
        return callback(
1✔
139
          new TimeoutError(`request took longer than ${options.timeout}ms`)
140
        );
141
      }
142

143
      return callback(err);
1✔
144
    });
145
};
146

147
/**
148
 * Make a HTTP GET request.
149
 * @param {BaseRequest} The request.
150
 * @param {Function} The callback function.
151
 */
152
HttpManager.get = function (request, callback) {
2✔
153
  var options = _getParametersFromRequest(request);
90✔
154
  var method = 'GET';
90✔
155

156
  HttpManager._makeRequest(method, options, request.getURI(), callback);
90✔
157
};
158

159
/**
160
 * Make a HTTP POST request.
161
 * @param {BaseRequest} The request.
162
 * @param {Function} The callback function.
163
 */
164
HttpManager.post = function (request, callback) {
2✔
165
  var options = _getParametersFromRequest(request);
15✔
166
  var method = 'POST';
15✔
167

168
  HttpManager._makeRequest(method, options, request.getURI(), callback);
15✔
169
};
170

171
/**
172
 * Make a HTTP DELETE request.
173
 * @param {BaseRequest} The request.
174
 * @param {Function} The callback function.
175
 */
176
HttpManager.del = function (request, callback) {
2✔
177
  var options = _getParametersFromRequest(request);
11✔
178
  var method = 'DELETE';
11✔
179

180
  HttpManager._makeRequest(method, options, request.getURI(), callback);
11✔
181
};
182

183
/**
184
 * Make a HTTP PUT request.
185
 * @param {BaseRequest} The request.
186
 * @param {Function} The callback function.
187
 */
188
HttpManager.put = function (request, callback) {
2✔
189
  var options = _getParametersFromRequest(request);
28✔
190
  var method = 'PUT';
28✔
191

192
  HttpManager._makeRequest(method, options, request.getURI(), callback);
28✔
193
};
194

195
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