@open-norantec/herbal 1.0.2-alpha.34 → 1.0.2-alpha.36
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 +3 -1
- package/dist/core.js +37 -16
- package/package.json +1 -1
package/dist/core.d.ts
CHANGED
|
@@ -8,12 +8,13 @@ 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;
|
|
11
12
|
export type MethodHandler<IS extends z.Schema<any>, OS extends z.Schema<any>> = (request: Request, input: unknown, headers: ReturnType<typeof HeaderUtil.parse>) => Promise<{
|
|
12
13
|
request: z.infer<IS>;
|
|
13
14
|
response: z.infer<OS>;
|
|
14
15
|
}>;
|
|
15
16
|
export declare class HerbalController {
|
|
16
|
-
private
|
|
17
|
+
private [HANDLE_REQUEST_SYMBOL];
|
|
17
18
|
}
|
|
18
19
|
type ClientGroups = Array<string> | null | undefined;
|
|
19
20
|
type ClienttGroupsFactory = (defaultGroupName: string) => ClientGroups;
|
|
@@ -42,6 +43,7 @@ declare class MethodConfig<IS extends z.Schema<any>, OS extends z.Schema<any>, C
|
|
|
42
43
|
declare class MethodPool {
|
|
43
44
|
protected readonly methods: Map<string, MethodConfig<any, any, any>>;
|
|
44
45
|
registerMethod<IS extends z.Schema<any>, OS extends z.Schema<any>, C>(name: string, options: MethodRegisterOptions<IS, OS>, callback: MethodCallback<IS, OS, C>): void;
|
|
46
|
+
transactionDisabled(name: string): boolean | undefined;
|
|
45
47
|
getCallFn(name: string): ((controller: any, callContext: MethodCallContext<any>) => Promise<any>) | null;
|
|
46
48
|
getAuthAdapters(name: string): Constructor<AuthAdapter>[] | null | undefined;
|
|
47
49
|
getOpenAPIPathsObject(group?: string): PathsObject;
|
package/dist/core.js
CHANGED
|
@@ -33,9 +33,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
33
33
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
34
34
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
35
35
|
};
|
|
36
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
37
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
38
|
-
};
|
|
39
36
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
40
37
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
41
38
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -111,10 +108,12 @@ var sequelize_typescript_1 = require("sequelize-typescript");
|
|
|
111
108
|
var decorators_1 = require("./decorators");
|
|
112
109
|
var zod_openapi_1 = require("zod-openapi");
|
|
113
110
|
__exportStar(require("@nestjs/core"), exports);
|
|
114
|
-
var
|
|
111
|
+
var HANDLE_REQUEST_SYMBOL = Symbol();
|
|
112
|
+
var HANDLE_REQUEST_INSTANCE_SYMBOL = '$handleRequestInstance';
|
|
113
|
+
var HerbalController = (function () {
|
|
115
114
|
function HerbalController() {
|
|
116
115
|
}
|
|
117
|
-
HerbalController.prototype
|
|
116
|
+
HerbalController.prototype[HANDLE_REQUEST_SYMBOL] = function (request) {
|
|
118
117
|
var _a, _b, _c, _d, _e, _f;
|
|
119
118
|
return __awaiter(this, void 0, void 0, function () {
|
|
120
119
|
var callFn, result, error_1;
|
|
@@ -170,14 +169,9 @@ var HerbalController = exports.HerbalController = (function () {
|
|
|
170
169
|
});
|
|
171
170
|
});
|
|
172
171
|
};
|
|
173
|
-
__decorate([
|
|
174
|
-
__param(0, (0, common_1.Req)()),
|
|
175
|
-
__metadata("design:type", Function),
|
|
176
|
-
__metadata("design:paramtypes", [Object]),
|
|
177
|
-
__metadata("design:returntype", Promise)
|
|
178
|
-
], HerbalController.prototype, "$handleRequest", null);
|
|
179
172
|
return HerbalController;
|
|
180
173
|
}());
|
|
174
|
+
exports.HerbalController = HerbalController;
|
|
181
175
|
var METHOD_POOL = Symbol();
|
|
182
176
|
var MethodConfig = (function () {
|
|
183
177
|
function MethodConfig(name, options, callback) {
|
|
@@ -241,6 +235,14 @@ var MethodPool = (function () {
|
|
|
241
235
|
throw new Error("Method name cannot contain slashes: ".concat(name));
|
|
242
236
|
this.methods.set(name, new MethodConfig(name, options, callback));
|
|
243
237
|
};
|
|
238
|
+
MethodPool.prototype.transactionDisabled = function (name) {
|
|
239
|
+
var _a, _b;
|
|
240
|
+
if (string_util_class_1.StringUtil.isFalsyString(name))
|
|
241
|
+
return;
|
|
242
|
+
if (name.includes('/'))
|
|
243
|
+
throw new Error("Method name cannot contain slashes: ".concat(name));
|
|
244
|
+
return !!((_b = (_a = this.methods.get(name)) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.disableTransaction);
|
|
245
|
+
};
|
|
244
246
|
MethodPool.prototype.getCallFn = function (name) {
|
|
245
247
|
var config = this.methods.get(name);
|
|
246
248
|
if (!(config instanceof MethodConfig))
|
|
@@ -371,7 +373,7 @@ function HerbalGuard(options) {
|
|
|
371
373
|
var _a, e_1, _b, _c;
|
|
372
374
|
var _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
373
375
|
return __awaiter(this, void 0, void 0, function () {
|
|
374
|
-
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;
|
|
376
|
+
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_3, _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult, error_4, _q;
|
|
375
377
|
var _this = this;
|
|
376
378
|
return __generator(this, function (_r) {
|
|
377
379
|
switch (_r.label) {
|
|
@@ -440,11 +442,14 @@ function HerbalGuard(options) {
|
|
|
440
442
|
_.attempt(function () { return _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":request:body] ").concat(request.rawBody)); });
|
|
441
443
|
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;
|
|
442
444
|
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;
|
|
443
|
-
handlerName = string_util_class_1.StringUtil.isFalsyString(
|
|
444
|
-
|
|
445
|
+
handlerName = string_util_class_1.StringUtil.isFalsyString(request.methodName) ? rawHandlerName : request.methodName;
|
|
446
|
+
methodPool = getMethodPool(handlerPropertype);
|
|
447
|
+
authAdapters = (_h = methodPool === null || methodPool === void 0 ? void 0 : methodPool.getAuthAdapters) === null || _h === void 0 ? void 0 : _h.call(methodPool, handlerName);
|
|
445
448
|
if (authAdapters === null)
|
|
446
449
|
authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPropertype, handlerName);
|
|
447
|
-
if (!(!(sequelizeInstance instanceof Error) &&
|
|
450
|
+
if (!(!(sequelizeInstance instanceof Error) &&
|
|
451
|
+
!decorators_1.NoTransaction.isDisabled(handlerPropertype, rawHandlerName) &&
|
|
452
|
+
!((_j = methodPool === null || methodPool === void 0 ? void 0 : methodPool.transactionDisabled) === null || _j === void 0 ? void 0 : _j.call(methodPool, handlerName)))) return [3, 20];
|
|
448
453
|
_r.label = 16;
|
|
449
454
|
case 16:
|
|
450
455
|
_r.trys.push([16, 18, , 19]);
|
|
@@ -539,6 +544,7 @@ var ControllerUtil = (function () {
|
|
|
539
544
|
function ControllerUtil() {
|
|
540
545
|
}
|
|
541
546
|
ControllerUtil.create = function (createOptions) {
|
|
547
|
+
var logger = new common_1.Logger('Herbal');
|
|
542
548
|
function Controller(options) {
|
|
543
549
|
return function (target) {
|
|
544
550
|
var _a;
|
|
@@ -549,6 +555,7 @@ var ControllerUtil = (function () {
|
|
|
549
555
|
: createOptions.prefix
|
|
550
556
|
: options.prefix;
|
|
551
557
|
var controllerName = _.camelCase(target.name.replace(/Controller$/g, ''));
|
|
558
|
+
var paths = [];
|
|
552
559
|
if (!(options === null || options === void 0 ? void 0 : options.ignoreControllerNamePostfix)) {
|
|
553
560
|
finalPrefix += "".concat(((_a = finalPrefix === null || finalPrefix === void 0 ? void 0 : finalPrefix.endsWith) === null || _a === void 0 ? void 0 : _a.call(finalPrefix, '/')) ? '' : '/').concat(controllerName);
|
|
554
561
|
}
|
|
@@ -556,7 +563,7 @@ var ControllerUtil = (function () {
|
|
|
556
563
|
if (string_util_class_1.StringUtil.isFalsyString(name) || typeof callback !== 'function')
|
|
557
564
|
return;
|
|
558
565
|
methodPool.registerMethod(name, options, callback);
|
|
559
|
-
|
|
566
|
+
paths.push(name.startsWith('/') ? name : "/".concat(name));
|
|
560
567
|
};
|
|
561
568
|
if (!finalPrefix.startsWith('/'))
|
|
562
569
|
finalPrefix = "/".concat(finalPrefix);
|
|
@@ -565,6 +572,20 @@ var ControllerUtil = (function () {
|
|
|
565
572
|
Reflect.defineMetadata(METHOD_POOL, methodPool, target.prototype);
|
|
566
573
|
if (typeof (options === null || options === void 0 ? void 0 : options.methods) === 'function')
|
|
567
574
|
options.methods(register);
|
|
575
|
+
Object.defineProperty(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL, {
|
|
576
|
+
enumerable: false,
|
|
577
|
+
writable: false,
|
|
578
|
+
value: function (request) {
|
|
579
|
+
return this[HANDLE_REQUEST_SYMBOL].call(this, request);
|
|
580
|
+
},
|
|
581
|
+
});
|
|
582
|
+
(0, common_1.Req)()(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL, 0);
|
|
583
|
+
if (paths.length > 0) {
|
|
584
|
+
(0, common_2.Post)(paths)(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL, Object.getOwnPropertyDescriptor(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL));
|
|
585
|
+
paths.forEach(function (path) {
|
|
586
|
+
logger.log("Mapped method: /".concat(controllerName).concat(path));
|
|
587
|
+
});
|
|
588
|
+
}
|
|
568
589
|
(0, common_2.Controller)(finalPrefix)(target);
|
|
569
590
|
(0, common_2.UseInterceptors)(ControllerInterceptor)(target);
|
|
570
591
|
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);
|