@open-norantec/herbal 1.0.2-alpha.35 → 1.0.2-alpha.37

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
@@ -8,14 +8,10 @@ import { Constructor } from 'type-fest';
8
8
  import { AuthAdapter } from './abstracts/auth-adapter.abstract.class';
9
9
  import { PathsObject } from 'zod-openapi/dist/openapi3-ts/dist/model/openapi31';
10
10
  export * from '@nestjs/core';
11
- declare const HANDLE_REQUEST_SYMBOL: unique symbol;
12
11
  export type MethodHandler<IS extends z.Schema<any>, OS extends z.Schema<any>> = (request: Request, input: unknown, headers: ReturnType<typeof HeaderUtil.parse>) => Promise<{
13
12
  request: z.infer<IS>;
14
13
  response: z.infer<OS>;
15
14
  }>;
16
- export declare class HerbalController {
17
- private [HANDLE_REQUEST_SYMBOL];
18
- }
19
15
  type ClientGroups = Array<string> | null | undefined;
20
16
  type ClienttGroupsFactory = (defaultGroupName: string) => ClientGroups;
21
17
  export interface MethodRegisterOptions<IS extends z.Schema<any>, OS extends z.Schema<any>> {
@@ -43,6 +39,7 @@ declare class MethodConfig<IS extends z.Schema<any>, OS extends z.Schema<any>, C
43
39
  declare class MethodPool {
44
40
  protected readonly methods: Map<string, MethodConfig<any, any, any>>;
45
41
  registerMethod<IS extends z.Schema<any>, OS extends z.Schema<any>, C>(name: string, options: MethodRegisterOptions<IS, OS>, callback: MethodCallback<IS, OS, C>): void;
42
+ transactionDisabled(name: string): boolean | undefined;
46
43
  getCallFn(name: string): ((controller: any, callContext: MethodCallContext<any>) => Promise<any>) | null;
47
44
  getAuthAdapters(name: string): Constructor<AuthAdapter>[] | null | undefined;
48
45
  getOpenAPIPathsObject(group?: string): PathsObject;
package/dist/core.js CHANGED
@@ -86,7 +86,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
86
86
  return to.concat(ar || Array.prototype.slice.call(from));
87
87
  };
88
88
  Object.defineProperty(exports, "__esModule", { value: true });
89
- exports.ControllerUtil = exports.getMethodPool = exports.getControllerName = exports.isHerbalController = exports.HerbalController = void 0;
89
+ exports.ControllerUtil = exports.getMethodPool = exports.getControllerName = exports.isHerbalController = void 0;
90
90
  require("reflect-metadata");
91
91
  var common_1 = require("@nestjs/common");
92
92
  var header_util_class_1 = require("@open-norantec/utilities/dist/header-util.class");
@@ -108,70 +108,7 @@ var sequelize_typescript_1 = require("sequelize-typescript");
108
108
  var decorators_1 = require("./decorators");
109
109
  var zod_openapi_1 = require("zod-openapi");
110
110
  __exportStar(require("@nestjs/core"), exports);
111
- var HANDLE_REQUEST_SYMBOL = Symbol();
112
111
  var HANDLE_REQUEST_INSTANCE_SYMBOL = '$handleRequestInstance';
113
- var HerbalController = (function () {
114
- function HerbalController() {
115
- }
116
- HerbalController.prototype[HANDLE_REQUEST_SYMBOL] = function (request) {
117
- var _a, _b, _c, _d, _e, _f;
118
- return __awaiter(this, void 0, void 0, function () {
119
- var callFn, result, error_1;
120
- var _g;
121
- var _this = this;
122
- return __generator(this, function (_h) {
123
- switch (_h.label) {
124
- case 0:
125
- callFn = (_b = (_a = getMethodPool(this)) === null || _a === void 0 ? void 0 : _a.getCallFn) === null || _b === void 0 ? void 0 : _b.call(_a, request.methodName);
126
- if (typeof callFn !== 'function')
127
- throw new common_1.NotFoundException("Method ".concat(request.methodName, " not found"));
128
- _h.label = 1;
129
- case 1:
130
- _h.trys.push([1, 4, , 6]);
131
- _g = {};
132
- return [4, callFn(this, {
133
- authenticateResult: request.authenticateResult,
134
- headers: header_util_class_1.HeaderUtil.parse((_c = request.headers) !== null && _c !== void 0 ? _c : {}),
135
- methodName: request.methodName,
136
- rawBody: request.rawBody,
137
- traceId: request.traceId,
138
- transaction: request.transaction,
139
- url: request.originalUrl,
140
- })];
141
- case 2:
142
- result = (_g.data = _h.sent(),
143
- _g.token = string_util_class_1.StringUtil.isFalsyString((_d = request === null || request === void 0 ? void 0 : request.authenticateResult) === null || _d === void 0 ? void 0 : _d.nextToken)
144
- ? null
145
- : request.authenticateResult.nextToken,
146
- _g);
147
- return [4, ((_f = (_e = request === null || request === void 0 ? void 0 : request.transaction) === null || _e === void 0 ? void 0 : _e.commit) === null || _f === void 0 ? void 0 : _f.call(_e))];
148
- case 3:
149
- _h.sent();
150
- return [2, result];
151
- case 4:
152
- error_1 = _h.sent();
153
- return [4, utilities_1.AttemptUtil.execPromise((function () { return __awaiter(_this, void 0, void 0, function () {
154
- var _a, _b;
155
- return __generator(this, function (_c) {
156
- switch (_c.label) {
157
- case 0: return [4, ((_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))];
158
- case 1:
159
- _c.sent();
160
- return [2];
161
- }
162
- });
163
- }); })())];
164
- case 5:
165
- _h.sent();
166
- throw error_1;
167
- case 6: return [2];
168
- }
169
- });
170
- });
171
- };
172
- return HerbalController;
173
- }());
174
- exports.HerbalController = HerbalController;
175
112
  var METHOD_POOL = Symbol();
176
113
  var MethodConfig = (function () {
177
114
  function MethodConfig(name, options, callback) {
@@ -182,7 +119,7 @@ var MethodConfig = (function () {
182
119
  MethodConfig.prototype.call = function (controller, callContext) {
183
120
  var _a, _b, _c, _d, _e, _f;
184
121
  return __awaiter(this, void 0, void 0, function () {
185
- var inputSchema, outputSchema, parsedBody_1, input, rawResponse_1, response, error_2;
122
+ var inputSchema, outputSchema, parsedBody_1, input, rawResponse_1, response, error_1;
186
123
  return __generator(this, function (_g) {
187
124
  switch (_g.label) {
188
125
  case 0:
@@ -215,8 +152,8 @@ var MethodConfig = (function () {
215
152
  throw response;
216
153
  return [2, response];
217
154
  case 3:
218
- error_2 = _g.sent();
219
- throw error_2;
155
+ error_1 = _g.sent();
156
+ throw error_1;
220
157
  case 4: return [2];
221
158
  }
222
159
  });
@@ -235,6 +172,14 @@ var MethodPool = (function () {
235
172
  throw new Error("Method name cannot contain slashes: ".concat(name));
236
173
  this.methods.set(name, new MethodConfig(name, options, callback));
237
174
  };
175
+ MethodPool.prototype.transactionDisabled = function (name) {
176
+ var _a, _b;
177
+ if (string_util_class_1.StringUtil.isFalsyString(name))
178
+ return;
179
+ if (name.includes('/'))
180
+ throw new Error("Method name cannot contain slashes: ".concat(name));
181
+ return !!((_b = (_a = this.methods.get(name)) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.disableTransaction);
182
+ };
238
183
  MethodPool.prototype.getCallFn = function (name) {
239
184
  var config = this.methods.get(name);
240
185
  if (!(config instanceof MethodConfig))
@@ -365,7 +310,7 @@ function HerbalGuard(options) {
365
310
  var _a, e_1, _b, _c;
366
311
  var _d, _e, _f, _g, _h, _j, _k, _l, _m;
367
312
  return __awaiter(this, void 0, void 0, function () {
368
- var sequelizeInstance, transaction, request, response, traceId, chunks, _o, request_1, request_1_1, chunk, e_1_1, _p, parsedBody, rawHandlerName, handlerPropertype, handlerName, authAdapters, error_3, _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult, error_4, _q;
313
+ var sequelizeInstance, transaction, request, response, traceId, chunks, _o, request_1, request_1_1, chunk, e_1_1, _p, parsedBody, rawHandlerName, handlerPropertype, handlerName, methodPool, authAdapters, error_2, _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult, error_3, _q;
369
314
  var _this = this;
370
315
  return __generator(this, function (_r) {
371
316
  switch (_r.label) {
@@ -435,10 +380,13 @@ function HerbalGuard(options) {
435
380
  rawHandlerName = (_e = (_d = context === null || context === void 0 ? void 0 : context.getHandler) === null || _d === void 0 ? void 0 : _d.call(context)) === null || _e === void 0 ? void 0 : _e.name;
436
381
  handlerPropertype = (_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;
437
382
  handlerName = string_util_class_1.StringUtil.isFalsyString(request.methodName) ? rawHandlerName : request.methodName;
438
- authAdapters = (_j = (_h = getMethodPool(handlerPropertype)) === null || _h === void 0 ? void 0 : _h.getAuthAdapters) === null || _j === void 0 ? void 0 : _j.call(_h, handlerName);
383
+ methodPool = getMethodPool(handlerPropertype);
384
+ authAdapters = (_h = methodPool === null || methodPool === void 0 ? void 0 : methodPool.getAuthAdapters) === null || _h === void 0 ? void 0 : _h.call(methodPool, handlerName);
439
385
  if (authAdapters === null)
440
386
  authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPropertype, handlerName);
441
- if (!(!(sequelizeInstance instanceof Error) && !decorators_1.NoTransaction.isDisabled(handlerPropertype, handlerName))) return [3, 20];
387
+ if (!(!(sequelizeInstance instanceof Error) &&
388
+ !decorators_1.NoTransaction.isDisabled(handlerPropertype, rawHandlerName) &&
389
+ !((_j = methodPool === null || methodPool === void 0 ? void 0 : methodPool.transactionDisabled) === null || _j === void 0 ? void 0 : _j.call(methodPool, handlerName)))) return [3, 20];
442
390
  _r.label = 16;
443
391
  case 16:
444
392
  _r.trys.push([16, 18, , 19]);
@@ -449,9 +397,9 @@ function HerbalGuard(options) {
449
397
  this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Started transaction for route: ").concat(handlerName));
450
398
  return [3, 19];
451
399
  case 18:
452
- error_3 = _r.sent();
453
- if (error_3 instanceof Error) {
454
- this.getLogger().error("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Failed to start transaction: ").concat(error_3 === null || error_3 === void 0 ? void 0 : error_3.message, "\n").concat(error_3 === null || error_3 === void 0 ? void 0 : error_3.stack));
400
+ error_2 = _r.sent();
401
+ if (error_2 instanceof Error) {
402
+ this.getLogger().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));
455
403
  }
456
404
  return [3, 19];
457
405
  case 19: return [3, 21];
@@ -484,12 +432,12 @@ function HerbalGuard(options) {
484
432
  case 25: throw new common_3.UnauthorizedException();
485
433
  case 26: return [3, 32];
486
434
  case 27:
487
- error_4 = _r.sent();
435
+ error_3 = _r.sent();
488
436
  _r.label = 28;
489
437
  case 28:
490
438
  _r.trys.push([28, 30, , 31]);
491
- if (error_4 instanceof Error) {
492
- this.getLogger().error("Got error when handling route: ".concat(error_4 === null || error_4 === void 0 ? void 0 : error_4.message, " ").concat(error_4 === null || error_4 === void 0 ? void 0 : error_4.stack));
439
+ if (error_3 instanceof Error) {
440
+ this.getLogger().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));
493
441
  }
494
442
  return [4, ((_m = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _m === void 0 ? void 0 : _m.call(transaction))];
495
443
  case 29:
@@ -498,7 +446,7 @@ function HerbalGuard(options) {
498
446
  case 30:
499
447
  _q = _r.sent();
500
448
  return [3, 31];
501
- case 31: throw error_4;
449
+ case 31: throw error_3;
502
450
  case 32: return [2, true];
503
451
  }
504
452
  });
@@ -561,11 +509,67 @@ var ControllerUtil = (function () {
561
509
  Reflect.defineMetadata(METHOD_POOL, methodPool, target.prototype);
562
510
  if (typeof (options === null || options === void 0 ? void 0 : options.methods) === 'function')
563
511
  options.methods(register);
512
+ function handleRequest(request) {
513
+ var _a, _b, _c, _d, _e, _f;
514
+ return __awaiter(this, void 0, void 0, function () {
515
+ var callFn, result, error_4;
516
+ var _g;
517
+ var _this = this;
518
+ return __generator(this, function (_h) {
519
+ switch (_h.label) {
520
+ case 0:
521
+ callFn = (_b = (_a = getMethodPool(this)) === null || _a === void 0 ? void 0 : _a.getCallFn) === null || _b === void 0 ? void 0 : _b.call(_a, request.methodName);
522
+ if (typeof callFn !== 'function')
523
+ throw new common_1.NotFoundException("Method ".concat(request.methodName, " not found"));
524
+ _h.label = 1;
525
+ case 1:
526
+ _h.trys.push([1, 4, , 6]);
527
+ _g = {};
528
+ return [4, callFn(this, {
529
+ authenticateResult: request.authenticateResult,
530
+ headers: header_util_class_1.HeaderUtil.parse((_c = request.headers) !== null && _c !== void 0 ? _c : {}),
531
+ methodName: request.methodName,
532
+ rawBody: request.rawBody,
533
+ traceId: request.traceId,
534
+ transaction: request.transaction,
535
+ url: request.originalUrl,
536
+ })];
537
+ case 2:
538
+ result = (_g.data = _h.sent(),
539
+ _g.token = string_util_class_1.StringUtil.isFalsyString((_d = request === null || request === void 0 ? void 0 : request.authenticateResult) === null || _d === void 0 ? void 0 : _d.nextToken)
540
+ ? null
541
+ : request.authenticateResult.nextToken,
542
+ _g);
543
+ return [4, ((_f = (_e = request === null || request === void 0 ? void 0 : request.transaction) === null || _e === void 0 ? void 0 : _e.commit) === null || _f === void 0 ? void 0 : _f.call(_e))];
544
+ case 3:
545
+ _h.sent();
546
+ return [2, result];
547
+ case 4:
548
+ error_4 = _h.sent();
549
+ return [4, utilities_1.AttemptUtil.execPromise((function () { return __awaiter(_this, void 0, void 0, function () {
550
+ var _a, _b;
551
+ return __generator(this, function (_c) {
552
+ switch (_c.label) {
553
+ case 0: return [4, ((_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))];
554
+ case 1:
555
+ _c.sent();
556
+ return [2];
557
+ }
558
+ });
559
+ }); })())];
560
+ case 5:
561
+ _h.sent();
562
+ throw error_4;
563
+ case 6: return [2];
564
+ }
565
+ });
566
+ });
567
+ }
564
568
  Object.defineProperty(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL, {
565
569
  enumerable: false,
566
570
  writable: false,
567
571
  value: function (request) {
568
- return this[HANDLE_REQUEST_SYMBOL].call(this, request);
572
+ return handleRequest.call(this, request);
569
573
  },
570
574
  });
571
575
  (0, common_1.Req)()(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL, 0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/herbal",
3
- "version": "1.0.2-alpha.35",
3
+ "version": "1.0.2-alpha.37",
4
4
  "description": "Herbal is a builder and toolchain for Nest.js applications",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {