ti2 1.0.50 → 1.0.51

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/cache.js +3 -2
  2. package/index.js +64 -8
  3. package/package.json +1 -1
package/cache.js CHANGED
@@ -3,7 +3,6 @@ const hash = require('object-hash');
3
3
 
4
4
  const REDIS_URL = process.env.REDIS_URL || 'redis://redis:6379';
5
5
  const cache = new Redis(`${REDIS_URL}/2`);
6
-
7
6
  const defaultTTL = 60 * 60; // 1 hour
8
7
 
9
8
  const save = async ({
@@ -57,10 +56,11 @@ const getOrExec = async ({
57
56
 
58
57
  const drop = async ({
59
58
  pluginName,
59
+ key: keyParam,
60
60
  fn,
61
61
  fnParams,
62
62
  }) => {
63
- let key = hash({ fn, fnParams });
63
+ let key = keyParam || hash({ fn, fnParams });
64
64
  key = `${pluginName}:${key}`;
65
65
  await cache.del(key);
66
66
  };
@@ -70,4 +70,5 @@ module.exports = {
70
70
  save,
71
71
  getOrExec,
72
72
  drop,
73
+ get,
73
74
  };
package/index.js CHANGED
@@ -3,6 +3,7 @@ const createMiddleware = require('swagger-express-middleware');
3
3
  const { connector } = require('swagger-routes-express');
4
4
  const swaggerUi = require('swagger-ui-express');
5
5
  const yaml = require('js-yaml');
6
+ const hash = require('object-hash');
6
7
  const fs = require('fs');
7
8
  const { pickBy } = require('ramda');
8
9
  const bb = require('bluebird');
@@ -11,6 +12,17 @@ const R = require('ramda');
11
12
  const { v4: uuidv4 } = require('uuid');
12
13
  const EventEmitter = require('eventemitter2');
13
14
 
15
+ const cacheSettings = {
16
+ '*': [
17
+ 'tokenTemplate',
18
+ 'getAffiliateAgents',
19
+ 'getAffiliateDesks',
20
+ 'getPickupPoints',
21
+ 'bookingsProductSearch',
22
+ ],
23
+ ventrata: [],
24
+ fareharbor: [],
25
+ };
14
26
  const ti2Events = new EventEmitter({ captureRejections: true, wildcard: true });
15
27
  ti2Events.on('event error', console.error);
16
28
 
@@ -136,11 +148,9 @@ module.exports = async ({
136
148
  eventHandlerPlugins.forEach(plugin => {
137
149
  plugin.eventHandler(ti2Events);
138
150
  });
139
-
140
- app.use((req, res, next) => {
141
- const startHrTime = process.hrtime();
151
+ const composeBodyFromReq = req => {
142
152
  const requestId = uuidv4();
143
- const body = {
153
+ return {
144
154
  requestId,
145
155
  date: Math.floor(Date.now() / 1e3),
146
156
  url: req.url,
@@ -152,16 +162,21 @@ module.exports = async ({
152
162
  operationId: R.path(['openapi', 'operation', 'operationId'], req),
153
163
  client: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
154
164
  };
165
+ };
166
+ app.use(async (req, res, next) => {
167
+ const startHrTime = process.hrtime();
168
+ const body = composeBodyFromReq(req);
169
+ req.customBody = body;
155
170
  ti2Events.emit('request.start', body);
156
- req.requestId = requestId;
157
- res.on('finish', () => {
171
+ req.requestId = body.requestId;
172
+ res.on('finish', async () => {
158
173
  const elapsedHrTime = process.hrtime(startHrTime);
159
174
  const responseTimeInMs = parseInt(
160
175
  elapsedHrTime[0] * 1e3 + elapsedHrTime[1] / 1e6,
161
176
  10,
162
177
  );
163
178
  ti2Events.emit('request.end', {
164
- ...body,
179
+ ...req.customBody,
165
180
  responseTimeInMs,
166
181
  responseStatusCode: res.statusCode,
167
182
  });
@@ -169,6 +184,46 @@ module.exports = async ({
169
184
  next();
170
185
  });
171
186
 
187
+ app.use(async (req, res, next) => {
188
+ try {
189
+ const currentPlugin = plugins.find(p => p.name === req.pathParams.appKey);
190
+ const cachingOperations = [
191
+ ...cacheSettings['*'],
192
+ ...(currentPlugin ? R.pathOr([], [currentPlugin.name], cacheSettings) : []),
193
+ ];
194
+ const body = req.customBody;
195
+ if (cachingOperations.indexOf(body.operationId) > -1) {
196
+ const cacheKey = hash(R.omit(['requestId', 'date'], body));
197
+
198
+ const foundCache = await cache.get({
199
+ pluginName: body.params.appKey,
200
+ key: cacheKey,
201
+ });
202
+ if (foundCache) {
203
+ // console.log('foundCache', cacheKey);
204
+ req.customBody.usedCache = cacheKey;
205
+ res.json(foundCache);
206
+ }
207
+ const realSend = res.json;
208
+ res.json = newData => { // new res.json
209
+ cache.save({
210
+ pluginName: req.pathParams.appKey,
211
+ key: cacheKey,
212
+ value: newData,
213
+ ttl: 60 * 60 * 24, // one day
214
+ });
215
+ // console.log('newData', cacheKey);
216
+ if (!res.headersSent) {
217
+ // console.log('send new data', cacheKey);
218
+ return realSend.apply(res, [newData]);
219
+ }
220
+ };
221
+ }
222
+ return next();
223
+ } catch (err) {
224
+ return next(err);
225
+ }
226
+ });
172
227
  connect(app);
173
228
  app.use(middleware.mock());
174
229
  // global error Handling
@@ -179,10 +234,11 @@ module.exports = async ({
179
234
  if (process.env.CONSOLE_ERRORS || process.env.JEST_WORKER_ID) {
180
235
  console.error(R.path(['response', 'data'], err), err);
181
236
  }
237
+ console.log('hit hre');
182
238
  return res.status((() => {
183
239
  if (!isNumber(err.status)) return 500;
184
240
  return Number(err.status);
185
- })()).json({
241
+ })()).send({
186
242
  message: R.path(['response', 'data', 'errorMessage'], err) || err.message || 'Internal Error',
187
243
  });
188
244
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ti2",
3
- "version": "1.0.50",
3
+ "version": "1.0.51",
4
4
  "description": "Tourist Industry Exchange (TI2)",
5
5
  "main": "index.js",
6
6
  "scripts": {