@open-norantec/herbal 1.0.2-alpha.9 → 2.0.0-alpha.0

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,337 +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.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 IS_CONTROLLER = Symbol();
91
- function isHerbalController(target) {
92
- return _.attempt(function () { return Reflect.getMetadata(IS_CONTROLLER, target.prototype); }) === true;
93
- }
94
- exports.isHerbalController = isHerbalController;
95
- var ControllerInterceptor = (function () {
96
- function ControllerInterceptor(ref) {
97
- this.ref = ref;
98
- }
99
- ControllerInterceptor.prototype.intercept = function (context, next) {
100
- var _this = this;
101
- var request = context.switchToHttp().getRequest();
102
- if (isHerbalController(context.getClass())) {
103
- _.attempt(function () {
104
- _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));
105
- });
106
- _.attempt(function () {
107
- 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)));
108
- });
109
- return this.handle(next, request);
110
- }
111
- return next.handle();
112
- };
113
- ControllerInterceptor.prototype.getLogger = function () {
114
- var loggerService = this.ref.get(logger_service_1.LoggerService, { strict: false });
115
- if (!(loggerService instanceof logger_service_1.LoggerService)) {
116
- return {
117
- log: function () { },
118
- error: function () { },
119
- };
120
- }
121
- return loggerService;
122
- };
123
- ControllerInterceptor.prototype.handle = function (next, request) {
124
- var _this = this;
125
- return next.handle().pipe((0, operators_1.map)(function (data) {
126
- _.attempt(function () {
127
- _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:raw] ").concat(JSON.stringify(data)));
128
- });
129
- _.attempt(function () {
130
- _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":response:final] ").concat(JSON.stringify(data)));
131
- });
132
- return data;
133
- }), (0, operators_1.catchError)(function (error) {
134
- _.attempt(function () {
135
- _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));
136
- });
137
- _.attempt(function () {
138
- _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));
139
- });
140
- _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));
141
- _.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 () { }); });
142
- return (0, rxjs_1.throwError)(function () { return error; });
143
- }));
144
- };
145
- ControllerInterceptor = __decorate([
146
- (0, common_2.Injectable)(),
147
- __metadata("design:paramtypes", [core_1.ModuleRef])
148
- ], ControllerInterceptor);
149
- return ControllerInterceptor;
150
- }());
151
- function HerbalGuard(options) {
152
- var HerbalGuardMixin = (function () {
153
- function HerbalGuardMixin(ref) {
154
- this.ref = ref;
155
- }
156
- HerbalGuardMixin.prototype.canActivate = function (context) {
157
- var _a, e_1, _b, _c;
158
- var _d, _e, _f, _g, _h, _j, _k;
159
- return __awaiter(this, void 0, void 0, function () {
160
- var sequelizeInstance, transaction, request, response, traceId, chunks, _l, request_1, request_1_1, chunk, e_1_1, _m, parsedBody, rawHandlerName, handlerPropertype, handlerName, authAdapters, e_2, _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult, error_1, _o;
161
- var _this = this;
162
- return __generator(this, function (_p) {
163
- switch (_p.label) {
164
- case 0:
165
- sequelizeInstance = _.attempt(function () { return _this.ref.get(sequelize_typescript_1.Sequelize, { strict: false }); });
166
- transaction = undefined;
167
- request = context.switchToHttp().getRequest();
168
- response = context.switchToHttp().getResponse();
169
- traceId = typeof (options === null || options === void 0 ? void 0 : options.getTraceId) === 'function'
170
- ? _.attempt(function () { return options.getTraceId(request); })
171
- : uuid_util_class_1.UUIDUtil.generateV4();
172
- if (traceId instanceof Error || string_util_class_1.StringUtil.isFalsyString(traceId))
173
- traceId = uuid_util_class_1.UUIDUtil.generateV4();
174
- request.traceId = traceId;
175
- request.methodName = request.url.split('/').pop();
176
- response.setHeader(headers_constant_1.HEADERS.TRACE_ID, traceId);
177
- chunks = [];
178
- _p.label = 1;
179
- case 1:
180
- _p.trys.push([1, 14, , 15]);
181
- _p.label = 2;
182
- case 2:
183
- _p.trys.push([2, 7, 8, 13]);
184
- _l = true, request_1 = __asyncValues(request);
185
- _p.label = 3;
186
- case 3: return [4, request_1.next()];
187
- case 4:
188
- if (!(request_1_1 = _p.sent(), _a = request_1_1.done, !_a)) return [3, 6];
189
- _c = request_1_1.value;
190
- _l = false;
191
- chunk = _c;
192
- chunks.push(chunk);
193
- _p.label = 5;
194
- case 5:
195
- _l = true;
196
- return [3, 3];
197
- case 6: return [3, 13];
198
- case 7:
199
- e_1_1 = _p.sent();
200
- e_1 = { error: e_1_1 };
201
- return [3, 13];
202
- case 8:
203
- _p.trys.push([8, , 11, 12]);
204
- if (!(!_l && !_a && (_b = request_1.return))) return [3, 10];
205
- return [4, _b.call(request_1)];
206
- case 9:
207
- _p.sent();
208
- _p.label = 10;
209
- case 10: return [3, 12];
210
- case 11:
211
- if (e_1) throw e_1.error;
212
- return [7];
213
- case 12: return [7];
214
- case 13: return [3, 15];
215
- case 14:
216
- _m = _p.sent();
217
- return [3, 15];
218
- case 15:
219
- parsedBody = _.attempt(function () { return Buffer.concat(chunks).toString('utf8'); });
220
- if (!(parsedBody instanceof Error)) {
221
- request.rawBody = parsedBody;
222
- }
223
- else {
224
- request.rawBody = null;
225
- }
226
- _.attempt(function () { return _this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":request:body] ").concat(request.rawBody)); });
227
- 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;
228
- 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;
229
- handlerName = string_util_class_1.StringUtil.isFalsyString(rawHandlerName) ? request.methodName : rawHandlerName;
230
- authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPropertype, handlerName);
231
- if (!(!(sequelizeInstance instanceof Error) && !decorators_1.NoTransaction.isDisabled(handlerPropertype, handlerName))) return [3, 20];
232
- _p.label = 16;
233
- case 16:
234
- _p.trys.push([16, 18, , 19]);
235
- 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); }))];
236
- case 17:
237
- transaction = _p.sent();
238
- request.transaction = transaction;
239
- this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Started transaction for route: ").concat(handlerName));
240
- return [3, 19];
241
- case 18:
242
- e_2 = _p.sent();
243
- this.getLogger().error("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Failed to start transaction: ").concat(e_2 === null || e_2 === void 0 ? void 0 : e_2.message, "\n").concat(e_2 === null || e_2 === void 0 ? void 0 : e_2.stack));
244
- return [3, 19];
245
- case 19: return [3, 21];
246
- case 20:
247
- if (decorators_1.NoTransaction.isDisabled(handlerPropertype, handlerName)) {
248
- this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Transaction is disabled for this route: ").concat(handlerName));
249
- }
250
- _p.label = 21;
251
- case 21:
252
- _p.trys.push([21, 27, , 32]);
253
- if (!(Array.isArray(authAdapters) && authAdapters.length > 0)) return [3, 26];
254
- _i = 0, authAdapters_1 = authAdapters;
255
- _p.label = 22;
256
- case 22:
257
- if (!(_i < authAdapters_1.length)) return [3, 25];
258
- AuthAdapterClass = authAdapters_1[_i];
259
- adapter = new AuthAdapterClass(request, this.ref);
260
- if (!adapter.match())
261
- return [3, 24];
262
- return [4, adapter.authenticate(transaction)];
263
- case 23:
264
- authenticateResult = _p.sent();
265
- if (!authenticateResult)
266
- return [3, 25];
267
- request.authenticateResult = __assign({ AuthenticatorClass: AuthAdapterClass }, authenticateResult);
268
- return [2, true];
269
- case 24:
270
- _i++;
271
- return [3, 22];
272
- case 25: throw new common_2.UnauthorizedException();
273
- case 26: return [3, 32];
274
- case 27:
275
- error_1 = _p.sent();
276
- _p.label = 28;
277
- case 28:
278
- _p.trys.push([28, 30, , 31]);
279
- this.getLogger().error("Got error when handling route: ".concat(error_1 === null || error_1 === void 0 ? void 0 : error_1.message, " ").concat(error_1 === null || error_1 === void 0 ? void 0 : error_1.stack));
280
- return [4, ((_k = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _k === void 0 ? void 0 : _k.call(transaction))];
281
- case 29:
282
- _p.sent();
283
- return [3, 31];
284
- case 30:
285
- _o = _p.sent();
286
- return [3, 31];
287
- case 31: throw error_1;
288
- case 32: return [2, true];
289
- }
290
- });
291
- });
292
- };
293
- HerbalGuardMixin.prototype.getLogger = function () {
294
- var loggerService = this.ref.get(logger_service_1.LoggerService, { strict: false });
295
- if (!(loggerService instanceof logger_service_1.LoggerService)) {
296
- return {
297
- log: function () { },
298
- error: function () { },
299
- };
300
- }
301
- return loggerService;
302
- };
303
- HerbalGuardMixin = __decorate([
304
- (0, common_2.Injectable)(),
305
- __metadata("design:paramtypes", [core_1.ModuleRef])
306
- ], HerbalGuardMixin);
307
- return HerbalGuardMixin;
308
- }());
309
- return (0, common_1.mixin)(HerbalGuardMixin);
310
- }
311
- var ControllerUtil = (function () {
312
- function ControllerUtil() {
313
- }
314
- ControllerUtil.create = function (createOptions) {
315
- function Controller(options) {
316
- return function (target) {
317
- var _a;
318
- var finalPrefix = string_util_class_1.StringUtil.isFalsyString(options === null || options === void 0 ? void 0 : options.prefix)
319
- ? string_util_class_1.StringUtil.isFalsyString(createOptions === null || createOptions === void 0 ? void 0 : createOptions.prefix)
320
- ? ''
321
- : createOptions.prefix
322
- : options.prefix;
323
- finalPrefix += "".concat(((_a = finalPrefix === null || finalPrefix === void 0 ? void 0 : finalPrefix.endsWith) === null || _a === void 0 ? void 0 : _a.call(finalPrefix, '/')) ? '' : '/').concat(_.camelCase(target.name.replace(/Controller$/g, '')));
324
- if (!finalPrefix.startsWith('/'))
325
- finalPrefix = "/".concat(finalPrefix);
326
- Reflect.defineMetadata(IS_CONTROLLER, true, target.prototype);
327
- (0, common_1.Controller)(finalPrefix)(target);
328
- (0, common_1.UseInterceptors)(ControllerInterceptor)(target);
329
- 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);
330
- };
331
- }
332
- Controller.isHerbalController = isHerbalController;
333
- return Controller;
334
- };
335
- return ControllerUtil;
336
- }());
337
- exports.ControllerUtil = ControllerUtil;