Skip to content

Commit

Permalink
feat(initial_constants): customize bottleneck options while new onfle…
Browse files Browse the repository at this point in the history
…et (#41)

Co-authored-by: Zach <[email protected]>
  • Loading branch information
zachcr-ws and Zach authored Jun 4, 2021
1 parent 88e3e3a commit bbd99b2
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 87 deletions.
10 changes: 10 additions & 0 deletions README.fr.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ En tant que champ facultatif, vous pouvez introduire un délai d'expiration pers
```js
const onfleet = new Onfleet('<clé_api>', 30000) // Cela mettra vos wrappers à expiration à 30000ms au lieu de 70000ms
```
En tant que champ facultatif, vous pouvez introduire un objet d'options pour [Bottleneck](https://www.npmjs.com/package/bottleneck).

```js
const onfleet = new Onfleet("<api_key>", 30000, {
LIMITER_RESERVOIR: 10, // default 20
LIMITER_WAIT_UPON_DEPLETION: 20000, // default 10000
LIMITER_MAX_CONCURRENT: 5, // default 1
LIMITER_MIN_TIME: 50, // default 50
});
```

### Authentification
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:
Expand Down
230 changes: 146 additions & 84 deletions README.md

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions README.zh-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ const onfleet = new Onfleet('<api_key>');
const onfleet = new Onfleet('<api_key>', 30000) // 在此設定執行逾時參數為30000ms
```

作為可選字段,您可以引入一個用於 Bottleneck 的選項對象 [Bottleneck](https://www.npmjs.com/package/bottleneck).
```js
const onfleet = new Onfleet('<api_key>', 30000, {
LIMITER_RESERVOIR: 10, // default 20
LIMITER_WAIT_UPON_DEPLETION: 20000, // default 10000
LIMITER_MAX_CONCURRENT: 5, // default 1
LIMITER_MIN_TIME: 50, // default 50
});
```

### 金鑰認證
當Onfleet物件成功被創建,表示您的應用程式介面金鑰是符合預期的。您可以嘗試使用verifyKey函式來測試您的金鑰是否合法,authentication這個endpoint會認證您的金鑰,回應為一布林值:
```js
Expand Down
2 changes: 1 addition & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ const constants = {
LIMITER_MIN_TIME: 50,
};

module.exports = Object.freeze(constants);
module.exports = constants;
36 changes: 35 additions & 1 deletion lib/onfleet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const DEFAULT_PATH = '/api';
const DEFAULT_API_VERSION = 'v2';
const DEFAULT_TIMEOUT = 70000;

const constants = require('./constants');
const util = require('./util');
const { ValidationError } = require('./error');
const { name, version } = require('../package.json');
Expand All @@ -26,7 +27,7 @@ resources.Webhooks = require('./resources/Webhooks');
*/

class Onfleet {
constructor(apiKey, userTimeout) {
constructor(apiKey, userTimeout, bottleneckOptions) {
if (!apiKey) {
throw new ValidationError('Onfleet API key not found, please obtain an API key from your organization admin');
} if (userTimeout > 70000) {
Expand All @@ -43,11 +44,44 @@ class Onfleet {
Authorization: `Basic ${util.encode(apiKey)}`,
},
};
if (bottleneckOptions) {
this.initBottleneckOptions(bottleneckOptions)
}

this.resources = resources;
this.initResources();
}
}

initBottleneckOptions(bottleneckOptions) {
const {
LIMITER_RESERVOIR,
LIMITER_WAIT_UPON_DEPLETION,
LIMITER_MAX_CONCURRENT,
LIMITER_MIN_TIME,
} = bottleneckOptions;
if (Number.isInteger(LIMITER_RESERVOIR) &&
LIMITER_RESERVOIR > 0 &&
LIMITER_RESERVOIR !== constants.LIMITER_RESERVOIR) {
constants.LIMITER_RESERVOIR = LIMITER_RESERVOIR;
}
if (Number.isInteger(LIMITER_WAIT_UPON_DEPLETION) &&
LIMITER_WAIT_UPON_DEPLETION > 0 &&
LIMITER_WAIT_UPON_DEPLETION !== constants.LIMITER_WAIT_UPON_DEPLETION) {
constants.LIMITER_WAIT_UPON_DEPLETION = LIMITER_WAIT_UPON_DEPLETION;
}
if (Number.isInteger(LIMITER_MAX_CONCURRENT) &&
LIMITER_MAX_CONCURRENT !== constants.LIMITER_MAX_CONCURRENT &&
LIMITER_MAX_CONCURRENT > 0) {
constants.LIMITER_MAX_CONCURRENT = LIMITER_MAX_CONCURRENT;
}
if (Number.isInteger(LIMITER_MIN_TIME) &&
LIMITER_MIN_TIME > 0 &&
LIMITER_MIN_TIME !== constants.LIMITER_MIN_TIME) {
constants.LIMITER_MIN_TIME = LIMITER_MIN_TIME;
}
}

initResources() {
for (let name in resources) { // eslint-disable-line
const endpoint = name.toLowerCase();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onfleet/node-onfleet",
"version": "1.2.3",
"version": "1.2.4",
"description": "Official client library for accessing the Onfleet API",
"main": "index.js",
"scripts": {
Expand Down
25 changes: 25 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ describe('Utility function testing - Auth test returns 200 ok', () => {
});
});

describe('Initial testing', () => {
it('without bottleneck options', () => {
const onfleetWithoutOptions = new Onfleet(apiKey);
const constans = require('../lib/constants');
assert.equal(constans.LIMITER_MAX_CONCURRENT, 1);
assert.equal(constans.LIMITER_MIN_TIME, 50);
assert.equal(constans.LIMITER_WAIT_UPON_DEPLETION, 10000);
assert.equal(constans.LIMITER_RESERVOIR, 20);
});

it('with bottleneck options', () => {
const onfleetWithOptions = new Onfleet(apiKey, undefined, {
LIMITER_RESERVOIR: 10,
LIMITER_WAIT_UPON_DEPLETION: 20000,
LIMITER_MAX_CONCURRENT: 5,
LIMITER_MIN_TIME: 10,
});
const constans = require('../lib/constants');
assert.equal(constans.LIMITER_MAX_CONCURRENT, 5);
assert.equal(constans.LIMITER_MIN_TIME, 10);
assert.equal(constans.LIMITER_WAIT_UPON_DEPLETION, 20000);
assert.equal(constans.LIMITER_RESERVOIR, 10);
});
});

describe('HTTP Request testing', () => {
const onfleet = new Onfleet(apiKey);
beforeEach(() => {
Expand Down

0 comments on commit bbd99b2

Please sign in to comment.