ti2 1.0.47 → 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) {
@@ -23,7 +23,11 @@ const getAllFuncs = toCheck => {
23
23
  } while (obj = Object.getPrototypeOf(obj));
24
24
 
25
25
  return props.sort().filter((e, i, arr) => {
26
- if (e !== arr[i + 1] && typeof toCheck[e] === 'function') return true;
26
+ try {
27
+ if (e !== arr[i + 1] && typeof toCheck[e] === 'function') return true;
28
+ } catch {
29
+ return undefined;
30
+ }
27
31
  return undefined;
28
32
  });
29
33
  };
@@ -42,6 +46,7 @@ const getAppMethods = plugins => async (req, res, next) => {
42
46
  };
43
47
 
44
48
  module.exports = plugins => ({
45
- userAppList,
49
+ getAllFuncs,
46
50
  getAppMethods: getAppMethods(plugins),
51
+ userAppList,
47
52
  });
package/index.js CHANGED
@@ -8,6 +8,11 @@ const { pickBy } = require('ramda');
8
8
  const bb = require('bluebird');
9
9
  const { Op } = require('sequelize');
10
10
  const R = require('ramda');
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);
11
16
 
12
17
  const schema = yaml.load(fs.readFileSync(`${__dirname}/api.yml`));
13
18
 
@@ -29,8 +34,6 @@ const isNumber = value => !Number.isNaN(Number(value));
29
34
 
30
35
  module.exports = async ({
31
36
  apiDocs = true,
32
- elasticLogIndex = 'apilog_ti2',
33
- elasticLogsClient,
34
37
  plugins: pluginsParam = {},
35
38
  port: portParam,
36
39
  startServer = true,
@@ -50,7 +53,7 @@ module.exports = async ({
50
53
  const nuName = attr.replace(/_/g, '-').replace(`ti2-${pluginName}-`, '');
51
54
  params[nuName] = value;
52
55
  });
53
- const pluginInstance = await new Plugin({ name: pluginName, ...params });
56
+ const pluginInstance = await new Plugin({ name: pluginName, events: ti2Events, ...params });
54
57
  return pluginInstance;
55
58
  });
56
59
  if (worker) {
@@ -129,110 +132,43 @@ module.exports = async ({
129
132
  notFound: (req, res) => res.sendStatus(404),
130
133
  notImplemented: (req, res) => res.sendStatus(501),
131
134
  });
132
- if (elasticLogsClient) { // API request logs are saved on elastic
133
- const mappings = {
134
- dynamic: true,
135
- properties: {
136
- body: { type: 'object' },
137
- client: { type: 'keyword' },
138
- date: { type: 'long' },
139
- method: { type: 'keyword' },
140
- operationId: { type: 'keyword' },
141
- params: { type: 'object' },
142
- query: { type: 'object' },
143
- url: { type: 'text' },
144
- responseStatusCode: { type: 'long' },
145
- responseTimeInMs: { type: 'long' },
146
- },
135
+ const eventHandlerPlugins = plugins.filter(currentPlugin => (api.getAllFuncs(currentPlugin).indexOf('eventHandler') > -1));
136
+ eventHandlerPlugins.forEach(plugin => {
137
+ plugin.eventHandler(ti2Events);
138
+ });
139
+
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,
147
154
  };
148
- // setup the index
149
- elasticLogsClient.indices.existsTemplate({
150
- name: 'apilog',
151
- }).then(async ({ body }) => {
152
- if (!body) {
153
- // the template has to be created
154
- console.log('=- creating index template -=');
155
- await elasticLogsClient.indices.putTemplate({
156
- name: 'apilog',
157
- body: {
158
- index_patterns: [`${elasticLogIndex}*`],
159
- settings: {
160
- number_of_shards: 1,
161
- },
162
- mappings,
163
- },
164
- });
165
- console.log('index template created');
166
- } else {
167
- // index exists, we might have to updated it
168
- const current = R.path(
169
- ['body', 'apilog', 'mappings'],
170
- await elasticLogsClient.indices.getTemplate({
171
- name: 'apilog',
172
- }),
173
- );
174
- if (!R.equals(current, mappings)) {
175
- console.log('=- updating index template -=');
176
- await elasticLogsClient.indices.putTemplate({
177
- name: 'apilog',
178
- body: {
179
- index_patterns: [`${elasticLogIndex}*`],
180
- settings: {
181
- number_of_shards: 1,
182
- },
183
- mappings,
184
- },
185
- });
186
- console.log('updated');
187
- }
188
- }
189
- const exists = await elasticLogsClient.indices.exists({
190
- index: elasticLogIndex,
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,
191
167
  });
192
- if (!exists || !exists.body) {
193
- console.log('=- creating index =-');
194
- await elasticLogsClient.indices.create({
195
- index: elasticLogIndex,
196
- });
197
- console.log('index created');
198
- }
199
- }).catch(console.log);
200
- app.use((req, res, next) => {
201
- const startHrTime = process.hrtime();
202
- const body = {
203
- date: Math.floor(Date.now() / 1e3),
204
- url: req.url,
205
- // body: req.body,
206
- params: req.pathParams,
207
- query: req.query,
208
- appRecord: req.appRecord,
209
- method: req.method,
210
- operationId: R.path(['openapi', 'operation', 'operationId'], req),
211
- client: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
212
- };
213
- elasticLogsClient.index({
214
- index: elasticLogIndex,
215
- body,
216
- }).then(({ body: { _id } }) => {
217
- res.on('finish', () => {
218
- const elapsedHrTime = process.hrtime(startHrTime);
219
- const responseTimeInMs = parseInt(elapsedHrTime[0] * 1e3 + elapsedHrTime[1] / 1e6, 10);
220
- elasticLogsClient.index({
221
- index: elasticLogIndex,
222
- id: _id,
223
- body: {
224
- ...body,
225
- ...{
226
- responseTimeInMs,
227
- responseStatusCode: res.statusCode,
228
- },
229
- },
230
- }).catch((err, response) => console.log(err, response));
231
- });
232
- }).catch((err, response) => console.log(err, response));
233
- next();
234
168
  });
235
- }
169
+ next();
170
+ });
171
+
236
172
  connect(app);
237
173
  app.use(middleware.mock());
238
174
  // global error Handling
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2",
3
- "version": "1.0.47",
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",
@@ -44,7 +45,7 @@
44
45
  "swagger-ui-express": "4.3.0",
45
46
  "throng": "^5.0.0",
46
47
  "umzug": "^3.1.1",
47
- "uninstall": "^0.0.0"
48
+ "uuid": "^9.0.0"
48
49
  },
49
50
  "devDependencies": {
50
51
  "@salvador/minami": "^1.2.3",