@open-norantec/herbal 1.0.2-alpha.5 → 1.0.2-alpha.7

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.
@@ -77,8 +77,8 @@ require("reflect-metadata");
77
77
  var common_1 = require("@nestjs/common");
78
78
  var _ = require("lodash");
79
79
  var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
80
+ var uuid_util_class_1 = require("@open-norantec/utilities/dist/uuid-util.class");
80
81
  var common_2 = require("@nestjs/common");
81
- var uuid_1 = require("uuid");
82
82
  var headers_constant_1 = require("../constants/headers.constant");
83
83
  var core_1 = require("@nestjs/core");
84
84
  var auth_adapter_decorator_1 = require("../decorators/auth-adapter.decorator");
@@ -136,6 +136,7 @@ var ControllerInterceptor = (function () {
136
136
  _.attempt(function () {
137
137
  _this.getLogger().error("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:error:stack] ").concat(error === null || error === void 0 ? void 0 : error.stack));
138
138
  });
139
+ _this.getLogger().error("Got error when handling route in interceptor: ".concat(error === null || error === void 0 ? void 0 : error.message, " ").concat(error === null || error === void 0 ? void 0 : error.stack));
139
140
  _.attempt(function () { var _a, _b, _c, _d; return (_d = (_c = (_b = (_a = request === null || request === void 0 ? void 0 : request.transaction) === null || _a === void 0 ? void 0 : _a.rollback) === null || _b === void 0 ? void 0 : _b.call(_a)) === null || _c === void 0 ? void 0 : _c.catch) === null || _d === void 0 ? void 0 : _d.call(_c, function () { }); });
140
141
  return (0, rxjs_1.throwError)(function () { return error; });
141
142
  }));
@@ -153,62 +154,64 @@ function HerbalGuard(options) {
153
154
  }
154
155
  HerbalGuardMixin.prototype.canActivate = function (context) {
155
156
  var _a, e_1, _b, _c;
156
- var _d, _e, _f, _g, _h;
157
+ var _d, _e, _f, _g, _h, _j, _k;
157
158
  return __awaiter(this, void 0, void 0, function () {
158
- var sequelizeInstance, transaction, request, response, traceId, chunks, _j, request_1, request_1_1, chunk, e_1_1, _k, parsedBody, authAdapters, _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult, error_1, _l;
159
+ var sequelizeInstance, transaction, request, response, traceId, chunks, _l, request_1, request_1_1, chunk, e_1_1, _m, parsedBody, handlerName, authAdapters, _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult, error_1, _o;
159
160
  var _this = this;
160
- return __generator(this, function (_m) {
161
- switch (_m.label) {
161
+ return __generator(this, function (_p) {
162
+ switch (_p.label) {
162
163
  case 0:
163
164
  sequelizeInstance = _.attempt(function () { return _this.ref.get(sequelize_typescript_1.Sequelize, { strict: false }); });
164
165
  transaction = undefined;
165
166
  if (!!(sequelizeInstance instanceof Error)) return [3, 2];
166
167
  return [4, ((_e = (_d = sequelizeInstance === null || sequelizeInstance === void 0 ? void 0 : sequelizeInstance.transaction) === null || _d === void 0 ? void 0 : _d.call(sequelizeInstance)) === null || _e === void 0 ? void 0 : _e.catch(function () { return Promise.resolve(undefined); }))];
167
168
  case 1:
168
- transaction = _m.sent();
169
- _m.label = 2;
169
+ transaction = _p.sent();
170
+ _p.label = 2;
170
171
  case 2:
171
172
  request = context.switchToHttp().getRequest();
172
173
  response = context.switchToHttp().getResponse();
173
- traceId = typeof (options === null || options === void 0 ? void 0 : options.getTraceId) === 'function' ? _.attempt(function () { return options.getTraceId(request); }) : (0, uuid_1.v4)();
174
- if (traceId instanceof Error)
175
- traceId = (0, uuid_1.v4)();
174
+ traceId = typeof (options === null || options === void 0 ? void 0 : options.getTraceId) === 'function'
175
+ ? _.attempt(function () { return options.getTraceId(request); })
176
+ : uuid_util_class_1.UUIDUtil.generateV4();
177
+ if (traceId instanceof Error || string_util_class_1.StringUtil.isFalsyString(traceId))
178
+ traceId = uuid_util_class_1.UUIDUtil.generateV4();
176
179
  request.traceId = traceId;
177
180
  request.methodName = request.url.split('/').pop();
178
181
  request.transaction = transaction;
179
182
  response.setHeader(headers_constant_1.HEADERS.TRACE_ID, traceId);
180
183
  chunks = [];
181
- _m.label = 3;
184
+ _p.label = 3;
182
185
  case 3:
183
- _m.trys.push([3, 16, , 17]);
184
- _m.label = 4;
186
+ _p.trys.push([3, 16, , 17]);
187
+ _p.label = 4;
185
188
  case 4:
186
- _m.trys.push([4, 9, 10, 15]);
187
- _j = true, request_1 = __asyncValues(request);
188
- _m.label = 5;
189
+ _p.trys.push([4, 9, 10, 15]);
190
+ _l = true, request_1 = __asyncValues(request);
191
+ _p.label = 5;
189
192
  case 5: return [4, request_1.next()];
190
193
  case 6:
191
- if (!(request_1_1 = _m.sent(), _a = request_1_1.done, !_a)) return [3, 8];
194
+ if (!(request_1_1 = _p.sent(), _a = request_1_1.done, !_a)) return [3, 8];
192
195
  _c = request_1_1.value;
193
- _j = false;
196
+ _l = false;
194
197
  chunk = _c;
195
198
  chunks.push(chunk);
196
- _m.label = 7;
199
+ _p.label = 7;
197
200
  case 7:
198
- _j = true;
201
+ _l = true;
199
202
  return [3, 5];
200
203
  case 8: return [3, 15];
201
204
  case 9:
202
- e_1_1 = _m.sent();
205
+ e_1_1 = _p.sent();
203
206
  e_1 = { error: e_1_1 };
204
207
  return [3, 15];
205
208
  case 10:
206
- _m.trys.push([10, , 13, 14]);
207
- if (!(!_j && !_a && (_b = request_1.return))) return [3, 12];
209
+ _p.trys.push([10, , 13, 14]);
210
+ if (!(!_l && !_a && (_b = request_1.return))) return [3, 12];
208
211
  return [4, _b.call(request_1)];
209
212
  case 11:
210
- _m.sent();
211
- _m.label = 12;
213
+ _p.sent();
214
+ _p.label = 12;
212
215
  case 12: return [3, 14];
213
216
  case 13:
214
217
  if (e_1) throw e_1.error;
@@ -216,7 +219,7 @@ function HerbalGuard(options) {
216
219
  case 14: return [7];
217
220
  case 15: return [3, 17];
218
221
  case 16:
219
- _k = _m.sent();
222
+ _m = _p.sent();
220
223
  return [3, 17];
221
224
  case 17:
222
225
  parsedBody = _.attempt(function () { return Buffer.concat(chunks).toString('utf8'); });
@@ -227,13 +230,14 @@ function HerbalGuard(options) {
227
230
  request.rawBody = null;
228
231
  }
229
232
  _.attempt(function () { return _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":request:body] ").concat(request.rawBody)); });
230
- authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters((_g = (_f = context === null || context === void 0 ? void 0 : context.getClass) === null || _f === void 0 ? void 0 : _f.call(context)) === null || _g === void 0 ? void 0 : _g.prototype, request.methodName);
231
- _m.label = 18;
233
+ handlerName = (_g = (_f = context === null || context === void 0 ? void 0 : context.getHandler) === null || _f === void 0 ? void 0 : _f.call(context)) === null || _g === void 0 ? void 0 : _g.name;
234
+ authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters((_j = (_h = context === null || context === void 0 ? void 0 : context.getClass) === null || _h === void 0 ? void 0 : _h.call(context)) === null || _j === void 0 ? void 0 : _j.prototype, string_util_class_1.StringUtil.isFalsyString(handlerName) ? request.methodName : handlerName);
235
+ _p.label = 18;
232
236
  case 18:
233
- _m.trys.push([18, 24, , 29]);
237
+ _p.trys.push([18, 24, , 29]);
234
238
  if (!(Array.isArray(authAdapters) && authAdapters.length > 0)) return [3, 23];
235
239
  _i = 0, authAdapters_1 = authAdapters;
236
- _m.label = 19;
240
+ _p.label = 19;
237
241
  case 19:
238
242
  if (!(_i < authAdapters_1.length)) return [3, 22];
239
243
  AuthAdapterClass = authAdapters_1[_i];
@@ -242,7 +246,7 @@ function HerbalGuard(options) {
242
246
  return [3, 21];
243
247
  return [4, adapter.authenticate(transaction)];
244
248
  case 20:
245
- authenticateResult = _m.sent();
249
+ authenticateResult = _p.sent();
246
250
  if (!authenticateResult)
247
251
  return [3, 22];
248
252
  request.authenticateResult = __assign({ AuthenticatorClass: AuthAdapterClass }, authenticateResult);
@@ -253,16 +257,17 @@ function HerbalGuard(options) {
253
257
  case 22: throw new common_2.UnauthorizedException();
254
258
  case 23: return [3, 29];
255
259
  case 24:
256
- error_1 = _m.sent();
257
- _m.label = 25;
260
+ error_1 = _p.sent();
261
+ _p.label = 25;
258
262
  case 25:
259
- _m.trys.push([25, 27, , 28]);
260
- return [4, ((_h = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _h === void 0 ? void 0 : _h.call(transaction))];
263
+ _p.trys.push([25, 27, , 28]);
264
+ this.getLogger().error("Got error when handling route: ".concat(error_1 === null || error_1 === void 0 ? void 0 : error_1.message, " ").concat(error_1 === null || error_1 === void 0 ? void 0 : error_1.stack));
265
+ return [4, ((_k = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _k === void 0 ? void 0 : _k.call(transaction))];
261
266
  case 26:
262
- _m.sent();
267
+ _p.sent();
263
268
  return [3, 28];
264
269
  case 27:
265
- _l = _m.sent();
270
+ _o = _p.sent();
266
271
  return [3, 28];
267
272
  case 28: throw error_1;
268
273
  case 29: return [2, true];
@@ -294,12 +299,13 @@ var ControllerUtil = (function () {
294
299
  ControllerUtil.create = function (createOptions) {
295
300
  function Controller(options) {
296
301
  return function (target) {
302
+ var _a;
297
303
  var finalPrefix = string_util_class_1.StringUtil.isFalsyString(options === null || options === void 0 ? void 0 : options.prefix)
298
304
  ? string_util_class_1.StringUtil.isFalsyString(createOptions === null || createOptions === void 0 ? void 0 : createOptions.prefix)
299
305
  ? ''
300
306
  : createOptions.prefix
301
307
  : options.prefix;
302
- finalPrefix += "/".concat(_.camelCase(target.name.replace(/Controller$/g, '')));
308
+ finalPrefix += "".concat(((_a = finalPrefix === null || finalPrefix === void 0 ? void 0 : finalPrefix.endsWith) === null || _a === void 0 ? void 0 : _a.call(finalPrefix, '/')) ? '' : '/').concat(_.camelCase(target.name.replace(/Controller$/g, '')));
303
309
  if (!finalPrefix.startsWith('/'))
304
310
  finalPrefix = "/".concat(finalPrefix);
305
311
  Reflect.defineMetadata(IS_CONTROLLER, true, target.prototype);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/herbal",
3
- "version": "1.0.2-alpha.5",
3
+ "version": "1.0.2-alpha.7",
4
4
  "description": "Herbal is a builder and toolchain for Nest.js applications",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {