@open-norantec/herbal 1.0.2-alpha.8 → 1.0.2

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