lesgo 0.7.3 → 0.7.6

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lesgo",
3
- "version": "0.7.3",
3
+ "version": "0.7.6",
4
4
  "description": "Core framework for lesgo node.js serverless framework.",
5
5
  "main": "./src/index.js",
6
6
  "author": "Sufiyan Rahmat",
@@ -51,14 +51,14 @@
51
51
  "prettier": "^1.18.2"
52
52
  },
53
53
  "dependencies": {
54
- "@elastic/elasticsearch": "^7.4.0",
55
- "@firebase/app": "^0.6.12",
54
+ "@elastic/elasticsearch": "^7.10.0",
55
+ "@firebase/app": "^0.7.16",
56
56
  "data-api-client": "^1.1.0",
57
57
  "firebase-admin": "^9.3.0",
58
58
  "jsonwebtoken": "^8.5.1",
59
59
  "memcached-elasticache": "^1.1.1",
60
60
  "mysql2": "^2.2.5",
61
- "nanoid": "^3.1.16"
61
+ "nanoid": "^3.2.0"
62
62
  },
63
63
  "husky": {
64
64
  "hooks": {
@@ -21,7 +21,7 @@ describe('MiddlewareGroup: test errorHandler middleware', () => {
21
21
  expect(dataBody).toHaveProperty('status', 'error');
22
22
  expect(dataBody).toHaveProperty('data', null);
23
23
  expect(dataBody).toHaveProperty('error');
24
- expect(dataBody).toHaveProperty('error.code', 'UNKNOWN_ERROR');
24
+ expect(dataBody).toHaveProperty('error.code', 'UNHANDLED_ERROR');
25
25
  expect(dataBody).toHaveProperty(
26
26
  'error.message',
27
27
  'Error: Test validation error'
@@ -78,7 +78,7 @@ describe('MiddlewareGroup: test errorHandler middleware', () => {
78
78
 
79
79
  expect(data.statusCode).toBe(500);
80
80
 
81
- expect(dataBody).toHaveProperty('error.code', 'UNKNOWN_ERROR');
81
+ expect(dataBody).toHaveProperty('error.code', 'UNHANDLED_ERROR');
82
82
  expect(dataBody).toHaveProperty('error.message', 'Test error message');
83
83
  expect(dataBody).toHaveProperty('error.details', '');
84
84
  });
@@ -126,7 +126,7 @@ describe('MiddlewareGroup: test errorHandler middleware', () => {
126
126
  expect(dataBody).toHaveProperty('status', 'error');
127
127
  expect(dataBody).toHaveProperty('data', null);
128
128
  expect(dataBody).toHaveProperty('error');
129
- expect(dataBody).toHaveProperty('error.code', 'UNKNOWN_ERROR');
129
+ expect(dataBody).toHaveProperty('error.code', 'UNHANDLED_ERROR');
130
130
  expect(dataBody).toHaveProperty('error.message', '');
131
131
  expect(dataBody).toHaveProperty('error.details', '');
132
132
  });
@@ -102,7 +102,6 @@ describe('MiddlewareGroup: test normalizeHttpRequestBeforeHandler', () => {
102
102
  };
103
103
 
104
104
  normalizeHttpRequestBeforeHandler(handler, () => {});
105
- expect(handler.event.requestContext.requestId).toBe('requestId');
106
105
  });
107
106
 
108
107
  it('should return auth.sub if Authorization exists', () => {
@@ -119,6 +118,44 @@ describe('MiddlewareGroup: test normalizeHttpRequestBeforeHandler', () => {
119
118
  expect(handler.event.auth.sub).toBe('f2b5349d-f5e3-44f5-9c08-ae6b01e95434');
120
119
  });
121
120
 
121
+ it.each`
122
+ version | tags
123
+ ${'1.0'} | ${{ path: '/v1/path', httpMethod: 'GET' }}
124
+ ${'2.0'} | ${{ path: '/v2/path', httpMethod: 'POST' }}
125
+ ${'3.0'} | ${{ path: '/v2/path', httpMethod: 'POST' }}
126
+ `(
127
+ 'should identify path and httpMethod based on version',
128
+ ({ version, tags }) => {
129
+ const handler = {
130
+ event: {
131
+ version,
132
+ headers: {},
133
+ ...tags,
134
+ requestContext: {
135
+ http: {
136
+ ...tags,
137
+ method: tags.httpMethod,
138
+ },
139
+ },
140
+ },
141
+ };
142
+ normalizeHttpRequestBeforeHandler(handler, () => {});
143
+ expect(logger.meta.tags).toStrictEqual(tags);
144
+ }
145
+ );
146
+
147
+ it('should not set tags when using API Gateway v2 and requestContext is empty', () => {
148
+ const handler = {
149
+ event: {
150
+ version: '2.0',
151
+ headers: {},
152
+ },
153
+ };
154
+
155
+ normalizeHttpRequestBeforeHandler(handler, () => {});
156
+ expect(logger.meta.tags).toStrictEqual({});
157
+ });
158
+
122
159
  it('should not set meta on debug', () => {
123
160
  app.debug = true;
124
161
  const handler = {
@@ -133,6 +170,7 @@ describe('MiddlewareGroup: test normalizeHttpRequestBeforeHandler', () => {
133
170
  },
134
171
  },
135
172
  };
173
+
136
174
  normalizeHttpRequestBeforeHandler(handler, () => {});
137
175
  expect(logger.meta.auth).toBe(handler.event.auth);
138
176
  expect(logger.meta.queryStringParameters).toStrictEqual(
@@ -1,6 +1,8 @@
1
1
  import logger from '../utils/logger';
2
2
  import isEmpty from '../utils/isEmpty';
3
3
 
4
+ const FILE = 'Lesgo/middlewares/errorHttpResponseMiddleware';
5
+
4
6
  export const errorHttpResponseHandler = async opts => {
5
7
  const defaults = {
6
8
  response: '',
@@ -29,7 +31,7 @@ export const errorHttpResponseHandler = async opts => {
29
31
  status: 'error',
30
32
  data: null,
31
33
  error: {
32
- code: options.error.code || 'UNKNOWN_ERROR',
34
+ code: options.error.code || 'UNHANDLED_ERROR',
33
35
  message: options.error.name
34
36
  ? `${options.error.name}: ${options.error.message}`
35
37
  : options.error.message || options.error,
@@ -40,18 +42,18 @@ export const errorHttpResponseHandler = async opts => {
40
42
 
41
43
  const statusCode = options.error.statusCode || options.statusCode;
42
44
 
43
- /* istanbul ignore next */
44
- if (statusCode === 500) {
45
- // this is likely an unhandled exception, log it
46
- logger.error(options.error);
45
+ if (!isEmpty(options.error)) {
46
+ logger.log(statusCode === 500 ? 'error' : 'warn', options.error);
47
47
  } else {
48
- logger.warn(options.error);
48
+ logger.log(statusCode === 500 ? 'error' : 'warn', jsonBody.error.message, {
49
+ error: jsonBody.error,
50
+ });
49
51
  }
50
52
 
51
53
  try {
52
54
  if (!isEmpty(opts.db)) await opts.db.end();
53
55
  } catch (err) {
54
- // do nothing
56
+ logger.error(`${FILE}::Failed to end db connection`, err);
55
57
  }
56
58
 
57
59
  return {
@@ -53,14 +53,26 @@ export const normalizeHttpRequestBeforeHandler = (handler, next) => {
53
53
  // eslint-disable-next-line no-param-reassign
54
54
  handler.event.auth = auth;
55
55
 
56
+ const tags = {};
57
+ switch (handler.event.version) {
58
+ case '2.0': {
59
+ if (handler.event.requestContext && handler.event.requestContext.http) {
60
+ tags.path = handler.event.requestContext.http.path;
61
+ tags.httpMethod = handler.event.requestContext.http.method;
62
+ }
63
+ break;
64
+ }
65
+ default:
66
+ tags.path = handler.event.path;
67
+ tags.httpMethod = handler.event.httpMethod;
68
+ break;
69
+ }
70
+
56
71
  logger.addMeta({
57
72
  requestId: handler.event.requestContext
58
73
  ? handler.event.requestContext.requestId
59
74
  : null,
60
- tags: {
61
- path: handler.event.path,
62
- httpMethod: handler.event.httpMethod,
63
- },
75
+ tags,
64
76
  });
65
77
 
66
78
  if (app.debug) {
@@ -38,19 +38,18 @@ class ElasticsearchService {
38
38
  return this.client;
39
39
  }
40
40
 
41
- search(body) {
41
+ async search(body) {
42
42
  const param = {
43
43
  index: this.index,
44
44
  type: this.type,
45
45
  body,
46
46
  };
47
47
 
48
- return this.client.search(param, (err, response) => {
49
- if (err) return Promise.reject(err);
48
+ const response = await this.client.search(param);
50
49
 
51
- this.result = response;
52
- return Promise.resolve(response);
53
- });
50
+ this.result = response;
51
+
52
+ return response;
54
53
  }
55
54
 
56
55
  /**
@@ -70,10 +69,7 @@ class ElasticsearchService {
70
69
  body: settings,
71
70
  };
72
71
 
73
- return this.client.indices.create(params, (err, response) => {
74
- if (err) return Promise.reject(err);
75
- return Promise.resolve(response);
76
- });
72
+ return this.client.indices.create(params);
77
73
  }
78
74
 
79
75
  deleteIndices(index, options = {}) {
@@ -82,28 +78,20 @@ class ElasticsearchService {
82
78
  ...options,
83
79
  };
84
80
 
85
- return this.client.indices.delete(params, (err, response) => {
86
- if (err) return Promise.reject(err);
87
- return Promise.resolve(response);
88
- });
81
+ return this.client.indices.delete(params);
89
82
  }
90
83
 
91
- existIndices(index, options = {}) {
84
+ async existIndices(index, options = {}) {
92
85
  const params = { index, ...options };
93
86
 
94
- return this.client.indices.exists(params, (err, response) => {
95
- if (err) return Promise.reject(err);
96
- return Promise.resolve(response.body);
97
- });
87
+ const response = await this.client.indices.exists(params);
88
+ return response.body;
98
89
  }
99
90
 
100
91
  putMapping(index, type, body) {
101
92
  const params = { index, type, body: { properties: body } };
102
93
 
103
- return this.client.indices.putMapping(params, (err, response) => {
104
- if (err) return Promise.reject(err);
105
- return Promise.resolve(response);
106
- });
94
+ return this.client.indices.putMapping(params);
107
95
  }
108
96
 
109
97
  get(id) {
@@ -113,10 +101,18 @@ class ElasticsearchService {
113
101
  id,
114
102
  };
115
103
 
116
- return this.client.get(params, (err, response) => {
117
- if (err) return Promise.reject(err);
118
- return Promise.resolve(response);
119
- });
104
+ return this.client.get(params);
105
+ }
106
+
107
+ /**
108
+ * Multi search API
109
+ *
110
+ * Executes several searches with a single API request.
111
+ *
112
+ * @see https://www.elastic.co/guide/en/elasticsearch/reference/7.16/search-multi-search.html
113
+ */
114
+ msearch(body) {
115
+ return this.client.msearch({ body });
120
116
  }
121
117
 
122
118
  indexOrCreateById(body, refresh = false) {
@@ -128,20 +124,11 @@ class ElasticsearchService {
128
124
  refresh,
129
125
  };
130
126
 
131
- return this.client.index(params, (err, response) => {
132
- if (err) return Promise.reject(err);
133
- return Promise.resolve(response);
134
- });
127
+ return this.client.index(params);
135
128
  }
136
129
 
137
130
  bulkIndex(bodies) {
138
- return this.client.bulk(
139
- { body: this.constructBulkIndex(bodies) },
140
- (err, response) => {
141
- if (err) return Promise.reject(err);
142
- return Promise.resolve(response);
143
- }
144
- );
131
+ return this.client.bulk({ body: this.constructBulkIndex(bodies) });
145
132
  }
146
133
 
147
134
  create(id, body) {
@@ -152,10 +139,7 @@ class ElasticsearchService {
152
139
  body,
153
140
  };
154
141
 
155
- return this.client.index(params, (err, response) => {
156
- if (err) return Promise.reject(err);
157
- return Promise.resolve(response);
158
- });
142
+ return this.client.index(params);
159
143
  }
160
144
 
161
145
  updateById(id) {
@@ -165,10 +149,7 @@ class ElasticsearchService {
165
149
  id,
166
150
  };
167
151
 
168
- return this.client.get(params, (err, response) => {
169
- if (err) return Promise.reject(err);
170
- return Promise.resolve(response);
171
- });
152
+ return this.client.get(params);
172
153
  }
173
154
 
174
155
  /**