amazon-sp-api
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -8,3 +8,3 @@ const qs = require("qs"); | ||
class Request { | ||
constructor(region, use_sandbox, user_agent) { | ||
constructor(region, options) { | ||
this._region = region; | ||
@@ -16,5 +16,5 @@ this._aws_regions = { | ||
}; | ||
let sandbox_prefix = use_sandbox ? "sandbox." : ""; | ||
this._options = options; | ||
let sandbox_prefix = this._options.use_sandbox ? "sandbox." : ""; | ||
this._api_endpoint = `${sandbox_prefix}sellingpartnerapi-${this._region}.amazon.com`; | ||
this._user_agent = user_agent; | ||
this._iso_date; | ||
@@ -80,3 +80,3 @@ } | ||
host: this._api_endpoint, | ||
"user-agent": this._user_agent, | ||
"user-agent": this._options.user_agent, | ||
"x-amz-access-token": access_token, | ||
@@ -90,4 +90,27 @@ "x-amz-date": this._getUTCISODate(), | ||
execute(req_options) { | ||
async _wait(restore_rate) { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(resolve, restore_rate * 1000); | ||
}); | ||
} | ||
async _retryRequest(req_options, req_params) { | ||
// When an ETIMEDOUT error is fired we retry after 10 seconds or after the restore rate | ||
// depending on which value is higher to give the server some time to recover | ||
const restore_rate = req_params.restore_rate | ||
? Math.max(...[req_params.restore_rate, 10]) | ||
: 10; | ||
if (this._options.debug_log) { | ||
console.log( | ||
`Request timed out, retrying a call of ${ | ||
req_params.operation || req_params.api_path || req_options.url | ||
} in ${restore_rate} seconds...` | ||
); | ||
await this._wait(restore_rate); | ||
} | ||
return await this.execute(req_options, req_params); | ||
} | ||
execute(req_options, req_params = {}) { | ||
return new Promise((resolve, reject) => { | ||
let url = new URL(req_options.url); | ||
@@ -131,4 +154,6 @@ let options = { | ||
req.on("error", (e) => { | ||
req.on("error", async (e) => { | ||
timeouts.onResEnd(); | ||
if (e.code === "ETIMEDOUT" && this._options.retry_remote_timeout) | ||
await this._retryRequest(req_options, req_params); | ||
reject(e); | ||
@@ -145,3 +170,3 @@ }); | ||
let req_options = this._constructRequestOptions(access_token, req_params); | ||
return await this.execute(req_options); | ||
return await this.execute(req_options, req_params); | ||
} | ||
@@ -148,0 +173,0 @@ } |
module.exports = { | ||
productPricing:{ | ||
__versions:[ | ||
'v0', | ||
'2022-05-01' | ||
productPricing: { | ||
__versions: ["v0", "2022-05-01"], | ||
__operations: [ | ||
"getPricing", | ||
"getCompetitivePricing", | ||
"getListingOffers", | ||
"getItemOffers", | ||
"getItemOffersBatch", | ||
"getListingOffersBatch", | ||
"getFeaturedOfferExpectedPriceBatch", | ||
"getCompetitiveSummary" | ||
], | ||
__operations:[ | ||
'getPricing', | ||
'getCompetitivePricing', | ||
'getListingOffers', | ||
'getItemOffers', | ||
'getItemOffersBatch', | ||
'getListingOffersBatch', | ||
'getFeaturedOfferExpectedPriceBatch' | ||
], | ||
...require('./versions/product_pricing/productPricing_v0'), | ||
...require('./versions/product_pricing/productPricing_2022-05-01') | ||
...require("./versions/product_pricing/productPricing_v0"), | ||
...require("./versions/product_pricing/productPricing_2022-05-01") | ||
} | ||
}; | ||
}; |
module.exports = { | ||
'2022-05-01':{ | ||
getFeaturedOfferExpectedPriceBatch:(req_params) => { | ||
"2022-05-01": { | ||
getFeaturedOfferExpectedPriceBatch: (req_params) => { | ||
return Object.assign(req_params, { | ||
method:'POST', | ||
api_path:'/batches/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice', | ||
restore_rate:30 | ||
method: "POST", | ||
api_path: | ||
"/batches/products/pricing/2022-05-01/offer/featuredOfferExpectedPrice", | ||
restore_rate: 30 | ||
}); | ||
}, | ||
getCompetitiveSummary: (req_params) => { | ||
return Object.assign(req_params, { | ||
method: "POST", | ||
api_path: | ||
"/batches/products/pricing/2022-05-01/items/competitiveSummary", | ||
restore_rate: 30 | ||
}); | ||
} | ||
} | ||
}; | ||
}; |
@@ -45,5 +45,5 @@ const CustomError = require("./CustomError"); | ||
// deadline:0 // Optional: The time in milliseconds until a deadline timeout is fired (time between starting the request and receiving the full response). | ||
// } | ||
// }, | ||
// retry_remote_timeout:true // Optional: Whether or not the client should retry a request to the remote server that failed with an ETIMEDOUT error | ||
// } | ||
// TODO: We could add something like a debug option, that will enable included info logs, i.e. sandbox activated, credentials load type (env, file, config), etc. | ||
constructor(config) { | ||
@@ -64,3 +64,4 @@ this._region = config.region; | ||
debug_log: false, | ||
timeouts: {} | ||
timeouts: {}, | ||
retry_remote_timeout: true | ||
}, | ||
@@ -93,7 +94,3 @@ config.options | ||
this._request = new Request( | ||
this._region, | ||
this._options.use_sandbox, | ||
this._options.user_agent | ||
); | ||
this._request = new Request(this._region, this._options); | ||
} | ||
@@ -499,2 +496,21 @@ | ||
async _retryThrottledRequest(req_params, res) { | ||
// Wait the restore rate before retrying the call if dynamic or static restore rate is set | ||
if (res?.headers?.["x-amzn-ratelimit-limit"] || req_params.restore_rate) { | ||
// Use dynamic restore rate from result header if given --> otherwise use defined default restore_rate of the operation | ||
let restore_rate = res?.headers?.["x-amzn-ratelimit-limit"] | ||
? 1 / (res.headers["x-amzn-ratelimit-limit"] * 1) | ||
: req_params.restore_rate; | ||
if (this._options.debug_log) { | ||
console.log( | ||
`Request throttled, retrying a call of ${ | ||
req_params.operation || req_params.api_path | ||
} in ${restore_rate} seconds...` | ||
); | ||
} | ||
await this._wait(restore_rate); | ||
} | ||
return await this.callAPI(req_params); | ||
} | ||
// Exchange an authorization code received from a getAuthorizationCode operation for a refresh token | ||
@@ -634,3 +650,3 @@ async exchange(auth_code) { | ||
} | ||
if (res.statusCode === 204 && req_params.method === "DELETE") { | ||
if (res.statusCode === 204) { | ||
return { success: true }; | ||
@@ -669,18 +685,3 @@ } | ||
) { | ||
// Wait the restore rate before retrying the call if dynamic or static restore rate is set | ||
if (res.headers["x-amzn-ratelimit-limit"] || req_params.restore_rate) { | ||
// Use dynamic restore rate from result header if given --> otherwise use defined default restore_rate of the operation | ||
let restore_rate = res.headers["x-amzn-ratelimit-limit"] | ||
? 1 / (res.headers["x-amzn-ratelimit-limit"] * 1) | ||
: req_params.restore_rate; | ||
if (this._options.debug_log) { | ||
console.log( | ||
`Request throttled, retrying a call of ${ | ||
req_params.operation || req_params.api_path | ||
} in ${restore_rate} seconds...` | ||
); | ||
} | ||
await this._wait(restore_rate); | ||
} | ||
return await this.callAPI(req_params); | ||
return await this._retryThrottledRequest(req_params, res); | ||
} else if ( | ||
@@ -738,2 +739,13 @@ error.code === "InternalFailure" && | ||
if (options.json) { | ||
if ( | ||
res.headers["content-type"] === | ||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | ||
) { | ||
throw new CustomError({ | ||
code: "PARSE_ERROR", | ||
message: | ||
"Report is a .xlsx file. Could not parse result to JSON. Remove the 'json:true' option." | ||
}); | ||
} | ||
// Transform content to json --> take content type from which to transform to json from result header | ||
@@ -740,0 +752,0 @@ try { |
@@ -6,3 +6,3 @@ import { | ||
GetOrderBuyerInfoResponse, | ||
GetOrderItemResponse, | ||
GetOrderItemsResponse, | ||
GetOrderItemsBuyerInfoPath, | ||
@@ -288,3 +288,3 @@ GetOrderItemsBuyerInfoQuery, | ||
: TOperation extends "getOrderItem" | ||
? GetOrderItemResponse | ||
? GetOrderItemsResponse | ||
: TOperation extends "getOrderItemsBuyerInfo" | ||
@@ -291,0 +291,0 @@ ? GetOrderItemsBuyerInfoResponse |
@@ -211,8 +211,1 @@ import { BaseResponse } from "../baseTypes"; | ||
} | ||
interface ListCatalogItem { | ||
Identifiers: IdentifierType; | ||
AttributeSets?: AttributeSet[]; | ||
Relationships: Relationship[]; | ||
SalesRankings: SalesRank[]; | ||
} |
{ | ||
"name": "amazon-sp-api", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Amazon Selling Partner API client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -134,3 +134,4 @@ # amazon-sp-api (client for the Amazon Selling Partner API) | ||
... | ||
} | ||
}, | ||
retry_remote_timeout:true | ||
} | ||
@@ -164,2 +165,3 @@ } | ||
| **timeouts**<br>_optional_ | object | - | Allows to set timeouts for requests. Valid keys are `response`, `idle` and `deadline`. Please see detailed information in the [Timeouts](#timeouts) section. | | ||
| **retry_remote_timeout**<br>_optional_ | boolean | true | Whether or not the client should retry a request to the remote server that failed with an ETIMEDOUT error | | ||
@@ -166,0 +168,0 @@ ### Exchange an authorization code for a refresh token |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
267831
6953
772
2