ti2-tourplan 1.0.9 → 1.0.11

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.
Files changed (2) hide show
  1. package/index.js +38 -6
  2. 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 axios = require('axios');
6
+ const axiosRaw = require('axios');
8
7
 
9
8
  const Normalizer = require('./normalizer');
10
9
 
@@ -20,17 +19,47 @@ 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
+ const axiosSafeRequest = R.pick(['headers', 'method', 'url', 'data']);
30
+ const axiosSafeResponse = response => {
31
+ const retVal = R.pick(['data', 'status', 'statusText', 'headers', 'request'], response);
32
+ retVal.request = axiosSafeRequest(retVal.request);
33
+ return retVal;
34
+ };
35
+
29
36
  class Plugin {
30
37
  constructor(params = {}) { // we get the env variables from here
31
38
  Object.entries(params).forEach(([attr, value]) => {
32
39
  this[attr] = value;
33
40
  });
41
+ if (this.events) {
42
+ axiosRaw.interceptors.request.use(request => {
43
+ this.events.emit(`${this.name}.axios.request`, axiosSafeRequest(request));
44
+ return request;
45
+ });
46
+ axiosRaw.interceptors.response.use(response => {
47
+ this.events.emit(`${this.name}.axios.response`, axiosSafeResponse(response));
48
+ return response;
49
+ });
50
+ }
51
+ const pluginObj = this;
52
+ this.axios = async (...args) => axiosRaw(...args).catch(err => {
53
+ const errMsg = R.omit(['config'], err.toJSON());
54
+ console.log(`error in ${this.name}`, args[0], errMsg);
55
+ if (pluginObj.events) {
56
+ pluginObj.events.emit(`${this.name}.axios.error`, {
57
+ request: args[0],
58
+ err: errMsg,
59
+ });
60
+ }
61
+ throw err;
62
+ });
34
63
  this.tokenTemplate = () => ({
35
64
  endpoint: {
36
65
  type: 'text',
@@ -58,6 +87,7 @@ class Plugin {
58
87
  username,
59
88
  password,
60
89
  },
90
+ requestId,
61
91
  }) {
62
92
  try {
63
93
  const model = {
@@ -70,11 +100,12 @@ class Plugin {
70
100
  js2xmlparser.parse('Request', model, xmlOptions),
71
101
  );
72
102
  data = data.replace(xmlOptions.dtd.name, `Request SYSTEM "${xmlOptions.dtd.name}"`);
73
- const reply = R.path(['data'], await axios({
103
+ const reply = R.path(['data'], await this.axios({
74
104
  metod: 'post',
75
105
  url: endpoint,
76
106
  data,
77
107
  headers: getHeaders({ length: data.length }),
108
+ requestId,
78
109
  }));
79
110
  const replyObj = await xmlParser.parseStringPromise(reply);
80
111
  assert(R.path(['Reply', 'AuthenticationReply', 0], replyObj) === '');
@@ -97,6 +128,7 @@ class Plugin {
97
128
  keyPath,
98
129
  appliesTo: appliesToFilter,
99
130
  },
131
+ requestId,
100
132
  }) {
101
133
  const verbose = R.path(['verbose'], this);
102
134
  const cleanLog = inputString =>
@@ -129,11 +161,11 @@ class Plugin {
129
161
  );
130
162
  data = data.replace(xmlOptions.dtd.name, `Request SYSTEM "${xmlOptions.dtd.name}"`);
131
163
  if (verbose) console.log('request', cleanLog(data));
132
- const reply = R.path(['data'], await axios({
164
+ const reply = R.path(['data'], await this.axios({
133
165
  metod: 'post',
134
166
  url: endpoint,
135
167
  data,
136
- headers: getHeaders({ length: data.length }),
168
+ headers: getHeaders({ length: data.length, requestId }),
137
169
  }));
138
170
  if (verbose) console.log('reply', cleanLog(reply));
139
171
  const returnObj = await xmlParser.parseStringPromise(reply);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2-tourplan",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Tourplan's TI2 Plugin",
5
5
  "main": "index.js",
6
6
  "scripts": {