@onfleet/node-onfleet
Advanced tools
Comparing version 1.1.1 to 1.2.0
const constants = { | ||
LIMITER_RESERVOIR: 20, | ||
LIMITER_REFRESH_INTERVAL: 250, | ||
LIMITER_WAIT_UPON_DEPLETION: 10000, | ||
LIMITER_MAX_CONCURRENT: 1, | ||
@@ -5,0 +5,0 @@ LIMITER_MIN_TIME: 50, |
@@ -0,1 +1,2 @@ | ||
/* eslint-disable no-console */ | ||
const Bottleneck = require('bottleneck'); | ||
@@ -15,3 +16,2 @@ const fetch = require('node-fetch'); | ||
reservoir: constants.LIMITER_RESERVOIR, | ||
reservoirRefreshInterval: constants.LIMITER_REFRESH_INTERVAL, | ||
maxConcurrent: constants.LIMITER_MAX_CONCURRENT, | ||
@@ -26,3 +26,3 @@ minTime: constants.LIMITER_MIN_TIME, | ||
limiter.updateSettings({ | ||
reservoirRefreshAmount: newRate, | ||
reservoir: newRate, | ||
}); | ||
@@ -32,2 +32,10 @@ } | ||
const wait = (ms) => { | ||
console.log('Waiting due to rate limiting'); | ||
// eslint-disable-next-line no-new | ||
new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
}; | ||
/** | ||
@@ -107,4 +115,17 @@ * The Method Factory | ||
}).then((res) => { | ||
// On response, update the rate | ||
reassignRate(res.headers.get('x-ratelimit-remaining')); | ||
// On reservoir depletion, we wait 10000ms and reset the rate again (20 req/second limitation) | ||
limiter.on('depleted', (empty) => { | ||
if (!empty) { | ||
wait(constants.LIMITER_WAIT_UPON_DEPLETION).then(() => { | ||
reassignRate(constants.LIMITER_RESERVOIR); | ||
}); | ||
} | ||
}); | ||
// For every request, we compare the reservoir with the remainding rate limit in the header | ||
limiter.currentReservoir() | ||
.then((reservoir) => { | ||
if (reservoir < res.headers.get('x-ratelimit-remaining')) { | ||
reassignRate(res.headers.get('x-ratelimit-remaining')); | ||
} | ||
}); | ||
@@ -111,0 +132,0 @@ if (res.ok) { |
@@ -28,5 +28,7 @@ const DEFAULT_URL = 'https://onfleet.com'; | ||
class Onfleet { | ||
constructor(apiKey) { | ||
constructor(apiKey, userTimeout) { | ||
if (!apiKey) { | ||
throw new ValidationError('Onfleet API key not found, please obtain an API key from your organization admin'); | ||
} if (userTimeout > 70000) { | ||
throw new ValidationError('User-defined timeout has to be shorter than 70000ms'); | ||
} else { | ||
@@ -36,3 +38,4 @@ this.apiKey = apiKey; | ||
baseUrl: `${DEFAULT_URL}${DEFAULT_PATH}/${DEFAULT_API_VERSION}`, | ||
timeout: DEFAULT_TIMEOUT, | ||
// eslint-disable-next-line no-unneeded-ternary | ||
timeout: (userTimeout ? userTimeout : DEFAULT_TIMEOUT), | ||
headers: { | ||
@@ -39,0 +42,0 @@ 'Content-Type': 'application/json', |
{ | ||
"name": "@onfleet/node-onfleet", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Official client library for accessing the Onfleet API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -65,2 +65,7 @@ # Onfleet Node.js Wrapper | ||
``` | ||
En tant que champ facultatif, vous pouvez introduire un délai d'expiration personnalisé inférieur à la valeur par défaut de 70000 ms (délai d'expiration de l'API Onfleet par défaut) en fournissant un 2ème paramètre: | ||
```js | ||
const onfleet = new Onfleet('<clé_api>', 30000) // Cela mettra vos wrappers à expiration à 30000ms au lieu de 70000ms | ||
``` | ||
### Authentification | ||
@@ -67,0 +72,0 @@ Une fois que l'objet Onfleet est créé, vous pouvez utiliser une fonction utilitaire pour tester le noeud final d'authentification. Cette fonction renvoie un booléen: |
@@ -64,2 +64,8 @@ # Onfleet Node.js Wrapper | ||
``` | ||
As an optional field, you can introduce a customized timeout that is less than the default 70000ms (default Onfleet API timeout) by providing a 2nd parameter: | ||
```js | ||
const onfleet = new Onfleet('<api_key>', 30000) // This will set your wrappers to timeout at 30000ms instead of 70000ms | ||
``` | ||
### Authentication | ||
@@ -66,0 +72,0 @@ Once the Onfleet object is created, you can use a utility function to test on the authentication endpoint, this function returns a boolean: |
@@ -67,2 +67,8 @@ # Onfleet Node.js Wrapper | ||
``` | ||
由於某些應用的執行逾時參數較低(例如Heroku的三十秒設定),您可以在創立物件時,提供一個低於70000ms、客製化的逾時參數: | ||
```js | ||
const onfleet = new Onfleet('<api_key>', 30000) // 在此設定執行逾時參數為30000ms | ||
``` | ||
### 金鑰認證 | ||
@@ -69,0 +75,0 @@ 當Onfleet物件成功被創建,表示您的應用程式介面金鑰是符合預期的。您可以嘗試使用verifyKey函式來測試您的金鑰是否合法,authentication這個endpoint會認證您的金鑰,回應為一布林值: |
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
66940
951
299