ti2 1.0.47 → 1.0.48

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.
@@ -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,7 @@ 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');
11
12
 
12
13
  const schema = yaml.load(fs.readFileSync(`${__dirname}/api.yml`));
13
14
 
@@ -29,8 +30,6 @@ const isNumber = value => !Number.isNaN(Number(value));
29
30
 
30
31
  module.exports = async ({
31
32
  apiDocs = true,
32
- elasticLogIndex = 'apilog_ti2',
33
- elasticLogsClient,
34
33
  plugins: pluginsParam = {},
35
34
  port: portParam,
36
35
  startServer = true,
@@ -129,77 +128,15 @@ module.exports = async ({
129
128
  notFound: (req, res) => res.sendStatus(404),
130
129
  notImplemented: (req, res) => res.sendStatus(501),
131
130
  });
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
- },
147
- };
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,
191
- });
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);
131
+ const onRequestStartPlugins = plugins.filter(currentPlugin => (api.getAllFuncs(currentPlugin).indexOf('onRequestStart') > -1));
132
+ const onRequestEndPlugins = plugins.filter(currentPlugin => (api.getAllFuncs(currentPlugin).indexOf('onRequestEnd') > -1));
133
+
134
+ if (onRequestStartPlugins.length > 0) {
200
135
  app.use((req, res, next) => {
201
136
  const startHrTime = process.hrtime();
137
+ const requestId = uuidv4();
202
138
  const body = {
139
+ requestId,
203
140
  date: Math.floor(Date.now() / 1e3),
204
141
  url: req.url,
205
142
  // body: req.body,
@@ -210,29 +147,37 @@ module.exports = async ({
210
147
  operationId: R.path(['openapi', 'operation', 'operationId'], req),
211
148
  client: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
212
149
  };
213
- elasticLogsClient.index({
214
- index: elasticLogIndex,
215
- body,
216
- }).then(({ body: { _id } }) => {
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) {
217
158
  res.on('finish', () => {
218
159
  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
- ...{
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,
226
168
  responseTimeInMs,
227
169
  responseStatusCode: res.statusCode,
228
- },
229
- },
230
- }).catch((err, response) => console.log(err, response));
170
+ });
171
+ } catch (err) {
172
+ console.log(`unable to log to ${endPlugin.name}`, err);
173
+ }
174
+ }).then();
231
175
  });
232
- }).catch((err, response) => console.log(err, response));
176
+ }
233
177
  next();
234
178
  });
235
179
  }
180
+
236
181
  connect(app);
237
182
  app.use(middleware.mock());
238
183
  // 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.48",
4
4
  "description": "Tourist Industry Exchange (TI2)",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -44,7 +44,7 @@
44
44
  "swagger-ui-express": "4.3.0",
45
45
  "throng": "^5.0.0",
46
46
  "umzug": "^3.1.1",
47
- "uninstall": "^0.0.0"
47
+ "uuid": "^9.0.0"
48
48
  },
49
49
  "devDependencies": {
50
50
  "@salvador/minami": "^1.2.3",