@autofleet/node-common 1.0.0 → 1.0.2
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 +37 -0
- package/index.js +1 -1
- package/network/index.js +20 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# AutoFleet Node Common
|
|
2
|
+
This respostory is made in order have as much of common code as we can.
|
|
3
|
+
|
|
4
|
+
Each line of code used in wrriten in this repo will be used in the enetire AutoFleet system.
|
|
5
|
+
|
|
6
|
+
Make sure you have:
|
|
7
|
+
* Tests
|
|
8
|
+
* Docs
|
|
9
|
+
|
|
10
|
+
## Network
|
|
11
|
+
Server 2 Servers communication.
|
|
12
|
+
|
|
13
|
+
Implement:
|
|
14
|
+
* Retriving service urls from environment
|
|
15
|
+
* Retry - TBD
|
|
16
|
+
* Caching - TBD
|
|
17
|
+
* Syntatic response for fail - TBD
|
|
18
|
+
* Circuit Breaking - TBD
|
|
19
|
+
|
|
20
|
+
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.
|
|
21
|
+
|
|
22
|
+
In case `serviceName` used the constractor will look for an environment varible with the the name `SERVICE_URL_<SERVICE_NAME>`.
|
|
23
|
+
|
|
24
|
+
For Example:
|
|
25
|
+
```
|
|
26
|
+
const { Network } = require('@autofleet/node-common');
|
|
27
|
+
|
|
28
|
+
n = new Network({ serviceName: 'TEST' });
|
|
29
|
+
|
|
30
|
+
n.get('/posts/1');
|
|
31
|
+
```
|
|
32
|
+
.env file:
|
|
33
|
+
```
|
|
34
|
+
SERVICE_URL_TEST=https://jsonplaceholder.typicode.com
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
To learn more [click here](https://blog.risingstack.com/designing-microservices-architecture-for-failure/).
|
package/index.js
CHANGED
package/network/index.js
CHANGED
|
@@ -10,29 +10,35 @@ const HTTPMethods = [
|
|
|
10
10
|
'patch',
|
|
11
11
|
'options'
|
|
12
12
|
]
|
|
13
|
+
const defaultSettings = {
|
|
14
|
+
timeout: 2500,
|
|
15
|
+
headers: { 'X-AF-AUTH': 'ANYONE' }
|
|
16
|
+
}
|
|
13
17
|
|
|
14
18
|
module.exports = class Network {
|
|
15
|
-
static defaultSettings = {
|
|
16
|
-
timeout: 2500,
|
|
17
|
-
headers: { 'X-AF-AUTH': 'ANYONE' }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
19
|
constructor(settings = {}) {
|
|
21
|
-
this.settings = Object.assign(
|
|
20
|
+
this.settings = Object.assign(defaultSettings, settings);
|
|
22
21
|
|
|
23
|
-
if(settings.serviceUrl) {
|
|
24
|
-
|
|
22
|
+
if (settings.serviceUrl) {
|
|
23
|
+
settings.baseURL = settings.serviceUrl;
|
|
25
24
|
} else if (settings.serviceName) {
|
|
26
|
-
|
|
25
|
+
settings.baseURL = process.env[`${settings.serviceName}_SERVICE_HOST`];
|
|
26
|
+
|
|
27
|
+
if (!settings.baseURL) {
|
|
28
|
+
throw new Error(`Missing enviermint varible: ${settings.serviceName}_SERVICE_HOST`);
|
|
29
|
+
}
|
|
27
30
|
}
|
|
28
|
-
|
|
29
|
-
if (!
|
|
31
|
+
|
|
32
|
+
if (!settings.baseURL) {
|
|
30
33
|
throw new Error('Missing serviceUrl or serviceName')
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
this.axios = axios.create(settings);
|
|
34
37
|
|
|
35
|
-
|
|
36
|
-
|
|
38
|
+
HTTPMethods.map((method) => this[method] = (...args) => this.axios[method](...args)
|
|
39
|
+
.then((response) => {
|
|
40
|
+
console.log('Request:', response.config.method.toUpperCase(), response.config.url)
|
|
41
|
+
console.log('Response:', response.data)
|
|
42
|
+
}));
|
|
37
43
|
}
|
|
38
|
-
}
|
|
44
|
+
}
|