@open-norantec/herbal 2.0.0-alpha.3 → 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 +18 -5
- package/dist/core.js +223 -140
- package/dist/decorators/auth-adapter.decorator.d.ts +1 -1
- package/dist/decorators/auth-adapter.decorator.js +3 -0
- package/dist/decorators/no-transaction.decorator.d.ts +1 -1
- package/dist/decorators/no-transaction.decorator.js +3 -0
- package/package.json +1 -1
package/dist/core.d.ts
CHANGED
|
@@ -4,7 +4,9 @@ import { Request, RequestContext } from './types/request.type';
|
|
|
4
4
|
import { z } from '@open-norantec/utilities';
|
|
5
5
|
import 'reflect-metadata';
|
|
6
6
|
import { Request as ExpressRequest } from 'express';
|
|
7
|
+
import { ModuleRef } from '@nestjs/core';
|
|
7
8
|
import { Constructor } from 'type-fest';
|
|
9
|
+
import { Sequelize } from 'sequelize-typescript';
|
|
8
10
|
import { AuthAdapter } from './abstracts/auth-adapter.abstract.class';
|
|
9
11
|
import { PathsObject } from 'openapi3-ts/oas31';
|
|
10
12
|
export * from '@nestjs/core';
|
|
@@ -39,9 +41,9 @@ declare class MethodConfig<IS extends z.ZodType<any>, OS extends z.ZodType<any>,
|
|
|
39
41
|
declare class MethodPool {
|
|
40
42
|
protected readonly methods: Map<string, MethodConfig<any, any, any>>;
|
|
41
43
|
registerMethod<IS extends z.ZodType<any>, OS extends z.ZodType<any>, C>(name: string, options: MethodRegisterOptions<IS, OS>, callback: MethodCallback<IS, OS, C>): void;
|
|
42
|
-
transactionDisabled(name
|
|
44
|
+
transactionDisabled(name?: string): boolean;
|
|
43
45
|
getCallFn(name: string): ((controller: any, callContext: MethodCallContext<any>) => Promise<any>) | null;
|
|
44
|
-
getAuthAdapters(name
|
|
46
|
+
getAuthAdapters(name?: string): Constructor<AuthAdapter>[] | null | undefined;
|
|
45
47
|
getOpenAPIPathsObject(group?: string): PathsObject;
|
|
46
48
|
}
|
|
47
49
|
export declare function isHerbalController(target: Function): boolean;
|
|
@@ -49,15 +51,25 @@ export declare function getControllerName(target: Function): any;
|
|
|
49
51
|
export interface HerbalControllerOptions<C> {
|
|
50
52
|
ignoreControllerNamePostfix?: boolean;
|
|
51
53
|
prefix?: string;
|
|
52
|
-
useHeadGuards?: Constructor<any>[];
|
|
53
|
-
useTailGuards?: Constructor<any>[];
|
|
54
54
|
methods?: (register: MethodRegisterFn<C>) => void;
|
|
55
|
+
onAfterParsingRequest?: (request: Request, moduleRef: ModuleRef) => void | Promise<void>;
|
|
56
|
+
onBeforeParsingRequest?: (request: Request, moduleRef: ModuleRef) => void | Promise<void>;
|
|
55
57
|
}
|
|
56
58
|
export interface ControllerUtilCreateOptions {
|
|
57
59
|
prefix?: string;
|
|
58
|
-
useGuards?: Constructor<any>[];
|
|
59
60
|
getTraceId?: (request: ExpressRequest) => string;
|
|
61
|
+
onAfterParsingRequest?: HerbalControllerOptions<any>['onAfterParsingRequest'];
|
|
62
|
+
onBeforeParsingRequest?: HerbalControllerOptions<any>['onBeforeParsingRequest'];
|
|
60
63
|
}
|
|
64
|
+
export declare function parseRequest({ request, traceId: inputTraceId, sequelize: sequelizeInstance, moduleRef, handlerName, handlerPrototype, onLog, }: {
|
|
65
|
+
handlerPrototype: object;
|
|
66
|
+
moduleRef: ModuleRef;
|
|
67
|
+
request: Request;
|
|
68
|
+
handlerName?: string;
|
|
69
|
+
sequelize?: Sequelize;
|
|
70
|
+
traceId?: string;
|
|
71
|
+
onLog?: (methodName: string, message: string) => void;
|
|
72
|
+
}): Promise<void>;
|
|
61
73
|
export declare function getMethodPool(targetPrototype: object): MethodPool | null;
|
|
62
74
|
export declare class ControllerUtil {
|
|
63
75
|
static create(createOptions?: ControllerUtilCreateOptions): {
|
|
@@ -66,3 +78,4 @@ export declare class ControllerUtil {
|
|
|
66
78
|
getControllerName: typeof getControllerName;
|
|
67
79
|
};
|
|
68
80
|
}
|
|
81
|
+
export declare function getRequestHandler(controller: object): ((request: Request) => Promise<any>) | null;
|
package/dist/core.js
CHANGED
|
@@ -76,17 +76,8 @@ 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
|
-
exports.ControllerUtil = exports.getMethodPool = exports.getControllerName = exports.isHerbalController = void 0;
|
|
80
|
+
exports.getRequestHandler = exports.ControllerUtil = exports.getMethodPool = exports.parseRequest = exports.getControllerName = exports.isHerbalController = void 0;
|
|
90
81
|
require("reflect-metadata");
|
|
91
82
|
var common_1 = require("@nestjs/common");
|
|
92
83
|
var header_util_class_1 = require("@open-norantec/utilities/dist/header-util.class");
|
|
@@ -173,7 +164,7 @@ var MethodPool = (function () {
|
|
|
173
164
|
MethodPool.prototype.transactionDisabled = function (name) {
|
|
174
165
|
var _a, _b;
|
|
175
166
|
if (string_util_class_1.StringUtil.isFalsyString(name))
|
|
176
|
-
return;
|
|
167
|
+
return false;
|
|
177
168
|
if (name.includes('/'))
|
|
178
169
|
throw new Error("Method name cannot contain slashes: ".concat(name));
|
|
179
170
|
return !!((_b = (_a = this.methods.get(name)) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.disableTransaction);
|
|
@@ -185,6 +176,8 @@ var MethodPool = (function () {
|
|
|
185
176
|
return config.call.bind(config);
|
|
186
177
|
};
|
|
187
178
|
MethodPool.prototype.getAuthAdapters = function (name) {
|
|
179
|
+
if (string_util_class_1.StringUtil.isFalsyString(name))
|
|
180
|
+
return null;
|
|
188
181
|
var config = this.methods.get(name);
|
|
189
182
|
if (!(config instanceof MethodConfig))
|
|
190
183
|
return null;
|
|
@@ -239,6 +232,8 @@ var MethodPool = (function () {
|
|
|
239
232
|
}());
|
|
240
233
|
var IS_HERBAL_CONTROLLER = Symbol();
|
|
241
234
|
var CONTROLLER_NAME = Symbol();
|
|
235
|
+
var AFTER_PARSING_REQUEST_HANDLERS = Symbol();
|
|
236
|
+
var BEFORE_PARSING_REQUEST_HANDLERS = Symbol();
|
|
242
237
|
function isHerbalController(target) {
|
|
243
238
|
return _.attempt(function () { return Reflect.getMetadata(IS_HERBAL_CONTROLLER, target.prototype); }) === true;
|
|
244
239
|
}
|
|
@@ -303,22 +298,189 @@ var ControllerInterceptor = (function () {
|
|
|
303
298
|
], ControllerInterceptor);
|
|
304
299
|
return ControllerInterceptor;
|
|
305
300
|
}());
|
|
301
|
+
function parseRequest(_a) {
|
|
302
|
+
var _b, request_1, request_1_1;
|
|
303
|
+
var _c, e_1, _d, _e;
|
|
304
|
+
var _f, _g, _h, _j, _k;
|
|
305
|
+
var request = _a.request, inputTraceId = _a.traceId, sequelizeInstance = _a.sequelize, moduleRef = _a.moduleRef, handlerName = _a.handlerName, handlerPrototype = _a.handlerPrototype, onLog = _a.onLog;
|
|
306
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
307
|
+
var transaction, beforeParsingRequestHandlers, afterParsingRequestHandlers, chunks_1, chunk, e_1_1, _l, parsedBody, methodPool, authAdapters, shouldHaveTransaction, error_2, authSucceeded, error_3, _m;
|
|
308
|
+
var _this = this;
|
|
309
|
+
return __generator(this, function (_o) {
|
|
310
|
+
switch (_o.label) {
|
|
311
|
+
case 0:
|
|
312
|
+
transaction = undefined;
|
|
313
|
+
beforeParsingRequestHandlers = Reflect.getMetadata(BEFORE_PARSING_REQUEST_HANDLERS, handlerPrototype);
|
|
314
|
+
afterParsingRequestHandlers = Reflect.getMetadata(AFTER_PARSING_REQUEST_HANDLERS, handlerPrototype);
|
|
315
|
+
request.traceId = string_util_class_1.StringUtil.isFalsyString(inputTraceId) ? uuid_util_class_1.UUIDUtil.generateV4() : inputTraceId;
|
|
316
|
+
request.methodName = request.url.split('/').pop();
|
|
317
|
+
if (!(typeof request.rawBody === 'undefined')) return [3, 16];
|
|
318
|
+
chunks_1 = [];
|
|
319
|
+
_o.label = 1;
|
|
320
|
+
case 1:
|
|
321
|
+
_o.trys.push([1, 14, , 15]);
|
|
322
|
+
_o.label = 2;
|
|
323
|
+
case 2:
|
|
324
|
+
_o.trys.push([2, 7, 8, 13]);
|
|
325
|
+
_b = true, request_1 = __asyncValues(request);
|
|
326
|
+
_o.label = 3;
|
|
327
|
+
case 3: return [4, request_1.next()];
|
|
328
|
+
case 4:
|
|
329
|
+
if (!(request_1_1 = _o.sent(), _c = request_1_1.done, !_c)) return [3, 6];
|
|
330
|
+
_e = request_1_1.value;
|
|
331
|
+
_b = false;
|
|
332
|
+
chunk = _e;
|
|
333
|
+
chunks_1.push(chunk);
|
|
334
|
+
_o.label = 5;
|
|
335
|
+
case 5:
|
|
336
|
+
_b = true;
|
|
337
|
+
return [3, 3];
|
|
338
|
+
case 6: return [3, 13];
|
|
339
|
+
case 7:
|
|
340
|
+
e_1_1 = _o.sent();
|
|
341
|
+
e_1 = { error: e_1_1 };
|
|
342
|
+
return [3, 13];
|
|
343
|
+
case 8:
|
|
344
|
+
_o.trys.push([8, , 11, 12]);
|
|
345
|
+
if (!(!_b && !_c && (_d = request_1.return))) return [3, 10];
|
|
346
|
+
return [4, _d.call(request_1)];
|
|
347
|
+
case 9:
|
|
348
|
+
_o.sent();
|
|
349
|
+
_o.label = 10;
|
|
350
|
+
case 10: return [3, 12];
|
|
351
|
+
case 11:
|
|
352
|
+
if (e_1) throw e_1.error;
|
|
353
|
+
return [7];
|
|
354
|
+
case 12: return [7];
|
|
355
|
+
case 13: return [3, 15];
|
|
356
|
+
case 14:
|
|
357
|
+
_l = _o.sent();
|
|
358
|
+
return [3, 15];
|
|
359
|
+
case 15:
|
|
360
|
+
parsedBody = _.attempt(function () { return Buffer.concat(chunks_1).toString('utf8'); });
|
|
361
|
+
if (!(parsedBody instanceof Error)) {
|
|
362
|
+
request.rawBody = parsedBody;
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
request.rawBody = null;
|
|
366
|
+
}
|
|
367
|
+
_o.label = 16;
|
|
368
|
+
case 16:
|
|
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
|
+
methodPool = getMethodPool(handlerPrototype);
|
|
376
|
+
authAdapters = (_f = methodPool === null || methodPool === void 0 ? void 0 : methodPool.getAuthAdapters) === null || _f === void 0 ? void 0 : _f.call(methodPool, request.methodName);
|
|
377
|
+
if (authAdapters === null)
|
|
378
|
+
authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPrototype, handlerName);
|
|
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, 22];
|
|
381
|
+
_o.label = 18;
|
|
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
|
+
transaction = _o.sent();
|
|
387
|
+
request.transaction = transaction;
|
|
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, 21];
|
|
390
|
+
case 20:
|
|
391
|
+
error_2 = _o.sent();
|
|
392
|
+
if (error_2 instanceof Error) {
|
|
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
|
+
}
|
|
395
|
+
return [3, 21];
|
|
396
|
+
case 21: return [3, 23];
|
|
397
|
+
case 22:
|
|
398
|
+
if (!shouldHaveTransaction) {
|
|
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
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
throw new Error('Sequelize instance not found, cannot start transaction');
|
|
403
|
+
}
|
|
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
|
+
return [4, (function () { return __awaiter(_this, void 0, void 0, function () {
|
|
409
|
+
var _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult;
|
|
410
|
+
return __generator(this, function (_a) {
|
|
411
|
+
switch (_a.label) {
|
|
412
|
+
case 0:
|
|
413
|
+
_i = 0, authAdapters_1 = authAdapters;
|
|
414
|
+
_a.label = 1;
|
|
415
|
+
case 1:
|
|
416
|
+
if (!(_i < authAdapters_1.length)) return [3, 4];
|
|
417
|
+
AuthAdapterClass = authAdapters_1[_i];
|
|
418
|
+
adapter = new AuthAdapterClass(request, moduleRef);
|
|
419
|
+
if (!adapter.match())
|
|
420
|
+
return [3, 3];
|
|
421
|
+
return [4, adapter.authenticate(transaction)];
|
|
422
|
+
case 2:
|
|
423
|
+
authenticateResult = _a.sent();
|
|
424
|
+
if (!authenticateResult)
|
|
425
|
+
return [3, 4];
|
|
426
|
+
request.authenticateResult = __assign({ AuthenticatorClass: AuthAdapterClass }, authenticateResult);
|
|
427
|
+
return [2, true];
|
|
428
|
+
case 3:
|
|
429
|
+
_i++;
|
|
430
|
+
return [3, 1];
|
|
431
|
+
case 4: return [2, false];
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
}); })()];
|
|
435
|
+
case 24:
|
|
436
|
+
authSucceeded = _o.sent();
|
|
437
|
+
if (!authSucceeded)
|
|
438
|
+
throw new common_3.UnauthorizedException();
|
|
439
|
+
_o.label = 25;
|
|
440
|
+
case 25: return [3, 33];
|
|
441
|
+
case 26:
|
|
442
|
+
error_3 = _o.sent();
|
|
443
|
+
_o.label = 27;
|
|
444
|
+
case 27:
|
|
445
|
+
_o.trys.push([27, 29, , 30]);
|
|
446
|
+
if (error_3 instanceof Error) {
|
|
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
|
+
}
|
|
449
|
+
return [4, ((_k = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _k === void 0 ? void 0 : _k.call(transaction))];
|
|
450
|
+
case 28:
|
|
451
|
+
_o.sent();
|
|
452
|
+
return [3, 30];
|
|
453
|
+
case 29:
|
|
454
|
+
_m = _o.sent();
|
|
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];
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
exports.parseRequest = parseRequest;
|
|
306
469
|
function HerbalGuard(options) {
|
|
307
470
|
var HerbalGuardMixin = (function () {
|
|
308
471
|
function HerbalGuardMixin(ref) {
|
|
309
472
|
this.ref = ref;
|
|
310
473
|
}
|
|
311
474
|
HerbalGuardMixin.prototype.canActivate = function (context) {
|
|
312
|
-
var _a,
|
|
313
|
-
var _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
475
|
+
var _a, _b, _c, _d, _e;
|
|
314
476
|
return __awaiter(this, void 0, void 0, function () {
|
|
315
|
-
var
|
|
477
|
+
var logger, sequelizeInstance, request, response, traceId;
|
|
316
478
|
var _this = this;
|
|
317
|
-
return __generator(this, function (
|
|
318
|
-
switch (
|
|
479
|
+
return __generator(this, function (_f) {
|
|
480
|
+
switch (_f.label) {
|
|
319
481
|
case 0:
|
|
482
|
+
logger = this.getLogger();
|
|
320
483
|
sequelizeInstance = _.attempt(function () { return _this.ref.get(sequelize_typescript_1.Sequelize, { strict: false }); });
|
|
321
|
-
transaction = undefined;
|
|
322
484
|
request = context.switchToHttp().getRequest();
|
|
323
485
|
response = context.switchToHttp().getResponse();
|
|
324
486
|
traceId = typeof (options === null || options === void 0 ? void 0 : options.getTraceId) === 'function'
|
|
@@ -326,129 +488,25 @@ function HerbalGuard(options) {
|
|
|
326
488
|
: uuid_util_class_1.UUIDUtil.generateV4();
|
|
327
489
|
if (traceId instanceof Error || string_util_class_1.StringUtil.isFalsyString(traceId))
|
|
328
490
|
traceId = uuid_util_class_1.UUIDUtil.generateV4();
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
491
|
+
return [4, parseRequest({
|
|
492
|
+
request: request,
|
|
493
|
+
moduleRef: this.ref,
|
|
494
|
+
sequelize: sequelizeInstance instanceof Error ? undefined : sequelizeInstance,
|
|
495
|
+
handlerPrototype: (_c = (_b = (_a = context === null || context === void 0 ? void 0 : context.getClass) === null || _a === void 0 ? void 0 : _a.call(context)) === null || _b === void 0 ? void 0 : _b.prototype) !== null && _c !== void 0 ? _c : {},
|
|
496
|
+
handlerName: (_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,
|
|
497
|
+
traceId: traceId,
|
|
498
|
+
onLog: function (method, message) {
|
|
499
|
+
var _a;
|
|
500
|
+
try {
|
|
501
|
+
(_a = logger[method]) === null || _a === void 0 ? void 0 : _a.call(logger, message);
|
|
502
|
+
}
|
|
503
|
+
catch (_b) { }
|
|
504
|
+
},
|
|
505
|
+
})];
|
|
334
506
|
case 1:
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
case 2:
|
|
338
|
-
_r.trys.push([2, 7, 8, 13]);
|
|
339
|
-
_o = true, request_1 = __asyncValues(request);
|
|
340
|
-
_r.label = 3;
|
|
341
|
-
case 3: return [4, request_1.next()];
|
|
342
|
-
case 4:
|
|
343
|
-
if (!(request_1_1 = _r.sent(), _a = request_1_1.done, !_a)) return [3, 6];
|
|
344
|
-
_c = request_1_1.value;
|
|
345
|
-
_o = false;
|
|
346
|
-
chunk = _c;
|
|
347
|
-
chunks.push(chunk);
|
|
348
|
-
_r.label = 5;
|
|
349
|
-
case 5:
|
|
350
|
-
_o = true;
|
|
351
|
-
return [3, 3];
|
|
352
|
-
case 6: return [3, 13];
|
|
353
|
-
case 7:
|
|
354
|
-
e_1_1 = _r.sent();
|
|
355
|
-
e_1 = { error: e_1_1 };
|
|
356
|
-
return [3, 13];
|
|
357
|
-
case 8:
|
|
358
|
-
_r.trys.push([8, , 11, 12]);
|
|
359
|
-
if (!(!_o && !_a && (_b = request_1.return))) return [3, 10];
|
|
360
|
-
return [4, _b.call(request_1)];
|
|
361
|
-
case 9:
|
|
362
|
-
_r.sent();
|
|
363
|
-
_r.label = 10;
|
|
364
|
-
case 10: return [3, 12];
|
|
365
|
-
case 11:
|
|
366
|
-
if (e_1) throw e_1.error;
|
|
367
|
-
return [7];
|
|
368
|
-
case 12: return [7];
|
|
369
|
-
case 13: return [3, 15];
|
|
370
|
-
case 14:
|
|
371
|
-
_p = _r.sent();
|
|
372
|
-
return [3, 15];
|
|
373
|
-
case 15:
|
|
374
|
-
parsedBody = _.attempt(function () { return Buffer.concat(chunks).toString('utf8'); });
|
|
375
|
-
if (!(parsedBody instanceof Error)) {
|
|
376
|
-
request.rawBody = parsedBody;
|
|
377
|
-
}
|
|
378
|
-
else {
|
|
379
|
-
request.rawBody = null;
|
|
380
|
-
}
|
|
381
|
-
_.attempt(function () { return _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":request:body] ").concat(request.rawBody)); });
|
|
382
|
-
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;
|
|
383
|
-
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;
|
|
384
|
-
handlerName = string_util_class_1.StringUtil.isFalsyString(request.methodName) ? rawHandlerName : request.methodName;
|
|
385
|
-
methodPool = getMethodPool(handlerPropertype);
|
|
386
|
-
authAdapters = (_h = methodPool === null || methodPool === void 0 ? void 0 : methodPool.getAuthAdapters) === null || _h === void 0 ? void 0 : _h.call(methodPool, handlerName);
|
|
387
|
-
if (authAdapters === null)
|
|
388
|
-
authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPropertype, handlerName);
|
|
389
|
-
shouldHaveTransaction = !decorators_1.NoTransaction.isDisabled(handlerPropertype, rawHandlerName) && !((_j = methodPool === null || methodPool === void 0 ? void 0 : methodPool.transactionDisabled) === null || _j === void 0 ? void 0 : _j.call(methodPool, handlerName));
|
|
390
|
-
if (!(!(sequelizeInstance instanceof Error) && shouldHaveTransaction)) return [3, 20];
|
|
391
|
-
_r.label = 16;
|
|
392
|
-
case 16:
|
|
393
|
-
_r.trys.push([16, 18, , 19]);
|
|
394
|
-
return [4, ((_l = (_k = sequelizeInstance === null || sequelizeInstance === void 0 ? void 0 : sequelizeInstance.transaction) === null || _k === void 0 ? void 0 : _k.call(sequelizeInstance)) === null || _l === void 0 ? void 0 : _l.catch(function () { return Promise.resolve(undefined); }))];
|
|
395
|
-
case 17:
|
|
396
|
-
transaction = _r.sent();
|
|
397
|
-
request.transaction = transaction;
|
|
398
|
-
this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Started transaction for route: ").concat(handlerName));
|
|
399
|
-
return [3, 19];
|
|
400
|
-
case 18:
|
|
401
|
-
error_2 = _r.sent();
|
|
402
|
-
if (error_2 instanceof Error) {
|
|
403
|
-
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));
|
|
404
|
-
}
|
|
405
|
-
return [3, 19];
|
|
406
|
-
case 19: return [3, 21];
|
|
407
|
-
case 20:
|
|
408
|
-
if (!shouldHaveTransaction) {
|
|
409
|
-
this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Transaction is disabled for this route: ").concat(handlerName));
|
|
410
|
-
}
|
|
411
|
-
_r.label = 21;
|
|
412
|
-
case 21:
|
|
413
|
-
_r.trys.push([21, 27, , 32]);
|
|
414
|
-
if (!(Array.isArray(authAdapters) && authAdapters.length > 0)) return [3, 26];
|
|
415
|
-
_i = 0, authAdapters_1 = authAdapters;
|
|
416
|
-
_r.label = 22;
|
|
417
|
-
case 22:
|
|
418
|
-
if (!(_i < authAdapters_1.length)) return [3, 25];
|
|
419
|
-
AuthAdapterClass = authAdapters_1[_i];
|
|
420
|
-
adapter = new AuthAdapterClass(request, this.ref);
|
|
421
|
-
if (!adapter.match())
|
|
422
|
-
return [3, 24];
|
|
423
|
-
return [4, adapter.authenticate(transaction)];
|
|
424
|
-
case 23:
|
|
425
|
-
authenticateResult = _r.sent();
|
|
426
|
-
if (!authenticateResult)
|
|
427
|
-
return [3, 25];
|
|
428
|
-
request.authenticateResult = __assign({ AuthenticatorClass: AuthAdapterClass }, authenticateResult);
|
|
507
|
+
_f.sent();
|
|
508
|
+
response.setHeader(headers_constant_1.HEADERS.TRACE_ID, traceId);
|
|
429
509
|
return [2, true];
|
|
430
|
-
case 24:
|
|
431
|
-
_i++;
|
|
432
|
-
return [3, 22];
|
|
433
|
-
case 25: throw new common_3.UnauthorizedException();
|
|
434
|
-
case 26: return [3, 32];
|
|
435
|
-
case 27:
|
|
436
|
-
error_3 = _r.sent();
|
|
437
|
-
_r.label = 28;
|
|
438
|
-
case 28:
|
|
439
|
-
_r.trys.push([28, 30, , 31]);
|
|
440
|
-
if (error_3 instanceof Error) {
|
|
441
|
-
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));
|
|
442
|
-
}
|
|
443
|
-
return [4, ((_m = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _m === void 0 ? void 0 : _m.call(transaction))];
|
|
444
|
-
case 29:
|
|
445
|
-
_r.sent();
|
|
446
|
-
return [3, 31];
|
|
447
|
-
case 30:
|
|
448
|
-
_q = _r.sent();
|
|
449
|
-
return [3, 31];
|
|
450
|
-
case 31: throw error_3;
|
|
451
|
-
case 32: return [2, true];
|
|
452
510
|
}
|
|
453
511
|
});
|
|
454
512
|
});
|
|
@@ -569,7 +627,7 @@ var ControllerUtil = (function () {
|
|
|
569
627
|
Object.defineProperty(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL, {
|
|
570
628
|
enumerable: false,
|
|
571
629
|
writable: false,
|
|
572
|
-
value: function (request) {
|
|
630
|
+
value: function $herbalInternal(request) {
|
|
573
631
|
return handleRequest.call(this, request);
|
|
574
632
|
},
|
|
575
633
|
});
|
|
@@ -580,9 +638,21 @@ var ControllerUtil = (function () {
|
|
|
580
638
|
logger.log("Mapped method: /".concat(controllerName).concat(path));
|
|
581
639
|
});
|
|
582
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);
|
|
583
653
|
(0, common_2.Controller)(finalPrefix)(target);
|
|
584
654
|
(0, common_2.UseInterceptors)(ControllerInterceptor)(target);
|
|
585
|
-
|
|
655
|
+
(0, common_2.UseGuards)(HerbalGuard(_.pick(createOptions, ['getTraceId'])))(target);
|
|
586
656
|
};
|
|
587
657
|
}
|
|
588
658
|
Controller.isHerbalController = isHerbalController;
|
|
@@ -592,3 +662,16 @@ var ControllerUtil = (function () {
|
|
|
592
662
|
return ControllerUtil;
|
|
593
663
|
}());
|
|
594
664
|
exports.ControllerUtil = ControllerUtil;
|
|
665
|
+
function getRequestHandler(controller) {
|
|
666
|
+
var _a;
|
|
667
|
+
try {
|
|
668
|
+
var requestHandler = (_a = Object.getOwnPropertyDescriptor(controller, HANDLE_REQUEST_INSTANCE_SYMBOL)) === null || _a === void 0 ? void 0 : _a.value;
|
|
669
|
+
if (typeof requestHandler !== 'function')
|
|
670
|
+
return null;
|
|
671
|
+
return requestHandler;
|
|
672
|
+
}
|
|
673
|
+
catch (_b) {
|
|
674
|
+
return null;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
exports.getRequestHandler = getRequestHandler;
|
|
@@ -3,5 +3,5 @@ import { Constructor } from 'type-fest';
|
|
|
3
3
|
import { AuthAdapter } from '../abstracts/auth-adapter.abstract.class';
|
|
4
4
|
export declare function AuthAdapters(adapters?: Constructor<AuthAdapter>[]): PropertyDecorator;
|
|
5
5
|
export declare namespace AuthAdapters {
|
|
6
|
-
var getAdapters: (target: object, propertyKey
|
|
6
|
+
var getAdapters: (target: object, propertyKey?: string | undefined) => Constructor<AuthAdapter>[];
|
|
7
7
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.AuthAdapters = void 0;
|
|
4
4
|
require("reflect-metadata");
|
|
5
5
|
var _ = require("lodash");
|
|
6
|
+
var utilities_1 = require("@open-norantec/utilities");
|
|
6
7
|
var AUTH_ADAPTERS = Symbol();
|
|
7
8
|
function AuthAdapters(adapters) {
|
|
8
9
|
return function (target, propertyKey) {
|
|
@@ -11,6 +12,8 @@ function AuthAdapters(adapters) {
|
|
|
11
12
|
}
|
|
12
13
|
exports.AuthAdapters = AuthAdapters;
|
|
13
14
|
AuthAdapters.getAdapters = function (target, propertyKey) {
|
|
15
|
+
if (utilities_1.StringUtil.isFalsyString(propertyKey))
|
|
16
|
+
return [];
|
|
14
17
|
var result = _.attempt(function () { return Reflect.getMetadata(AUTH_ADAPTERS, target, propertyKey); });
|
|
15
18
|
if (Array.isArray(result))
|
|
16
19
|
return result;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
export declare function NoTransaction(): PropertyDecorator;
|
|
3
3
|
export declare namespace NoTransaction {
|
|
4
|
-
var isDisabled: (target: object, propertyKey
|
|
4
|
+
var isDisabled: (target: object, propertyKey?: string | undefined) => boolean;
|
|
5
5
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.NoTransaction = void 0;
|
|
4
4
|
require("reflect-metadata");
|
|
5
5
|
var _ = require("lodash");
|
|
6
|
+
var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
|
|
6
7
|
var TRANSACTION_DISABLED = Symbol();
|
|
7
8
|
function NoTransaction() {
|
|
8
9
|
return function (target, propertyKey) {
|
|
@@ -11,6 +12,8 @@ function NoTransaction() {
|
|
|
11
12
|
}
|
|
12
13
|
exports.NoTransaction = NoTransaction;
|
|
13
14
|
NoTransaction.isDisabled = function (target, propertyKey) {
|
|
15
|
+
if (string_util_class_1.StringUtil.isFalsyString(propertyKey))
|
|
16
|
+
return false;
|
|
14
17
|
var result = _.attempt(function () { return Reflect.getMetadata(TRANSACTION_DISABLED, target, propertyKey); });
|
|
15
18
|
return result === true;
|
|
16
19
|
};
|