ti2-ventrata 1.0.20 → 1.0.22

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 +52 -18
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -15,13 +15,6 @@ if (process.env.debug) {
15
15
  curlirize(axiosRaw);
16
16
  }
17
17
 
18
- const axios = async (...args) => axiosRaw(...args)
19
- .catch(err => {
20
- const errMsg = R.path(['response', 'data', 'errorMessage'], err);
21
- console.log('error in ti2-ventrata', args[0], errMsg);
22
- if (errMsg) throw new Error(errMsg);
23
- throw err;
24
- });
25
18
  const isNilOrEmpty = R.either(R.isNil, R.isEmpty);
26
19
 
27
20
  const getHeaders = ({
@@ -29,6 +22,7 @@ const getHeaders = ({
29
22
  acceptLanguage,
30
23
  octoEnv,
31
24
  resellerId,
25
+ requestId,
32
26
  }) => ({
33
27
  Authorization: `Bearer ${apiKey}`,
34
28
  ...octoEnv ? { 'Octo-Env': octoEnv } : {},
@@ -36,6 +30,7 @@ const getHeaders = ({
36
30
  'Content-Type': 'application/json',
37
31
  ...resellerId ? { Referer: resellerId } : {},
38
32
  'Octo-Capabilities': 'octo/pricing,octo/pickups',
33
+ ...requestId ? { requestId } : {},
39
34
  // 'Octo-Capabilities': 'octo/pricing',
40
35
  });
41
36
 
@@ -44,6 +39,31 @@ class Plugin {
44
39
  Object.entries(params).forEach(([attr, value]) => {
45
40
  this[attr] = value;
46
41
  });
42
+ if (this.events) {
43
+ axiosRaw.interceptors.request.use(request => {
44
+ this.events.emit(`${this.name}.axios.request`, JSON.parse(JSON.stringify(request)));
45
+ return request;
46
+ });
47
+ axiosRaw.interceptors.response.use(response => {
48
+ this.events.emit(`${this.name}.axios.response`, JSON.parse(JSON.stringify(response)));
49
+ return response;
50
+ });
51
+ }
52
+ const pluginObj = this;
53
+ this.axios = async (...args) => axiosRaw(...args).catch(err => {
54
+ const errMsg = R.path(['response', 'data', 'errorMessage'], err);
55
+ console.log(`error in ${this.name}`, args[0], errMsg || err);
56
+ if (pluginObj.events) {
57
+ pluginObj.events.emit(`${this.name}.axios.error`, {
58
+ request: args[0],
59
+ err: JSON.parse(JSON.stringify(err)),
60
+ errMsg,
61
+ });
62
+ }
63
+ if (errMsg) throw new Error(errMsg);
64
+ throw err;
65
+ });
66
+
47
67
  this.tokenTemplate = () => ({
48
68
  apiKey: {
49
69
  type: 'text',
@@ -85,6 +105,7 @@ class Plugin {
85
105
  acceptLanguage,
86
106
  resellerId,
87
107
  },
108
+ requestId,
88
109
  }) {
89
110
  const url = `${endpoint || this.endpoint}/whoami?token=${apiKey}`;
90
111
  const headers = getHeaders({
@@ -93,9 +114,10 @@ class Plugin {
93
114
  octoEnv,
94
115
  acceptLanguage,
95
116
  resellerId,
117
+ requestId,
96
118
  });
97
119
  try {
98
- const connectionId = R.path(['data', 'connection', 'id'], await axios({
120
+ const connectionId = R.path(['data', 'connection', 'id'], await this.axios({
99
121
  method: 'get',
100
122
  url,
101
123
  headers,
@@ -119,6 +141,7 @@ class Plugin {
119
141
  productTypeDefs,
120
142
  productQuery,
121
143
  },
144
+ requestId,
122
145
  }) {
123
146
  let url = `${endpoint || this.endpoint}/products`;
124
147
  if (!isNilOrEmpty(payload)) {
@@ -132,8 +155,9 @@ class Plugin {
132
155
  octoEnv,
133
156
  acceptLanguage,
134
157
  resellerId,
158
+ requestId,
135
159
  });
136
- let results = R.pathOr([], ['data'], await axios({
160
+ let results = R.pathOr([], ['data'], await this.axios({
137
161
  method: 'get',
138
162
  url,
139
163
  headers,
@@ -202,6 +226,7 @@ class Plugin {
202
226
  availTypeDefs,
203
227
  availQuery,
204
228
  },
229
+ requestId,
205
230
  }) {
206
231
  assert(this.jwtKey, 'JWT secret should be set');
207
232
  assert(
@@ -222,6 +247,7 @@ class Plugin {
222
247
  octoEnv,
223
248
  acceptLanguage,
224
249
  resellerId,
250
+ requestId,
225
251
  });
226
252
  const url = `${endpoint || this.endpoint}/availability`;
227
253
  const availability = (
@@ -234,7 +260,7 @@ class Plugin {
234
260
  units: units[ix].map(u => ({ id: u.unitId, quantity: u.quantity })),
235
261
  };
236
262
  if (currency) data.currency = currency;
237
- const result = R.path(['data'], await axios({
263
+ const result = R.path(['data'], await this.axios({
238
264
  method: 'post',
239
265
  url,
240
266
  data,
@@ -278,6 +304,7 @@ class Plugin {
278
304
  availTypeDefs,
279
305
  availQuery,
280
306
  },
307
+ requestId,
281
308
  }) {
282
309
  assert(this.jwtKey, 'JWT secret should be set');
283
310
  assert(
@@ -298,6 +325,7 @@ class Plugin {
298
325
  octoEnv,
299
326
  acceptLanguage,
300
327
  resellerId,
328
+ requestId,
301
329
  });
302
330
  const url = `${endpoint || this.endpoint}/availability/calendar`;
303
331
  const availability = (
@@ -311,7 +339,7 @@ class Plugin {
311
339
  units: units[ix].map(u => ({ id: u.unitId, quantity: u.quantity })),
312
340
  };
313
341
  if (currency) data.currency = currency;
314
- const result = await axios({
342
+ const result = await this.axios({
315
343
  method: 'post',
316
344
  url,
317
345
  data,
@@ -348,6 +376,7 @@ class Plugin {
348
376
  bookingTypeDefs,
349
377
  bookingQuery,
350
378
  },
379
+ requestId,
351
380
  }) {
352
381
  assert(availabilityKey, 'an availability code is required !');
353
382
  assert(R.path(['name'], holder), 'first name is required');
@@ -358,9 +387,10 @@ class Plugin {
358
387
  octoEnv,
359
388
  acceptLanguage,
360
389
  resellerId,
390
+ requestId,
361
391
  });
362
392
  const dataForCreateBooking = await jwt.verify(availabilityKey, this.jwtKey);
363
- let booking = R.path(['data'], await axios({
393
+ let booking = R.path(['data'], await this.axios({
364
394
  method: rebookingId ? 'patch' : 'post',
365
395
  url: `${endpoint || this.endpoint}/bookings${rebookingId ? `/${rebookingId}` : ''}`,
366
396
  data: {
@@ -385,7 +415,7 @@ class Plugin {
385
415
  resellerReference: reference,
386
416
  settlementMethod,
387
417
  };
388
- booking = R.path(['data'], await axios({
418
+ booking = R.path(['data'], await this.axios({
389
419
  method: 'post',
390
420
  url: `${endpoint || this.endpoint}/bookings/${booking.uuid}/confirm`,
391
421
  data: dataForConfirmBooking,
@@ -418,6 +448,7 @@ class Plugin {
418
448
  bookingTypeDefs,
419
449
  bookingQuery,
420
450
  },
451
+ requestId,
421
452
  }) {
422
453
  assert(!isNilOrEmpty(bookingId) || !isNilOrEmpty(id), 'Invalid booking id');
423
454
  const headers = getHeaders({
@@ -426,9 +457,10 @@ class Plugin {
426
457
  octoEnv,
427
458
  acceptLanguage,
428
459
  resellerId,
460
+ requestId,
429
461
  });
430
462
  const url = `${endpoint || this.endpoint}/bookings/${bookingId || id}`;
431
- const booking = R.path(['data'], await axios({
463
+ const booking = R.path(['data'], await this.axios({
432
464
  method: 'delete',
433
465
  url,
434
466
  data: { reason },
@@ -463,6 +495,7 @@ class Plugin {
463
495
  bookingTypeDefs,
464
496
  bookingQuery,
465
497
  },
498
+ requestId,
466
499
  }) {
467
500
  assert(
468
501
  !isNilOrEmpty(bookingId)
@@ -479,10 +512,11 @@ class Plugin {
479
512
  octoEnv,
480
513
  acceptLanguage,
481
514
  resellerId,
515
+ requestId,
482
516
  });
483
517
  const searchByUrl = async url => {
484
518
  try {
485
- return R.path(['data'], await axios({
519
+ return R.path(['data'], await this.axios({
486
520
  method: 'get',
487
521
  url,
488
522
  headers,
@@ -502,7 +536,7 @@ class Plugin {
502
536
  }
503
537
  if (!isNilOrEmpty(resellerReference)) {
504
538
  url = `${endpoint || this.endpoint}/bookings?resellerReference=${resellerReference}`;
505
- return R.path(['data'], await axios({
539
+ return R.path(['data'], await this.axios({
506
540
  method: 'get',
507
541
  url,
508
542
  headers,
@@ -510,7 +544,7 @@ class Plugin {
510
544
  }
511
545
  if (!isNilOrEmpty(supplierBookingId)) {
512
546
  url = `${endpoint || this.endpoint}/bookings?supplierReference=${supplierBookingId}`;
513
- return R.path(['data'], await axios({
547
+ return R.path(['data'], await this.axios({
514
548
  method: 'get',
515
549
  url,
516
550
  headers,
@@ -520,7 +554,7 @@ class Plugin {
520
554
  const localDateStart = moment(travelDateStart, dateFormat).format();
521
555
  const localDateEnd = moment(travelDateEnd, dateFormat).format();
522
556
  url = `${endpoint || this.endpoint}/bookings?localDateStart=${encodeURIComponent(localDateStart)}&localDateEnd=${encodeURIComponent(localDateEnd)}`;
523
- return R.path(['data'], await axios({
557
+ return R.path(['data'], await this.axios({
524
558
  method: 'get',
525
559
  url,
526
560
  headers,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2-ventrata",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "Ventrata's TI2 Plugin",
5
5
  "main": "index.js",
6
6
  "scripts": {