@amazon-sp-api-release/amazon-sp-api-sdk-js 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,827 @@
1
+ /**
2
+ * The Selling Partner API for Finances
3
+ * The Selling Partner API for Finances helps you obtain financial information relevant to a seller's business. You can obtain financial events for a given order or date range without having to wait until a statement period closes.
4
+ *
5
+ * The version of the OpenAPI document: 2024-06-19
6
+ *
7
+ *
8
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
9
+ * https://openapi-generator.tech
10
+ * Do not edit the class manually.
11
+ *
12
+ */
13
+
14
+ import superagent from 'superagent'
15
+ import querystring from 'querystring'
16
+ import { readFileSync } from 'node:fs'
17
+ import { URL } from 'node:url'
18
+ import { RateLimitConfiguration } from '../../helper/RateLimitConfiguration.mjs'
19
+ import { SuperagentRateLimiter } from '../../helper/SuperagentRateLimiter.mjs'
20
+
21
+ /**
22
+ * @module finances_2024_06_19/ApiClient
23
+ * @version 2024-06-19
24
+ */
25
+
26
+ /**
27
+ * Private class used by ApiClient class. Used for executing LWA refresh token flow.
28
+ * @class
29
+ */
30
+ class LwaOAuthClient {
31
+ /**
32
+ * Private member to store LWA credential, refresh token or scope.
33
+ * @type {Object<String, String>}
34
+ */
35
+ #lwaClientInfo = {}
36
+
37
+ /**
38
+ * Private member to cache access token that is retrieved by auto-retrieval.
39
+ * @type {Map<String, {String, Number}> | null}
40
+ */
41
+ #cachedTokenMap = null
42
+
43
+ /**
44
+ * Constructs a new LwaOAuthClient.
45
+ * @param {String} clientId LWA Client ID.
46
+ * @param {String} clientSecret LWA Client Secret.
47
+ * @param {String|null} refreshToken LWA Refresh token.
48
+ * @param {String|null} scope LWA scope(s) for grantless operations.
49
+ */
50
+ constructor (clientId, clientSecret, refreshToken = null, scope = null) {
51
+ this.#lwaClientInfo = {
52
+ clientId, clientSecret, refreshToken, scope
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Execute LWA refresh token flow to retrieve access token to be used for SP-API calls.
58
+ * @returns {Promise<String>} LWA access token.
59
+ */
60
+ retrieveAccessToken = async () => {
61
+ const key = JSON.stringify(this.#lwaClientInfo)
62
+
63
+ if (this.#cachedTokenMap) {
64
+ const cachedTokenItem = this.#cachedTokenMap.get(key)
65
+
66
+ if (cachedTokenItem) {
67
+ const cachedToken = cachedTokenItem.cachedToken
68
+ const cachedTokenExpiration = cachedTokenItem.cachedTokenExpiration
69
+ // Adjustment in milliseconds (60s) to avoid using nearly expired tokens
70
+ const adjustedExpiryTime = cachedTokenExpiration - 60000
71
+ if (adjustedExpiryTime > Date.now()) {
72
+ return Promise.resolve(cachedToken)
73
+ } else {
74
+ this.#cachedTokenMap.delete(key)
75
+ }
76
+ }
77
+ }
78
+
79
+ const res = await this.#doRefresh()
80
+ if (!this.#cachedTokenMap) {
81
+ this.#cachedTokenMap = new Map()
82
+ }
83
+ this.#cachedTokenMap.set(key, { cachedToken: res.access_token, cachedTokenExpiration: Date.now() + res.expires_in * 1000 })
84
+ return res.access_token
85
+ }
86
+
87
+ /**
88
+ * Private method to execute LWA token refresh flow.
89
+ * @returns {Promise<Object>} LWA token response.
90
+ */
91
+ #doRefresh = async () => {
92
+ let requestBody = null
93
+ if (this.#lwaClientInfo.scope) {
94
+ // grantless operations
95
+ requestBody = `grant_type=client_credentials&client_id=${this.#lwaClientInfo.clientId}&client_secret=${this.#lwaClientInfo.clientSecret}&scope=${this.#lwaClientInfo.scope}`
96
+ } else {
97
+ requestBody = `grant_type=refresh_token&refresh_token=${this.#lwaClientInfo.refreshToken}&client_id=${this.#lwaClientInfo.clientId}&client_secret=${this.#lwaClientInfo.clientSecret}`
98
+ }
99
+ const res = await superagent.post('https://api.amazon.com/auth/o2/token')
100
+ .send(requestBody)
101
+ .set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')
102
+ return res.body
103
+ }
104
+
105
+ clearCachedTokenMap () {
106
+ if (this.#cachedTokenMap) {
107
+ this.#cachedTokenMap.clear()
108
+ }
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Private class used by ApiClient class. Used for executing LWA refresh token flow and calling Tokens API to retrieve RDT.
114
+ * @class
115
+ */
116
+ class TokensApiClient {
117
+ /**
118
+ * Private member to execute LWA refresh token flow.
119
+ * @type {Object<LwaOAuthClient>}
120
+ */
121
+ #lwaClient = null
122
+
123
+ /**
124
+ * Private member to store 'dataElements' parameter for Tokens API call.
125
+ * @type {Array<String>}
126
+ */
127
+ #dataElements = null
128
+
129
+ /**
130
+ * Constructs a new TokensApiClient.
131
+ * @param {String} clientId LWA Client ID.
132
+ * @param {String} clientSecret LWA Client Secret.
133
+ * @param {String} refreshToken LWA Refresh token.
134
+ * @param {Array<String>} dataElements Optional specifiers for PII data elements tp retrieve.
135
+ */
136
+ constructor (clientId, clientSecret, refreshToken, dataElements) {
137
+ this.#lwaClient = new LwaOAuthClient(clientId, clientSecret, refreshToken, null)
138
+ if (dataElements && !Array.isArray(dataElements)) {
139
+ throw new Error(`dataElements parameter to TokensApiClient constructor must be array of string. Illegal parameter:${dataElements}`)
140
+ }
141
+ this.#dataElements = dataElements || null
142
+ }
143
+
144
+ /**
145
+ * Execute createRestrictedDataToken to retrieve RDT to be used for PII-handling SP-API calls.
146
+ * @param {String} method method for SP-API request.
147
+ * @param {String} url URL for SP-API call to be made.
148
+ * @param {Array<String>} dataElements specify PII information to get from "buyerInfo", "shippingAddress" and "buyerTaxInformation".
149
+ * @returns {Promise<String>} Restricted Data Token (RDT).
150
+ */
151
+ retrieveRestrictedDataToken = async (method, url) => {
152
+ const accessToken = await this.#lwaClient.retrieveAccessToken()
153
+ const urlToRequest = new URL(url)
154
+ const res = await this.#doCreateRestrictedDataToken(accessToken, method, urlToRequest)
155
+ return res.restrictedDataToken
156
+ }
157
+
158
+ /**
159
+ * Private method to execute createRestrictedDataToken
160
+ * @param {String} accessToken Access token to call RDT request.
161
+ * @param {URL} url URL object for URL string manipulation.
162
+ * @param {Array<String>} dataElements specify PII information to get from "buyerInfo", "shippingAddress" and "buyerTaxInformation".
163
+ * @returns {Promise<Object>} createRestrictedDataToken response.
164
+ */
165
+ #doCreateRestrictedDataToken = async (accessToken, method, url) => {
166
+ const tokenApiUrl = `${url.origin}/tokens/2021-03-01/restrictedDataToken`
167
+ const pathToRequest = url.pathname
168
+ const res = await superagent.post(tokenApiUrl)
169
+ .send(
170
+ {
171
+ restrictedResources: [
172
+ {
173
+ method,
174
+ path: pathToRequest,
175
+ dataElements: this.#dataElements ? this.#dataElements : undefined
176
+ }
177
+ ]
178
+ }
179
+ ).set('Content-Type', 'application/json')
180
+ .set({ 'x-amz-access-token': accessToken })
181
+ return res.body
182
+ }
183
+ }
184
+
185
+ /**
186
+ * Manages low level client-server communications, parameter marshalling, etc. There should not be any need for an
187
+ * application to use this class directly - the *Api and model classes provide the public API for the service. The
188
+ * contents of this file should be regarded as internal but are documented for completeness.
189
+ * @alias module:finances_2024_06_19/ApiClient
190
+ * @class
191
+ */
192
+ export class ApiClient {
193
+ #tokenForApiCall = null
194
+ #lwaClient = null
195
+ #rdtClient = null
196
+ #customizedRateLimiterMap = null
197
+ #useRateLimiter = true
198
+
199
+ /**
200
+ * Constructs a new ApiClient.
201
+ * @param {String} baseUrl Base URL of endpoint ex. "https://sellingpartnerapi-na.amazon.com"
202
+ */
203
+ constructor (baseUrl) {
204
+ /**
205
+ * The base URL against which to resolve every API call's (relative) path.
206
+ * @type {String}
207
+ * @default https://sellingpartnerapi-na.amazon.com
208
+ */
209
+ this.basePath = baseUrl ? baseUrl.replace(/\/+$/, '') : 'https://sellingpartnerapi-na.amazon.com'.replace(/\/+$/, '')
210
+
211
+ /**
212
+ * The authentication methods to be included for all API calls.
213
+ * @type {Array<String>}
214
+ */
215
+ this.authentications = {
216
+ }
217
+
218
+ const pkg = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url)))
219
+ const version = pkg.version ? pkg.version : 'undefined'
220
+
221
+ /**
222
+ * The default HTTP headers to be included for all API calls.
223
+ * @type {Array<String>}
224
+ * @default {}
225
+ */
226
+ this.defaultHeaders = {
227
+ 'User-Agent': `amazon-selling-partner-api-sdk/${version}/JavaScript`
228
+ }
229
+
230
+ /**
231
+ * The default HTTP timeout for all API calls.
232
+ * @type {Number}
233
+ * @default 60000
234
+ */
235
+ this.timeout = 60000
236
+
237
+ /**
238
+ * If set to false an additional timestamp parameter is added to all API GET calls to
239
+ * prevent browser caching
240
+ * @type {Boolean}
241
+ * @default true
242
+ */
243
+ this.cache = true
244
+
245
+ /**
246
+ * If set to true, the client will save the cookies from each server
247
+ * response, and return them in the next request.
248
+ * @default false
249
+ */
250
+ this.enableCookies = false
251
+
252
+ /*
253
+ * Used to save and return cookies in a node.js (non-browser) setting,
254
+ * if this.enableCookies is set to true.
255
+ */
256
+ if (typeof window === 'undefined') {
257
+ this.agent = new superagent.agent()
258
+ }
259
+
260
+ /*
261
+ * Allow user to override superagent agent
262
+ */
263
+ this.requestAgent = null
264
+
265
+ /*
266
+ * Initialize customized rate limiter map
267
+ */
268
+ this.#customizedRateLimiterMap = new Map()
269
+ }
270
+
271
+ /**
272
+ * Set customized rate limiter for one operation
273
+ * For operations that customized rate limiter are not set
274
+ * Will use default rate limiter
275
+ * @param {String} operationName
276
+ * @param {RateLimitConfiguration} config
277
+ */
278
+ setCustomizedRateLimiterForOperation (operationName, config) {
279
+ this.#customizedRateLimiterMap.set(operationName, new SuperagentRateLimiter(config))
280
+ }
281
+
282
+ /**
283
+ * Disable customized rate limiter for one operation
284
+ * Fall back to default rate limiter
285
+ * @param {String} operationName
286
+ */
287
+ disableCustomizedRatelimiterForOperation (operationName) {
288
+ this.#customizedRateLimiterMap.delete(operationName)
289
+ }
290
+
291
+ /**
292
+ * Clear customized rate limiter for all operations
293
+ * All operations will fall back to default rate limiter
294
+ * @param {String} operationName
295
+ */
296
+ disableCustomizedRatelimiterForAll () {
297
+ this.#customizedRateLimiterMap.clear()
298
+ }
299
+
300
+ /**
301
+ * Disable both default and customized rate limiter for all operations
302
+ */
303
+ disableRateLimiter () {
304
+ this.#useRateLimiter = false
305
+ }
306
+
307
+ /**
308
+ * Enable default or customized rate limiter for all operations
309
+ */
310
+ enableRateLimiter () {
311
+ this.#useRateLimiter = true
312
+ }
313
+
314
+ /**
315
+ * Returns this ApiClient so that you can chain the methods.
316
+ * @param {String} clientId LWA client ID.
317
+ * @param {String} clientSecret LWA client secret.
318
+ * @param {String} refreshToken LWA refresh token.
319
+ * @param {Array<String>} dataElementsOption specify PII information to get from "buyerInfo", "shippingAddress" and "buyerTaxInformation".
320
+ * @returns {ApiClient} This ApiClient, which is going to use give accessToken for all API calls.
321
+ */
322
+ enableAutoRetrievalRestrictedDataToken (clientId, clientSecret, refreshToken, dataElementsOption) {
323
+ if (!clientId || !clientSecret || !refreshToken) {
324
+ throw new Error('invalid parameter(s) to enableAutoRetrievalRestrictedDataToken.')
325
+ }
326
+ this.#rdtClient = new TokensApiClient(clientId, clientSecret, refreshToken, dataElementsOption)
327
+ return this
328
+ }
329
+
330
+ /**
331
+ * Returns this ApiClient so that you can chain the methods.
332
+ * @param {String} clientId LWA client ID.
333
+ * @param {String} clientSecret LWA client secret.
334
+ * @param {String|null} refreshToken LWA refresh token.
335
+ * @param {String|null} scope LWA scope(s) for grantless operations.
336
+ * @returns {ApiClient} This ApiClient, which is going to use give accessToken for all API calls.
337
+ */
338
+ enableAutoRetrievalAccessToken (clientId, clientSecret, refreshToken = null, scope = null) {
339
+ if (!clientId || !clientSecret) {
340
+ throw new Error('invalid parameter(s) to enableAutoRetrievalAccessToken: clientId or clientSecret is null or undefined.')
341
+ } else if ((!refreshToken && !scope) || (refreshToken && scope)) {
342
+ throw new Error('invalid parameter(s) to enableAutoRetrievalAccessToken: either refreshToken or scope must be defined.')
343
+ }
344
+ this.#lwaClient = new LwaOAuthClient(clientId, clientSecret, refreshToken, scope)
345
+ return this
346
+ }
347
+
348
+ /**
349
+ * Clear Token Cache
350
+ */
351
+ clearAccessTokenCache () {
352
+ if (this.#lwaClient) {
353
+ this.#lwaClient.clearCachedTokenMap()
354
+ }
355
+ }
356
+
357
+ /**
358
+ * Returns this ApiClient so that you can chain the methods.
359
+ * @param {String} restrictedDataToken RDT token to use for SP-API call.
360
+ * @returns {ApiClient} This ApiClient, which is going to use give RDT for all API calls.
361
+ */
362
+ applyRestrictedDataToken (restrictedDataToken) {
363
+ this.#tokenForApiCall = restrictedDataToken
364
+ return this
365
+ }
366
+
367
+ /**
368
+ * Applies authentication headers to the request.
369
+ * @param {String} accessOrRdtToken Either Access Token or Restricted Data Token to add as 'x-amz-access-token'.
370
+ * @returns {ApiClient} This ApiClient, which is going to use give RDT for all API calls.
371
+ */
372
+ applyXAmzAccessTokenToRequest (accessOrRdtToken) {
373
+ if (!accessOrRdtToken) {
374
+ throw new Error('empty string, null or undefined passed to applyXAmzAccessTokenToRequest')
375
+ }
376
+ this.#tokenForApiCall = accessOrRdtToken
377
+ return this
378
+ }
379
+
380
+ /**
381
+ * Returns a string representation for an actual parameter.
382
+ * @param param The actual parameter.
383
+ * @returns {String} The string representation of <code>param</code>.
384
+ */
385
+ paramToString (param) {
386
+ if (param == undefined || param == null) {
387
+ return ''
388
+ }
389
+ if (param instanceof Date) {
390
+ return param.toJSON()
391
+ }
392
+ return param.toString()
393
+ }
394
+
395
+ /**
396
+ * Builds full URL by appending the given path to the base URL and replacing path parameter place-holders with parameter values.
397
+ * NOTE: query parameters are not handled here.
398
+ * @param {String} path The path to append to the base URL.
399
+ * @param {Object} pathParams The parameter values to append.
400
+ * @returns {String} The encoded path with parameter values substituted.
401
+ */
402
+ buildUrl (path, pathParams) {
403
+ if (!path.match(/^\//)) {
404
+ path = '/' + path
405
+ }
406
+ let url = this.basePath + path
407
+ url = url.replace(/\{([\w-]+)\}/g, (fullMatch, key) => {
408
+ let value
409
+ if (pathParams.hasOwnProperty(key)) {
410
+ value = this.paramToString(pathParams[key])
411
+ } else {
412
+ value = fullMatch
413
+ }
414
+ return encodeURIComponent(value)
415
+ })
416
+ return url
417
+ }
418
+
419
+ /**
420
+ * Checks whether the given content type represents JSON.<br>
421
+ * JSON content type examples:<br>
422
+ * <ul>
423
+ * <li>application/json</li>
424
+ * <li>application/json; charset=UTF8</li>
425
+ * <li>APPLICATION/JSON</li>
426
+ * </ul>
427
+ * @param {String} contentType The MIME content type to check.
428
+ * @returns {Boolean} <code>true</code> if <code>contentType</code> represents JSON, otherwise <code>false</code>.
429
+ */
430
+ isJsonMime (contentType) {
431
+ return Boolean(contentType != null && contentType.match(/^application\/json(;.*)?$/i))
432
+ }
433
+
434
+ /**
435
+ * Chooses a content type from the given array, with JSON preferred; i.e. return JSON if included, otherwise return the first.
436
+ * @param {Array<String>} contentTypes
437
+ * @returns {String} The chosen content type, preferring JSON.
438
+ */
439
+ jsonPreferredMime (contentTypes) {
440
+ for (let i = 0; i < contentTypes.length; i++) {
441
+ if (this.isJsonMime(contentTypes[i])) {
442
+ return contentTypes[i]
443
+ }
444
+ }
445
+ return contentTypes[0]
446
+ }
447
+
448
+ /**
449
+ * Checks whether the given parameter value represents file-like content.
450
+ * @param param The parameter to check.
451
+ * @returns {Boolean} <code>true</code> if <code>param</code> represents a file.
452
+ */
453
+ isFileParam (param) {
454
+ // fs.ReadStream in Node.js and Electron (but not in runtime like browserify)
455
+ if (typeof require === 'function') {
456
+ let fs
457
+ try {
458
+ fs = require('fs')
459
+ } catch (err) {}
460
+ if (fs && fs.ReadStream && param instanceof fs.ReadStream) {
461
+ return true
462
+ }
463
+ }
464
+
465
+ // Buffer in Node.js
466
+ if (typeof Buffer === 'function' && param instanceof Buffer) {
467
+ return true
468
+ }
469
+
470
+ // Blob in browser
471
+ if (typeof Blob === 'function' && param instanceof Blob) {
472
+ return true
473
+ }
474
+
475
+ // File in browser (it seems File object is also instance of Blob, but keep this for safe)
476
+ if (typeof File === 'function' && param instanceof File) {
477
+ return true
478
+ }
479
+
480
+ return false
481
+ }
482
+
483
+ /**
484
+ * Normalizes parameter values:
485
+ * <ul>
486
+ * <li>remove nils</li>
487
+ * <li>keep files and arrays</li>
488
+ * <li>format to string with `paramToString` for other cases</li>
489
+ * </ul>
490
+ * @param {Object<String, Object>} params The parameters as object properties.
491
+ * @returns {Object<String, Object>} normalized parameters.
492
+ */
493
+ normalizeParams (params) {
494
+ const newParams = {}
495
+ for (const key in params) {
496
+ if (params.hasOwnProperty(key) && params[key] != undefined && params[key] != null) {
497
+ const value = params[key]
498
+ if (this.isFileParam(value) || Array.isArray(value)) {
499
+ newParams[key] = value
500
+ } else {
501
+ newParams[key] = this.paramToString(value)
502
+ }
503
+ }
504
+ }
505
+ return newParams
506
+ }
507
+
508
+ /**
509
+ * Enumeration of collection format separator strategies.
510
+ * @enum {String}
511
+ * @readonly
512
+ */
513
+ static CollectionFormatEnum = {
514
+ /**
515
+ * Comma-separated values. Value: <code>csv</code>
516
+ * @const
517
+ */
518
+ CSV: ',',
519
+
520
+ /**
521
+ * Space-separated values. Value: <code>ssv</code>
522
+ * @const
523
+ */
524
+ SSV: ' ',
525
+
526
+ /**
527
+ * Tab-separated values. Value: <code>tsv</code>
528
+ * @const
529
+ */
530
+ TSV: '\t',
531
+
532
+ /**
533
+ * Pipe(|)-separated values. Value: <code>pipes</code>
534
+ * @const
535
+ */
536
+ PIPES: '|',
537
+
538
+ /**
539
+ * Native array. Value: <code>multi</code>
540
+ * @const
541
+ */
542
+ MULTI: 'multi'
543
+ }
544
+
545
+ /**
546
+ * Builds a string representation of an array-type actual parameter, according to the given collection format.
547
+ * @param {Array} param An array parameter.
548
+ * @param {module:finances_2024_06_19/ApiClient.CollectionFormatEnum} collectionFormat The array element separator strategy.
549
+ * @returns {String|Array} A string representation of the supplied collection, using the specified delimiter. Returns
550
+ * <code>param</code> as is if <code>collectionFormat</code> is <code>multi</code>.
551
+ */
552
+ buildCollectionParam (param, collectionFormat) {
553
+ if (param == null) {
554
+ return null
555
+ }
556
+ switch (collectionFormat) {
557
+ case 'csv':
558
+ return param.map(this.paramToString).join(',')
559
+ case 'ssv':
560
+ return param.map(this.paramToString).join(' ')
561
+ case 'tsv':
562
+ return param.map(this.paramToString).join('\t')
563
+ case 'pipes':
564
+ return param.map(this.paramToString).join('|')
565
+ case 'multi':
566
+ // return the array directly as SuperAgent will handle it as expected
567
+ return param.map(this.paramToString)
568
+ default:
569
+ throw new Error('Unknown collection format: ' + collectionFormat)
570
+ }
571
+ }
572
+
573
+ /**
574
+ * Deserializes an HTTP response body into a value of the specified type.
575
+ * @param {Object} response A SuperAgent response object.
576
+ * @param {(String|Array<String>|Object<String, Object>|Function)} returnType The type to return. Pass a string for simple types
577
+ * or the constructor function for a complex type. Pass an array containing the type name to return an array of that type. To
578
+ * return an object, pass an object with one property whose name is the key type and whose value is the corresponding value type:
579
+ * all properties on <code>data<code> will be converted to this type.
580
+ * @returns A value of the specified type.
581
+ */
582
+ deserialize (response, returnType) {
583
+ if (response == null || returnType == null || response.status == 204) {
584
+ return null
585
+ }
586
+
587
+ // Rely on SuperAgent for parsing response body.
588
+ // See http://visionmedia.github.io/superagent/#parsing-response-bodies
589
+ let data = response.body
590
+ if (data == null || (typeof data === 'object' && typeof data.length === 'undefined' && !Object.keys(data).length)) {
591
+ // SuperAgent does not always produce a body; use the unparsed response as a fallback
592
+ data = response.text
593
+ }
594
+
595
+ return ApiClient.convertToType(data, returnType)
596
+ }
597
+
598
+ /**
599
+ * Invokes the REST service using the supplied settings and parameters.
600
+ * @param {String} operation operation name.
601
+ * @param {String} path The base URL to invoke.
602
+ * @param {String} httpMethod The HTTP method to use.
603
+ * @param {Object<String, String>} pathParams A map of path parameters and their values.
604
+ * @param {Object<String, Object>} queryParams A map of query parameters and their values.
605
+ * @param {Object<String, Object>} headerParams A map of header parameters and their values.
606
+ * @param {Object<String, Object>} formParams A map of form parameters and their values.
607
+ * @param {Object} bodyParam The value to pass as the request body.
608
+ * @param {Array<String>} contentTypes An array of request MIME types.
609
+ * @param {Array<String>} accepts An array of acceptable response MIME types.
610
+ * @param {(String|Array|ObjectFunction)} returnType The required type to return; can be a string for simple types or the
611
+ * constructor for a complex type.
612
+ * @param {SuperagentRateLimiter} defaultRateLimiter The default rate limiter.
613
+ * @returns {Promise} A {@link https://www.promisejs.org/|Promise} object.
614
+ */
615
+ async callApi (operation, path, httpMethod, pathParams,
616
+ queryParams, headerParams, formParams, bodyParam, contentTypes, accepts,
617
+ returnType, defaultRateLimiter) {
618
+ const url = this.buildUrl(path, pathParams)
619
+ const request = superagent(httpMethod, url)
620
+ if (!this.#tokenForApiCall && !this.#lwaClient && !this.#rdtClient) {
621
+ throw new Error('none of accessToken, RDT token and auto-retrieval is set.')
622
+ }
623
+
624
+ if (this.#useRateLimiter) {
625
+ // Set rate limiter
626
+ if (this.#customizedRateLimiterMap.get(operation)) {
627
+ request.use(this.#customizedRateLimiterMap.get(operation).getPlugin())
628
+ } else if (defaultRateLimiter) {
629
+ request.use(defaultRateLimiter.getPlugin())
630
+ }
631
+ }
632
+
633
+ // set query parameters
634
+ if (httpMethod.toUpperCase() === 'GET' && this.cache === false) {
635
+ queryParams._ = new Date().getTime()
636
+ }
637
+
638
+ request.query(this.normalizeParams(queryParams))
639
+
640
+ // set header parameters
641
+ request.set(this.defaultHeaders).set(this.normalizeParams(headerParams))
642
+
643
+ // set x-amz-access-token header
644
+ if (this.#tokenForApiCall) {
645
+ // add custom header for SP-API call.
646
+ request.set({ 'x-amz-access-token': this.#tokenForApiCall })
647
+ } else if (this.#rdtClient) {
648
+ const rdt = await this.#rdtClient.retrieveRestrictedDataToken(httpMethod, url)
649
+ request.set({ 'x-amz-access-token': rdt })
650
+ } else if (this.#lwaClient) {
651
+ // execute token refresh.
652
+ const accessToken = await this.#lwaClient.retrieveAccessToken()
653
+ request.set({ 'x-amz-access-token': accessToken })
654
+ }
655
+
656
+ // set requestAgent if it is set by user
657
+ if (this.requestAgent) {
658
+ request.agent(this.requestAgent)
659
+ }
660
+
661
+ // set request timeout
662
+ request.timeout(this.timeout)
663
+
664
+ const contentType = this.jsonPreferredMime(contentTypes)
665
+ if (contentType) {
666
+ // Issue with superagent and multipart/form-data (https://github.com/visionmedia/superagent/issues/746)
667
+ if (contentType != 'multipart/form-data') {
668
+ request.type(contentType)
669
+ }
670
+ } else if (!request.header['Content-Type']) {
671
+ request.type('application/json')
672
+ }
673
+
674
+ if (contentType === 'application/x-www-form-urlencoded') {
675
+ request.send(querystring.stringify(this.normalizeParams(formParams)))
676
+ } else if (contentType == 'multipart/form-data') {
677
+ const _formParams = this.normalizeParams(formParams)
678
+ for (const key in _formParams) {
679
+ if (_formParams.hasOwnProperty(key)) {
680
+ if (this.isFileParam(_formParams[key])) {
681
+ // file field
682
+ request.attach(key, _formParams[key])
683
+ } else {
684
+ request.field(key, _formParams[key])
685
+ }
686
+ }
687
+ }
688
+ } else if (bodyParam) {
689
+ request.send(bodyParam)
690
+ }
691
+
692
+ const accept = this.jsonPreferredMime(accepts)
693
+ if (accept) {
694
+ request.accept(accept)
695
+ }
696
+
697
+ if (returnType === 'Blob') {
698
+ request.responseType('blob')
699
+ } else if (returnType === 'String') {
700
+ request.responseType('string')
701
+ }
702
+
703
+ // Attach previously saved cookies, if enabled
704
+ if (this.enableCookies) {
705
+ if (typeof window === 'undefined') {
706
+ this.agent.attachCookies(request)
707
+ } else {
708
+ request.withCredentials()
709
+ }
710
+ }
711
+
712
+ return new Promise((resolve, reject) => {
713
+ request.end((error, response) => {
714
+ if (error) {
715
+ reject(error)
716
+ } else {
717
+ try {
718
+ const data = this.deserialize(response, returnType)
719
+ if (this.enableCookies && typeof window === 'undefined') {
720
+ this.agent.saveCookies(response)
721
+ }
722
+ resolve({ data, response })
723
+ } catch (err) {
724
+ reject(err)
725
+ }
726
+ }
727
+ })
728
+ })
729
+ }
730
+
731
+ /**
732
+ * Parses an ISO-8601 string representation of a date value.
733
+ * @param {String} str The date value as a string.
734
+ * @returns {Date} The parsed date object.
735
+ */
736
+ static parseDate (str) {
737
+ return new Date(str)
738
+ }
739
+
740
+ /**
741
+ * Converts a value to the specified type.
742
+ * @param {(String|Object)} data The data to convert, as a string or object.
743
+ * @param {(String|Array<String>|Object<String, Object>|Function)} type The type to return. Pass a string for simple types
744
+ * or the constructor function for a complex type. Pass an array containing the type name to return an array of that type. To
745
+ * return an object, pass an object with one property whose name is the key type and whose value is the corresponding value type:
746
+ * all properties on <code>data<code> will be converted to this type.
747
+ * @returns An instance of the specified type or null or undefined if data is null or undefined.
748
+ */
749
+ static convertToType (data, type) {
750
+ if (data === null || data === undefined) { return data }
751
+
752
+ switch (type) {
753
+ case 'Boolean':
754
+ return Boolean(data)
755
+ case 'Integer':
756
+ return parseInt(data, 10)
757
+ case 'Number':
758
+ return parseFloat(data)
759
+ case 'String':
760
+ return String(data)
761
+ case 'Date':
762
+ return ApiClient.parseDate(String(data))
763
+ case 'Blob':
764
+ return data
765
+ default:
766
+ if (type === Object) {
767
+ // generic object, return directly
768
+ return data
769
+ } else if (typeof type === 'function') {
770
+ // for model type like: User
771
+ return type.constructFromObject(data)
772
+ } else if (Array.isArray(type)) {
773
+ // for array type like: ['String']
774
+ const itemType = type[0]
775
+ return data.map((item) => {
776
+ return ApiClient.convertToType(item, itemType)
777
+ })
778
+ } else if (typeof type === 'object') {
779
+ // for plain object type like: {'String': 'Integer'}
780
+ let keyType, valueType
781
+ for (var k in type) {
782
+ if (type.hasOwnProperty(k)) {
783
+ keyType = k
784
+ valueType = type[k]
785
+ break
786
+ }
787
+ }
788
+
789
+ const result = {}
790
+ for (var k in data) {
791
+ if (data.hasOwnProperty(k)) {
792
+ const key = ApiClient.convertToType(k, keyType)
793
+ const value = ApiClient.convertToType(data[k], valueType)
794
+ result[key] = value
795
+ }
796
+ }
797
+
798
+ return result
799
+ } else {
800
+ // for unknown type, return the data directly
801
+ return data
802
+ }
803
+ }
804
+ }
805
+
806
+ /**
807
+ * Constructs a new map or array model from REST data.
808
+ * @param data {Object|Array} The REST data.
809
+ * @param obj {Object|Array} The target object or array.
810
+ */
811
+ static constructFromObject (data, obj, itemType) {
812
+ if (Array.isArray(data)) {
813
+ for (let i = 0; i < data.length; i++) {
814
+ if (data.hasOwnProperty(i)) { obj[i] = ApiClient.convertToType(data[i], itemType) }
815
+ }
816
+ } else {
817
+ for (const k in data) {
818
+ if (data.hasOwnProperty(k)) { obj[k] = ApiClient.convertToType(data[k], itemType) }
819
+ }
820
+ }
821
+ }
822
+ }
823
+ /**
824
+ * The default API client implementation.
825
+ * @type {module:finances_2024_06_19/ApiClient}
826
+ */
827
+ ApiClient.instance = new ApiClient()