@autofleet/node-common 2.0.0 → 3.0.0
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 +0 -29
- package/package.json +21 -18
- package/router/index.js +17 -2
- package/settings/index.js +8 -5
- package/settings/index.test.js +1 -3
package/README.md
CHANGED
|
@@ -18,35 +18,6 @@ Currently we suppurt:
|
|
|
18
18
|
}
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
## Network
|
|
22
|
-
Server 2 Servers communication.
|
|
23
|
-
|
|
24
|
-
Implemented:
|
|
25
|
-
* Retriving service urls from environment
|
|
26
|
-
* Retry - Using https://github.com/softonic/axios-retry
|
|
27
|
-
* Caching - TBD
|
|
28
|
-
* Syntatic response for fail - TBD
|
|
29
|
-
* Circuit Breaking - TBD
|
|
30
|
-
|
|
31
|
-
The API is just like [axios](https://github.com/axios/axios) api but the creation of new instance **must** have either `serviceName` or `serviceUrl` in options.
|
|
32
|
-
|
|
33
|
-
In case `serviceName` used the constractor will look for an environment varible with the the name `<SERVICE_NAME>_SERVICE_HOST`.
|
|
34
|
-
|
|
35
|
-
For Example:
|
|
36
|
-
```
|
|
37
|
-
const { Network } = require('@autofleet/node-common');
|
|
38
|
-
|
|
39
|
-
n = new Network({ serviceName: 'TEST' });
|
|
40
|
-
|
|
41
|
-
n.get('/posts/1');
|
|
42
|
-
```
|
|
43
|
-
.env file:
|
|
44
|
-
```
|
|
45
|
-
RIDE_SERVICE_HOST=jsonplaceholder.typicode.com
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
To learn more [click here](https://blog.risingstack.com/designing-microservices-architecture-for-failure/).
|
|
49
|
-
|
|
50
21
|
## Settings
|
|
51
22
|
|
|
52
23
|
### Adding settings
|
package/package.json
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/node-common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"coverage": "jest --coverage --forceExit --runInBand",
|
|
7
7
|
"test": "jest --forceExit --runInBand",
|
|
8
8
|
"test-auto": "jest --watch --runInBand",
|
|
9
|
-
"linter": "
|
|
9
|
+
"linter": "eslint ."
|
|
10
10
|
},
|
|
11
11
|
"jest": {
|
|
12
|
-
"
|
|
13
|
-
|
|
12
|
+
"setupFilesAfterEnv": [
|
|
13
|
+
"jest-extended"
|
|
14
|
+
],
|
|
15
|
+
"testEnvironmentOptions": {
|
|
16
|
+
"url": "http://localhost:8085/"
|
|
17
|
+
}
|
|
14
18
|
},
|
|
15
19
|
"repository": {
|
|
16
20
|
"type": "git",
|
|
@@ -23,25 +27,24 @@
|
|
|
23
27
|
},
|
|
24
28
|
"homepage": "https://github.com/Autofleet/node-common",
|
|
25
29
|
"dependencies": {
|
|
26
|
-
"axios": "^0.
|
|
30
|
+
"axios": "^0.28.1",
|
|
27
31
|
"axios-retry": "^3.1.0",
|
|
28
32
|
"dotenv": "^5.0.1",
|
|
29
|
-
"express": "^4.
|
|
30
|
-
"
|
|
31
|
-
"mock-socket": "^7.1.0",
|
|
32
|
-
"node-cache": "^4.2.0",
|
|
33
|
-
"node-resque": "^5.4.1",
|
|
34
|
-
"portfinder": "^1.0.13",
|
|
35
|
-
"qs": "^6.5.2",
|
|
36
|
-
"timekeeper": "^2.1.2",
|
|
37
|
-
"winston": "^3.0.0",
|
|
38
|
-
"ws": "^5.2.1"
|
|
33
|
+
"express": "^4.19.2",
|
|
34
|
+
"node-cache": "^4.2.0"
|
|
39
35
|
},
|
|
40
36
|
"devDependencies": {
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"eslint
|
|
37
|
+
"@autofleet/logger": "^4.0.6",
|
|
38
|
+
"@autofleet/network": "^1.5.1",
|
|
39
|
+
"eslint": "^8.57.0",
|
|
40
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
41
|
+
"eslint-plugin-import": "^2.29.1",
|
|
42
|
+
"jest": "^29.7.0",
|
|
44
43
|
"jest-extended": "^0.7.1",
|
|
45
44
|
"nock": "^10.0.2"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@autofleet/logger": ">=4.0.6",
|
|
48
|
+
"@autofleet/network": ">=1.0.0"
|
|
46
49
|
}
|
|
47
50
|
}
|
package/router/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
const { Router } = require('express');
|
|
2
|
-
const
|
|
2
|
+
const { default: Logger } = require('@autofleet/logger');
|
|
3
|
+
|
|
4
|
+
const logger = Logger();
|
|
3
5
|
|
|
4
6
|
const METHODS = [
|
|
5
7
|
'all',
|
|
@@ -12,8 +14,16 @@ const METHODS = [
|
|
|
12
14
|
'head',
|
|
13
15
|
];
|
|
14
16
|
|
|
17
|
+
/**
|
|
18
|
+
* @param {(
|
|
19
|
+
* req: import('express').Request,
|
|
20
|
+
* res: import('express').Response,
|
|
21
|
+
* nextFn: import('express').NextFunction
|
|
22
|
+
* ) => void | PromiseLike<void>} func
|
|
23
|
+
* @returns {import('express').Handler}
|
|
24
|
+
*/
|
|
15
25
|
// eslint-disable-next-line consistent-return
|
|
16
|
-
const AfEntryPoint = func => async (req, res, next) => {
|
|
26
|
+
const AfEntryPoint = (func) => async (req, res, next) => {
|
|
17
27
|
try {
|
|
18
28
|
await func(req, res, next);
|
|
19
29
|
} catch (e) {
|
|
@@ -25,6 +35,11 @@ const AfEntryPoint = func => async (req, res, next) => {
|
|
|
25
35
|
}
|
|
26
36
|
};
|
|
27
37
|
|
|
38
|
+
/**
|
|
39
|
+
* @param {Parameters<typeof Router>[0]} [options]
|
|
40
|
+
* @returns {Router} a monkey-patched express router that
|
|
41
|
+
* will handle async routes (and force a 400 status for codes <500)
|
|
42
|
+
*/
|
|
28
43
|
const AfRouter = (options) => {
|
|
29
44
|
const myRouter = Router({ mergeParams: true, ...options });
|
|
30
45
|
METHODS.map((method) => {
|
package/settings/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const EventEmitter = require('events');
|
|
2
2
|
const NodeCache = require('node-cache');
|
|
3
|
-
const Network = require('
|
|
4
|
-
const Logger = require('
|
|
3
|
+
const Network = require('@autofleet/network');
|
|
4
|
+
const { default: Logger } = require('@autofleet/logger');
|
|
5
5
|
// const keysMap = require('./map');
|
|
6
6
|
|
|
7
7
|
const util = require('util');
|
|
@@ -14,9 +14,9 @@ require('dotenv').config();
|
|
|
14
14
|
const fiveMinutes = 60 * 5;
|
|
15
15
|
const waitingToNetwork = 'waitingToNetwork';
|
|
16
16
|
|
|
17
|
-
const findUrl = serviceUrl => serviceUrl
|
|
18
|
-
(process.env.SETTING_MS_SERVICE_HOST
|
|
19
|
-
(process.env.NODE_ENV !== 'test' ? 'http://setting-ms.autofleet.io/' : 'http://localhost:9999/');
|
|
17
|
+
const findUrl = (serviceUrl) => serviceUrl
|
|
18
|
+
|| (process.env.SETTING_MS_SERVICE_HOST?.length > 0 ? `http://${process.env.SETTING_MS_SERVICE_HOST}/` : undefined)
|
|
19
|
+
|| (process.env.NODE_ENV !== 'test' ? 'http://setting-ms.autofleet.io/' : 'http://localhost:9999/');
|
|
20
20
|
|
|
21
21
|
class SettingsManager {
|
|
22
22
|
static get NEVER_DEFAULT_VALUE() {
|
|
@@ -33,6 +33,9 @@ class SettingsManager {
|
|
|
33
33
|
return returnValue;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/**
|
|
37
|
+
* @param {{ serviceUrl: string; ttl?: number }} param0
|
|
38
|
+
*/
|
|
36
39
|
constructor({ serviceUrl, ttl = fiveMinutes } = {}) {
|
|
37
40
|
const localServiceUrl = findUrl(serviceUrl);
|
|
38
41
|
|
package/settings/index.test.js
CHANGED
|
@@ -72,7 +72,6 @@ describe('Settings', () => {
|
|
|
72
72
|
expect(value2).toEqual('value1');
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
-
|
|
76
75
|
it('Use only one request for similar keys', async () => {
|
|
77
76
|
const m = mockSetting('key1EM', 'value1EM');
|
|
78
77
|
const settings = new Settings({
|
|
@@ -119,7 +118,7 @@ describe('Settings', () => {
|
|
|
119
118
|
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
120
119
|
});
|
|
121
120
|
|
|
122
|
-
it('Throws an error if network error and no default value and success the next time',
|
|
121
|
+
it('Throws an error if network error and no default value and success the next time', (done) => {
|
|
123
122
|
const settings = new Settings({
|
|
124
123
|
serviceUrl: `http://${serviceUrl}/`,
|
|
125
124
|
});
|
|
@@ -128,7 +127,6 @@ describe('Settings', () => {
|
|
|
128
127
|
expect(f())
|
|
129
128
|
.rejects.toEqual(new Error('Cannot find value from network or cache, default value is set to never'));
|
|
130
129
|
|
|
131
|
-
|
|
132
130
|
setTimeout(async () => {
|
|
133
131
|
const m = mockSetting('key1', 'value1');
|
|
134
132
|
expect(await settings.get('key1', settings.NEVER_DEFAULT_VALUE)).toEqual('value1');
|