@open-norantec/herbal 2.0.0-alpha.4 → 2.0.0-alpha.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.
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ModuleRef } from '@nestjs/core';
|
|
2
1
|
import { Constructor } from 'type-fest';
|
|
3
2
|
import { Request } from 'express';
|
|
4
3
|
import { Transaction } from 'sequelize';
|
|
4
|
+
import { GetModuleFn } from '../types';
|
|
5
5
|
export interface AuthenticateReturn {
|
|
6
6
|
challengeValue: string;
|
|
7
7
|
identifier: string;
|
|
@@ -13,8 +13,8 @@ export interface AuthenticateResult extends AuthenticateReturn {
|
|
|
13
13
|
}
|
|
14
14
|
export declare abstract class AuthAdapter {
|
|
15
15
|
protected readonly request: Request;
|
|
16
|
-
protected readonly
|
|
17
|
-
constructor(request: Request,
|
|
16
|
+
protected readonly getModule: GetModuleFn;
|
|
17
|
+
constructor(request: Request, getModule: GetModuleFn);
|
|
18
18
|
abstract match(): boolean;
|
|
19
19
|
abstract authenticate(transaction?: Transaction): Promise<AuthenticateReturn | null>;
|
|
20
20
|
}
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AuthAdapter = void 0;
|
|
4
4
|
var AuthAdapter = (function () {
|
|
5
|
-
function AuthAdapter(request,
|
|
5
|
+
function AuthAdapter(request, getModule) {
|
|
6
6
|
this.request = request;
|
|
7
|
-
this.
|
|
7
|
+
this.getModule = getModule;
|
|
8
8
|
}
|
|
9
9
|
return AuthAdapter;
|
|
10
10
|
}());
|
package/dist/core.d.ts
CHANGED
|
@@ -4,11 +4,11 @@ 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';
|
|
8
7
|
import { Constructor } from 'type-fest';
|
|
9
8
|
import { Sequelize } from 'sequelize-typescript';
|
|
10
9
|
import { AuthAdapter } from './abstracts/auth-adapter.abstract.class';
|
|
11
10
|
import { PathsObject } from 'openapi3-ts/oas31';
|
|
11
|
+
import { GetModuleFn } from './types';
|
|
12
12
|
export * from '@nestjs/core';
|
|
13
13
|
export type MethodHandler<IS extends z.ZodType<any>, OS extends z.ZodType<any>> = (request: Request, input: unknown, headers: ReturnType<typeof HeaderUtil.parse>) => Promise<{
|
|
14
14
|
request: z.infer<IS>;
|
|
@@ -51,22 +51,23 @@ 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, getModule: GetModuleFn) => void | Promise<void>;
|
|
56
|
+
onBeforeParsingRequest?: (request: Request, getModule: GetModuleFn) => 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
|
-
export declare function parseRequest({ request, traceId: inputTraceId, sequelize: sequelizeInstance,
|
|
64
|
+
export declare function parseRequest({ request, traceId: inputTraceId, sequelize: sequelizeInstance, handlerName, handlerPrototype, onLog, getModule, }: {
|
|
64
65
|
handlerPrototype: object;
|
|
65
|
-
moduleRef: ModuleRef;
|
|
66
66
|
request: Request;
|
|
67
67
|
handlerName?: string;
|
|
68
68
|
sequelize?: Sequelize;
|
|
69
69
|
traceId?: string;
|
|
70
|
+
getModule: GetModuleFn;
|
|
70
71
|
onLog?: (methodName: string, message: string) => void;
|
|
71
72
|
}): Promise<void>;
|
|
72
73
|
export declare function getMethodPool(targetPrototype: object): MethodPool | null;
|
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
|
}
|
|
@@ -309,14 +302,16 @@ function parseRequest(_a) {
|
|
|
309
302
|
var _b, request_1, request_1_1;
|
|
310
303
|
var _c, e_1, _d, _e;
|
|
311
304
|
var _f, _g, _h, _j, _k;
|
|
312
|
-
var request = _a.request, inputTraceId = _a.traceId, sequelizeInstance = _a.sequelize,
|
|
305
|
+
var request = _a.request, inputTraceId = _a.traceId, sequelizeInstance = _a.sequelize, handlerName = _a.handlerName, handlerPrototype = _a.handlerPrototype, onLog = _a.onLog, getModule = _a.getModule;
|
|
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, getModule);
|
|
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,
|
|
381
|
-
_o.label =
|
|
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,
|
|
390
|
-
case
|
|
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,
|
|
396
|
-
case
|
|
397
|
-
case
|
|
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 =
|
|
405
|
-
case
|
|
406
|
-
_o.trys.push([
|
|
407
|
-
if (!(Array.isArray(authAdapters) && authAdapters.length > 0 && typeof request.authenticateResult === 'undefined')) return [3,
|
|
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) {
|
|
@@ -415,7 +415,7 @@ function parseRequest(_a) {
|
|
|
415
415
|
case 1:
|
|
416
416
|
if (!(_i < authAdapters_1.length)) return [3, 4];
|
|
417
417
|
AuthAdapterClass = authAdapters_1[_i];
|
|
418
|
-
adapter = new AuthAdapterClass(request,
|
|
418
|
+
adapter = new AuthAdapterClass(request, getModule);
|
|
419
419
|
if (!adapter.match())
|
|
420
420
|
return [3, 3];
|
|
421
421
|
return [4, adapter.authenticate(transaction)];
|
|
@@ -432,29 +432,35 @@ function parseRequest(_a) {
|
|
|
432
432
|
}
|
|
433
433
|
});
|
|
434
434
|
}); })()];
|
|
435
|
-
case
|
|
435
|
+
case 24:
|
|
436
436
|
authSucceeded = _o.sent();
|
|
437
437
|
if (!authSucceeded)
|
|
438
438
|
throw new common_3.UnauthorizedException();
|
|
439
|
-
_o.label =
|
|
440
|
-
case
|
|
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.
|
|
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,
|
|
456
|
-
case
|
|
457
|
-
case
|
|
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, getModule);
|
|
459
|
+
}))];
|
|
460
|
+
case 32:
|
|
461
|
+
_o.sent();
|
|
462
|
+
return [7];
|
|
463
|
+
case 33: return [2];
|
|
458
464
|
}
|
|
459
465
|
});
|
|
460
466
|
});
|
|
@@ -484,11 +490,11 @@ function HerbalGuard(options) {
|
|
|
484
490
|
traceId = uuid_util_class_1.UUIDUtil.generateV4();
|
|
485
491
|
return [4, parseRequest({
|
|
486
492
|
request: request,
|
|
487
|
-
moduleRef: this.ref,
|
|
488
493
|
sequelize: sequelizeInstance instanceof Error ? undefined : sequelizeInstance,
|
|
489
494
|
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 : {},
|
|
490
495
|
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,
|
|
491
496
|
traceId: traceId,
|
|
497
|
+
getModule: this.ref.get.bind(this.ref),
|
|
492
498
|
onLog: function (method, message) {
|
|
493
499
|
var _a;
|
|
494
500
|
try {
|
|
@@ -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
|
-
|
|
655
|
+
(0, common_2.UseGuards)(HerbalGuard(_.pick(createOptions, ['getTraceId'])))(target);
|
|
638
656
|
};
|
|
639
657
|
}
|
|
640
658
|
Controller.isHerbalController = isHerbalController;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
+
import { Constructor } from 'type-fest';
|
|
1
2
|
export * from './request.type';
|
|
2
3
|
export * from './http-response-body.type';
|
|
4
|
+
export type GetModuleFn = <TInput = any, TResult = TInput>(typeOrToken: Constructor<TInput> | Function | string | symbol, options: {
|
|
5
|
+
strict?: boolean;
|
|
6
|
+
each?: undefined | false;
|
|
7
|
+
}) => TResult;
|