@autofleet/node-common 1.0.0 → 1.0.1
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/Network/index.js +44 -0
- package/index.js +1 -1
- package/package.json +1 -1
- package/network/index.js +0 -38
package/Network/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
}
|
package/index.js
CHANGED
package/package.json
CHANGED
package/network/index.js
DELETED
|
@@ -1,38 +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
|
-
|
|
14
|
-
module.exports = class Network {
|
|
15
|
-
static defaultSettings = {
|
|
16
|
-
timeout: 2500,
|
|
17
|
-
headers: { 'X-AF-AUTH': 'ANYONE' }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
constructor(settings = {}) {
|
|
21
|
-
this.settings = Object.assign(Network.defaultSettings, settings);
|
|
22
|
-
|
|
23
|
-
if(settings.serviceUrl) {
|
|
24
|
-
this.baseURL = settings.serviceUrl;
|
|
25
|
-
} else if (settings.serviceName) {
|
|
26
|
-
this.baseURL = process.env[`SERVICE_URL_${settings.serviceUrl}`];
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (!this.baseURL){
|
|
30
|
-
throw new Error('Missing serviceUrl or serviceName')
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
this.axios = axios.create(settings);
|
|
34
|
-
|
|
35
|
-
// Temp
|
|
36
|
-
HTTPMethods.map((method) => this[method] = (...args) => this.axios[method](...args));
|
|
37
|
-
}
|
|
38
|
-
}
|