ti2-tourplan 1.0.9 → 1.0.10
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 +27 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
// const axios = require('axios');
|
|
2
1
|
const R = require('ramda');
|
|
3
2
|
const assert = require('assert');
|
|
4
3
|
const moment = require('moment');
|
|
5
4
|
const js2xmlparser = require('js2xmlparser');
|
|
6
5
|
const xml2js = require('xml2js');
|
|
7
|
-
const
|
|
6
|
+
const axiosRaw = require('axios');
|
|
8
7
|
|
|
9
8
|
const Normalizer = require('./normalizer');
|
|
10
9
|
|
|
@@ -20,10 +19,11 @@ const xmlOptions = {
|
|
|
20
19
|
},
|
|
21
20
|
};
|
|
22
21
|
|
|
23
|
-
const getHeaders = ({ length }) => ({
|
|
22
|
+
const getHeaders = ({ length, requestId }) => ({
|
|
24
23
|
Accept: 'application/xml',
|
|
25
24
|
'Content-Type': 'application/xml; charset=utf-8',
|
|
26
25
|
'Content-Length': length,
|
|
26
|
+
...requestId ? { requestId } : {},
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
class Plugin {
|
|
@@ -31,6 +31,24 @@ class Plugin {
|
|
|
31
31
|
Object.entries(params).forEach(([attr, value]) => {
|
|
32
32
|
this[attr] = value;
|
|
33
33
|
});
|
|
34
|
+
if (this.events) {
|
|
35
|
+
axiosRaw.interceptors.request.use(request => {
|
|
36
|
+
this.events.emit(`${this.name}.axios.request`, JSON.parse(JSON.stringify(request)));
|
|
37
|
+
return request;
|
|
38
|
+
});
|
|
39
|
+
axiosRaw.interceptors.response.use(response => {
|
|
40
|
+
this.events.emit(`${this.name}.axios.response`, JSON.parse(JSON.stringify(response)));
|
|
41
|
+
return response;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
const pluginObj = this;
|
|
45
|
+
this.axios = async (...args) => axiosRaw(...args).catch(err => {
|
|
46
|
+
console.log('error in ti2-tourplan', err);
|
|
47
|
+
if (pluginObj.events) {
|
|
48
|
+
pluginObj.events.emit(`${this.name}.axios.error`, { request: args[0], err });
|
|
49
|
+
}
|
|
50
|
+
throw err;
|
|
51
|
+
});
|
|
34
52
|
this.tokenTemplate = () => ({
|
|
35
53
|
endpoint: {
|
|
36
54
|
type: 'text',
|
|
@@ -58,6 +76,7 @@ class Plugin {
|
|
|
58
76
|
username,
|
|
59
77
|
password,
|
|
60
78
|
},
|
|
79
|
+
requestId,
|
|
61
80
|
}) {
|
|
62
81
|
try {
|
|
63
82
|
const model = {
|
|
@@ -70,11 +89,12 @@ class Plugin {
|
|
|
70
89
|
js2xmlparser.parse('Request', model, xmlOptions),
|
|
71
90
|
);
|
|
72
91
|
data = data.replace(xmlOptions.dtd.name, `Request SYSTEM "${xmlOptions.dtd.name}"`);
|
|
73
|
-
const reply = R.path(['data'], await axios({
|
|
92
|
+
const reply = R.path(['data'], await this.axios({
|
|
74
93
|
metod: 'post',
|
|
75
94
|
url: endpoint,
|
|
76
95
|
data,
|
|
77
96
|
headers: getHeaders({ length: data.length }),
|
|
97
|
+
requestId,
|
|
78
98
|
}));
|
|
79
99
|
const replyObj = await xmlParser.parseStringPromise(reply);
|
|
80
100
|
assert(R.path(['Reply', 'AuthenticationReply', 0], replyObj) === '');
|
|
@@ -97,6 +117,7 @@ class Plugin {
|
|
|
97
117
|
keyPath,
|
|
98
118
|
appliesTo: appliesToFilter,
|
|
99
119
|
},
|
|
120
|
+
requestId,
|
|
100
121
|
}) {
|
|
101
122
|
const verbose = R.path(['verbose'], this);
|
|
102
123
|
const cleanLog = inputString =>
|
|
@@ -129,11 +150,11 @@ class Plugin {
|
|
|
129
150
|
);
|
|
130
151
|
data = data.replace(xmlOptions.dtd.name, `Request SYSTEM "${xmlOptions.dtd.name}"`);
|
|
131
152
|
if (verbose) console.log('request', cleanLog(data));
|
|
132
|
-
const reply = R.path(['data'], await axios({
|
|
153
|
+
const reply = R.path(['data'], await this.axios({
|
|
133
154
|
metod: 'post',
|
|
134
155
|
url: endpoint,
|
|
135
156
|
data,
|
|
136
|
-
headers: getHeaders({ length: data.length }),
|
|
157
|
+
headers: getHeaders({ length: data.length, requestId }),
|
|
137
158
|
}));
|
|
138
159
|
if (verbose) console.log('reply', cleanLog(reply));
|
|
139
160
|
const returnObj = await xmlParser.parseStringPromise(reply);
|