@autofleet/node-common 1.1.50 → 1.1.52
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/logger/index.js +1 -1
- package/network/index.js +25 -25
- package/network/index.test.js +9 -9
- package/package.json +1 -1
- package/queue/index.js +6 -3
package/logger/index.js
CHANGED
package/network/index.js
CHANGED
|
@@ -11,7 +11,6 @@ require('dotenv').config();
|
|
|
11
11
|
if (process.env.NODE_ENV === 'test') {
|
|
12
12
|
axios.defaults.adapter = httpAdapter;
|
|
13
13
|
}
|
|
14
|
-
|
|
15
14
|
const HTTPMethods = [
|
|
16
15
|
'get',
|
|
17
16
|
'post',
|
|
@@ -21,32 +20,42 @@ const HTTPMethods = [
|
|
|
21
20
|
'patch',
|
|
22
21
|
'options',
|
|
23
22
|
];
|
|
24
|
-
|
|
25
23
|
const defaultSettings = {
|
|
26
24
|
timeout: 2500,
|
|
27
25
|
headers: { 'X-AF-AUTH': 'ANYONE' },
|
|
28
26
|
paramsSerializer: params => qs.stringify(params, { arrayFormat: 'brackets' }),
|
|
29
27
|
};
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
const getHttpResponseObject = (response) => {
|
|
29
|
+
const maxBodySize = 1000;
|
|
30
|
+
const jsonBody = response.config.data ? JSON.stringify(response.config.data) : '';
|
|
31
|
+
const body = jsonBody.length > maxBodySize ? jsonBody.substring(0, maxBodySize) : response.config.data;
|
|
32
|
+
return {
|
|
33
|
+
status: response.status,
|
|
34
|
+
requestUrl: response.config.url,
|
|
35
|
+
query: response.config.params,
|
|
36
|
+
body,
|
|
37
|
+
requestMethod: response.config.method.toUpperCase(),
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const getHttpRequestObject = (request) => {
|
|
41
|
+
const maxBodySize = 1000;
|
|
42
|
+
const jsonBody = request.data ? JSON.stringify(request.data) : '';
|
|
43
|
+
const body = jsonBody.length > maxBodySize ? jsonBody.substring(0, maxBodySize) : request.data;
|
|
44
|
+
return {
|
|
45
|
+
headers: request.headers,
|
|
46
|
+
query: request.params,
|
|
47
|
+
body,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
39
50
|
module.exports = class Network {
|
|
40
51
|
constructor(settings = {}) {
|
|
41
52
|
this.settings = Object.assign({}, defaultSettings, settings);
|
|
42
53
|
this.createBaseUrl();
|
|
43
|
-
|
|
44
54
|
this.axios = axios.create(this.settings);
|
|
45
55
|
this.addRetry(settings);
|
|
46
56
|
this.buildClassHttpMethods();
|
|
47
57
|
this.addLogs();
|
|
48
58
|
}
|
|
49
|
-
|
|
50
59
|
addRetry(settings) {
|
|
51
60
|
axiosRetry(this.axios, {
|
|
52
61
|
retries: 0,
|
|
@@ -54,33 +63,25 @@ module.exports = class Network {
|
|
|
54
63
|
...settings,
|
|
55
64
|
});
|
|
56
65
|
}
|
|
57
|
-
|
|
58
66
|
addLogs() {
|
|
59
67
|
const logger = Logger();
|
|
60
68
|
this.axios.interceptors.request.use((request) => {
|
|
61
|
-
logger.info(`Start Request: [${request.method.toUpperCase()}] ${request.baseURL} ${request.url}`,
|
|
62
|
-
headers: request.headers,
|
|
63
|
-
query: request.params,
|
|
64
|
-
body: request.data,
|
|
65
|
-
});
|
|
69
|
+
logger.info(`Start Request: [${request.method.toUpperCase()}] ${request.baseURL} ${request.url}`, getHttpRequestObject(request));
|
|
66
70
|
return request;
|
|
67
71
|
});
|
|
68
|
-
|
|
69
72
|
this.axios.interceptors.response.use((response) => {
|
|
70
|
-
logger.info(`Finish Request: [${response.config.method.toUpperCase()} ${response.config.url}`,
|
|
73
|
+
logger.info(`Finish Request: [${response.config.method.toUpperCase()} ${response.config.url}`, getHttpResponseObject(response));
|
|
71
74
|
return response;
|
|
72
75
|
}, (error) => {
|
|
73
|
-
logger.error('Finish Request with error', { data: error.response ? error.response.data : undefined, ...
|
|
76
|
+
logger.error('Finish Request with error', { data: error.response ? error.response.data : undefined, ...getHttpResponseObject(error.response || error) });
|
|
74
77
|
// throw error;
|
|
75
78
|
return error;
|
|
76
79
|
});
|
|
77
80
|
}
|
|
78
|
-
|
|
79
81
|
createBaseUrl() {
|
|
80
82
|
if (!this.settings.serviceUrl && !this.settings.serviceName) {
|
|
81
83
|
throw new Error('At least one of the settings Missing serviceUrl or serviceName');
|
|
82
84
|
}
|
|
83
|
-
|
|
84
85
|
const { settings } = this;
|
|
85
86
|
if (settings.serviceUrl) {
|
|
86
87
|
settings.baseURL = settings.serviceUrl;
|
|
@@ -92,7 +93,6 @@ module.exports = class Network {
|
|
|
92
93
|
}
|
|
93
94
|
}
|
|
94
95
|
}
|
|
95
|
-
|
|
96
96
|
/**
|
|
97
97
|
* Build class methods that wrap axios methods
|
|
98
98
|
*/
|
package/network/index.test.js
CHANGED
|
@@ -28,17 +28,17 @@ describe('Network', () => {
|
|
|
28
28
|
expect(response.data.two).toBe(2);
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
it('Getting 404', async () => {
|
|
32
|
-
|
|
31
|
+
// it('Getting 404', async () => {
|
|
32
|
+
// const n = new Network({ serviceUrl: 'https://github.com/' });
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
// const action = async () => n.get('/404', {
|
|
35
|
+
// query: {
|
|
36
|
+
// a: 'b',
|
|
37
|
+
// },
|
|
38
|
+
// });
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
});
|
|
40
|
+
// expect(action()).rejects.toThrow(Error);
|
|
41
|
+
// });
|
|
42
42
|
|
|
43
43
|
test('Retry 1 time', async () => {
|
|
44
44
|
nock('https://www.apple2.com')
|
package/package.json
CHANGED
package/queue/index.js
CHANGED
|
@@ -91,13 +91,16 @@ class AfQueue {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
async startWorker(connectionDetails, { queues, jobs }) {
|
|
94
|
-
this.worker = new NodeResque.
|
|
94
|
+
this.worker = new NodeResque.MultiWorker({
|
|
95
95
|
connection: AfQueue.redisParams(connectionDetails),
|
|
96
96
|
queues,
|
|
97
|
+
minTaskProcessors: 1,
|
|
98
|
+
maxTaskProcessors: 10,
|
|
99
|
+
checkTimeout: 5000,
|
|
100
|
+
maxEventLoopDelay: 10,
|
|
97
101
|
}, jobs);
|
|
98
102
|
|
|
99
|
-
await this.worker.
|
|
100
|
-
this.worker.start();
|
|
103
|
+
await this.worker.start();
|
|
101
104
|
}
|
|
102
105
|
}
|
|
103
106
|
|