@open-norantec/herbal 1.0.2-alpha.33 → 1.0.2-alpha.35
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/abstracts/client.abstract.class.js +4 -4
- package/dist/core.d.ts +61 -3
- package/dist/core.js +484 -46
- package/dist/utilities/index.d.ts +0 -1
- package/dist/utilities/index.js +0 -1
- package/package.json +1 -1
- package/dist/utilities/controller-util.class.d.ts +0 -62
- package/dist/utilities/controller-util.class.js +0 -488
|
@@ -4,7 +4,7 @@ exports.Client = void 0;
|
|
|
4
4
|
require("reflect-metadata");
|
|
5
5
|
var nest_util_class_1 = require("../utilities/nest-util.class");
|
|
6
6
|
var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
|
|
7
|
-
var
|
|
7
|
+
var core_1 = require("../core");
|
|
8
8
|
var Client = (function () {
|
|
9
9
|
function Client(options) {
|
|
10
10
|
this.options = options;
|
|
@@ -22,10 +22,10 @@ var Client = (function () {
|
|
|
22
22
|
if (string_util_class_1.StringUtil.isFalsyString(group) && typeof group !== 'undefined')
|
|
23
23
|
return;
|
|
24
24
|
nest_util_class_1.NestUtil.getControllerClasses(this.options.Module).forEach(function (Class) {
|
|
25
|
-
if (string_util_class_1.StringUtil.isFalsyString(Class === null || Class === void 0 ? void 0 : Class.name) || !(0,
|
|
25
|
+
if (string_util_class_1.StringUtil.isFalsyString(Class === null || Class === void 0 ? void 0 : Class.name) || !(0, core_1.isHerbalController)(Class))
|
|
26
26
|
return;
|
|
27
|
-
var controllerName = (0,
|
|
28
|
-
var pool =
|
|
27
|
+
var controllerName = (0, core_1.getControllerName)(Class);
|
|
28
|
+
var pool = (0, core_1.getMethodPool)(Class.prototype);
|
|
29
29
|
if (string_util_class_1.StringUtil.isFalsyString(controllerName) || pool === null)
|
|
30
30
|
return;
|
|
31
31
|
Object.entries(pool.getOpenAPIPathsObject(group)).forEach(function (_a) {
|
package/dist/core.d.ts
CHANGED
|
@@ -1,13 +1,71 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
2
|
import { HeaderUtil } from '@open-norantec/utilities/dist/header-util.class';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
-
import { Request } from './types/request.type';
|
|
4
|
+
import { Request, RequestContext } from './types/request.type';
|
|
5
|
+
import 'reflect-metadata';
|
|
6
|
+
import { Request as ExpressRequest } from 'express';
|
|
7
|
+
import { Constructor } from 'type-fest';
|
|
8
|
+
import { AuthAdapter } from './abstracts/auth-adapter.abstract.class';
|
|
9
|
+
import { PathsObject } from 'zod-openapi/dist/openapi3-ts/dist/model/openapi31';
|
|
5
10
|
export * from '@nestjs/core';
|
|
11
|
+
declare const HANDLE_REQUEST_SYMBOL: unique symbol;
|
|
6
12
|
export type MethodHandler<IS extends z.Schema<any>, OS extends z.Schema<any>> = (request: Request, input: unknown, headers: ReturnType<typeof HeaderUtil.parse>) => Promise<{
|
|
7
13
|
request: z.infer<IS>;
|
|
8
14
|
response: z.infer<OS>;
|
|
9
15
|
}>;
|
|
10
16
|
export declare class HerbalController {
|
|
11
|
-
private
|
|
12
|
-
|
|
17
|
+
private [HANDLE_REQUEST_SYMBOL];
|
|
18
|
+
}
|
|
19
|
+
type ClientGroups = Array<string> | null | undefined;
|
|
20
|
+
type ClienttGroupsFactory = (defaultGroupName: string) => ClientGroups;
|
|
21
|
+
export interface MethodRegisterOptions<IS extends z.Schema<any>, OS extends z.Schema<any>> {
|
|
22
|
+
inputSchema: IS;
|
|
23
|
+
outputSchema: OS;
|
|
24
|
+
authAdapters?: Constructor<AuthAdapter>[];
|
|
25
|
+
clientGroups?: ClientGroups | ClienttGroupsFactory;
|
|
26
|
+
disableTransaction?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export type MethodRegisterFn<C> = <IS extends z.Schema<any>, OS extends z.Schema<any>>(name: string, options: MethodRegisterOptions<IS, OS>, callback: MethodCallback<IS, OS, C>) => void;
|
|
29
|
+
export interface MethodContext<IS extends z.Schema<any>> extends RequestContext {
|
|
30
|
+
headers: ReturnType<typeof HeaderUtil.parse>;
|
|
31
|
+
input: z.infer<IS>;
|
|
32
|
+
url: string;
|
|
33
|
+
}
|
|
34
|
+
export type MethodCallContext<IS extends z.Schema<any>> = Omit<MethodContext<IS>, 'input'>;
|
|
35
|
+
export type MethodCallback<IS extends z.Schema<any>, OS extends z.Schema<any>, C> = (this: C, context: MethodContext<IS>) => Promise<z.infer<OS>>;
|
|
36
|
+
declare class MethodConfig<IS extends z.Schema<any>, OS extends z.Schema<any>, C> {
|
|
37
|
+
readonly name: string;
|
|
38
|
+
readonly options: MethodRegisterOptions<IS, OS>;
|
|
39
|
+
protected readonly callback: MethodCallback<IS, OS, C>;
|
|
40
|
+
constructor(name: string, options: MethodRegisterOptions<IS, OS>, callback: MethodCallback<IS, OS, C>);
|
|
41
|
+
call(controller: C, callContext: MethodCallContext<IS>): Promise<z.TypeOf<OS>>;
|
|
42
|
+
}
|
|
43
|
+
declare class MethodPool {
|
|
44
|
+
protected readonly methods: Map<string, MethodConfig<any, any, any>>;
|
|
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
|
+
getCallFn(name: string): ((controller: any, callContext: MethodCallContext<any>) => Promise<any>) | null;
|
|
47
|
+
getAuthAdapters(name: string): Constructor<AuthAdapter>[] | null | undefined;
|
|
48
|
+
getOpenAPIPathsObject(group?: string): PathsObject;
|
|
49
|
+
}
|
|
50
|
+
export declare function isHerbalController(target: Function): boolean;
|
|
51
|
+
export declare function getControllerName(target: Function): any;
|
|
52
|
+
export interface HerbalControllerOptions<C> {
|
|
53
|
+
ignoreControllerNamePostfix?: boolean;
|
|
54
|
+
prefix?: string;
|
|
55
|
+
useHeadGuards?: Constructor<any>[];
|
|
56
|
+
useTailGuards?: Constructor<any>[];
|
|
57
|
+
methods?: (register: MethodRegisterFn<C>) => void;
|
|
58
|
+
}
|
|
59
|
+
export interface ControllerUtilCreateOptions {
|
|
60
|
+
prefix?: string;
|
|
61
|
+
useGuards?: Constructor<any>[];
|
|
62
|
+
getTraceId?: (request: ExpressRequest) => string;
|
|
63
|
+
}
|
|
64
|
+
export declare function getMethodPool(targetPrototype: object): MethodPool | null;
|
|
65
|
+
export declare class ControllerUtil {
|
|
66
|
+
static create(createOptions?: ControllerUtilCreateOptions): {
|
|
67
|
+
<C>(options?: HerbalControllerOptions<C> | undefined): ClassDecorator;
|
|
68
|
+
isHerbalController: typeof isHerbalController;
|
|
69
|
+
getControllerName: typeof getControllerName;
|
|
70
|
+
};
|
|
13
71
|
}
|
package/dist/core.js
CHANGED
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
2
13
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
14
|
if (k2 === undefined) k2 = k;
|
|
4
15
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -22,9 +33,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
22
33
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
23
34
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
24
35
|
};
|
|
25
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
26
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
27
|
-
};
|
|
28
36
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
37
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
38
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -61,50 +69,87 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
61
69
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
70
|
}
|
|
63
71
|
};
|
|
72
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
73
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
74
|
+
var m = o[Symbol.asyncIterator], i;
|
|
75
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
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
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
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
|
+
};
|
|
64
88
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
65
|
-
exports.HerbalController = void 0;
|
|
89
|
+
exports.ControllerUtil = exports.getMethodPool = exports.getControllerName = exports.isHerbalController = exports.HerbalController = void 0;
|
|
66
90
|
require("reflect-metadata");
|
|
67
91
|
var common_1 = require("@nestjs/common");
|
|
68
92
|
var header_util_class_1 = require("@open-norantec/utilities/dist/header-util.class");
|
|
93
|
+
var zod_1 = require("zod");
|
|
69
94
|
var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
|
|
70
95
|
var utilities_1 = require("@open-norantec/utilities");
|
|
71
|
-
|
|
96
|
+
require("reflect-metadata");
|
|
97
|
+
var common_2 = require("@nestjs/common");
|
|
98
|
+
var _ = require("lodash");
|
|
99
|
+
var uuid_util_class_1 = require("@open-norantec/utilities/dist/uuid-util.class");
|
|
100
|
+
var common_3 = require("@nestjs/common");
|
|
101
|
+
var headers_constant_1 = require("./constants/headers.constant");
|
|
102
|
+
var core_1 = require("@nestjs/core");
|
|
103
|
+
var auth_adapter_decorator_1 = require("./decorators/auth-adapter.decorator");
|
|
104
|
+
var rxjs_1 = require("rxjs");
|
|
105
|
+
var operators_1 = require("rxjs/operators");
|
|
106
|
+
var logger_service_1 = require("./modules/logger/logger.service");
|
|
107
|
+
var sequelize_typescript_1 = require("sequelize-typescript");
|
|
108
|
+
var decorators_1 = require("./decorators");
|
|
109
|
+
var zod_openapi_1 = require("zod-openapi");
|
|
72
110
|
__exportStar(require("@nestjs/core"), exports);
|
|
73
|
-
var
|
|
111
|
+
var HANDLE_REQUEST_SYMBOL = Symbol();
|
|
112
|
+
var HANDLE_REQUEST_INSTANCE_SYMBOL = '$handleRequestInstance';
|
|
113
|
+
var HerbalController = (function () {
|
|
74
114
|
function HerbalController() {
|
|
75
115
|
}
|
|
76
|
-
HerbalController.prototype
|
|
77
|
-
var _a, _b, _c, _d;
|
|
116
|
+
HerbalController.prototype[HANDLE_REQUEST_SYMBOL] = function (request) {
|
|
117
|
+
var _a, _b, _c, _d, _e, _f;
|
|
78
118
|
return __awaiter(this, void 0, void 0, function () {
|
|
79
|
-
var result, error_1;
|
|
80
|
-
var
|
|
119
|
+
var callFn, result, error_1;
|
|
120
|
+
var _g;
|
|
81
121
|
var _this = this;
|
|
82
|
-
return __generator(this, function (
|
|
83
|
-
switch (
|
|
122
|
+
return __generator(this, function (_h) {
|
|
123
|
+
switch (_h.label) {
|
|
84
124
|
case 0:
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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, {
|
|
88
133
|
authenticateResult: request.authenticateResult,
|
|
89
|
-
headers: header_util_class_1.HeaderUtil.parse((
|
|
134
|
+
headers: header_util_class_1.HeaderUtil.parse((_c = request.headers) !== null && _c !== void 0 ? _c : {}),
|
|
90
135
|
methodName: request.methodName,
|
|
91
136
|
rawBody: request.rawBody,
|
|
92
137
|
traceId: request.traceId,
|
|
93
138
|
transaction: request.transaction,
|
|
94
139
|
url: request.originalUrl,
|
|
95
140
|
})];
|
|
96
|
-
case
|
|
97
|
-
result = (
|
|
98
|
-
|
|
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)
|
|
99
144
|
? null
|
|
100
145
|
: request.authenticateResult.nextToken,
|
|
101
|
-
|
|
102
|
-
return [4, ((
|
|
103
|
-
case 2:
|
|
104
|
-
_f.sent();
|
|
105
|
-
return [2, result];
|
|
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))];
|
|
106
148
|
case 3:
|
|
107
|
-
|
|
149
|
+
_h.sent();
|
|
150
|
+
return [2, result];
|
|
151
|
+
case 4:
|
|
152
|
+
error_1 = _h.sent();
|
|
108
153
|
return [4, utilities_1.AttemptUtil.execPromise((function () { return __awaiter(_this, void 0, void 0, function () {
|
|
109
154
|
var _a, _b;
|
|
110
155
|
return __generator(this, function (_c) {
|
|
@@ -116,36 +161,429 @@ var HerbalController = exports.HerbalController = (function () {
|
|
|
116
161
|
}
|
|
117
162
|
});
|
|
118
163
|
}); })())];
|
|
119
|
-
case
|
|
120
|
-
|
|
164
|
+
case 5:
|
|
165
|
+
_h.sent();
|
|
121
166
|
throw error_1;
|
|
122
|
-
case
|
|
167
|
+
case 6: return [2];
|
|
123
168
|
}
|
|
124
169
|
});
|
|
125
170
|
});
|
|
126
171
|
};
|
|
127
|
-
HerbalController
|
|
128
|
-
|
|
172
|
+
return HerbalController;
|
|
173
|
+
}());
|
|
174
|
+
exports.HerbalController = HerbalController;
|
|
175
|
+
var METHOD_POOL = Symbol();
|
|
176
|
+
var MethodConfig = (function () {
|
|
177
|
+
function MethodConfig(name, options, callback) {
|
|
178
|
+
this.name = name;
|
|
179
|
+
this.options = options;
|
|
180
|
+
this.callback = callback;
|
|
181
|
+
}
|
|
182
|
+
MethodConfig.prototype.call = function (controller, callContext) {
|
|
183
|
+
var _a, _b, _c, _d, _e, _f;
|
|
129
184
|
return __awaiter(this, void 0, void 0, function () {
|
|
130
|
-
var
|
|
131
|
-
return __generator(this, function (
|
|
132
|
-
switch (
|
|
185
|
+
var inputSchema, outputSchema, parsedBody_1, input, rawResponse_1, response, error_2;
|
|
186
|
+
return __generator(this, function (_g) {
|
|
187
|
+
switch (_g.label) {
|
|
133
188
|
case 0:
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
189
|
+
inputSchema = this.options.inputSchema;
|
|
190
|
+
outputSchema = this.options.outputSchema;
|
|
191
|
+
_g.label = 1;
|
|
192
|
+
case 1:
|
|
193
|
+
_g.trys.push([1, 3, , 4]);
|
|
194
|
+
parsedBody_1 = _.attempt(function () { return JSON.parse((callContext === null || callContext === void 0 ? void 0 : callContext.rawBody) || ''); });
|
|
195
|
+
input = _.attempt(function () { return (parsedBody_1 instanceof Error ? undefined : inputSchema.parse(parsedBody_1)); });
|
|
196
|
+
if (input instanceof zod_1.ZodError) {
|
|
197
|
+
throw new common_2.BadRequestException({
|
|
198
|
+
from: 'request',
|
|
199
|
+
invalidParams: (_c = (_b = (_a = input === null || input === void 0 ? void 0 : input.issues) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.call(_a, function (item) { var _a, _b; return (_b = (_a = item === null || item === void 0 ? void 0 : item.path) === null || _a === void 0 ? void 0 : _a.join) === null || _b === void 0 ? void 0 : _b.call(_a, '.'); })) !== null && _c !== void 0 ? _c : [],
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
else if (input instanceof Error)
|
|
203
|
+
throw input;
|
|
204
|
+
return [4, this.callback.call(controller, __assign(__assign({}, callContext), { input: input }))];
|
|
205
|
+
case 2:
|
|
206
|
+
rawResponse_1 = _g.sent();
|
|
207
|
+
response = _.attempt(function () { return outputSchema.parse(rawResponse_1); });
|
|
208
|
+
if (response instanceof zod_1.ZodError) {
|
|
209
|
+
throw new common_2.BadRequestException({
|
|
210
|
+
from: 'response',
|
|
211
|
+
invalidParams: (_f = (_e = (_d = response === null || response === void 0 ? void 0 : response.issues) === null || _d === void 0 ? void 0 : _d.map) === null || _e === void 0 ? void 0 : _e.call(_d, function (item) { var _a, _b; return (_b = (_a = item === null || item === void 0 ? void 0 : item.path) === null || _a === void 0 ? void 0 : _a.join) === null || _b === void 0 ? void 0 : _b.call(_a, '.'); })) !== null && _f !== void 0 ? _f : [],
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
else if (response instanceof Error)
|
|
215
|
+
throw response;
|
|
216
|
+
return [2, response];
|
|
217
|
+
case 3:
|
|
218
|
+
error_2 = _g.sent();
|
|
219
|
+
throw error_2;
|
|
220
|
+
case 4: return [2];
|
|
139
221
|
}
|
|
140
222
|
});
|
|
141
223
|
});
|
|
142
224
|
};
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
225
|
+
return MethodConfig;
|
|
226
|
+
}());
|
|
227
|
+
var MethodPool = (function () {
|
|
228
|
+
function MethodPool() {
|
|
229
|
+
this.methods = new Map();
|
|
230
|
+
}
|
|
231
|
+
MethodPool.prototype.registerMethod = function (name, options, callback) {
|
|
232
|
+
if (string_util_class_1.StringUtil.isFalsyString(name))
|
|
233
|
+
return;
|
|
234
|
+
if (name.includes('/'))
|
|
235
|
+
throw new Error("Method name cannot contain slashes: ".concat(name));
|
|
236
|
+
this.methods.set(name, new MethodConfig(name, options, callback));
|
|
237
|
+
};
|
|
238
|
+
MethodPool.prototype.getCallFn = function (name) {
|
|
239
|
+
var config = this.methods.get(name);
|
|
240
|
+
if (!(config instanceof MethodConfig))
|
|
241
|
+
return null;
|
|
242
|
+
return config.call.bind(config);
|
|
243
|
+
};
|
|
244
|
+
MethodPool.prototype.getAuthAdapters = function (name) {
|
|
245
|
+
var config = this.methods.get(name);
|
|
246
|
+
if (!(config instanceof MethodConfig))
|
|
247
|
+
return null;
|
|
248
|
+
return config.options.authAdapters;
|
|
249
|
+
};
|
|
250
|
+
MethodPool.prototype.getOpenAPIPathsObject = function (group) {
|
|
251
|
+
var result = {};
|
|
252
|
+
Array.from(this.methods.entries()).forEach(function (_a) {
|
|
253
|
+
var _b;
|
|
254
|
+
var name = _a[0], config = _a[1];
|
|
255
|
+
var defaultGroupName = "".concat(Date.now(), "_").concat(Math.random().toString(16).slice(2));
|
|
256
|
+
var currentGroupName = string_util_class_1.StringUtil.isFalsyString(group) ? defaultGroupName : group;
|
|
257
|
+
var clientGroups = typeof config.options.clientGroups === 'function'
|
|
258
|
+
? config.options.clientGroups(defaultGroupName)
|
|
259
|
+
: (_b = config === null || config === void 0 ? void 0 : config.options) === null || _b === void 0 ? void 0 : _b.clientGroups;
|
|
260
|
+
if (!Array.isArray(clientGroups) && !string_util_class_1.StringUtil.isFalsyString(group) && defaultGroupName !== group)
|
|
261
|
+
return;
|
|
262
|
+
if (Array.isArray(clientGroups) && !clientGroups.includes(currentGroupName))
|
|
263
|
+
return;
|
|
264
|
+
result["/".concat(name)] = {
|
|
265
|
+
post: {
|
|
266
|
+
requestBody: {
|
|
267
|
+
description: 'Request body for method ' + name,
|
|
268
|
+
required: true,
|
|
269
|
+
content: {
|
|
270
|
+
'application/json': {
|
|
271
|
+
schema: (0, zod_openapi_1.createSchema)(config.options.inputSchema).schema,
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
responses: {
|
|
276
|
+
'200': {
|
|
277
|
+
description: 'Response for method ' + name,
|
|
278
|
+
content: {
|
|
279
|
+
'application/json': (0, zod_openapi_1.createSchema)(zod_1.z.object({
|
|
280
|
+
data: config.options.outputSchema,
|
|
281
|
+
token: zod_1.z.string().nullable(),
|
|
282
|
+
})),
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
});
|
|
289
|
+
return result;
|
|
290
|
+
};
|
|
291
|
+
return MethodPool;
|
|
292
|
+
}());
|
|
293
|
+
var IS_HERBAL_CONTROLLER = Symbol();
|
|
294
|
+
var CONTROLLER_NAME = Symbol();
|
|
295
|
+
function isHerbalController(target) {
|
|
296
|
+
return _.attempt(function () { return Reflect.getMetadata(IS_HERBAL_CONTROLLER, target.prototype); }) === true;
|
|
297
|
+
}
|
|
298
|
+
exports.isHerbalController = isHerbalController;
|
|
299
|
+
function getControllerName(target) {
|
|
300
|
+
return Reflect.getMetadata(CONTROLLER_NAME, target.prototype);
|
|
301
|
+
}
|
|
302
|
+
exports.getControllerName = getControllerName;
|
|
303
|
+
var ControllerInterceptor = (function () {
|
|
304
|
+
function ControllerInterceptor(ref) {
|
|
305
|
+
this.ref = ref;
|
|
306
|
+
}
|
|
307
|
+
ControllerInterceptor.prototype.intercept = function (context, next) {
|
|
308
|
+
var _this = this;
|
|
309
|
+
var request = context.switchToHttp().getRequest();
|
|
310
|
+
if (isHerbalController(context.getClass())) {
|
|
311
|
+
_.attempt(function () {
|
|
312
|
+
_this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:url] ").concat(request === null || request === void 0 ? void 0 : request.originalUrl));
|
|
313
|
+
});
|
|
314
|
+
_.attempt(function () {
|
|
315
|
+
return _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":request:headers] ").concat(JSON.stringify(request === null || request === void 0 ? void 0 : request.headers)));
|
|
316
|
+
});
|
|
317
|
+
return this.handle(next, request);
|
|
318
|
+
}
|
|
319
|
+
return next.handle();
|
|
320
|
+
};
|
|
321
|
+
ControllerInterceptor.prototype.getLogger = function () {
|
|
322
|
+
var loggerService = this.ref.get(logger_service_1.LoggerService, { strict: false });
|
|
323
|
+
if (!(loggerService instanceof logger_service_1.LoggerService)) {
|
|
324
|
+
return {
|
|
325
|
+
log: function () { },
|
|
326
|
+
error: function () { },
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
return loggerService;
|
|
330
|
+
};
|
|
331
|
+
ControllerInterceptor.prototype.handle = function (next, request) {
|
|
332
|
+
var _this = this;
|
|
333
|
+
return next.handle().pipe((0, operators_1.map)(function (data) {
|
|
334
|
+
_.attempt(function () {
|
|
335
|
+
_this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:raw] ").concat(JSON.stringify(data)));
|
|
336
|
+
});
|
|
337
|
+
_.attempt(function () {
|
|
338
|
+
_this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:final] ").concat(JSON.stringify(data)));
|
|
339
|
+
});
|
|
340
|
+
return data;
|
|
341
|
+
}), (0, operators_1.catchError)(function (error) {
|
|
342
|
+
_.attempt(function () {
|
|
343
|
+
_this.getLogger().error("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:error:message] ").concat(error === null || error === void 0 ? void 0 : error.message));
|
|
344
|
+
});
|
|
345
|
+
_.attempt(function () {
|
|
346
|
+
_this.getLogger().error("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:error:stack] ").concat(error === null || error === void 0 ? void 0 : error.stack));
|
|
347
|
+
});
|
|
348
|
+
_this.getLogger().error("Got error when handling route in interceptor: ".concat(error === null || error === void 0 ? void 0 : error.message, " ").concat(error === null || error === void 0 ? void 0 : error.stack));
|
|
349
|
+
_.attempt(function () { var _a, _b, _c, _d; return (_d = (_c = (_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)) === null || _c === void 0 ? void 0 : _c.catch) === null || _d === void 0 ? void 0 : _d.call(_c, function () { }); });
|
|
350
|
+
return (0, rxjs_1.throwError)(function () { return error; });
|
|
351
|
+
}));
|
|
352
|
+
};
|
|
353
|
+
ControllerInterceptor = __decorate([
|
|
354
|
+
(0, common_3.Injectable)(),
|
|
355
|
+
__metadata("design:paramtypes", [core_1.ModuleRef])
|
|
356
|
+
], ControllerInterceptor);
|
|
357
|
+
return ControllerInterceptor;
|
|
358
|
+
}());
|
|
359
|
+
function HerbalGuard(options) {
|
|
360
|
+
var HerbalGuardMixin = (function () {
|
|
361
|
+
function HerbalGuardMixin(ref) {
|
|
362
|
+
this.ref = ref;
|
|
363
|
+
}
|
|
364
|
+
HerbalGuardMixin.prototype.canActivate = function (context) {
|
|
365
|
+
var _a, e_1, _b, _c;
|
|
366
|
+
var _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
367
|
+
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;
|
|
369
|
+
var _this = this;
|
|
370
|
+
return __generator(this, function (_r) {
|
|
371
|
+
switch (_r.label) {
|
|
372
|
+
case 0:
|
|
373
|
+
sequelizeInstance = _.attempt(function () { return _this.ref.get(sequelize_typescript_1.Sequelize, { strict: false }); });
|
|
374
|
+
transaction = undefined;
|
|
375
|
+
request = context.switchToHttp().getRequest();
|
|
376
|
+
response = context.switchToHttp().getResponse();
|
|
377
|
+
traceId = typeof (options === null || options === void 0 ? void 0 : options.getTraceId) === 'function'
|
|
378
|
+
? _.attempt(function () { return options.getTraceId(request); })
|
|
379
|
+
: uuid_util_class_1.UUIDUtil.generateV4();
|
|
380
|
+
if (traceId instanceof Error || string_util_class_1.StringUtil.isFalsyString(traceId))
|
|
381
|
+
traceId = uuid_util_class_1.UUIDUtil.generateV4();
|
|
382
|
+
request.traceId = traceId;
|
|
383
|
+
request.methodName = request.url.split('/').pop();
|
|
384
|
+
response.setHeader(headers_constant_1.HEADERS.TRACE_ID, traceId);
|
|
385
|
+
chunks = [];
|
|
386
|
+
_r.label = 1;
|
|
387
|
+
case 1:
|
|
388
|
+
_r.trys.push([1, 14, , 15]);
|
|
389
|
+
_r.label = 2;
|
|
390
|
+
case 2:
|
|
391
|
+
_r.trys.push([2, 7, 8, 13]);
|
|
392
|
+
_o = true, request_1 = __asyncValues(request);
|
|
393
|
+
_r.label = 3;
|
|
394
|
+
case 3: return [4, request_1.next()];
|
|
395
|
+
case 4:
|
|
396
|
+
if (!(request_1_1 = _r.sent(), _a = request_1_1.done, !_a)) return [3, 6];
|
|
397
|
+
_c = request_1_1.value;
|
|
398
|
+
_o = false;
|
|
399
|
+
chunk = _c;
|
|
400
|
+
chunks.push(chunk);
|
|
401
|
+
_r.label = 5;
|
|
402
|
+
case 5:
|
|
403
|
+
_o = true;
|
|
404
|
+
return [3, 3];
|
|
405
|
+
case 6: return [3, 13];
|
|
406
|
+
case 7:
|
|
407
|
+
e_1_1 = _r.sent();
|
|
408
|
+
e_1 = { error: e_1_1 };
|
|
409
|
+
return [3, 13];
|
|
410
|
+
case 8:
|
|
411
|
+
_r.trys.push([8, , 11, 12]);
|
|
412
|
+
if (!(!_o && !_a && (_b = request_1.return))) return [3, 10];
|
|
413
|
+
return [4, _b.call(request_1)];
|
|
414
|
+
case 9:
|
|
415
|
+
_r.sent();
|
|
416
|
+
_r.label = 10;
|
|
417
|
+
case 10: return [3, 12];
|
|
418
|
+
case 11:
|
|
419
|
+
if (e_1) throw e_1.error;
|
|
420
|
+
return [7];
|
|
421
|
+
case 12: return [7];
|
|
422
|
+
case 13: return [3, 15];
|
|
423
|
+
case 14:
|
|
424
|
+
_p = _r.sent();
|
|
425
|
+
return [3, 15];
|
|
426
|
+
case 15:
|
|
427
|
+
parsedBody = _.attempt(function () { return Buffer.concat(chunks).toString('utf8'); });
|
|
428
|
+
if (!(parsedBody instanceof Error)) {
|
|
429
|
+
request.rawBody = parsedBody;
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
request.rawBody = null;
|
|
433
|
+
}
|
|
434
|
+
_.attempt(function () { return _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":request:body] ").concat(request.rawBody)); });
|
|
435
|
+
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
|
+
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
|
+
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);
|
|
439
|
+
if (authAdapters === null)
|
|
440
|
+
authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPropertype, handlerName);
|
|
441
|
+
if (!(!(sequelizeInstance instanceof Error) && !decorators_1.NoTransaction.isDisabled(handlerPropertype, handlerName))) return [3, 20];
|
|
442
|
+
_r.label = 16;
|
|
443
|
+
case 16:
|
|
444
|
+
_r.trys.push([16, 18, , 19]);
|
|
445
|
+
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); }))];
|
|
446
|
+
case 17:
|
|
447
|
+
transaction = _r.sent();
|
|
448
|
+
request.transaction = transaction;
|
|
449
|
+
this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Started transaction for route: ").concat(handlerName));
|
|
450
|
+
return [3, 19];
|
|
451
|
+
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));
|
|
455
|
+
}
|
|
456
|
+
return [3, 19];
|
|
457
|
+
case 19: return [3, 21];
|
|
458
|
+
case 20:
|
|
459
|
+
if (decorators_1.NoTransaction.isDisabled(handlerPropertype, handlerName)) {
|
|
460
|
+
this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Transaction is disabled for this route: ").concat(handlerName));
|
|
461
|
+
}
|
|
462
|
+
_r.label = 21;
|
|
463
|
+
case 21:
|
|
464
|
+
_r.trys.push([21, 27, , 32]);
|
|
465
|
+
if (!(Array.isArray(authAdapters) && authAdapters.length > 0)) return [3, 26];
|
|
466
|
+
_i = 0, authAdapters_1 = authAdapters;
|
|
467
|
+
_r.label = 22;
|
|
468
|
+
case 22:
|
|
469
|
+
if (!(_i < authAdapters_1.length)) return [3, 25];
|
|
470
|
+
AuthAdapterClass = authAdapters_1[_i];
|
|
471
|
+
adapter = new AuthAdapterClass(request, this.ref);
|
|
472
|
+
if (!adapter.match())
|
|
473
|
+
return [3, 24];
|
|
474
|
+
return [4, adapter.authenticate(transaction)];
|
|
475
|
+
case 23:
|
|
476
|
+
authenticateResult = _r.sent();
|
|
477
|
+
if (!authenticateResult)
|
|
478
|
+
return [3, 25];
|
|
479
|
+
request.authenticateResult = __assign({ AuthenticatorClass: AuthAdapterClass }, authenticateResult);
|
|
480
|
+
return [2, true];
|
|
481
|
+
case 24:
|
|
482
|
+
_i++;
|
|
483
|
+
return [3, 22];
|
|
484
|
+
case 25: throw new common_3.UnauthorizedException();
|
|
485
|
+
case 26: return [3, 32];
|
|
486
|
+
case 27:
|
|
487
|
+
error_4 = _r.sent();
|
|
488
|
+
_r.label = 28;
|
|
489
|
+
case 28:
|
|
490
|
+
_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));
|
|
493
|
+
}
|
|
494
|
+
return [4, ((_m = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _m === void 0 ? void 0 : _m.call(transaction))];
|
|
495
|
+
case 29:
|
|
496
|
+
_r.sent();
|
|
497
|
+
return [3, 31];
|
|
498
|
+
case 30:
|
|
499
|
+
_q = _r.sent();
|
|
500
|
+
return [3, 31];
|
|
501
|
+
case 31: throw error_4;
|
|
502
|
+
case 32: return [2, true];
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
};
|
|
507
|
+
HerbalGuardMixin.prototype.getLogger = function () {
|
|
508
|
+
var loggerService = this.ref.get(logger_service_1.LoggerService, { strict: false });
|
|
509
|
+
if (!(loggerService instanceof logger_service_1.LoggerService)) {
|
|
510
|
+
return {
|
|
511
|
+
log: function () { },
|
|
512
|
+
error: function () { },
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
return loggerService;
|
|
516
|
+
};
|
|
517
|
+
HerbalGuardMixin = __decorate([
|
|
518
|
+
(0, common_3.Injectable)(),
|
|
519
|
+
__metadata("design:paramtypes", [core_1.ModuleRef])
|
|
520
|
+
], HerbalGuardMixin);
|
|
521
|
+
return HerbalGuardMixin;
|
|
522
|
+
}());
|
|
523
|
+
return (0, common_2.mixin)(HerbalGuardMixin);
|
|
524
|
+
}
|
|
525
|
+
function getMethodPool(targetPrototype) {
|
|
526
|
+
var methodPool = Reflect.getMetadata(METHOD_POOL, targetPrototype);
|
|
527
|
+
if (!(methodPool instanceof MethodPool))
|
|
528
|
+
return null;
|
|
529
|
+
return methodPool;
|
|
530
|
+
}
|
|
531
|
+
exports.getMethodPool = getMethodPool;
|
|
532
|
+
var ControllerUtil = (function () {
|
|
533
|
+
function ControllerUtil() {
|
|
534
|
+
}
|
|
535
|
+
ControllerUtil.create = function (createOptions) {
|
|
536
|
+
var logger = new common_1.Logger('Herbal');
|
|
537
|
+
function Controller(options) {
|
|
538
|
+
return function (target) {
|
|
539
|
+
var _a;
|
|
540
|
+
var methodPool = new MethodPool();
|
|
541
|
+
var finalPrefix = string_util_class_1.StringUtil.isFalsyString(options === null || options === void 0 ? void 0 : options.prefix)
|
|
542
|
+
? string_util_class_1.StringUtil.isFalsyString(createOptions === null || createOptions === void 0 ? void 0 : createOptions.prefix)
|
|
543
|
+
? ''
|
|
544
|
+
: createOptions.prefix
|
|
545
|
+
: options.prefix;
|
|
546
|
+
var controllerName = _.camelCase(target.name.replace(/Controller$/g, ''));
|
|
547
|
+
var paths = [];
|
|
548
|
+
if (!(options === null || options === void 0 ? void 0 : options.ignoreControllerNamePostfix)) {
|
|
549
|
+
finalPrefix += "".concat(((_a = finalPrefix === null || finalPrefix === void 0 ? void 0 : finalPrefix.endsWith) === null || _a === void 0 ? void 0 : _a.call(finalPrefix, '/')) ? '' : '/').concat(controllerName);
|
|
550
|
+
}
|
|
551
|
+
var register = function (name, options, callback) {
|
|
552
|
+
if (string_util_class_1.StringUtil.isFalsyString(name) || typeof callback !== 'function')
|
|
553
|
+
return;
|
|
554
|
+
methodPool.registerMethod(name, options, callback);
|
|
555
|
+
paths.push(name.startsWith('/') ? name : "/".concat(name));
|
|
556
|
+
};
|
|
557
|
+
if (!finalPrefix.startsWith('/'))
|
|
558
|
+
finalPrefix = "/".concat(finalPrefix);
|
|
559
|
+
Reflect.defineMetadata(IS_HERBAL_CONTROLLER, true, target.prototype);
|
|
560
|
+
Reflect.defineMetadata(CONTROLLER_NAME, controllerName, target.prototype);
|
|
561
|
+
Reflect.defineMetadata(METHOD_POOL, methodPool, target.prototype);
|
|
562
|
+
if (typeof (options === null || options === void 0 ? void 0 : options.methods) === 'function')
|
|
563
|
+
options.methods(register);
|
|
564
|
+
Object.defineProperty(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL, {
|
|
565
|
+
enumerable: false,
|
|
566
|
+
writable: false,
|
|
567
|
+
value: function (request) {
|
|
568
|
+
return this[HANDLE_REQUEST_SYMBOL].call(this, request);
|
|
569
|
+
},
|
|
570
|
+
});
|
|
571
|
+
(0, common_1.Req)()(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL, 0);
|
|
572
|
+
if (paths.length > 0) {
|
|
573
|
+
(0, common_2.Post)(paths)(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL, Object.getOwnPropertyDescriptor(target.prototype, HANDLE_REQUEST_INSTANCE_SYMBOL));
|
|
574
|
+
paths.forEach(function (path) {
|
|
575
|
+
logger.log("Mapped method: /".concat(controllerName).concat(path));
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
(0, common_2.Controller)(finalPrefix)(target);
|
|
579
|
+
(0, common_2.UseInterceptors)(ControllerInterceptor)(target);
|
|
580
|
+
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);
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
Controller.isHerbalController = isHerbalController;
|
|
584
|
+
Controller.getControllerName = getControllerName;
|
|
585
|
+
return Controller;
|
|
586
|
+
};
|
|
587
|
+
return ControllerUtil;
|
|
151
588
|
}());
|
|
589
|
+
exports.ControllerUtil = ControllerUtil;
|
package/dist/utilities/index.js
CHANGED
|
@@ -14,6 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./controller-util.class"), exports);
|
|
18
17
|
__exportStar(require("./model-util.class"), exports);
|
|
19
18
|
__exportStar(require("./nest-util.class"), exports);
|
package/package.json
CHANGED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
import { RequestContext } from '../types/request.type';
|
|
3
|
-
import { Request as ExpressRequest } from 'express';
|
|
4
|
-
import { Constructor } from 'type-fest';
|
|
5
|
-
import { z } from 'zod';
|
|
6
|
-
import { AuthAdapter } from '../abstracts/auth-adapter.abstract.class';
|
|
7
|
-
import { HeaderUtil } from '@open-norantec/utilities/dist/header-util.class';
|
|
8
|
-
import { PathsObject } from 'zod-openapi/dist/openapi3-ts/dist/model/openapi31';
|
|
9
|
-
type ClientGroups = Array<string> | null | undefined;
|
|
10
|
-
type ClienttGroupsFactory = (defaultGroupName: string) => ClientGroups;
|
|
11
|
-
export interface MethodRegisterOptions<IS extends z.Schema<any>, OS extends z.Schema<any>> {
|
|
12
|
-
inputSchema: IS;
|
|
13
|
-
outputSchema: OS;
|
|
14
|
-
authAdapters?: Constructor<AuthAdapter>[];
|
|
15
|
-
clientGroups?: ClientGroups | ClienttGroupsFactory;
|
|
16
|
-
disableTransaction?: boolean;
|
|
17
|
-
}
|
|
18
|
-
export type MethodRegisterFn<C> = <IS extends z.Schema<any>, OS extends z.Schema<any>>(name: string, options: MethodRegisterOptions<IS, OS>, callback: MethodCallback<IS, OS, C>) => void;
|
|
19
|
-
export interface MethodContext<IS extends z.Schema<any>> extends RequestContext {
|
|
20
|
-
headers: ReturnType<typeof HeaderUtil.parse>;
|
|
21
|
-
input: z.infer<IS>;
|
|
22
|
-
url: string;
|
|
23
|
-
}
|
|
24
|
-
export type MethodCallContext<IS extends z.Schema<any>> = Omit<MethodContext<IS>, 'input'>;
|
|
25
|
-
export type MethodCallback<IS extends z.Schema<any>, OS extends z.Schema<any>, C> = (this: C, context: MethodContext<IS>) => Promise<z.infer<OS>>;
|
|
26
|
-
declare class MethodConfig<IS extends z.Schema<any>, OS extends z.Schema<any>, C> {
|
|
27
|
-
readonly name: string;
|
|
28
|
-
readonly options: MethodRegisterOptions<IS, OS>;
|
|
29
|
-
protected readonly callback: MethodCallback<IS, OS, C>;
|
|
30
|
-
constructor(name: string, options: MethodRegisterOptions<IS, OS>, callback: MethodCallback<IS, OS, C>);
|
|
31
|
-
call(controller: C, callContext: MethodCallContext<IS>): Promise<z.TypeOf<OS>>;
|
|
32
|
-
}
|
|
33
|
-
declare class MethodPool {
|
|
34
|
-
protected readonly methods: Map<string, MethodConfig<any, any, any>>;
|
|
35
|
-
registerMethod<IS extends z.Schema<any>, OS extends z.Schema<any>, C>(name: string, options: MethodRegisterOptions<IS, OS>, callback: MethodCallback<IS, OS, C>): void;
|
|
36
|
-
getCallFn(name: string): ((controller: any, callContext: MethodCallContext<any>) => Promise<any>) | null;
|
|
37
|
-
getAuthAdapters(name: string): Constructor<AuthAdapter>[] | null | undefined;
|
|
38
|
-
getOpenAPIPathsObject(group?: string): PathsObject;
|
|
39
|
-
}
|
|
40
|
-
export declare function isHerbalController(target: Function): boolean;
|
|
41
|
-
export declare function getControllerName(target: Function): any;
|
|
42
|
-
export interface HerbalControllerOptions<C> {
|
|
43
|
-
ignoreControllerNamePostfix?: boolean;
|
|
44
|
-
prefix?: string;
|
|
45
|
-
useHeadGuards?: Constructor<any>[];
|
|
46
|
-
useTailGuards?: Constructor<any>[];
|
|
47
|
-
methods?: (register: MethodRegisterFn<C>) => void;
|
|
48
|
-
}
|
|
49
|
-
export interface ControllerUtilCreateOptions {
|
|
50
|
-
prefix?: string;
|
|
51
|
-
useGuards?: Constructor<any>[];
|
|
52
|
-
getTraceId?: (request: ExpressRequest) => string;
|
|
53
|
-
}
|
|
54
|
-
export declare class ControllerUtil {
|
|
55
|
-
static getPool(targetPrototype: object): MethodPool | null;
|
|
56
|
-
static create(createOptions?: ControllerUtilCreateOptions): {
|
|
57
|
-
<C>(options?: HerbalControllerOptions<C> | undefined): ClassDecorator;
|
|
58
|
-
isHerbalController: typeof isHerbalController;
|
|
59
|
-
getControllerName: typeof getControllerName;
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
export {};
|
|
@@ -1,488 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
14
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
15
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
16
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
17
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
18
|
-
};
|
|
19
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
20
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
21
|
-
};
|
|
22
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
23
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
24
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
25
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
26
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
27
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
28
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
32
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
33
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
34
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
35
|
-
function step(op) {
|
|
36
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
37
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
38
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
39
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
40
|
-
switch (op[0]) {
|
|
41
|
-
case 0: case 1: t = op; break;
|
|
42
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
43
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
44
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
45
|
-
default:
|
|
46
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
47
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
48
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
49
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
50
|
-
if (t[2]) _.ops.pop();
|
|
51
|
-
_.trys.pop(); continue;
|
|
52
|
-
}
|
|
53
|
-
op = body.call(thisArg, _);
|
|
54
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
55
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
59
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
60
|
-
var m = o[Symbol.asyncIterator], i;
|
|
61
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
62
|
-
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); }); }; }
|
|
63
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
64
|
-
};
|
|
65
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
-
if (ar || !(i in from)) {
|
|
68
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
-
ar[i] = from[i];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
-
};
|
|
74
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
75
|
-
exports.ControllerUtil = exports.getControllerName = exports.isHerbalController = void 0;
|
|
76
|
-
require("reflect-metadata");
|
|
77
|
-
var common_1 = require("@nestjs/common");
|
|
78
|
-
var _ = require("lodash");
|
|
79
|
-
var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
|
|
80
|
-
var uuid_util_class_1 = require("@open-norantec/utilities/dist/uuid-util.class");
|
|
81
|
-
var common_2 = require("@nestjs/common");
|
|
82
|
-
var headers_constant_1 = require("../constants/headers.constant");
|
|
83
|
-
var core_1 = require("@nestjs/core");
|
|
84
|
-
var auth_adapter_decorator_1 = require("../decorators/auth-adapter.decorator");
|
|
85
|
-
var rxjs_1 = require("rxjs");
|
|
86
|
-
var operators_1 = require("rxjs/operators");
|
|
87
|
-
var logger_service_1 = require("../modules/logger/logger.service");
|
|
88
|
-
var sequelize_typescript_1 = require("sequelize-typescript");
|
|
89
|
-
var decorators_1 = require("../decorators");
|
|
90
|
-
var zod_1 = require("zod");
|
|
91
|
-
var zod_openapi_1 = require("zod-openapi");
|
|
92
|
-
var METHOD_POOL = Symbol();
|
|
93
|
-
var MethodConfig = (function () {
|
|
94
|
-
function MethodConfig(name, options, callback) {
|
|
95
|
-
this.name = name;
|
|
96
|
-
this.options = options;
|
|
97
|
-
this.callback = callback;
|
|
98
|
-
}
|
|
99
|
-
MethodConfig.prototype.call = function (controller, callContext) {
|
|
100
|
-
var _a, _b, _c, _d, _e, _f;
|
|
101
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
102
|
-
var inputSchema, outputSchema, parsedBody_1, input, rawResponse_1, response, error_1;
|
|
103
|
-
return __generator(this, function (_g) {
|
|
104
|
-
switch (_g.label) {
|
|
105
|
-
case 0:
|
|
106
|
-
inputSchema = this.options.inputSchema;
|
|
107
|
-
outputSchema = this.options.outputSchema;
|
|
108
|
-
_g.label = 1;
|
|
109
|
-
case 1:
|
|
110
|
-
_g.trys.push([1, 3, , 4]);
|
|
111
|
-
parsedBody_1 = _.attempt(function () { return JSON.parse((callContext === null || callContext === void 0 ? void 0 : callContext.rawBody) || ''); });
|
|
112
|
-
input = _.attempt(function () { return (parsedBody_1 instanceof Error ? undefined : inputSchema.parse(parsedBody_1)); });
|
|
113
|
-
if (input instanceof zod_1.ZodError) {
|
|
114
|
-
throw new common_1.BadRequestException({
|
|
115
|
-
from: 'request',
|
|
116
|
-
invalidParams: (_c = (_b = (_a = input === null || input === void 0 ? void 0 : input.issues) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.call(_a, function (item) { var _a, _b; return (_b = (_a = item === null || item === void 0 ? void 0 : item.path) === null || _a === void 0 ? void 0 : _a.join) === null || _b === void 0 ? void 0 : _b.call(_a, '.'); })) !== null && _c !== void 0 ? _c : [],
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
else if (input instanceof Error)
|
|
120
|
-
throw input;
|
|
121
|
-
return [4, this.callback.call(controller, __assign(__assign({}, callContext), { input: input }))];
|
|
122
|
-
case 2:
|
|
123
|
-
rawResponse_1 = _g.sent();
|
|
124
|
-
response = _.attempt(function () { return outputSchema.parse(rawResponse_1); });
|
|
125
|
-
if (response instanceof zod_1.ZodError) {
|
|
126
|
-
throw new common_1.BadRequestException({
|
|
127
|
-
from: 'response',
|
|
128
|
-
invalidParams: (_f = (_e = (_d = response === null || response === void 0 ? void 0 : response.issues) === null || _d === void 0 ? void 0 : _d.map) === null || _e === void 0 ? void 0 : _e.call(_d, function (item) { var _a, _b; return (_b = (_a = item === null || item === void 0 ? void 0 : item.path) === null || _a === void 0 ? void 0 : _a.join) === null || _b === void 0 ? void 0 : _b.call(_a, '.'); })) !== null && _f !== void 0 ? _f : [],
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
else if (response instanceof Error)
|
|
132
|
-
throw response;
|
|
133
|
-
return [2, response];
|
|
134
|
-
case 3:
|
|
135
|
-
error_1 = _g.sent();
|
|
136
|
-
throw error_1;
|
|
137
|
-
case 4: return [2];
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
};
|
|
142
|
-
return MethodConfig;
|
|
143
|
-
}());
|
|
144
|
-
var MethodPool = (function () {
|
|
145
|
-
function MethodPool() {
|
|
146
|
-
this.methods = new Map();
|
|
147
|
-
}
|
|
148
|
-
MethodPool.prototype.registerMethod = function (name, options, callback) {
|
|
149
|
-
if (string_util_class_1.StringUtil.isFalsyString(name))
|
|
150
|
-
return;
|
|
151
|
-
if (name.includes('/'))
|
|
152
|
-
throw new Error("Method name cannot contain slashes: ".concat(name));
|
|
153
|
-
this.methods.set(name, new MethodConfig(name, options, callback));
|
|
154
|
-
};
|
|
155
|
-
MethodPool.prototype.getCallFn = function (name) {
|
|
156
|
-
var config = this.methods.get(name);
|
|
157
|
-
if (!(config instanceof MethodConfig))
|
|
158
|
-
return null;
|
|
159
|
-
return config.call.bind(config);
|
|
160
|
-
};
|
|
161
|
-
MethodPool.prototype.getAuthAdapters = function (name) {
|
|
162
|
-
var config = this.methods.get(name);
|
|
163
|
-
if (!(config instanceof MethodConfig))
|
|
164
|
-
return null;
|
|
165
|
-
return config.options.authAdapters;
|
|
166
|
-
};
|
|
167
|
-
MethodPool.prototype.getOpenAPIPathsObject = function (group) {
|
|
168
|
-
var result = {};
|
|
169
|
-
Array.from(this.methods.entries()).forEach(function (_a) {
|
|
170
|
-
var _b;
|
|
171
|
-
var name = _a[0], config = _a[1];
|
|
172
|
-
var defaultGroupName = "".concat(Date.now(), "_").concat(Math.random().toString(16).slice(2));
|
|
173
|
-
var currentGroupName = string_util_class_1.StringUtil.isFalsyString(group) ? defaultGroupName : group;
|
|
174
|
-
var clientGroups = typeof config.options.clientGroups === 'function'
|
|
175
|
-
? config.options.clientGroups(defaultGroupName)
|
|
176
|
-
: (_b = config === null || config === void 0 ? void 0 : config.options) === null || _b === void 0 ? void 0 : _b.clientGroups;
|
|
177
|
-
if (!Array.isArray(clientGroups) && !string_util_class_1.StringUtil.isFalsyString(group) && defaultGroupName !== group)
|
|
178
|
-
return;
|
|
179
|
-
if (Array.isArray(clientGroups) && !clientGroups.includes(currentGroupName))
|
|
180
|
-
return;
|
|
181
|
-
result["/".concat(name)] = {
|
|
182
|
-
post: {
|
|
183
|
-
requestBody: {
|
|
184
|
-
description: 'Request body for method ' + name,
|
|
185
|
-
required: true,
|
|
186
|
-
content: {
|
|
187
|
-
'application/json': {
|
|
188
|
-
schema: (0, zod_openapi_1.createSchema)(config.options.inputSchema).schema,
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
},
|
|
192
|
-
responses: {
|
|
193
|
-
'200': {
|
|
194
|
-
description: 'Response for method ' + name,
|
|
195
|
-
content: {
|
|
196
|
-
'application/json': (0, zod_openapi_1.createSchema)(zod_1.z.object({
|
|
197
|
-
data: config.options.outputSchema,
|
|
198
|
-
token: zod_1.z.string().nullable(),
|
|
199
|
-
})),
|
|
200
|
-
},
|
|
201
|
-
},
|
|
202
|
-
},
|
|
203
|
-
},
|
|
204
|
-
};
|
|
205
|
-
});
|
|
206
|
-
return result;
|
|
207
|
-
};
|
|
208
|
-
return MethodPool;
|
|
209
|
-
}());
|
|
210
|
-
var IS_HERBAL_CONTROLLER = Symbol();
|
|
211
|
-
var CONTROLLER_NAME = Symbol();
|
|
212
|
-
function isHerbalController(target) {
|
|
213
|
-
return _.attempt(function () { return Reflect.getMetadata(IS_HERBAL_CONTROLLER, target.prototype); }) === true;
|
|
214
|
-
}
|
|
215
|
-
exports.isHerbalController = isHerbalController;
|
|
216
|
-
function getControllerName(target) {
|
|
217
|
-
return Reflect.getMetadata(CONTROLLER_NAME, target.prototype);
|
|
218
|
-
}
|
|
219
|
-
exports.getControllerName = getControllerName;
|
|
220
|
-
var ControllerInterceptor = (function () {
|
|
221
|
-
function ControllerInterceptor(ref) {
|
|
222
|
-
this.ref = ref;
|
|
223
|
-
}
|
|
224
|
-
ControllerInterceptor.prototype.intercept = function (context, next) {
|
|
225
|
-
var _this = this;
|
|
226
|
-
var request = context.switchToHttp().getRequest();
|
|
227
|
-
if (isHerbalController(context.getClass())) {
|
|
228
|
-
_.attempt(function () {
|
|
229
|
-
_this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:url] ").concat(request === null || request === void 0 ? void 0 : request.originalUrl));
|
|
230
|
-
});
|
|
231
|
-
_.attempt(function () {
|
|
232
|
-
return _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":request:headers] ").concat(JSON.stringify(request === null || request === void 0 ? void 0 : request.headers)));
|
|
233
|
-
});
|
|
234
|
-
return this.handle(next, request);
|
|
235
|
-
}
|
|
236
|
-
return next.handle();
|
|
237
|
-
};
|
|
238
|
-
ControllerInterceptor.prototype.getLogger = function () {
|
|
239
|
-
var loggerService = this.ref.get(logger_service_1.LoggerService, { strict: false });
|
|
240
|
-
if (!(loggerService instanceof logger_service_1.LoggerService)) {
|
|
241
|
-
return {
|
|
242
|
-
log: function () { },
|
|
243
|
-
error: function () { },
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
return loggerService;
|
|
247
|
-
};
|
|
248
|
-
ControllerInterceptor.prototype.handle = function (next, request) {
|
|
249
|
-
var _this = this;
|
|
250
|
-
return next.handle().pipe((0, operators_1.map)(function (data) {
|
|
251
|
-
_.attempt(function () {
|
|
252
|
-
_this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:raw] ").concat(JSON.stringify(data)));
|
|
253
|
-
});
|
|
254
|
-
_.attempt(function () {
|
|
255
|
-
_this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:final] ").concat(JSON.stringify(data)));
|
|
256
|
-
});
|
|
257
|
-
return data;
|
|
258
|
-
}), (0, operators_1.catchError)(function (error) {
|
|
259
|
-
_.attempt(function () {
|
|
260
|
-
_this.getLogger().error("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:error:message] ").concat(error === null || error === void 0 ? void 0 : error.message));
|
|
261
|
-
});
|
|
262
|
-
_.attempt(function () {
|
|
263
|
-
_this.getLogger().error("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:error:stack] ").concat(error === null || error === void 0 ? void 0 : error.stack));
|
|
264
|
-
});
|
|
265
|
-
_this.getLogger().error("Got error when handling route in interceptor: ".concat(error === null || error === void 0 ? void 0 : error.message, " ").concat(error === null || error === void 0 ? void 0 : error.stack));
|
|
266
|
-
_.attempt(function () { var _a, _b, _c, _d; return (_d = (_c = (_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)) === null || _c === void 0 ? void 0 : _c.catch) === null || _d === void 0 ? void 0 : _d.call(_c, function () { }); });
|
|
267
|
-
return (0, rxjs_1.throwError)(function () { return error; });
|
|
268
|
-
}));
|
|
269
|
-
};
|
|
270
|
-
ControllerInterceptor = __decorate([
|
|
271
|
-
(0, common_2.Injectable)(),
|
|
272
|
-
__metadata("design:paramtypes", [core_1.ModuleRef])
|
|
273
|
-
], ControllerInterceptor);
|
|
274
|
-
return ControllerInterceptor;
|
|
275
|
-
}());
|
|
276
|
-
function HerbalGuard(options) {
|
|
277
|
-
var HerbalGuardMixin = (function () {
|
|
278
|
-
function HerbalGuardMixin(ref) {
|
|
279
|
-
this.ref = ref;
|
|
280
|
-
}
|
|
281
|
-
HerbalGuardMixin.prototype.canActivate = function (context) {
|
|
282
|
-
var _a, e_1, _b, _c;
|
|
283
|
-
var _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
284
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
285
|
-
var sequelizeInstance, transaction, request, response, traceId, chunks, _o, request_1, request_1_1, chunk, e_1_1, _p, parsedBody, rawHandlerName, handlerPropertype, handlerName, authAdapters, error_2, _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult, error_3, _q;
|
|
286
|
-
var _this = this;
|
|
287
|
-
return __generator(this, function (_r) {
|
|
288
|
-
switch (_r.label) {
|
|
289
|
-
case 0:
|
|
290
|
-
sequelizeInstance = _.attempt(function () { return _this.ref.get(sequelize_typescript_1.Sequelize, { strict: false }); });
|
|
291
|
-
transaction = undefined;
|
|
292
|
-
request = context.switchToHttp().getRequest();
|
|
293
|
-
response = context.switchToHttp().getResponse();
|
|
294
|
-
traceId = typeof (options === null || options === void 0 ? void 0 : options.getTraceId) === 'function'
|
|
295
|
-
? _.attempt(function () { return options.getTraceId(request); })
|
|
296
|
-
: uuid_util_class_1.UUIDUtil.generateV4();
|
|
297
|
-
if (traceId instanceof Error || string_util_class_1.StringUtil.isFalsyString(traceId))
|
|
298
|
-
traceId = uuid_util_class_1.UUIDUtil.generateV4();
|
|
299
|
-
request.traceId = traceId;
|
|
300
|
-
request.methodName = request.url.split('/').pop();
|
|
301
|
-
response.setHeader(headers_constant_1.HEADERS.TRACE_ID, traceId);
|
|
302
|
-
chunks = [];
|
|
303
|
-
_r.label = 1;
|
|
304
|
-
case 1:
|
|
305
|
-
_r.trys.push([1, 14, , 15]);
|
|
306
|
-
_r.label = 2;
|
|
307
|
-
case 2:
|
|
308
|
-
_r.trys.push([2, 7, 8, 13]);
|
|
309
|
-
_o = true, request_1 = __asyncValues(request);
|
|
310
|
-
_r.label = 3;
|
|
311
|
-
case 3: return [4, request_1.next()];
|
|
312
|
-
case 4:
|
|
313
|
-
if (!(request_1_1 = _r.sent(), _a = request_1_1.done, !_a)) return [3, 6];
|
|
314
|
-
_c = request_1_1.value;
|
|
315
|
-
_o = false;
|
|
316
|
-
chunk = _c;
|
|
317
|
-
chunks.push(chunk);
|
|
318
|
-
_r.label = 5;
|
|
319
|
-
case 5:
|
|
320
|
-
_o = true;
|
|
321
|
-
return [3, 3];
|
|
322
|
-
case 6: return [3, 13];
|
|
323
|
-
case 7:
|
|
324
|
-
e_1_1 = _r.sent();
|
|
325
|
-
e_1 = { error: e_1_1 };
|
|
326
|
-
return [3, 13];
|
|
327
|
-
case 8:
|
|
328
|
-
_r.trys.push([8, , 11, 12]);
|
|
329
|
-
if (!(!_o && !_a && (_b = request_1.return))) return [3, 10];
|
|
330
|
-
return [4, _b.call(request_1)];
|
|
331
|
-
case 9:
|
|
332
|
-
_r.sent();
|
|
333
|
-
_r.label = 10;
|
|
334
|
-
case 10: return [3, 12];
|
|
335
|
-
case 11:
|
|
336
|
-
if (e_1) throw e_1.error;
|
|
337
|
-
return [7];
|
|
338
|
-
case 12: return [7];
|
|
339
|
-
case 13: return [3, 15];
|
|
340
|
-
case 14:
|
|
341
|
-
_p = _r.sent();
|
|
342
|
-
return [3, 15];
|
|
343
|
-
case 15:
|
|
344
|
-
parsedBody = _.attempt(function () { return Buffer.concat(chunks).toString('utf8'); });
|
|
345
|
-
if (!(parsedBody instanceof Error)) {
|
|
346
|
-
request.rawBody = parsedBody;
|
|
347
|
-
}
|
|
348
|
-
else {
|
|
349
|
-
request.rawBody = null;
|
|
350
|
-
}
|
|
351
|
-
_.attempt(function () { return _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":request:body] ").concat(request.rawBody)); });
|
|
352
|
-
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;
|
|
353
|
-
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;
|
|
354
|
-
handlerName = string_util_class_1.StringUtil.isFalsyString(rawHandlerName) ? request.methodName : rawHandlerName;
|
|
355
|
-
authAdapters = (_j = (_h = ControllerUtil.getPool(handlerPropertype)) === null || _h === void 0 ? void 0 : _h.getAuthAdapters) === null || _j === void 0 ? void 0 : _j.call(_h, handlerName);
|
|
356
|
-
if (authAdapters === null)
|
|
357
|
-
authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPropertype, handlerName);
|
|
358
|
-
if (!(!(sequelizeInstance instanceof Error) && !decorators_1.NoTransaction.isDisabled(handlerPropertype, handlerName))) return [3, 20];
|
|
359
|
-
_r.label = 16;
|
|
360
|
-
case 16:
|
|
361
|
-
_r.trys.push([16, 18, , 19]);
|
|
362
|
-
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); }))];
|
|
363
|
-
case 17:
|
|
364
|
-
transaction = _r.sent();
|
|
365
|
-
request.transaction = transaction;
|
|
366
|
-
this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Started transaction for route: ").concat(handlerName));
|
|
367
|
-
return [3, 19];
|
|
368
|
-
case 18:
|
|
369
|
-
error_2 = _r.sent();
|
|
370
|
-
if (error_2 instanceof Error) {
|
|
371
|
-
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));
|
|
372
|
-
}
|
|
373
|
-
return [3, 19];
|
|
374
|
-
case 19: return [3, 21];
|
|
375
|
-
case 20:
|
|
376
|
-
if (decorators_1.NoTransaction.isDisabled(handlerPropertype, handlerName)) {
|
|
377
|
-
this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Transaction is disabled for this route: ").concat(handlerName));
|
|
378
|
-
}
|
|
379
|
-
_r.label = 21;
|
|
380
|
-
case 21:
|
|
381
|
-
_r.trys.push([21, 27, , 32]);
|
|
382
|
-
if (!(Array.isArray(authAdapters) && authAdapters.length > 0)) return [3, 26];
|
|
383
|
-
_i = 0, authAdapters_1 = authAdapters;
|
|
384
|
-
_r.label = 22;
|
|
385
|
-
case 22:
|
|
386
|
-
if (!(_i < authAdapters_1.length)) return [3, 25];
|
|
387
|
-
AuthAdapterClass = authAdapters_1[_i];
|
|
388
|
-
adapter = new AuthAdapterClass(request, this.ref);
|
|
389
|
-
if (!adapter.match())
|
|
390
|
-
return [3, 24];
|
|
391
|
-
return [4, adapter.authenticate(transaction)];
|
|
392
|
-
case 23:
|
|
393
|
-
authenticateResult = _r.sent();
|
|
394
|
-
if (!authenticateResult)
|
|
395
|
-
return [3, 25];
|
|
396
|
-
request.authenticateResult = __assign({ AuthenticatorClass: AuthAdapterClass }, authenticateResult);
|
|
397
|
-
return [2, true];
|
|
398
|
-
case 24:
|
|
399
|
-
_i++;
|
|
400
|
-
return [3, 22];
|
|
401
|
-
case 25: throw new common_2.UnauthorizedException();
|
|
402
|
-
case 26: return [3, 32];
|
|
403
|
-
case 27:
|
|
404
|
-
error_3 = _r.sent();
|
|
405
|
-
_r.label = 28;
|
|
406
|
-
case 28:
|
|
407
|
-
_r.trys.push([28, 30, , 31]);
|
|
408
|
-
if (error_3 instanceof Error) {
|
|
409
|
-
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));
|
|
410
|
-
}
|
|
411
|
-
return [4, ((_m = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _m === void 0 ? void 0 : _m.call(transaction))];
|
|
412
|
-
case 29:
|
|
413
|
-
_r.sent();
|
|
414
|
-
return [3, 31];
|
|
415
|
-
case 30:
|
|
416
|
-
_q = _r.sent();
|
|
417
|
-
return [3, 31];
|
|
418
|
-
case 31: throw error_3;
|
|
419
|
-
case 32: return [2, true];
|
|
420
|
-
}
|
|
421
|
-
});
|
|
422
|
-
});
|
|
423
|
-
};
|
|
424
|
-
HerbalGuardMixin.prototype.getLogger = function () {
|
|
425
|
-
var loggerService = this.ref.get(logger_service_1.LoggerService, { strict: false });
|
|
426
|
-
if (!(loggerService instanceof logger_service_1.LoggerService)) {
|
|
427
|
-
return {
|
|
428
|
-
log: function () { },
|
|
429
|
-
error: function () { },
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
|
-
return loggerService;
|
|
433
|
-
};
|
|
434
|
-
HerbalGuardMixin = __decorate([
|
|
435
|
-
(0, common_2.Injectable)(),
|
|
436
|
-
__metadata("design:paramtypes", [core_1.ModuleRef])
|
|
437
|
-
], HerbalGuardMixin);
|
|
438
|
-
return HerbalGuardMixin;
|
|
439
|
-
}());
|
|
440
|
-
return (0, common_1.mixin)(HerbalGuardMixin);
|
|
441
|
-
}
|
|
442
|
-
var ControllerUtil = (function () {
|
|
443
|
-
function ControllerUtil() {
|
|
444
|
-
}
|
|
445
|
-
ControllerUtil.getPool = function (targetPrototype) {
|
|
446
|
-
var pool = Reflect.getMetadata(METHOD_POOL, targetPrototype);
|
|
447
|
-
if (!(pool instanceof MethodPool))
|
|
448
|
-
return null;
|
|
449
|
-
return pool;
|
|
450
|
-
};
|
|
451
|
-
ControllerUtil.create = function (createOptions) {
|
|
452
|
-
function Controller(options) {
|
|
453
|
-
return function (target) {
|
|
454
|
-
var _a;
|
|
455
|
-
var methodPool = new MethodPool();
|
|
456
|
-
var finalPrefix = string_util_class_1.StringUtil.isFalsyString(options === null || options === void 0 ? void 0 : options.prefix)
|
|
457
|
-
? string_util_class_1.StringUtil.isFalsyString(createOptions === null || createOptions === void 0 ? void 0 : createOptions.prefix)
|
|
458
|
-
? ''
|
|
459
|
-
: createOptions.prefix
|
|
460
|
-
: options.prefix;
|
|
461
|
-
var controllerName = _.camelCase(target.name.replace(/Controller$/g, ''));
|
|
462
|
-
if (!(options === null || options === void 0 ? void 0 : options.ignoreControllerNamePostfix)) {
|
|
463
|
-
finalPrefix += "".concat(((_a = finalPrefix === null || finalPrefix === void 0 ? void 0 : finalPrefix.endsWith) === null || _a === void 0 ? void 0 : _a.call(finalPrefix, '/')) ? '' : '/').concat(controllerName);
|
|
464
|
-
}
|
|
465
|
-
var register = function (name, options, callback) {
|
|
466
|
-
if (string_util_class_1.StringUtil.isFalsyString(name) || typeof callback !== 'function')
|
|
467
|
-
return;
|
|
468
|
-
methodPool.registerMethod(name, options, callback);
|
|
469
|
-
};
|
|
470
|
-
if (!finalPrefix.startsWith('/'))
|
|
471
|
-
finalPrefix = "/".concat(finalPrefix);
|
|
472
|
-
Reflect.defineMetadata(IS_HERBAL_CONTROLLER, true, target.prototype);
|
|
473
|
-
Reflect.defineMetadata(CONTROLLER_NAME, controllerName, target.prototype);
|
|
474
|
-
Reflect.defineMetadata(METHOD_POOL, methodPool, target.prototype);
|
|
475
|
-
if (typeof (options === null || options === void 0 ? void 0 : options.methods) === 'function')
|
|
476
|
-
options.methods(register);
|
|
477
|
-
(0, common_1.Controller)(finalPrefix)(target);
|
|
478
|
-
(0, common_1.UseInterceptors)(ControllerInterceptor)(target);
|
|
479
|
-
common_1.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);
|
|
480
|
-
};
|
|
481
|
-
}
|
|
482
|
-
Controller.isHerbalController = isHerbalController;
|
|
483
|
-
Controller.getControllerName = getControllerName;
|
|
484
|
-
return Controller;
|
|
485
|
-
};
|
|
486
|
-
return ControllerUtil;
|
|
487
|
-
}());
|
|
488
|
-
exports.ControllerUtil = ControllerUtil;
|