@autofleet/node-common 1.1.4 → 1.1.7
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.
- package/README.md +21 -1
- package/consts/index.js +5 -0
- package/network/index.js +1 -1
- package/package.json +6 -2
- package/settings/example.js +1 -1
- package/settings/index.js +2 -2
- package/settings/index.test.js +21 -1
package/README.md
CHANGED
|
@@ -7,10 +7,21 @@ Make sure you have:
|
|
|
7
7
|
* Tests
|
|
8
8
|
* Docs
|
|
9
9
|
|
|
10
|
+
## Consts
|
|
11
|
+
|
|
12
|
+
Currently we suppurt:
|
|
13
|
+
```
|
|
14
|
+
{
|
|
15
|
+
'OK'
|
|
16
|
+
'ERROR'
|
|
17
|
+
'FAIL'
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
10
21
|
## Network
|
|
11
22
|
Server 2 Servers communication.
|
|
12
23
|
|
|
13
|
-
|
|
24
|
+
Implemented:
|
|
14
25
|
* Retriving service urls from environment
|
|
15
26
|
* Retry - Using https://github.com/softonic/axios-retry
|
|
16
27
|
* Caching - TBD
|
|
@@ -36,7 +47,16 @@ RIDE_SERVICE_HOST=jsonplaceholder.typicode.com
|
|
|
36
47
|
|
|
37
48
|
To learn more [click here](https://blog.risingstack.com/designing-microservices-architecture-for-failure/).
|
|
38
49
|
|
|
50
|
+
## Settings
|
|
51
|
+
|
|
52
|
+
See example.
|
|
53
|
+
|
|
54
|
+
## DeLorean
|
|
55
|
+
|
|
56
|
+
Use this model to mock time on server - more info TBD.
|
|
57
|
+
|
|
39
58
|
## Publish package
|
|
59
|
+
|
|
40
60
|
bump the version number in package.json
|
|
41
61
|
and run
|
|
42
62
|
```
|
package/consts/index.js
ADDED
package/network/index.js
CHANGED
|
@@ -25,7 +25,7 @@ const HTTPMethods = [
|
|
|
25
25
|
const defaultSettings = {
|
|
26
26
|
timeout: 2500,
|
|
27
27
|
headers: { 'X-AF-AUTH': 'ANYONE' },
|
|
28
|
-
paramsSerializer: params => qs.stringify(params),
|
|
28
|
+
paramsSerializer: params => qs.stringify(params, { arrayFormat: 'brackets' }),
|
|
29
29
|
};
|
|
30
30
|
|
|
31
31
|
module.exports = class Network {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/node-common",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.7",
|
|
4
4
|
"description": "",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"coverage": "jest --coverage --forceExit --runInBand",
|
|
@@ -26,9 +26,13 @@
|
|
|
26
26
|
"axios-retry": "^3.1.0",
|
|
27
27
|
"dotenv": "^5.0.1",
|
|
28
28
|
"jest": "^22.4.3",
|
|
29
|
+
"mock-socket": "^7.1.0",
|
|
29
30
|
"node-cache": "^4.2.0",
|
|
31
|
+
"portfinder": "^1.0.13",
|
|
30
32
|
"qs": "^6.5.2",
|
|
31
|
-
"winston": "^3.0.0-rc4"
|
|
33
|
+
"winston": "^3.0.0-rc4",
|
|
34
|
+
"ws": "^5.2.0",
|
|
35
|
+
"timekeeper": "^2.1.2"
|
|
32
36
|
},
|
|
33
37
|
"devDependencies": {
|
|
34
38
|
"eslint": "^4.19.1",
|
package/settings/example.js
CHANGED
package/settings/index.js
CHANGED
|
@@ -8,7 +8,7 @@ require('dotenv').config();
|
|
|
8
8
|
const fiveMinutes = 60 * 5;
|
|
9
9
|
|
|
10
10
|
module.exports = class SettingsManager {
|
|
11
|
-
constructor({ serviceUrl, ttl = fiveMinutes }) {
|
|
11
|
+
constructor({ serviceUrl, ttl = fiveMinutes } = {}) {
|
|
12
12
|
const localServiceUrl = serviceUrl || process.env.SETTING_MS_SERVICE_HOST;
|
|
13
13
|
|
|
14
14
|
this.ttl = ttl;
|
|
@@ -31,7 +31,7 @@ module.exports = class SettingsManager {
|
|
|
31
31
|
|
|
32
32
|
let networkValue;
|
|
33
33
|
try {
|
|
34
|
-
const networkReponse = await this.network.get(`/api/v1/
|
|
34
|
+
const networkReponse = await this.network.get(`/api/v1/settings/get-setting/${key}`, {
|
|
35
35
|
params: {
|
|
36
36
|
labels,
|
|
37
37
|
},
|
package/settings/index.test.js
CHANGED
|
@@ -4,11 +4,31 @@ const Settings = require('./index');
|
|
|
4
4
|
const serviceUrl = 'http://localhost:8085';
|
|
5
5
|
|
|
6
6
|
const mockSetting = (key, value, labels, response = 200) => nock(serviceUrl)
|
|
7
|
-
.get(`/api/v1/
|
|
7
|
+
.get(`/api/v1/settings/get-setting/${key}`)
|
|
8
8
|
.query(labels ? { labels } : undefined)
|
|
9
9
|
.reply(response, { value });
|
|
10
10
|
|
|
11
11
|
describe('Settings', () => {
|
|
12
|
+
it('Throws exeption if there is no defualt value', async () => {
|
|
13
|
+
const settings = new Settings({
|
|
14
|
+
serviceUrl,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
expect(settings.get('key1'))
|
|
18
|
+
.rejects.toEqual(new Error('Can\'t get a key without defaultValue'));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('Uses env.SETTING_MS_SERVICE_HOST as serivce host if no one defind', async () => {
|
|
22
|
+
const m = mockSetting('key1', 'value1');
|
|
23
|
+
process.env.SETTING_MS_SERVICE_HOST = serviceUrl;
|
|
24
|
+
const settings = new Settings();
|
|
25
|
+
|
|
26
|
+
const value = await settings.get('key1', 'dv');
|
|
27
|
+
expect(value).toEqual('value1');
|
|
28
|
+
expect(m.isDone()).toBeTruthy();
|
|
29
|
+
process.env.SETTING_MS_SERVICE_HOST = '';
|
|
30
|
+
});
|
|
31
|
+
|
|
12
32
|
it('Get a key from server', async () => {
|
|
13
33
|
const m = mockSetting('key1', 'value1');
|
|
14
34
|
const settings = new Settings({
|