@open-norantec/herbal 1.0.2-alpha.20 → 1.0.2-alpha.21

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.
@@ -11,7 +11,7 @@ type ClienttGroupsFactory = (defaultGroupName: string) => ClientGroups;
11
11
  export interface MethodOptions<IS extends z.Schema<any>, OS extends z.Schema<any>> {
12
12
  inputSchema: IS;
13
13
  outputSchema: OS;
14
- authAdapters?: AuthAdapter[];
14
+ authAdapters?: Constructor<AuthAdapter>[];
15
15
  clientGroups?: ClientGroups | ClienttGroupsFactory;
16
16
  disableTransaction?: boolean;
17
17
  }
@@ -34,7 +34,8 @@ declare class MethodConfig<IS extends z.Schema<any>, OS extends z.Schema<any>> {
34
34
  declare class MethodPool {
35
35
  protected readonly methods: Map<string, MethodConfig<any, any>>;
36
36
  registerMethod<IS extends z.Schema<any>, OS extends z.Schema<any>>(name: string, options: MethodOptions<IS, OS>, callback: MethodCallback<IS, OS>): void;
37
- getCallFn(name: string): any;
37
+ getCallFn(name: string): ((callContext: MethodCallContext<any>) => Promise<any>) | null;
38
+ getAuthAdapters(name: string): Constructor<AuthAdapter>[] | null | undefined;
38
39
  getOpenAPIPathsObject(group?: string): PathsObject;
39
40
  }
40
41
  export declare function Method<IS extends z.Schema<any>, OS extends z.Schema<any>>(...parameters: Parameters<typeof MethodPool.prototype.registerMethod<IS, OS>>): ClassDecorator;
@@ -64,16 +64,17 @@ var MethodConfig = (function () {
64
64
  MethodConfig.prototype.call = function (callContext) {
65
65
  var _a, _b, _c, _d, _e, _f;
66
66
  return __awaiter(this, void 0, void 0, function () {
67
- var parsedBody_1, input, rawResponse_1, response, error_1;
68
- var _this = this;
67
+ var inputSchema, outputSchema, parsedBody_1, input, rawResponse_1, response, error_1;
69
68
  return __generator(this, function (_g) {
70
69
  switch (_g.label) {
71
70
  case 0:
72
- _g.trys.push([0, 2, , 3]);
71
+ inputSchema = this.options.inputSchema;
72
+ outputSchema = this.options.outputSchema;
73
+ _g.label = 1;
74
+ case 1:
75
+ _g.trys.push([1, 3, , 4]);
73
76
  parsedBody_1 = _.attempt(function () { return JSON.parse((callContext === null || callContext === void 0 ? void 0 : callContext.rawBody) || ''); });
74
- input = _.attempt(function () {
75
- return parsedBody_1 instanceof Error ? undefined : _this.options.inputSchema.parse(parsedBody_1);
76
- });
77
+ input = _.attempt(function () { return (parsedBody_1 instanceof Error ? undefined : inputSchema.parse(parsedBody_1)); });
77
78
  if (input instanceof zod_1.ZodError) {
78
79
  throw new common_1.BadRequestException({
79
80
  from: 'request',
@@ -83,9 +84,9 @@ var MethodConfig = (function () {
83
84
  else if (input instanceof Error)
84
85
  throw input;
85
86
  return [4, this.callback(__assign(__assign({}, callContext), { input: input }))];
86
- case 1:
87
+ case 2:
87
88
  rawResponse_1 = _g.sent();
88
- response = _.attempt(function () { return _this.options.outputSchema.parse(rawResponse_1); });
89
+ response = _.attempt(function () { return outputSchema.parse(rawResponse_1); });
89
90
  if (response instanceof zod_1.ZodError) {
90
91
  throw new common_1.BadRequestException({
91
92
  from: 'response',
@@ -95,10 +96,10 @@ var MethodConfig = (function () {
95
96
  else if (response instanceof Error)
96
97
  throw response;
97
98
  return [2, response];
98
- case 2:
99
+ case 3:
99
100
  error_1 = _g.sent();
100
101
  throw error_1;
101
- case 3: return [2];
102
+ case 4: return [2];
102
103
  }
103
104
  });
104
105
  });
@@ -117,9 +118,16 @@ var MethodPool = (function () {
117
118
  this.methods.set(name, new MethodConfig(name, options, callback));
118
119
  };
119
120
  MethodPool.prototype.getCallFn = function (name) {
120
- var _a;
121
- var callFn = (_a = this.methods.get(name)) === null || _a === void 0 ? void 0 : _a.call;
122
- return typeof callFn === 'function' ? callFn.bind(this) : null;
121
+ var config = this.methods.get(name);
122
+ if (!(config instanceof MethodConfig))
123
+ return null;
124
+ return config.call.bind(config);
125
+ };
126
+ MethodPool.prototype.getAuthAdapters = function (name) {
127
+ var config = this.methods.get(name);
128
+ if (!(config instanceof MethodConfig))
129
+ return null;
130
+ return config.options.authAdapters;
123
131
  };
124
132
  MethodPool.prototype.getOpenAPIPathsObject = function (group) {
125
133
  var result = {};
@@ -160,12 +160,12 @@ function HerbalGuard(options) {
160
160
  }
161
161
  HerbalGuardMixin.prototype.canActivate = function (context) {
162
162
  var _a, e_1, _b, _c;
163
- var _d, _e, _f, _g, _h, _j, _k;
163
+ var _d, _e, _f, _g, _h, _j, _k, _l, _m;
164
164
  return __awaiter(this, void 0, void 0, function () {
165
- var sequelizeInstance, transaction, request, response, traceId, chunks, _l, request_1, request_1_1, chunk, e_1_1, _m, parsedBody, rawHandlerName, handlerPropertype, handlerName, authAdapters, error_1, _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult, error_2, _o;
165
+ var sequelizeInstance, transaction, request, response, traceId, chunks, _o, request_1, request_1_1, chunk, e_1_1, _p, parsedBody, rawHandlerName, handlerPropertype, handlerName, authAdapters, error_1, _i, authAdapters_1, AuthAdapterClass, adapter, authenticateResult, error_2, _q;
166
166
  var _this = this;
167
- return __generator(this, function (_p) {
168
- switch (_p.label) {
167
+ return __generator(this, function (_r) {
168
+ switch (_r.label) {
169
169
  case 0:
170
170
  sequelizeInstance = _.attempt(function () { return _this.ref.get(sequelize_typescript_1.Sequelize, { strict: false }); });
171
171
  transaction = undefined;
@@ -181,37 +181,37 @@ function HerbalGuard(options) {
181
181
  request.moduleRef = this.ref;
182
182
  response.setHeader(headers_constant_1.HEADERS.TRACE_ID, traceId);
183
183
  chunks = [];
184
- _p.label = 1;
184
+ _r.label = 1;
185
185
  case 1:
186
- _p.trys.push([1, 14, , 15]);
187
- _p.label = 2;
186
+ _r.trys.push([1, 14, , 15]);
187
+ _r.label = 2;
188
188
  case 2:
189
- _p.trys.push([2, 7, 8, 13]);
190
- _l = true, request_1 = __asyncValues(request);
191
- _p.label = 3;
189
+ _r.trys.push([2, 7, 8, 13]);
190
+ _o = true, request_1 = __asyncValues(request);
191
+ _r.label = 3;
192
192
  case 3: return [4, request_1.next()];
193
193
  case 4:
194
- if (!(request_1_1 = _p.sent(), _a = request_1_1.done, !_a)) return [3, 6];
194
+ if (!(request_1_1 = _r.sent(), _a = request_1_1.done, !_a)) return [3, 6];
195
195
  _c = request_1_1.value;
196
- _l = false;
196
+ _o = false;
197
197
  chunk = _c;
198
198
  chunks.push(chunk);
199
- _p.label = 5;
199
+ _r.label = 5;
200
200
  case 5:
201
- _l = true;
201
+ _o = true;
202
202
  return [3, 3];
203
203
  case 6: return [3, 13];
204
204
  case 7:
205
- e_1_1 = _p.sent();
205
+ e_1_1 = _r.sent();
206
206
  e_1 = { error: e_1_1 };
207
207
  return [3, 13];
208
208
  case 8:
209
- _p.trys.push([8, , 11, 12]);
210
- if (!(!_l && !_a && (_b = request_1.return))) return [3, 10];
209
+ _r.trys.push([8, , 11, 12]);
210
+ if (!(!_o && !_a && (_b = request_1.return))) return [3, 10];
211
211
  return [4, _b.call(request_1)];
212
212
  case 9:
213
- _p.sent();
214
- _p.label = 10;
213
+ _r.sent();
214
+ _r.label = 10;
215
215
  case 10: return [3, 12];
216
216
  case 11:
217
217
  if (e_1) throw e_1.error;
@@ -219,7 +219,7 @@ function HerbalGuard(options) {
219
219
  case 12: return [7];
220
220
  case 13: return [3, 15];
221
221
  case 14:
222
- _m = _p.sent();
222
+ _p = _r.sent();
223
223
  return [3, 15];
224
224
  case 15:
225
225
  parsedBody = _.attempt(function () { return Buffer.concat(chunks).toString('utf8'); });
@@ -233,19 +233,21 @@ function HerbalGuard(options) {
233
233
  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;
234
234
  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;
235
235
  handlerName = string_util_class_1.StringUtil.isFalsyString(rawHandlerName) ? request.methodName : rawHandlerName;
236
- authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPropertype, handlerName);
236
+ authAdapters = (_j = (_h = decorators_1.Method.getPool(handlerPropertype)) === null || _h === void 0 ? void 0 : _h.getAuthAdapters) === null || _j === void 0 ? void 0 : _j.call(_h, handlerName);
237
+ if (authAdapters === null)
238
+ authAdapters = auth_adapter_decorator_1.AuthAdapters.getAdapters(handlerPropertype, handlerName);
237
239
  if (!(!(sequelizeInstance instanceof Error) && !decorators_1.NoTransaction.isDisabled(handlerPropertype, handlerName))) return [3, 20];
238
- _p.label = 16;
240
+ _r.label = 16;
239
241
  case 16:
240
- _p.trys.push([16, 18, , 19]);
241
- 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); }))];
242
+ _r.trys.push([16, 18, , 19]);
243
+ 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); }))];
242
244
  case 17:
243
- transaction = _p.sent();
245
+ transaction = _r.sent();
244
246
  request.transaction = transaction;
245
247
  this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Started transaction for route: ").concat(handlerName));
246
248
  return [3, 19];
247
249
  case 18:
248
- error_1 = _p.sent();
250
+ error_1 = _r.sent();
249
251
  if (error_1 instanceof Error) {
250
252
  this.getLogger().error("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Failed to start transaction: ").concat(error_1 === null || error_1 === void 0 ? void 0 : error_1.message, "\n").concat(error_1 === null || error_1 === void 0 ? void 0 : error_1.stack));
251
253
  }
@@ -255,12 +257,12 @@ function HerbalGuard(options) {
255
257
  if (decorators_1.NoTransaction.isDisabled(handlerPropertype, handlerName)) {
256
258
  this.getLogger().log("[trace:".concat(request === null || request === void 0 ? void 0 : request.traceId, ":transaction] Transaction is disabled for this route: ").concat(handlerName));
257
259
  }
258
- _p.label = 21;
260
+ _r.label = 21;
259
261
  case 21:
260
- _p.trys.push([21, 27, , 32]);
262
+ _r.trys.push([21, 27, , 32]);
261
263
  if (!(Array.isArray(authAdapters) && authAdapters.length > 0)) return [3, 26];
262
264
  _i = 0, authAdapters_1 = authAdapters;
263
- _p.label = 22;
265
+ _r.label = 22;
264
266
  case 22:
265
267
  if (!(_i < authAdapters_1.length)) return [3, 25];
266
268
  AuthAdapterClass = authAdapters_1[_i];
@@ -269,7 +271,7 @@ function HerbalGuard(options) {
269
271
  return [3, 24];
270
272
  return [4, adapter.authenticate(transaction)];
271
273
  case 23:
272
- authenticateResult = _p.sent();
274
+ authenticateResult = _r.sent();
273
275
  if (!authenticateResult)
274
276
  return [3, 25];
275
277
  request.authenticateResult = __assign({ AuthenticatorClass: AuthAdapterClass }, authenticateResult);
@@ -280,19 +282,19 @@ function HerbalGuard(options) {
280
282
  case 25: throw new common_2.UnauthorizedException();
281
283
  case 26: return [3, 32];
282
284
  case 27:
283
- error_2 = _p.sent();
284
- _p.label = 28;
285
+ error_2 = _r.sent();
286
+ _r.label = 28;
285
287
  case 28:
286
- _p.trys.push([28, 30, , 31]);
288
+ _r.trys.push([28, 30, , 31]);
287
289
  if (error_2 instanceof Error) {
288
290
  this.getLogger().error("Got error when handling route: ".concat(error_2 === null || error_2 === void 0 ? void 0 : error_2.message, " ").concat(error_2 === null || error_2 === void 0 ? void 0 : error_2.stack));
289
291
  }
290
- return [4, ((_k = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _k === void 0 ? void 0 : _k.call(transaction))];
292
+ return [4, ((_m = transaction === null || transaction === void 0 ? void 0 : transaction.rollback) === null || _m === void 0 ? void 0 : _m.call(transaction))];
291
293
  case 29:
292
- _p.sent();
294
+ _r.sent();
293
295
  return [3, 31];
294
296
  case 30:
295
- _o = _p.sent();
297
+ _q = _r.sent();
296
298
  return [3, 31];
297
299
  case 31: throw error_2;
298
300
  case 32: return [2, true];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-norantec/herbal",
3
- "version": "1.0.2-alpha.20",
3
+ "version": "1.0.2-alpha.21",
4
4
  "description": "Herbal is a builder and toolchain for Nest.js applications",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {