ti2-tourplan 1.0.8 → 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.
Files changed (3) hide show
  1. package/index.js +46 -6
  2. package/index.test.js +25 -0
  3. 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,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,43 @@ 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
+ });
52
+ this.tokenTemplate = () => ({
53
+ endpoint: {
54
+ type: 'text',
55
+ regExp: /^(?!mailto:)(?:(?:http|https|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))|localhost)(?::\d{2,5})?(?:(\/|\?|#)[^\s]*)?$/i,
56
+ description: 'The uri for the tourplan adapter',
57
+ },
58
+ username: {
59
+ type: 'text',
60
+ regExp: /.+/,
61
+ description: 'The tourplan provided username',
62
+ default: 'en',
63
+ },
64
+ password: {
65
+ type: 'text',
66
+ regExp: /.+/,
67
+ description: 'The tourplan provided password',
68
+ default: 'en',
69
+ },
70
+ });
34
71
  }
35
72
 
36
73
  async validateToken({
@@ -39,6 +76,7 @@ class Plugin {
39
76
  username,
40
77
  password,
41
78
  },
79
+ requestId,
42
80
  }) {
43
81
  try {
44
82
  const model = {
@@ -51,11 +89,12 @@ class Plugin {
51
89
  js2xmlparser.parse('Request', model, xmlOptions),
52
90
  );
53
91
  data = data.replace(xmlOptions.dtd.name, `Request SYSTEM "${xmlOptions.dtd.name}"`);
54
- const reply = R.path(['data'], await axios({
92
+ const reply = R.path(['data'], await this.axios({
55
93
  metod: 'post',
56
94
  url: endpoint,
57
95
  data,
58
96
  headers: getHeaders({ length: data.length }),
97
+ requestId,
59
98
  }));
60
99
  const replyObj = await xmlParser.parseStringPromise(reply);
61
100
  assert(R.path(['Reply', 'AuthenticationReply', 0], replyObj) === '');
@@ -78,6 +117,7 @@ class Plugin {
78
117
  keyPath,
79
118
  appliesTo: appliesToFilter,
80
119
  },
120
+ requestId,
81
121
  }) {
82
122
  const verbose = R.path(['verbose'], this);
83
123
  const cleanLog = inputString =>
@@ -110,11 +150,11 @@ class Plugin {
110
150
  );
111
151
  data = data.replace(xmlOptions.dtd.name, `Request SYSTEM "${xmlOptions.dtd.name}"`);
112
152
  if (verbose) console.log('request', cleanLog(data));
113
- const reply = R.path(['data'], await axios({
153
+ const reply = R.path(['data'], await this.axios({
114
154
  metod: 'post',
115
155
  url: endpoint,
116
156
  data,
117
- headers: getHeaders({ length: data.length }),
157
+ headers: getHeaders({ length: data.length, requestId }),
118
158
  }));
119
159
  if (verbose) console.log('reply', cleanLog(reply));
120
160
  const returnObj = await xmlParser.parseStringPromise(reply);
package/index.test.js CHANGED
@@ -56,6 +56,31 @@ describe('search tests', () => {
56
56
  expect(retVal).toBeFalsy();
57
57
  });
58
58
  });
59
+ describe('template tests', () => {
60
+ let template;
61
+ it('get the template', async () => {
62
+ template = await app.tokenTemplate();
63
+ const rules = Object.keys(template);
64
+ expect(rules).toContain('endpoint');
65
+ expect(rules).toContain('password');
66
+ expect(rules).toContain('username');
67
+ });
68
+ it('username', () => {
69
+ const username = template.username.regExp;
70
+ expect(username.test('')).toBeFalsy();
71
+ expect(username.test('someuser')).toBeTruthy();
72
+ });
73
+ it('endpoint', () => {
74
+ const endpoint = template.endpoint.regExp;
75
+ expect(endpoint.test('something')).toBeFalsy();
76
+ expect(endpoint.test('https://www.google.com')).toBeTruthy();
77
+ });
78
+ it('password', () => {
79
+ const password = template.password.regExp;
80
+ expect(password.test('')).toBeFalsy();
81
+ expect(password.test('somepassword')).toBeTruthy();
82
+ });
83
+ });
59
84
  });
60
85
  it('read allotment empty', async () => {
61
86
  const request = axios.mockImplementation(getFixture);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2-tourplan",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "Tourplan's TI2 Plugin",
5
5
  "main": "index.js",
6
6
  "scripts": {