@open-norantec/herbal 2.0.0-alpha.4 → 2.0.0-alpha.5

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/dist/core.d.ts CHANGED
@@ -51,14 +51,15 @@ export declare function getControllerName(target: Function): any;
51
51
  export interface HerbalControllerOptions<C> {
52
52
  ignoreControllerNamePostfix?: boolean;
53
53
  prefix?: string;
54
- useHeadGuards?: Constructor<any>[];
55
- useTailGuards?: Constructor<any>[];
56
54
  methods?: (register: MethodRegisterFn<C>) => void;
55
+ onAfterParsingRequest?: (request: Request, moduleRef: ModuleRef) => void | Promise<void>;
56
+ onBeforeParsingRequest?: (request: Request, moduleRef: ModuleRef) => void | Promise<void>;
57
57
  }
58
58
  export interface ControllerUtilCreateOptions {
59
59
  prefix?: string;
60
- useGuards?: Constructor<any>[];
61
60
  getTraceId?: (request: ExpressRequest) => string;
61
+ onAfterParsingRequest?: HerbalControllerOptions<any>['onAfterParsingRequest'];
62
+ onBeforeParsingRequest?: HerbalControllerOptions<any>['onBeforeParsingRequest'];
62
63
  }
63
64
  export declare function parseRequest({ request, traceId: inputTraceId, sequelize: sequelizeInstance, moduleRef, handlerName, handlerPrototype, onLog, }: {
64
65
  handlerPrototype: object;
package/dist/core.js CHANGED
@@ -76,15 +76,6 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
76
76
  function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
77
77
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
78
78
  };
79
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
80
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
81
- if (ar || !(i in from)) {
82
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
83
- ar[i] = from[i];
84
- }
85
- }
86
- return to.concat(ar || Array.prototype.slice.call(from));
87
- };
88
79
  Object.defineProperty(exports, "__esModule", { value: true });
89
80
  exports.getRequestHandler = exports.ControllerUtil = exports.getMethodPool = exports.parseRequest = exports.getControllerName = exports.isHerbalController = void 0;
90
81
  require("reflect-metadata");
@@ -241,6 +232,8 @@ var MethodPool = (function () {
241
232
  }());
242
233
  var IS_HERBAL_CONTROLLER = Symbol();
243
234
  var CONTROLLER_NAME = Symbol();
235
+ var AFTER_PARSING_REQUEST_HANDLERS = Symbol();
236
+ var BEFORE_PARSING_REQUEST_HANDLERS = Symbol();
244
237
  function isHerbalController(target) {
245
238
  return _.attempt(function () { return Reflect.getMetadata(IS_HERBAL_CONTROLLER, target.prototype); }) === true;
246
239
  }
@@ -311,12 +304,14 @@ function parseRequest(_a) {
311
304
  var _f, _g, _h, _j, _k;
312
305
  var request = _a.request, inputTraceId = _a.traceId, sequelizeInstance = _a.sequelize, moduleRef = _a.moduleRef, handlerName = _a.handlerName, handlerPrototype = _a.handlerPrototype, onLog = _a.onLog;
313
306
  return __awaiter(this, void 0, void 0, function () {
314
- var transaction, chunks_1, chunk, e_1_1, _l, parsedBody, methodPool, authAdapters, shouldHaveTransaction, error_2, authSucceeded, error_3, _m;
307
+ var transaction, beforeParsingRequestHandlers, afterParsingRequestHandlers, chunks_1, chunk, e_1_1, _l, parsedBody, methodPool, authAdapters, shouldHaveTransaction, error_2, authSucceeded, error_3, _m;
315
308
  var _this = this;
316
309
  return __generator(this, function (_o) {
317
310
  switch (_o.label) {
318
311
  case 0:
319
312
  transaction = undefined;
313
+ beforeParsingRequestHandlers = Reflect.getMetadata(BEFORE_PARSING_REQUEST_HANDLERS, handlerPrototype);
314
+ afterParsingRequestHandlers = Reflect.getMetadata(AFTER_PARSING_REQUEST_HANDLERS, handlerPrototype);
320
315
  request.traceId = string_util_class_1.StringUtil.isFalsyString(inputTraceId) ? uuid_util_class_1.UUIDUtil.generateV4() : inputTraceId;
321
316
  request.methodName = request.url.split('/').pop();
322
317
  if (!(typeof request.rawBody === 'undefined')) return [3, 16];
@@ -372,39 +367,44 @@ function parseRequest(_a) {
372
367
  _o.label = 16;
373
368
  case 16:
374
369
  _.attempt(function () { return onLog === null || onLog === void 0 ? void 0 : onLog('log', "[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":request:body] ").concat(request.rawBody)); });
370
+ return [4, Promise.all((Array.isArray(beforeParsingRequestHandlers) ? beforeParsingRequestHandlers : []).map(function (handler) {
371
+ return handler(request, moduleRef);
372
+ }))];
373
+ case 17:
374
+ _o.sent();
375
375
  methodPool = getMethodPool(handlerPrototype);
376
376
  authAdapters = (_f = methodPool === null || methodPool === void 0 ? void 0 : methodPool.getAuthAdapters) === null || _f === void 0 ? void 0 : _f.call(methodPool, request.methodName);
377
377
  if (authAdapters === null)
378
378
  authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPrototype, handlerName);
379
379
  shouldHaveTransaction = !decorators_1.NoTransaction.isDisabled(handlerPrototype, handlerName) && !((_g = methodPool === null || methodPool === void 0 ? void 0 : methodPool.transactionDisabled) === null || _g === void 0 ? void 0 : _g.call(methodPool, request.methodName));
380
- if (!(sequelizeInstance instanceof sequelize_typescript_1.Sequelize && shouldHaveTransaction)) return [3, 21];
381
- _o.label = 17;
382
- case 17:
383
- _o.trys.push([17, 19, , 20]);
384
- return [4, ((_j = (_h = sequelizeInstance === null || sequelizeInstance === void 0 ? void 0 : sequelizeInstance.transaction) === null || _h === void 0 ? void 0 : _h.call(sequelizeInstance)) === null || _j === void 0 ? void 0 : _j.catch(function () { return Promise.resolve(undefined); }))];
380
+ if (!(sequelizeInstance instanceof sequelize_typescript_1.Sequelize && shouldHaveTransaction)) return [3, 22];
381
+ _o.label = 18;
385
382
  case 18:
383
+ _o.trys.push([18, 20, , 21]);
384
+ return [4, ((_j = (_h = sequelizeInstance === null || sequelizeInstance === void 0 ? void 0 : sequelizeInstance.transaction) === null || _h === void 0 ? void 0 : _h.call(sequelizeInstance)) === null || _j === void 0 ? void 0 : _j.catch(function () { return Promise.resolve(undefined); }))];
385
+ case 19:
386
386
  transaction = _o.sent();
387
387
  request.transaction = transaction;
388
388
  onLog === null || onLog === void 0 ? void 0 : onLog('log', "[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Started transaction for route: ").concat(request.url));
389
- return [3, 20];
390
- case 19:
389
+ return [3, 21];
390
+ case 20:
391
391
  error_2 = _o.sent();
392
392
  if (error_2 instanceof Error) {
393
393
  onLog === null || onLog === void 0 ? void 0 : onLog('error', "[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Failed to start transaction: ").concat(error_2 === null || error_2 === void 0 ? void 0 : error_2.message, "\n").concat(error_2 === null || error_2 === void 0 ? void 0 : error_2.stack));
394
394
  }
395
- return [3, 20];
396
- case 20: return [3, 22];
397
- case 21:
395
+ return [3, 21];
396
+ case 21: return [3, 23];
397
+ case 22:
398
398
  if (!shouldHaveTransaction) {
399
399
  onLog === null || onLog === void 0 ? void 0 : onLog('log', "[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Transaction is disabled for this route: ").concat(request.url));
400
400
  }
401
401
  else {
402
402
  throw new Error('Sequelize instance not found, cannot start transaction');
403
403
  }
404
- _o.label = 22;
405
- case 22:
406
- _o.trys.push([22, 25, , 30]);
407
- if (!(Array.isArray(authAdapters) && authAdapters.length > 0 && typeof request.authenticateResult === 'undefined')) return [3, 24];
404
+ _o.label = 23;
405
+ case 23:
406
+ _o.trys.push([23, 26, 31, 33]);
407
+ if (!(Array.isArray(authAdapters) && authAdapters.length > 0 && typeof request.authenticateResult === 'undefined')) return [3, 25];
408
408
  return [4, (function () { return __awaiter(_this, void 0, void 0, function () {
409
409
  var _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult;
410
410
  return __generator(this, function (_a) {
@@ -432,29 +432,35 @@ function parseRequest(_a) {
432
432
  }
433
433
  });
434
434
  }); })()];
435
- case 23:
435
+ case 24:
436
436
  authSucceeded = _o.sent();
437
437
  if (!authSucceeded)
438
438
  throw new common_3.UnauthorizedException();
439
- _o.label = 24;
440
- case 24: return [3, 30];
441
- case 25:
442
- error_3 = _o.sent();
443
- _o.label = 26;
439
+ _o.label = 25;
440
+ case 25: return [3, 33];
444
441
  case 26:
445
- _o.trys.push([26, 28, , 29]);
442
+ error_3 = _o.sent();
443
+ _o.label = 27;
444
+ case 27:
445
+ _o.trys.push([27, 29, , 30]);
446
446
  if (error_3 instanceof Error) {
447
447
  onLog === null || onLog === void 0 ? void 0 : onLog('error', "[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":error] Got error when handling route: ").concat(error_3 === null || error_3 === void 0 ? void 0 : error_3.message, " ").concat(error_3 === null || error_3 === void 0 ? void 0 : error_3.stack));
448
448
  }
449
449
  return [4, ((_k = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _k === void 0 ? void 0 : _k.call(transaction))];
450
- case 27:
451
- _o.sent();
452
- return [3, 29];
453
450
  case 28:
451
+ _o.sent();
452
+ return [3, 30];
453
+ case 29:
454
454
  _m = _o.sent();
455
- return [3, 29];
456
- case 29: throw error_3;
457
- case 30: return [2];
455
+ return [3, 30];
456
+ case 30: throw error_3;
457
+ case 31: return [4, Promise.all((Array.isArray(afterParsingRequestHandlers) ? afterParsingRequestHandlers : []).map(function (handler) {
458
+ return handler(request, moduleRef);
459
+ }))];
460
+ case 32:
461
+ _o.sent();
462
+ return [7];
463
+ case 33: return [2];
458
464
  }
459
465
  });
460
466
  });
@@ -632,9 +638,21 @@ var ControllerUtil = (function () {
632
638
  logger.log("Mapped method: /".concat(controllerName).concat(path));
633
639
  });
634
640
  }
641
+ var beforeParsingRequestHandlers = [];
642
+ var afterParsingRequestHandlers = [];
643
+ if (typeof (createOptions === null || createOptions === void 0 ? void 0 : createOptions.onBeforeParsingRequest) === 'function')
644
+ beforeParsingRequestHandlers.push(createOptions.onBeforeParsingRequest);
645
+ if (typeof (options === null || options === void 0 ? void 0 : options.onBeforeParsingRequest) === 'function')
646
+ beforeParsingRequestHandlers.push(options.onBeforeParsingRequest);
647
+ if (typeof (options === null || options === void 0 ? void 0 : options.onAfterParsingRequest) === 'function')
648
+ afterParsingRequestHandlers.push(options.onAfterParsingRequest);
649
+ if (typeof (createOptions === null || createOptions === void 0 ? void 0 : createOptions.onAfterParsingRequest) === 'function')
650
+ afterParsingRequestHandlers.push(createOptions.onAfterParsingRequest);
651
+ Reflect.defineMetadata(BEFORE_PARSING_REQUEST_HANDLERS, beforeParsingRequestHandlers, target.prototype);
652
+ Reflect.defineMetadata(AFTER_PARSING_REQUEST_HANDLERS, afterParsingRequestHandlers, target.prototype);
635
653
  (0, common_2.Controller)(finalPrefix)(target);
636
654
  (0, common_2.UseInterceptors)(ControllerInterceptor)(target);
637
- common_2.UseGuards.apply(void 0, __spreadArray(__spreadArray(__spreadArray([HerbalGuard(_.pick(createOptions, ['getTraceId']))], (Array.isArray(options === null || options === void 0 ? void 0 : options.useHeadGuards) ? options.useHeadGuards : []), false), (Array.isArray(createOptions === null || createOptions === void 0 ? void 0 : createOptions.useGuards) ? createOptions.useGuards : []), false), (Array.isArray(options === null || options === void 0 ? void 0 : options.useTailGuards) ? options.useTailGuards : []), false))(target);
655
+ (0, common_2.UseGuards)(HerbalGuard(_.pick(createOptions, ['getTraceId'])))(target);
638
656
  };
639
657
  }
640
658
  Controller.isHerbalController = isHerbalController;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/herbal",
3
- "version": "2.0.0-alpha.4",
3
+ "version": "2.0.0-alpha.5",
4
4
  "description": "Herbal is a builder and toolchain for Nest.js applications",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {