@onfleet/node-onfleet
Advanced tools
Comparing version 1.0.5 to 1.0.7
@@ -0,2 +1,4 @@ | ||
const Bottleneck = require('bottleneck'); | ||
const fetch = require('node-fetch'); | ||
const constants = require('./constants'); | ||
const util = require('./util'); | ||
@@ -10,2 +12,17 @@ const { | ||
// Create new rate limiter using defined constants | ||
const limiter = new Bottleneck({ | ||
reservoirRefreshInterval: constants.LIMITER_REFRESH_INTERVAL, | ||
maxConcurrent: constants.LIMITER_MAX_CONCURRENT, | ||
minTime: constants.LIMITER_MIN_TIME, | ||
}); | ||
// Rate reservoir refresh on every response | ||
// New rate is determined by x-ratelimit-remaining in headers | ||
const reassignRate = (newRate) => { | ||
limiter.updateSettings({ | ||
reservoirRefreshAmount: newRate, | ||
}); | ||
}; | ||
/** | ||
@@ -78,4 +95,4 @@ * The Method Factory | ||
// Send the HTTP request | ||
return fetch(url, { | ||
// Send the HTTP request through the rate limiter | ||
return limiter.schedule(() => fetch(url, { | ||
method: operations, | ||
@@ -87,2 +104,4 @@ headers: api.api.headers, | ||
if (res.ok) { | ||
// On response, update the rate | ||
reassignRate(res.headers.get('x-ratelimit-remaining')); | ||
// Return status code for deletion as the API does, else, return the body of the response | ||
@@ -112,5 +131,5 @@ return operations === 'DELETE' ? res.status : res.json(); | ||
throw (error); | ||
}); | ||
})); | ||
}; | ||
module.exports = Methods; |
{ | ||
"name": "@onfleet/node-onfleet", | ||
"version": "1.0.5", | ||
"version": "1.0.7", | ||
"description": "Official client library for accessing the Onfleet API", | ||
@@ -23,3 +23,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"node-fetch": "^2.3.0" | ||
"bottleneck": "^2.19.5", | ||
"node-fetch": "^2.6.0" | ||
}, | ||
@@ -32,2 +33,3 @@ "devDependencies": { | ||
"eslint-plugin-import": "^2.16.0", | ||
"minimist": "^0.2.1", | ||
"mocha": "^6.1.4", | ||
@@ -34,0 +36,0 @@ "nock": "^10.0.6" |
@@ -77,3 +77,3 @@ # Onfleet Node.js Wrapper | ||
### étranglement | ||
La limitation de débit est appliquée par l'API avec un seuil de 20 demandes par seconde pour toutes les clés d'API de votre organisation. Pour en savoir plus, [cliquez ici](https://docs.onfleet.com/reference#throttling). | ||
La limitation de débit est appliquée par l'API avec un seuil de 20 demandes par seconde pour toutes les clés d'API de votre organisation. Pour en savoir plus, [cliquez ici](https://docs.onfleet.com/reference#throttling). Nous avons mis en place un limiteur sur le wrapper lui-même pour vous éviter de dépasser involontairement vos limitations de taux et éventuellement être banni. | ||
@@ -80,0 +80,0 @@ ### Réponses |
@@ -76,3 +76,3 @@ # Onfleet Node.js Wrapper | ||
### Throttling | ||
Rate limiting is enforced by the API with a threshold of 20 requests per second across all your organization's API keys, learn more about it [here](https://docs.onfleet.com/reference#throttling). | ||
Rate limiting is enforced by the API with a threshold of 20 requests per second across all your organization's API keys, learn more about it [here](https://docs.onfleet.com/reference#throttling). We have implemented a limiter on the wrapper itself to avoid you from unintentionally exceeding your rate limitations and eventually be banned for. | ||
@@ -79,0 +79,0 @@ ### Responses |
@@ -79,3 +79,3 @@ # Onfleet Node.js Wrapper | ||
### API速限 | ||
原則上API的速限為每秒鐘20次請求,詳情請參考[官方文件](https://docs.onfleet.com/reference#throttling) | ||
原則上API的速限為每秒鐘20次請求,詳情請參考[官方文件](https://docs.onfleet.com/reference#throttling)。在此模組內我們也提供了限速,以避免您無意間超過了API請求的速限而導致帳號被禁的狀況。 | ||
@@ -82,0 +82,0 @@ ### 請求回應 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65050
28
924
2
8
+ Addedbottleneck@^2.19.5
+ Addedbottleneck@2.19.5(transitive)
Updatednode-fetch@^2.6.0