ti2 1.0.48 → 1.0.49

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.
@@ -27,6 +27,7 @@ const queryAllotment = plugins => async (req, res, next) => {
27
27
  const results = await app.queryAllotment({
28
28
  token,
29
29
  payload: query,
30
+ requestId: req.requestId,
30
31
  });
31
32
  return res.json(results);
32
33
  } catch (err) {
@@ -190,6 +190,7 @@ const validateAppToken = plugins => async (req, res, next) => {
190
190
  assert(app.validateToken, `could not find the validateToken method for ${appKey}`);
191
191
  const valid = await app.validateToken({
192
192
  token,
193
+ requestId: req.requestId,
193
194
  });
194
195
  return res.json({ valid });
195
196
  } catch (err) {
@@ -38,6 +38,7 @@ const bookingsSearch = plugins => async (req, res, next) => {
38
38
  token,
39
39
  payload: body,
40
40
  typeDefsAndQueries,
41
+ requestId: req.requestId,
41
42
  });
42
43
  return res.json(results);
43
44
  } catch (err) {
@@ -66,6 +67,7 @@ const bookingsCancel = plugins => async (req, res, next) => {
66
67
  token,
67
68
  payload: body,
68
69
  typeDefsAndQueries,
70
+ requestId: req.requestId,
69
71
  });
70
72
  return res.json(results);
71
73
  } catch (err) {
@@ -78,6 +80,7 @@ const $bookingsProductSearch = plugins => async ({
78
80
  userId,
79
81
  hint,
80
82
  payload,
83
+ requestId,
81
84
  }) => {
82
85
  const app = plugins.find(({ name }) => name === appKey);
83
86
  // const app = load(appKey);
@@ -94,6 +97,7 @@ const $bookingsProductSearch = plugins => async ({
94
97
  token,
95
98
  payload,
96
99
  typeDefsAndQueries,
100
+ requestId,
97
101
  });
98
102
  return results;
99
103
  };
@@ -102,9 +106,10 @@ const bookingsProductSearch = plugins => async (req, res, next) => {
102
106
  const {
103
107
  params,
104
108
  body: payload,
109
+ requestId,
105
110
  } = req;
106
111
  try {
107
- return res.json(await $bookingsProductSearch(plugins)({ ...params, payload }));
112
+ return res.json(await $bookingsProductSearch(plugins)({ ...params, payload, requestId }));
108
113
  } catch (err) {
109
114
  return next(err);
110
115
  }
@@ -131,6 +136,7 @@ const bookingsAvailabilitySearch = plugins => async (req, res, next) => {
131
136
  token,
132
137
  payload,
133
138
  typeDefsAndQueries,
139
+ requestId: req.requestId,
134
140
  });
135
141
  return res.json(results);
136
142
  } catch (err) {
@@ -143,6 +149,7 @@ const $bookingsAvailabilityCalendar = plugins => async ({
143
149
  userId,
144
150
  hint,
145
151
  payload,
152
+ requestId,
146
153
  }) => {
147
154
  const app = plugins.find(({ name }) => name === appKey);
148
155
  // const app = load(appKey);
@@ -159,6 +166,7 @@ const $bookingsAvailabilityCalendar = plugins => async ({
159
166
  token,
160
167
  payload,
161
168
  typeDefsAndQueries,
169
+ requestId,
162
170
  });
163
171
  };
164
172
 
@@ -166,9 +174,14 @@ const bookingsAvailabilityCalendar = plugins => async (req, res, next) => {
166
174
  const {
167
175
  params,
168
176
  body: payload,
177
+ requestId,
169
178
  } = req;
170
179
  try {
171
- return res.json(await $bookingsAvailabilityCalendar(plugins)({ ...params, payload }));
180
+ return res.json(await $bookingsAvailabilityCalendar(plugins)({
181
+ ...params,
182
+ payload,
183
+ requestId,
184
+ }));
172
185
  } catch (err) {
173
186
  return next(err);
174
187
  }
@@ -196,6 +209,7 @@ const searchQuote = plugins => async (req, res, next) => {
196
209
  token,
197
210
  payload,
198
211
  typeDefsAndQueries,
212
+ requestId: req.requestId,
199
213
  });
200
214
  return res.json(results);
201
215
  } catch (err) {
@@ -225,6 +239,7 @@ const createBooking = plugins => async (req, res, next) => {
225
239
  token,
226
240
  payload,
227
241
  typeDefsAndQueries,
242
+ requestId: req.requestId,
228
243
  });
229
244
  return res.json(results);
230
245
  } catch (err) {
@@ -253,6 +268,7 @@ const getAffiliateAgents = plugins => async (req, res, next) => {
253
268
  const results = await app.getAffiliateAgents({
254
269
  token,
255
270
  payload,
271
+ requestId: req.requestId,
256
272
  });
257
273
  return res.json(results);
258
274
  } catch (err) {
@@ -281,6 +297,7 @@ const getAffiliateDesks = plugins => async (req, res, next) => {
281
297
  const results = await app.getAffiliateDesks({
282
298
  token,
283
299
  payload,
300
+ requestId: req.requestId,
284
301
  });
285
302
  return res.json(results);
286
303
  } catch (err) {
package/index.js CHANGED
@@ -9,6 +9,10 @@ const bb = require('bluebird');
9
9
  const { Op } = require('sequelize');
10
10
  const R = require('ramda');
11
11
  const { v4: uuidv4 } = require('uuid');
12
+ const EventEmitter = require('eventemitter2');
13
+
14
+ const ti2Events = new EventEmitter({ captureRejections: true, wildcard: true });
15
+ ti2Events.on('event error', console.error);
12
16
 
13
17
  const schema = yaml.load(fs.readFileSync(`${__dirname}/api.yml`));
14
18
 
@@ -49,7 +53,7 @@ module.exports = async ({
49
53
  const nuName = attr.replace(/_/g, '-').replace(`ti2-${pluginName}-`, '');
50
54
  params[nuName] = value;
51
55
  });
52
- const pluginInstance = await new Plugin({ name: pluginName, ...params });
56
+ const pluginInstance = await new Plugin({ name: pluginName, events: ti2Events, ...params });
53
57
  return pluginInstance;
54
58
  });
55
59
  if (worker) {
@@ -128,55 +132,42 @@ module.exports = async ({
128
132
  notFound: (req, res) => res.sendStatus(404),
129
133
  notImplemented: (req, res) => res.sendStatus(501),
130
134
  });
131
- const onRequestStartPlugins = plugins.filter(currentPlugin => (api.getAllFuncs(currentPlugin).indexOf('onRequestStart') > -1));
132
- const onRequestEndPlugins = plugins.filter(currentPlugin => (api.getAllFuncs(currentPlugin).indexOf('onRequestEnd') > -1));
135
+ const eventHandlerPlugins = plugins.filter(currentPlugin => (api.getAllFuncs(currentPlugin).indexOf('eventHandler') > -1));
136
+ eventHandlerPlugins.forEach(plugin => {
137
+ plugin.eventHandler(ti2Events);
138
+ });
133
139
 
134
- if (onRequestStartPlugins.length > 0) {
135
- app.use((req, res, next) => {
136
- const startHrTime = process.hrtime();
137
- const requestId = uuidv4();
138
- const body = {
139
- requestId,
140
- date: Math.floor(Date.now() / 1e3),
141
- url: req.url,
142
- // body: req.body,
143
- params: req.pathParams,
144
- query: req.query,
145
- appRecord: req.appRecord,
146
- method: req.method,
147
- operationId: R.path(['openapi', 'operation', 'operationId'], req),
148
- client: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
149
- };
150
- bb.each(onRequestStartPlugins, async startPlugin => {
151
- try {
152
- await startPlugin.onRequestStart(body);
153
- } catch (err) {
154
- console.log(`unable to log to ${startPlugin.name}`, err);
155
- }
156
- }).then();
157
- if (onRequestEndPlugins.length > 0) {
158
- res.on('finish', () => {
159
- const elapsedHrTime = process.hrtime(startHrTime);
160
- const responseTimeInMs = parseInt(
161
- elapsedHrTime[0] * 1e3 + elapsedHrTime[1] / 1e6,
162
- 10,
163
- );
164
- bb.each(onRequestEndPlugins, async endPlugin => {
165
- try {
166
- await endPlugin.onRequestEnd({
167
- ...body,
168
- responseTimeInMs,
169
- responseStatusCode: res.statusCode,
170
- });
171
- } catch (err) {
172
- console.log(`unable to log to ${endPlugin.name}`, err);
173
- }
174
- }).then();
175
- });
176
- }
177
- next();
140
+ app.use((req, res, next) => {
141
+ const startHrTime = process.hrtime();
142
+ const requestId = uuidv4();
143
+ const body = {
144
+ requestId,
145
+ date: Math.floor(Date.now() / 1e3),
146
+ url: req.url,
147
+ body: req.body,
148
+ params: req.pathParams,
149
+ query: req.query,
150
+ appRecord: req.appRecord,
151
+ method: req.method,
152
+ operationId: R.path(['openapi', 'operation', 'operationId'], req),
153
+ client: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
154
+ };
155
+ ti2Events.emit('request.start', body);
156
+ req.requestId = requestId;
157
+ res.on('finish', () => {
158
+ const elapsedHrTime = process.hrtime(startHrTime);
159
+ const responseTimeInMs = parseInt(
160
+ elapsedHrTime[0] * 1e3 + elapsedHrTime[1] / 1e6,
161
+ 10,
162
+ );
163
+ ti2Events.emit('request.end', {
164
+ ...body,
165
+ responseTimeInMs,
166
+ responseStatusCode: res.statusCode,
167
+ });
178
168
  });
179
- }
169
+ next();
170
+ });
180
171
 
181
172
  connect(app);
182
173
  app.use(middleware.mock());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2",
3
- "version": "1.0.48",
3
+ "version": "1.0.49",
4
4
  "description": "Tourist Industry Exchange (TI2)",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,6 +28,7 @@
28
28
  "bluebird": "^3.7.2",
29
29
  "bull": "^4.8.2",
30
30
  "commander": "^9.2.0",
31
+ "eventemitter2": "^6.4.9",
31
32
  "express": "^4.17.2",
32
33
  "ioredis": "^5.0.4",
33
34
  "js-yaml": "^4.1.0",