@autofleet/node-common 1.0.1 → 1.0.3
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/network/index.js +60 -0
- package/package.json +1 -1
- package/Network/index.js +0 -44
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/network/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
require('dotenv').config();
|
|
3
|
+
/**
|
|
4
|
+
* Add support for nock testing
|
|
5
|
+
* see s://github.com/axios/axios/issues/305
|
|
6
|
+
*/
|
|
7
|
+
if (process.env.NODE_ENV === 'test') {
|
|
8
|
+
axios.defaults.adapter = require('axios/lib/adapters/http')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const Methods = [
|
|
12
|
+
'get',
|
|
13
|
+
'post',
|
|
14
|
+
'delete',
|
|
15
|
+
'head',
|
|
16
|
+
'put',
|
|
17
|
+
'patch',
|
|
18
|
+
'options'
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
const defaultSettings = {
|
|
22
|
+
timeout: 2500,
|
|
23
|
+
headers: { 'X-AF-AUTH': 'ANYONE' }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
module.exports = class Network {
|
|
27
|
+
constructor(settings = {}) {
|
|
28
|
+
this.settings = Object.assign(defaultSettings, settings)
|
|
29
|
+
this.validate()
|
|
30
|
+
this.createBaseUrl()
|
|
31
|
+
this.buildClassHttpMethods()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
validate() {
|
|
35
|
+
if (!this.settings.serviceUrl && ! this.settings.serviceName) {
|
|
36
|
+
throw new Error('At least one of the settings Missing serviceUrl or serviceName')
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
createBaseUrl() {
|
|
41
|
+
let settings = this.settings
|
|
42
|
+
if (settings.serviceUrl) {
|
|
43
|
+
settings.baseURL = settings.serviceUrl;
|
|
44
|
+
} else if (settings.serviceName) {
|
|
45
|
+
const envServiceHostName = `${settings.serviceName}_SERVICE_HOST`
|
|
46
|
+
settings.baseURL = 'http://' + process.env[envServiceHostName]
|
|
47
|
+
if(!settings.baseURL) {
|
|
48
|
+
throw new Error(`Missing environment variable: ${envServiceHostName}`)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Build class methods that wrap axios methods
|
|
55
|
+
*/
|
|
56
|
+
buildClassHttpMethods() {
|
|
57
|
+
this.axios = axios.create(this.settings);
|
|
58
|
+
Methods.map((method) => this[method] = (...args) => this.axios[method](...args))
|
|
59
|
+
}
|
|
60
|
+
}
|
package/package.json
CHANGED
package/Network/index.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
const axios = require('axios');
|
|
2
|
-
require('dotenv').config();
|
|
3
|
-
|
|
4
|
-
const HTTPMethods = [
|
|
5
|
-
'get',
|
|
6
|
-
'post',
|
|
7
|
-
'delete',
|
|
8
|
-
'head',
|
|
9
|
-
'put',
|
|
10
|
-
'patch',
|
|
11
|
-
'options'
|
|
12
|
-
]
|
|
13
|
-
const defaultSettings = {
|
|
14
|
-
timeout: 2500,
|
|
15
|
-
headers: { 'X-AF-AUTH': 'ANYONE' }
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
module.exports = class Network {
|
|
19
|
-
constructor(settings = {}) {
|
|
20
|
-
this.settings = Object.assign(defaultSettings, settings);
|
|
21
|
-
|
|
22
|
-
if (settings.serviceUrl) {
|
|
23
|
-
settings.baseURL = settings.serviceUrl;
|
|
24
|
-
} else if (settings.serviceName) {
|
|
25
|
-
settings.baseURL = process.env[`SERVICE_URL_${settings.serviceName}`];
|
|
26
|
-
|
|
27
|
-
if (!settings.baseURL) {
|
|
28
|
-
throw new Error(`Missing enviermint varible: SERVICE_URL_${settings.serviceName}`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (!settings.baseURL) {
|
|
33
|
-
throw new Error('Missing serviceUrl or serviceName')
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
this.axios = axios.create(settings);
|
|
37
|
-
|
|
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
|
-
}));
|
|
43
|
-
}
|
|
44
|
-
}
|