ti2 1.0.55 → 1.0.57

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.
@@ -11,7 +11,7 @@ const {
11
11
  },
12
12
  } = process;
13
13
 
14
- const invalid = (res, msg) => res.status(401).json({ error: msg || 'Unauthorized' });
14
+ const invalid = (res, msg) => res.status(401).send({ error: msg || 'Unauthorized' });
15
15
 
16
16
  const getToken = ({ req }) => {
17
17
  if (
@@ -4,6 +4,7 @@ const { UserAppKey } = require('../models/index');
4
4
 
5
5
  const queryAllotment = plugins => async (req, res, next) => {
6
6
  const {
7
+ axios,
7
8
  params: {
8
9
  appKey,
9
10
  hint,
@@ -25,6 +26,7 @@ const queryAllotment = plugins => async (req, res, next) => {
25
26
  const token = await userAppKeys.token;
26
27
  assert(app.queryAllotment, 'could not find the allotment method');
27
28
  const results = await app.queryAllotment({
29
+ axios,
28
30
  token,
29
31
  payload: query,
30
32
  requestId: req.requestId,
@@ -167,6 +167,7 @@ const deleteAppToken = async (req, res, next) => {
167
167
 
168
168
  const validateAppToken = plugins => async (req, res, next) => {
169
169
  const {
170
+ axios,
170
171
  body: {
171
172
  tokenHint: hint,
172
173
  },
@@ -189,6 +190,7 @@ const validateAppToken = plugins => async (req, res, next) => {
189
190
  const token = await userAppKeys.token;
190
191
  assert(app.validateToken, `could not find the validateToken method for ${appKey}`);
191
192
  const valid = await app.validateToken({
193
+ axios,
192
194
  token,
193
195
  requestId: req.requestId,
194
196
  });
@@ -22,6 +22,7 @@ const typeDefsAndQueries = {
22
22
 
23
23
  const bookingsSearch = plugins => async (req, res, next) => {
24
24
  const {
25
+ axios,
25
26
  params: { appKey, userId, hint },
26
27
  body,
27
28
  } = req;
@@ -38,6 +39,7 @@ const bookingsSearch = plugins => async (req, res, next) => {
38
39
  const token = await userAppKeys.token;
39
40
  const search = (app.searchHotelBooking || app.searchBooking).bind(app);
40
41
  const results = await search({
42
+ axios,
41
43
  token,
42
44
  payload: body,
43
45
  typeDefsAndQueries,
@@ -51,6 +53,7 @@ const bookingsSearch = plugins => async (req, res, next) => {
51
53
 
52
54
  const bookingsCancel = plugins => async (req, res, next) => {
53
55
  const {
56
+ axios,
54
57
  params: { appKey, userId, hint },
55
58
  body,
56
59
  } = req;
@@ -67,6 +70,7 @@ const bookingsCancel = plugins => async (req, res, next) => {
67
70
  assert(userAppKeys, 'could not find the app key');
68
71
  const token = await userAppKeys.token;
69
72
  const results = await app.cancelBooking({
73
+ axios,
70
74
  token,
71
75
  payload: body,
72
76
  typeDefsAndQueries,
@@ -79,6 +83,7 @@ const bookingsCancel = plugins => async (req, res, next) => {
79
83
  };
80
84
 
81
85
  const $bookingsProductSearch = plugins => async ({
86
+ axios,
82
87
  appKey,
83
88
  userId,
84
89
  hint,
@@ -97,6 +102,7 @@ const $bookingsProductSearch = plugins => async ({
97
102
  assert(userAppKeys, 'could not find the app key');
98
103
  const token = await userAppKeys.token;
99
104
  const results = await app.searchProducts({
105
+ axios,
100
106
  token,
101
107
  payload,
102
108
  typeDefsAndQueries,
@@ -107,12 +113,18 @@ const $bookingsProductSearch = plugins => async ({
107
113
 
108
114
  const bookingsProductSearch = plugins => async (req, res, next) => {
109
115
  const {
116
+ axios,
110
117
  params,
111
118
  body: payload,
112
119
  requestId,
113
120
  } = req;
114
121
  try {
115
- return res.json(await $bookingsProductSearch(plugins)({ ...params, payload, requestId }));
122
+ return res.json(await $bookingsProductSearch(plugins)({
123
+ axios,
124
+ ...params,
125
+ payload,
126
+ requestId,
127
+ }));
116
128
  } catch (err) {
117
129
  return next(err);
118
130
  }
@@ -120,6 +132,7 @@ const bookingsProductSearch = plugins => async (req, res, next) => {
120
132
 
121
133
  const bookingsAvailabilitySearch = plugins => async (req, res, next) => {
122
134
  const {
135
+ axios,
123
136
  params: { appKey, userId, hint },
124
137
  body: payload,
125
138
  } = req;
@@ -136,6 +149,7 @@ const bookingsAvailabilitySearch = plugins => async (req, res, next) => {
136
149
  assert(userAppKeys, 'could not find the app key');
137
150
  const token = await userAppKeys.token;
138
151
  const results = await app.searchAvailability({
152
+ axios,
139
153
  token,
140
154
  payload,
141
155
  typeDefsAndQueries,
@@ -148,6 +162,7 @@ const bookingsAvailabilitySearch = plugins => async (req, res, next) => {
148
162
  };
149
163
 
150
164
  const $bookingsAvailabilityCalendar = plugins => async ({
165
+ axios,
151
166
  appKey,
152
167
  userId,
153
168
  hint,
@@ -166,6 +181,7 @@ const $bookingsAvailabilityCalendar = plugins => async ({
166
181
  assert(userAppKeys, 'could not find the app key');
167
182
  const token = await userAppKeys.token;
168
183
  return app.availabilityCalendar({
184
+ axios,
169
185
  token,
170
186
  payload,
171
187
  typeDefsAndQueries,
@@ -175,12 +191,14 @@ const $bookingsAvailabilityCalendar = plugins => async ({
175
191
 
176
192
  const bookingsAvailabilityCalendar = plugins => async (req, res, next) => {
177
193
  const {
194
+ axios,
178
195
  params,
179
196
  body: payload,
180
197
  requestId,
181
198
  } = req;
182
199
  try {
183
200
  return res.json(await $bookingsAvailabilityCalendar(plugins)({
201
+ axios,
184
202
  ...params,
185
203
  payload,
186
204
  requestId,
@@ -192,6 +210,7 @@ const bookingsAvailabilityCalendar = plugins => async (req, res, next) => {
192
210
 
193
211
  const searchQuote = plugins => async (req, res, next) => {
194
212
  const {
213
+ axios,
195
214
  params: { appKey, userId, hint },
196
215
  body: payload,
197
216
  } = req;
@@ -209,6 +228,7 @@ const searchQuote = plugins => async (req, res, next) => {
209
228
  const token = await userAppKeys.token;
210
229
  assert(payload.id, 'the availability id is required');
211
230
  const results = await app.searchQuote({
231
+ axios,
212
232
  token,
213
233
  payload,
214
234
  typeDefsAndQueries,
@@ -222,6 +242,7 @@ const searchQuote = plugins => async (req, res, next) => {
222
242
 
223
243
  const createBooking = plugins => async (req, res, next) => {
224
244
  const {
245
+ axios,
225
246
  params: { appKey, userId, hint },
226
247
  body: payload,
227
248
  } = req;
@@ -239,6 +260,7 @@ const createBooking = plugins => async (req, res, next) => {
239
260
  const token = await userAppKeys.token;
240
261
  assert(payload.id, 'the quote id is required');
241
262
  const results = await app.createBooking({
263
+ axios,
242
264
  token,
243
265
  payload,
244
266
  typeDefsAndQueries,
@@ -252,6 +274,7 @@ const createBooking = plugins => async (req, res, next) => {
252
274
 
253
275
  const getAffiliateAgents = plugins => async (req, res, next) => {
254
276
  const {
277
+ axios,
255
278
  params: { appKey, userId, hint },
256
279
  body: payload,
257
280
  } = req;
@@ -269,6 +292,7 @@ const getAffiliateAgents = plugins => async (req, res, next) => {
269
292
  const token = await userAppKeys.token;
270
293
  assert(app.getAffiliateAgents, `getAffiliateAgents is not available for ${appKey}`);
271
294
  const results = await app.getAffiliateAgents({
295
+ axios,
272
296
  token,
273
297
  payload,
274
298
  requestId: req.requestId,
@@ -281,6 +305,7 @@ const getAffiliateAgents = plugins => async (req, res, next) => {
281
305
 
282
306
  const getAffiliateDesks = plugins => async (req, res, next) => {
283
307
  const {
308
+ axios,
284
309
  params: { appKey, userId, hint },
285
310
  body: payload,
286
311
  } = req;
@@ -298,6 +323,7 @@ const getAffiliateDesks = plugins => async (req, res, next) => {
298
323
  const token = await userAppKeys.token;
299
324
  assert(app.getAffiliateDesks, `getAffiliateDesks is not available for ${appKey}`);
300
325
  const results = await app.getAffiliateDesks({
326
+ axios,
301
327
  token,
302
328
  payload,
303
329
  requestId: req.requestId,
@@ -310,6 +336,7 @@ const getAffiliateDesks = plugins => async (req, res, next) => {
310
336
 
311
337
  const getPickupPoints = plugins => async (req, res, next) => {
312
338
  const {
339
+ axios,
313
340
  params: { appKey, userId, hint },
314
341
  body: payload,
315
342
  } = req;
@@ -327,6 +354,7 @@ const getPickupPoints = plugins => async (req, res, next) => {
327
354
  const token = await userAppKeys.token;
328
355
  assert(app.getPickupPoints, `getPickupPoints is not available for ${appKey}`);
329
356
  const results = await app.getPickupPoints({
357
+ axios,
330
358
  token,
331
359
  payload,
332
360
  typeDefsAndQueries,
package/index.js CHANGED
@@ -12,10 +12,15 @@ const { Op } = require('sequelize');
12
12
  const R = require('ramda');
13
13
  const { v4: uuidv4 } = require('uuid');
14
14
  const EventEmitter = require('eventemitter2');
15
+ const axios = require('axios');
16
+ const curlirize = require('axios-curlirize');
17
+
18
+ if (process.env.debug) {
19
+ curlirize(axios);
20
+ }
15
21
 
16
22
  const cacheSettings = {
17
23
  '*': [
18
- 'tokenTemplate',
19
24
  'getAffiliateAgents',
20
25
  'getAffiliateDesks',
21
26
  'getPickupPoints',
@@ -46,6 +51,14 @@ const rebuild = fn => obj =>
46
51
 
47
52
  const isNumber = value => !Number.isNaN(Number(value));
48
53
 
54
+ const axiosSafeRequest = R.pick(['headers', 'method', 'url', 'data']);
55
+ const axiosSafeResponse = response => {
56
+ const retVal = R.pick(['data', 'status', 'statusText', 'headers', 'request', 'config'], response);
57
+ retVal.request = axiosSafeRequest(retVal.request);
58
+ retVal.config = axiosSafeRequest(retVal.config);
59
+ return retVal;
60
+ };
61
+
49
62
  module.exports = async ({
50
63
  apiDocs = true,
51
64
  plugins: pluginsParam = {},
@@ -67,7 +80,11 @@ module.exports = async ({
67
80
  const nuName = attr.replace(/_/g, '-').replace(`ti2-${pluginName}-`, '');
68
81
  params[nuName] = value;
69
82
  });
70
- const pluginInstance = await new Plugin({ name: pluginName, events: ti2Events, ...params });
83
+ const pluginInstance = await new Plugin({
84
+ name: pluginName,
85
+ events: ti2Events,
86
+ ...params,
87
+ });
71
88
  return pluginInstance;
72
89
  });
73
90
  if (worker) {
@@ -185,6 +202,36 @@ module.exports = async ({
185
202
  responseStatusCode: res.statusCode,
186
203
  });
187
204
  });
205
+
206
+ const axiosPlugin = axios.create({ headers: { requestId: req.requestId } });
207
+ const pluginName = R.pathOr(
208
+ R.pathOr('ti2', ['params', 'app'], body),
209
+ ['params', 'appKey'],
210
+ body,
211
+ );
212
+ const userId = R.pathOr(undefined, ['params', 'userId'], body);
213
+ axiosPlugin.interceptors.request.use(request => {
214
+ ti2Events.emit(`${pluginName}.axios.request`, { ...axiosSafeRequest(request), userId });
215
+ request.headers.common.requestId = req.requestId;
216
+ return request;
217
+ });
218
+ axiosPlugin.interceptors.response.use(response => {
219
+ ti2Events.emit(`${pluginName}.axios.response`, { ...axiosSafeResponse(response), userId });
220
+ return response;
221
+ });
222
+ req.axios = async (...args) => axiosPlugin(...args).catch(err => {
223
+ const errMsg = R.omit(['config'], err.toJSON());
224
+ console.log(`error in ${pluginName}`, args[0], errMsg);
225
+ if (ti2Events.events) {
226
+ ti2Events.events.emit(`${pluginName}.axios.error`, {
227
+ request: args[0],
228
+ err: errMsg,
229
+ });
230
+ }
231
+ throw R.pathOr(err, ['response', 'data', 'errorMessage'], err);
232
+ });
233
+ req.axios = axiosPlugin;
234
+
188
235
  next();
189
236
  });
190
237
 
@@ -209,12 +256,14 @@ module.exports = async ({
209
256
  }
210
257
  const realSend = res.json;
211
258
  res.json = newData => { // new res.json
212
- cache.save({
213
- pluginName: req.pathParams.appKey,
214
- key: cacheKey,
215
- value: newData,
216
- ttl: 60 * 60 * 24, // one day
217
- });
259
+ if (res.statusCode === 200) {
260
+ cache.save({
261
+ pluginName: req.pathParams.appKey,
262
+ key: cacheKey,
263
+ value: newData,
264
+ ttl: 60 * 60 * 24, // one day
265
+ });
266
+ }
218
267
  // console.log('newData', cacheKey);
219
268
  if (!res.headersSent) {
220
269
  // console.log('send new data', cacheKey);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2",
3
- "version": "1.0.55",
3
+ "version": "1.0.57",
4
4
  "description": "Tourist Industry Exchange (TI2)",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,6 +25,8 @@
25
25
  },
26
26
  "homepage": "https://github.com/ti2travel/ti2#readme",
27
27
  "dependencies": {
28
+ "axios": "^0.27.2",
29
+ "axios-curlirize": "^1.3.7",
28
30
  "bluebird": "^3.7.2",
29
31
  "bull": "^4.8.2",
30
32
  "commander": "^9.2.0",