-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathhttpClient.js
More file actions
431 lines (367 loc) · 13.1 KB
/
httpClient.js
File metadata and controls
431 lines (367 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* eslint camelcase: 0 */
const JSONbig = require('json-bigint')
const querystring = require('querystring')
const path = require('path')
const fsPromises = require('fs').promises
const rateLimiting = require('./rateLimiting')
/**
* Options passed to the underlying HTTP request (e.g. axios).
* @typedef {Object} RequestOptions
* @property {string} url
* @property {string} [method]
* @property {unknown} [body]
* @property {Record<string, string|null|undefined>} [headers]
* @property {string | undefined} [baseUrl]
* @property {string} [responseType]
* @property {boolean} [resolveWithFullResponse]
* @property {boolean} [simple]
* @property {Record<string, unknown>} [form]
* @property {unknown} [multipart]
*/
/**
* Full response when resolveWithFullResponse is true.
* @typedef {Object} FullResponse
* @property {Record<string, string|string[]|undefined>} headers
* @property {unknown} [body]
*/
/**
* Type for the underlying request function used by HttpClient.
* @typedef {(options: RequestOptions) => Promise<FullResponse|string|object>} RequestFunction
*/
/**
* Common args for GET endpoint calls.
* @typedef {Object} GetEndpointArgs
* @property {string | undefined} [access_token]
* @property {string} [responseType]
*/
/**
* Args for PUT endpoint calls (body is stringified as form).
* @typedef {Object} PutEndpointArgs
* @property {string} [access_token]
* @property {string} [responseType]
* @property {Record<string, string|number|boolean>} [body]
* @property {Record<string, unknown>} [form]
*/
/**
* Args for POST endpoint calls.
* @typedef {Object} PostEndpointArgs
* @property {string} [access_token]
* @property {string} [responseType]
* @property {unknown} [body]
* @property {Record<string, unknown>} [form]
* @property {unknown} [multipart]
*/
/**
* Args for DELETE endpoint calls.
* @typedef {Object} DeleteEndpointArgs
* @property {string} [access_token]
* @property {string} [responseType]
* @property {Record<string, string|number|boolean>} [body]
*/
/**
* Args for postUpload (file upload).
* @typedef {Object} PostUploadArgs
* @property {string} [access_token]
* @property {Record<string, unknown>} [formData]
* @property {string} [file] - File path to upload (e.g., 'data/your_file.gpx')
*/
/**
* Pagination query args (deprecated for activity list endpoints; use CursorPaginationArgs).
* @typedef {Object} PaginationArgs
* @property {number} [page]
* @property {number} [per_page]
*/
/**
* Cursor-based pagination query args (page_size, after_cursor).
* @typedef {Object} CursorPaginationArgs
* @property {number} [page_size]
* @property {string} [after_cursor]
*/
/**
* Response shape returned by the uploads API.
* @typedef {{ id: string, id_str?: string, external_id?: string, error?: string, status?: string, activity_id?: string }} UploadResponse
*/
/**
* Client interface for route modules: the set of HttpClient methods they use.
* @typedef {Object} StravaRouteClient
* @property {(endpoint: string, args?: GetEndpointArgs) => Promise<object|string>} getEndpoint
* @property {(endpoint: string, args?: PutEndpointArgs) => Promise<object|string>} putEndpoint
* @property {(endpoint: string, args?: PostEndpointArgs) => Promise<object|string>} postEndpoint
* @property {(endpoint: string, args?: DeleteEndpointArgs) => Promise<object|string>} deleteEndpoint
* @property {(args?: PostUploadArgs) => Promise<UploadResponse>} postUpload
* @property {(args?: object) => string} getPaginationQS
* @property {(args?: CursorPaginationArgs) => string} getCursorPaginationQS
* @property {(allowedProps: string[], args?: object) => string} getQS
* @property {(allowedProps: string[], args?: object) => Record<string, string|number|boolean>} getRequestBodyObj
* @property {(options: RequestOptions) => Promise<object|string>} _requestHelper
*/
/**
* HTTP client that wraps a request function for Strava API calls.
* @param {RequestFunction} request
*/
var HttpClient = function (request) {
this.request = request
}
/**
* Performs a GET request to the Strava API.
* Only Promises are supported; callback-style usage is not supported.
*
* @param {string} endpoint
* @param {GetEndpointArgs} [args]
* @returns {Promise<object|string>}
*/
HttpClient.prototype.getEndpoint = async function (endpoint, args) {
if (!args) {
args = {}
}
/** @type {RequestOptions} */
var options = {
url: endpoint,
responseType: args.responseType || 'json'
}
if (args.access_token) {
options.headers = { Authorization: 'Bearer ' + args.access_token }
}
return this._requestHelper(options)
}
/**
* Performs a PUT request to the Strava API.
* Only Promises are supported; callback-style usage is not supported.
*
* @param {string} endpoint
* @param {PutEndpointArgs} [args]
* @returns {Promise<object|string>}
*/
HttpClient.prototype.putEndpoint = async function (endpoint, args) {
if (!args) {
args = {}
}
// stringify the body object for passage
let qs = querystring.stringify(args.body || {})
/** @type {RequestOptions} */
const options = {
url: endpoint,
method: 'PUT',
body: qs,
responseType: args.responseType || 'json'
}
if (args.access_token) {
options.headers = { Authorization: 'Bearer ' + args.access_token }
}
// add form data if present
if (args.form) { options.form = args.form }
return this._requestHelper(options)
}
/**
* Performs a POST request to the Strava API.
* Only Promises are supported; callback-style usage is not supported.
*
* @param {string} endpoint
* @param {PostEndpointArgs} [args]
* @returns {Promise<object|string>}
*/
HttpClient.prototype.postEndpoint = async function (endpoint, args) {
if (!args) {
args = {}
}
/** @type {RequestOptions} */
var options = {
url: endpoint,
method: 'POST',
body: args.body,
responseType: args.responseType || 'json'
}
if (args.access_token) {
options.headers = { Authorization: 'Bearer ' + args.access_token }
}
// add form data if present
if (args.form) { options.form = args.form }
// add multipart data if present
if (args.multipart) { options.multipart = args.multipart }
return this._requestHelper(options)
}
/**
* Performs a DELETE request to the Strava API.
* Only Promises are supported; callback-style usage is not supported.
*
* @param {string} endpoint
* @param {DeleteEndpointArgs} [args]
* @returns {Promise<object|string>}
*/
HttpClient.prototype.deleteEndpoint = async function (endpoint, args) {
if (!args) {
args = {}
}
// stringify the body object for passage
const qs = querystring.stringify(args.body || {})
/** @type {RequestOptions} */
var options = {
url: endpoint,
method: 'DELETE',
body: qs,
responseType: args.responseType || 'json'
}
if (args.access_token) {
options.headers = { Authorization: 'Bearer ' + args.access_token }
}
return this._requestHelper(options)
}
/**
* Uploads a file (e.g. GPX/TCX) to create a new activity.
* Uses global FormData and Blob (Node 18+) so multipart has a proper boundary.
* @param {PostUploadArgs} [args]
* @returns {Promise<UploadResponse>}
*/
HttpClient.prototype.postUpload = async function (args = {}) {
if (!args.file) {
throw new Error('postUpload requires args.file')
}
var form = new FormData()
var formData = args.formData || {}
for (var key in formData) {
if (Object.prototype.hasOwnProperty.call(formData, key)) {
form.append(key, String(formData[key]))
}
}
var buffer = await fsPromises.readFile(args.file)
form.append('file', new Blob([buffer]), path.basename(args.file))
/** @type {RequestOptions} */
var options = {
url: 'uploads',
method: 'POST',
body: form,
responseType: 'formdata',
headers: args.access_token ? { Authorization: 'Bearer ' + args.access_token } : {}
}
return /** @type {UploadResponse} */ (await this._requestHelper(options))
}
/**
* Builds a query string for pagination (page, per_page).
* @param {object} [args] - Object that may contain page, per_page (e.g. PaginationArgs or route args)
* @returns {string}
*/
HttpClient.prototype.getPaginationQS = function (args) {
var paginationArgs = /** @type {{ page?: number; per_page?: number }} */ (args || {})
// setup pagination query args
var page = typeof paginationArgs.page !== 'undefined' ? paginationArgs.page : null
var per_page = typeof paginationArgs.per_page !== 'undefined' ? paginationArgs.per_page : null
/** @type {Record<string, string|number|null>} */
var qa = {}
var qs
if (page) { qa.page = page }
if (per_page !== null) { qa.per_page = per_page }
qs = querystring.stringify(/** @type {import('querystring').ParsedUrlQueryInput} */ (qa))
return qs
}
/**
* Builds a query string for cursor-based pagination (page_size, after_cursor).
* @param {CursorPaginationArgs} [args]
* @returns {string}
*/
HttpClient.prototype.getCursorPaginationQS = function (args) {
args = args || {}
var page_size = typeof args.page_size !== 'undefined' ? args.page_size : null
var after_cursor = typeof args.after_cursor !== 'undefined' ? args.after_cursor : null
/** @type {Record<string, string|number|null>} */
var qa = {}
var qs
if (page_size !== null) { qa.page_size = page_size }
if (after_cursor) { qa.after_cursor = after_cursor }
qs = querystring.stringify(/** @type {import('querystring').ParsedUrlQueryInput} */ (qa))
return qs
}
/**
* Builds a query string from allowed property names and an args object.
* @param {string[]} allowedProps
* @param {Object} [args]
* @returns {string}
*/
HttpClient.prototype.getQS = function (allowedProps, args) {
const safeArgs = /** @type {Record<string, unknown>} */ (args || {})
/** @type {Record<string, unknown>} */
var qa = {}
var qs
for (var i = 0; i < allowedProps.length; i++) {
if (Object.prototype.hasOwnProperty.call(safeArgs, allowedProps[i])) { qa[allowedProps[i]] = safeArgs[allowedProps[i]] }
}
qs = querystring.stringify(/** @type {import('querystring').ParsedUrlQueryInput} */ (qa))
return qs
}
/**
* Returns a request body object containing only allowed props from args.
* @param {string[]} allowedProps
* @param {Object} [args]
* @returns {Record<string, string|number|boolean>}
*/
HttpClient.prototype.getRequestBodyObj = function (allowedProps, args) {
const safeArgs = /** @type {Record<string, unknown>} */ (args || {})
/** @type {Record<string, string|number|boolean>} */
var body = {}
for (var i = 0; i < allowedProps.length; i++) {
if (Object.prototype.hasOwnProperty.call(safeArgs, allowedProps[i])) {
body[allowedProps[i]] = /** @type {string|number|boolean} */ (safeArgs[allowedProps[i]])
}
}
return body
}
/**
* Internal: runs the request, parses the response, and updates rate limits.
* @param {RequestOptions} options
* @returns {Promise<object|string>}
*/
HttpClient.prototype._requestHelper = async function (options) {
// We need the full response so we can get at the headers
options.resolveWithFullResponse = true
// reject promise with 'StatusCodeError' for non-2xx responses.
// This would include 3xx redirects and 304 Request-Not-Modified,
// Neither of which the Strava API is expected to return.
options.simple = true
try {
const response = await this.request(options)
// Update rate limits using headers from the successful response
if (typeof response === 'object' && response !== null && 'headers' in response) {
rateLimiting.updateRateLimits(/** @type {FullResponse} */ (response).headers)
}
// FullResponse with body (handler returned { headers, body }): extract body so route modules get the payload
if (typeof response === 'object' && response !== null && 'headers' in response && 'body' in response) {
const fullRes = /** @type {FullResponse} */ (response)
const body = fullRes.body
if (typeof body === 'string') {
if (options.responseType === 'text') {
return body
}
if (options.responseType === 'json' || options.responseType === 'formdata') {
return /** @type {object} */ (JSONbig.parse(body))
}
return body
}
return /** @type {string | object} */ (body)
}
// Handler returned the payload directly (object without headers wrapper)
if (typeof response === 'object' && response !== null) {
return response
}
if (typeof response === 'string') {
// If responseType is 'text', return the raw string
if (options.responseType === 'text') {
return response
}
// For json or formdata, parse the raw JSON string with big-integer reviver for 16+ digit numbers
if (options.responseType === 'json' || options.responseType === 'formdata') {
return /** @type {object} */ (JSONbig.parse(response))
}
// Default: return as-is
return response
}
return response
} catch (e) {
// If the error includes a response, update the rate limits using its headers
const err = /** @type {{ response?: { headers?: Record<string, string|string[]|undefined> } }} */ (e)
if (err.response && err.response.headers) {
rateLimiting.updateRateLimits(err.response.headers)
}
// Re-throw the error to ensure it's handled elsewhere
throw e
}
}
module.exports = HttpClient