@autofleet/node-common 1.0.2 → 1.0.4
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/index.js +2 -2
- package/network/index.js +34 -18
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
Network: require('./
|
|
3
|
-
}
|
|
2
|
+
Network: require('./network'),
|
|
3
|
+
}
|
package/network/index.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
const axios = require('axios');
|
|
2
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
|
+
}
|
|
3
10
|
|
|
4
|
-
const
|
|
11
|
+
const Methods = [
|
|
5
12
|
'get',
|
|
6
13
|
'post',
|
|
7
14
|
'delete',
|
|
@@ -10,6 +17,7 @@ const HTTPMethods = [
|
|
|
10
17
|
'patch',
|
|
11
18
|
'options'
|
|
12
19
|
]
|
|
20
|
+
|
|
13
21
|
const defaultSettings = {
|
|
14
22
|
timeout: 2500,
|
|
15
23
|
headers: { 'X-AF-AUTH': 'ANYONE' }
|
|
@@ -17,28 +25,36 @@ const defaultSettings = {
|
|
|
17
25
|
|
|
18
26
|
module.exports = class Network {
|
|
19
27
|
constructor(settings = {}) {
|
|
20
|
-
this.settings = Object.assign(defaultSettings, 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
|
+
}
|
|
21
39
|
|
|
40
|
+
createBaseUrl() {
|
|
41
|
+
let settings = this.settings
|
|
22
42
|
if (settings.serviceUrl) {
|
|
23
43
|
settings.baseURL = settings.serviceUrl;
|
|
24
44
|
} else if (settings.serviceName) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if
|
|
28
|
-
throw new Error(`Missing
|
|
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}`)
|
|
29
49
|
}
|
|
30
50
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
.then((response) => {
|
|
40
|
-
console.log('Request:', response.config.method.toUpperCase(), response.config.url)
|
|
41
|
-
console.log('Response:', response.data)
|
|
42
|
-
}));
|
|
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))
|
|
43
59
|
}
|
|
44
60
|
}
|