@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 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
- Implement:
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
  ```
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ ok: 'OK',
3
+ error: 'ERROR',
4
+ fail: 'FAIL',
5
+ };
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.4",
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",
@@ -2,7 +2,7 @@
2
2
  const Settings = require('./index');
3
3
 
4
4
  const settings = new Settings({
5
- serviceUrl: 'http://localhost:8085/',
5
+ serviceUrl: 'http://setting-ms.autofleet.io/',
6
6
  });
7
7
 
8
8
  const fleetId = 'e7bbfcad-98a8-421d-962f-fd1c7790c789';
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/setting/get-setting/${key}`, {
34
+ const networkReponse = await this.network.get(`/api/v1/settings/get-setting/${key}`, {
35
35
  params: {
36
36
  labels,
37
37
  },
@@ -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/setting/get-setting/${key}`)
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({