@open-norantec/herbal 1.0.2-alpha.3 → 1.0.2-alpha.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/core.js CHANGED
@@ -66,89 +66,76 @@ exports.HerbalController = void 0;
66
66
  require("reflect-metadata");
67
67
  var common_1 = require("@nestjs/common");
68
68
  var header_util_class_1 = require("@open-norantec/utilities/dist/header-util.class");
69
- var zod_1 = require("zod");
70
- var _ = require("lodash");
71
69
  var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
70
+ var utilities_1 = require("@open-norantec/utilities");
71
+ var controller_util_class_1 = require("./utilities/controller-util.class");
72
72
  __exportStar(require("@nestjs/core"), exports);
73
73
  var HerbalController = exports.HerbalController = (function () {
74
74
  function HerbalController() {
75
- var _this = this;
76
- this.registerMethod = function (inputSchema, outputSchema, callback) {
77
- return function (request, rawInput, headers) { return __awaiter(_this, void 0, void 0, function () {
78
- var input, responseData;
79
- var _a, _b, _c;
80
- return __generator(this, function (_d) {
81
- switch (_d.label) {
82
- case 0:
83
- input = inputSchema instanceof zod_1.ZodAny ? rawInput : _.attempt(function () { return inputSchema.parse(rawInput); });
84
- if (input instanceof Error) {
85
- if (input instanceof zod_1.ZodError) {
86
- throw new common_1.BadRequestException({
87
- 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 : [],
88
- });
89
- }
90
- throw input;
91
- }
92
- return [4, callback({ input: input, headers: headers, request: request })];
93
- case 1:
94
- responseData = _d.sent();
95
- return [2, {
96
- request: input,
97
- response: outputSchema instanceof zod_1.ZodAny ? responseData : outputSchema.parse(responseData),
98
- }];
99
- }
100
- });
101
- }); };
102
- };
103
75
  }
104
- HerbalController.prototype.handler = function (request) {
105
- var _a, _b, _c, _d, _e, _f;
76
+ HerbalController.prototype.$handleRequest = function (request) {
77
+ var _a, _b, _c, _d;
106
78
  return __awaiter(this, void 0, void 0, function () {
107
- var methodHandler, parsedBody, result, _g, _h, error_1;
108
- var _j;
109
- return __generator(this, function (_k) {
110
- switch (_k.label) {
79
+ var result, error_1;
80
+ var _e;
81
+ var _this = this;
82
+ return __generator(this, function (_f) {
83
+ switch (_f.label) {
111
84
  case 0:
112
- methodHandler = this[request === null || request === void 0 ? void 0 : request.methodName];
113
- parsedBody = _.attempt(function () { return JSON.parse((request === null || request === void 0 ? void 0 : request.rawBody) || ''); });
114
- _k.label = 1;
85
+ _f.trys.push([0, 3, , 5]);
86
+ _e = {};
87
+ return [4, this.$call(request.methodName, {
88
+ authenticateResult: request.authenticateResult,
89
+ headers: header_util_class_1.HeaderUtil.parse((_a = request.headers) !== null && _a !== void 0 ? _a : {}),
90
+ methodName: request.methodName,
91
+ rawBody: request.rawBody,
92
+ traceId: request.traceId,
93
+ transaction: request.transaction,
94
+ url: request.originalUrl,
95
+ })];
115
96
  case 1:
116
- _k.trys.push([1, 12, , 13]);
117
- if (!(typeof methodHandler === 'function')) return [3, 7];
118
- _j = {};
119
- return [4, methodHandler(request, parsedBody instanceof Error ? undefined : parsedBody, header_util_class_1.HeaderUtil.parse((_a = request.headers) !== null && _a !== void 0 ? _a : {})).then(function (response) { return response === null || response === void 0 ? void 0 : response.response; })];
120
- case 2:
121
- result = (_j.data = _k.sent(),
122
- _j.token = string_util_class_1.StringUtil.isFalsyString((_b = request === null || request === void 0 ? void 0 : request.authenticateResult) === null || _b === void 0 ? void 0 : _b.nextToken)
97
+ result = (_e.data = _f.sent(),
98
+ _e.token = string_util_class_1.StringUtil.isFalsyString((_b = request === null || request === void 0 ? void 0 : request.authenticateResult) === null || _b === void 0 ? void 0 : _b.nextToken)
123
99
  ? null
124
100
  : request.authenticateResult.nextToken,
125
- _j);
126
- _k.label = 3;
127
- case 3:
128
- _k.trys.push([3, 5, , 6]);
101
+ _e);
129
102
  return [4, ((_d = (_c = request === null || request === void 0 ? void 0 : request.transaction) === null || _c === void 0 ? void 0 : _c.commit) === null || _d === void 0 ? void 0 : _d.call(_c))];
103
+ case 2:
104
+ _f.sent();
105
+ return [2, result];
106
+ case 3:
107
+ error_1 = _f.sent();
108
+ return [4, utilities_1.AttemptUtil.execPromise((function () { return __awaiter(_this, void 0, void 0, function () {
109
+ var _a, _b;
110
+ return __generator(this, function (_c) {
111
+ switch (_c.label) {
112
+ case 0: return [4, ((_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))];
113
+ case 1:
114
+ _c.sent();
115
+ return [2];
116
+ }
117
+ });
118
+ }); })())];
130
119
  case 4:
131
- _k.sent();
132
- return [3, 6];
133
- case 5:
134
- _g = _k.sent();
135
- return [3, 6];
136
- case 6: return [2, result];
137
- case 7:
138
- _k.trys.push([7, 9, , 10]);
139
- return [4, ((_f = (_e = request === null || request === void 0 ? void 0 : request.transaction) === null || _e === void 0 ? void 0 : _e.rollback) === null || _f === void 0 ? void 0 : _f.call(_e))];
140
- case 8:
141
- _k.sent();
142
- return [3, 10];
143
- case 9:
144
- _h = _k.sent();
145
- return [3, 10];
146
- case 10: throw new common_1.NotFoundException();
147
- case 11: return [3, 13];
148
- case 12:
149
- error_1 = _k.sent();
120
+ _f.sent();
150
121
  throw error_1;
151
- case 13: return [2];
122
+ case 5: return [2];
123
+ }
124
+ });
125
+ });
126
+ };
127
+ HerbalController.prototype.$call = function (name, context) {
128
+ var _a, _b;
129
+ return __awaiter(this, void 0, void 0, function () {
130
+ var callFn;
131
+ return __generator(this, function (_c) {
132
+ switch (_c.label) {
133
+ case 0:
134
+ callFn = (_b = (_a = controller_util_class_1.ControllerUtil.getPool(this)) === null || _a === void 0 ? void 0 : _a.getCallFn) === null || _b === void 0 ? void 0 : _b.call(_a, name);
135
+ if (typeof callFn !== 'function')
136
+ throw new common_1.NotFoundException("Method ".concat(name, " not found"));
137
+ return [4, callFn(this, context)];
138
+ case 1: return [2, _c.sent()];
152
139
  }
153
140
  });
154
141
  });
@@ -159,6 +146,6 @@ var HerbalController = exports.HerbalController = (function () {
159
146
  __metadata("design:type", Function),
160
147
  __metadata("design:paramtypes", [Object]),
161
148
  __metadata("design:returntype", Promise)
162
- ], HerbalController.prototype, "handler", null);
149
+ ], HerbalController.prototype, "$handleRequest", null);
163
150
  return HerbalController;
164
151
  }());
@@ -1,11 +1,9 @@
1
1
  import 'reflect-metadata';
2
2
  import { CorsOptions, CorsOptionsDelegate } from '@nestjs/common/interfaces/external/cors-options.interface';
3
3
  import { Constructor } from 'type-fest';
4
- import { GroupsFactory } from './decorators/client-groups.decorator';
5
4
  import { CanActivate, ExceptionFilter, INestApplication, NestApplicationOptions, NestInterceptor, PipeTransform, WebSocketAdapter } from '@nestjs/common';
6
5
  export type Resolver = <T>(Class: Constructor<T>) => Promise<T>;
7
- export type TypeCustomizerFn = (dataTypeMapName: string, name: string, genericName: string) => string[];
8
- export interface CreateOptions {
6
+ export interface CreateApplicationOptions {
9
7
  Module: Constructor<any>;
10
8
  cors?: CorsOptions | CorsOptionsDelegate<any> | false;
11
9
  factoryOptions?: NestApplicationOptions;
@@ -16,12 +14,14 @@ export interface CreateOptions {
16
14
  uses?: any[];
17
15
  websocketAdapter?: WebSocketAdapter;
18
16
  getListenPort: (resolver: Resolver) => number | Promise<number>;
19
- allowedClientGroupsFactory?: GroupsFactory;
20
17
  callback?: (listenPort: number, app: INestApplication<any>) => void | Promise<void>;
21
18
  onBeforeBootstrap?: () => void | Promise<void>;
22
19
  worker?: (resolver: Resolver, listenPort: number) => any;
23
20
  }
24
- export declare function create(options: CreateOptions): {
25
- options: CreateOptions;
26
- generateClientSourceFile: () => string;
27
- };
21
+ declare class Application {
22
+ readonly options: CreateApplicationOptions;
23
+ constructor(options: CreateApplicationOptions);
24
+ }
25
+ export declare function isApplication(input: any): boolean;
26
+ export declare function createApplication(options: CreateApplicationOptions): Application;
27
+ export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createApplication = exports.isApplication = void 0;
4
+ require("reflect-metadata");
5
+ var Application = (function () {
6
+ function Application(options) {
7
+ this.options = options;
8
+ }
9
+ return Application;
10
+ }());
11
+ function isApplication(input) {
12
+ return input instanceof Application;
13
+ }
14
+ exports.isApplication = isApplication;
15
+ function createApplication(options) {
16
+ return new Application(options);
17
+ }
18
+ exports.createApplication = createApplication;
@@ -0,0 +1,9 @@
1
+ import { Constructor } from 'type-fest';
2
+ import { Client as ClientFactory, CreateClientOptions } from '../abstracts/client.abstract.class';
3
+ declare class Client {
4
+ readonly instance: ClientFactory;
5
+ constructor(instance: ClientFactory);
6
+ }
7
+ export declare function isClient(input: any): boolean;
8
+ export declare function createClient(Class: Constructor<ClientFactory>, options: CreateClientOptions): Client;
9
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createClient = exports.isClient = void 0;
4
+ var Client = (function () {
5
+ function Client(instance) {
6
+ this.instance = instance;
7
+ }
8
+ return Client;
9
+ }());
10
+ function isClient(input) {
11
+ return input instanceof Client;
12
+ }
13
+ exports.isClient = isClient;
14
+ function createClient(Class, options) {
15
+ return new Client(new Class(options));
16
+ }
17
+ exports.createClient = createClient;
@@ -0,0 +1,2 @@
1
+ export * from './create-application';
2
+ export * from './create-client';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./create-application"), exports);
18
+ __exportStar(require("./create-client"), exports);
@@ -1,2 +1,3 @@
1
1
  export * from './auth-adapter.decorator';
2
2
  export * from './client-groups.decorator';
3
+ export * from './no-transaction.decorator';
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./auth-adapter.decorator"), exports);
18
18
  __exportStar(require("./client-groups.decorator"), exports);
19
+ __exportStar(require("./no-transaction.decorator"), exports);
@@ -0,0 +1,5 @@
1
+ import 'reflect-metadata';
2
+ export declare function NoTransaction(): PropertyDecorator;
3
+ export declare namespace NoTransaction {
4
+ var isDisabled: (target: object, propertyKey: string) => boolean;
5
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NoTransaction = void 0;
4
+ require("reflect-metadata");
5
+ var _ = require("lodash");
6
+ var TRANSACTION_DISABLED = Symbol();
7
+ function NoTransaction() {
8
+ return function (target, propertyKey) {
9
+ Reflect.defineMetadata(TRANSACTION_DISABLED, true, target, propertyKey);
10
+ };
11
+ }
12
+ exports.NoTransaction = NoTransaction;
13
+ NoTransaction.isDisabled = function (target, propertyKey) {
14
+ var result = _.attempt(function () { return Reflect.getMetadata(TRANSACTION_DISABLED, target, propertyKey); });
15
+ return result === true;
16
+ };
@@ -46,12 +46,10 @@ function Table(_a) {
46
46
  newOptions.indexes = [];
47
47
  newOptions.indexes = Array.from(Array.isArray(indexes) ? indexes : []).map(function (item, index) {
48
48
  if (typeof item === 'string') {
49
- return [
50
- {
51
- name: "sidx__".concat(index),
52
- fields: [item],
53
- },
54
- ];
49
+ return {
50
+ name: "sidx__".concat(index),
51
+ fields: [item],
52
+ };
55
53
  }
56
54
  return item;
57
55
  }).concat({
@@ -1,10 +1,11 @@
1
1
  import { Request as ExpressRequest } from 'express';
2
2
  import { AuthenticateResult } from '../abstracts/auth-adapter.abstract.class';
3
3
  import { Transaction } from 'sequelize';
4
- export type Request = ExpressRequest & {
4
+ export interface RequestContext {
5
5
  methodName: string;
6
6
  rawBody: string | null;
7
7
  traceId: string;
8
8
  authenticateResult?: AuthenticateResult;
9
9
  transaction?: Transaction;
10
- };
10
+ }
11
+ export type Request = ExpressRequest & RequestContext;
@@ -1,11 +1,49 @@
1
1
  import 'reflect-metadata';
2
+ import { RequestContext } from '../types/request.type';
2
3
  import { Request as ExpressRequest } from 'express';
3
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
+ }
4
40
  export declare function isHerbalController(target: Function): boolean;
5
- export interface HerbalControllerOptions {
41
+ export declare function getControllerName(target: Function): any;
42
+ export interface HerbalControllerOptions<C> {
6
43
  prefix?: string;
7
44
  useHeadGuards?: Constructor<any>[];
8
45
  useTailGuards?: Constructor<any>[];
46
+ methods?: (register: MethodRegisterFn<C>) => void;
9
47
  }
10
48
  export interface ControllerUtilCreateOptions {
11
49
  prefix?: string;
@@ -13,8 +51,11 @@ export interface ControllerUtilCreateOptions {
13
51
  getTraceId?: (request: ExpressRequest) => string;
14
52
  }
15
53
  export declare class ControllerUtil {
54
+ static getPool(targetPrototype: object): MethodPool | null;
16
55
  static create(createOptions?: ControllerUtilCreateOptions): {
17
- (options?: HerbalControllerOptions): ClassDecorator;
56
+ <C>(options?: HerbalControllerOptions<C> | undefined): ClassDecorator;
18
57
  isHerbalController: typeof isHerbalController;
58
+ getControllerName: typeof getControllerName;
19
59
  };
20
60
  }
61
+ export {};