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

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.
Files changed (34) hide show
  1. package/dist/abstracts/client.abstract.class.d.ts +13 -0
  2. package/dist/abstracts/client.abstract.class.js +40 -0
  3. package/dist/abstracts/index.d.ts +1 -0
  4. package/dist/abstracts/index.js +1 -0
  5. package/dist/cli/herbal.js +129 -71
  6. package/dist/clients/index.d.ts +1 -0
  7. package/dist/clients/index.js +17 -0
  8. package/dist/clients/typescript-client.class.d.ts +22 -0
  9. package/dist/clients/typescript-client.class.js +374 -0
  10. package/dist/core.d.ts +4 -3
  11. package/dist/core.js +59 -41
  12. package/dist/{create.d.ts → create/create-application.d.ts} +8 -8
  13. package/dist/create/create-application.js +18 -0
  14. package/dist/create/create-client.d.ts +9 -0
  15. package/dist/create/create-client.js +17 -0
  16. package/dist/create/index.d.ts +2 -0
  17. package/dist/create/index.js +18 -0
  18. package/dist/decorators/index.d.ts +2 -0
  19. package/dist/decorators/index.js +2 -0
  20. package/dist/decorators/method.decorator.d.ts +44 -0
  21. package/dist/decorators/method.decorator.js +185 -0
  22. package/dist/decorators/no-transaction.decorator.d.ts +5 -0
  23. package/dist/decorators/no-transaction.decorator.js +16 -0
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.js +1 -0
  26. package/dist/sequelize-di.d.ts +4 -8
  27. package/dist/sequelize-di.js +12 -31
  28. package/dist/transformers/reflect-declaration.d.ts +1 -1
  29. package/dist/transformers/reflect-declaration.js +2 -2
  30. package/dist/types/request.type.d.ts +5 -2
  31. package/dist/utilities/controller-util.class.d.ts +2 -0
  32. package/dist/utilities/controller-util.class.js +124 -82
  33. package/package.json +8 -12
  34. package/dist/create.js +0 -154
@@ -0,0 +1,44 @@
1
+ import 'reflect-metadata';
2
+ import { z } from 'zod';
3
+ import { AuthAdapter } from '../abstracts';
4
+ import { HeaderUtil } from '@open-norantec/utilities/dist/header-util.class';
5
+ import { Constructor } from 'type-fest';
6
+ import { Type } from '@nestjs/common';
7
+ import { RequestContext } from '../types';
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 MethodOptions<IS extends z.Schema<any>, OS extends z.Schema<any>> {
12
+ inputSchema: IS;
13
+ outputSchema: OS;
14
+ authAdapters?: AuthAdapter[];
15
+ clientGroups?: ClientGroups | ClienttGroupsFactory;
16
+ disableTransaction?: boolean;
17
+ }
18
+ export type DependencyGetter = <T>(dependency: Constructor<T>) => T;
19
+ export interface MethodContext<IS extends z.Schema<any>> extends Omit<RequestContext, 'moduleRef'> {
20
+ headers: ReturnType<typeof HeaderUtil.parse>;
21
+ input: z.infer<IS>;
22
+ url: string;
23
+ getProvider: <TInput = any, TResult = TInput>(typeOrToken: Type<TInput> | Function | string | symbol) => TResult;
24
+ }
25
+ export type MethodCallContext<IS extends z.Schema<any>> = Omit<MethodContext<IS>, 'input'>;
26
+ export type MethodCallback<IS extends z.Schema<any>, OS extends z.Schema<any>> = (context: MethodContext<IS>) => Promise<z.infer<OS>>;
27
+ declare class MethodConfig<IS extends z.Schema<any>, OS extends z.Schema<any>> {
28
+ readonly name: string;
29
+ readonly options: MethodOptions<IS, OS>;
30
+ protected readonly callback: MethodCallback<IS, OS>;
31
+ constructor(name: string, options: MethodOptions<IS, OS>, callback: MethodCallback<IS, OS>);
32
+ call(callContext: MethodCallContext<IS>): Promise<z.TypeOf<OS>>;
33
+ }
34
+ declare class MethodPool {
35
+ protected readonly methods: Map<string, MethodConfig<any, any>>;
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;
38
+ getOpenAPIPathsObject(group?: string): PathsObject;
39
+ }
40
+ export declare function Method<IS extends z.Schema<any>, OS extends z.Schema<any>>(...parameters: Parameters<typeof MethodPool.prototype.registerMethod<IS, OS>>): ClassDecorator;
41
+ export declare namespace Method {
42
+ var getPool: (targetPrototype: object) => MethodPool | null;
43
+ }
44
+ export {};
@@ -0,0 +1,185 @@
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ Object.defineProperty(exports, "__esModule", { value: true });
50
+ exports.Method = void 0;
51
+ require("reflect-metadata");
52
+ var zod_1 = require("zod");
53
+ var utilities_1 = require("@open-norantec/utilities");
54
+ var common_1 = require("@nestjs/common");
55
+ var _ = require("lodash");
56
+ var zod_openapi_1 = require("zod-openapi");
57
+ var METHOD_POOL = Symbol();
58
+ var MethodConfig = (function () {
59
+ function MethodConfig(name, options, callback) {
60
+ this.name = name;
61
+ this.options = options;
62
+ this.callback = callback;
63
+ }
64
+ MethodConfig.prototype.call = function (callContext) {
65
+ var _a, _b, _c, _d, _e, _f;
66
+ return __awaiter(this, void 0, void 0, function () {
67
+ var parsedBody_1, input, rawResponse_1, response, error_1;
68
+ var _this = this;
69
+ return __generator(this, function (_g) {
70
+ switch (_g.label) {
71
+ case 0:
72
+ _g.trys.push([0, 2, , 3]);
73
+ 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
+ if (input instanceof zod_1.ZodError) {
78
+ throw new common_1.BadRequestException({
79
+ from: 'request',
80
+ 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 : [],
81
+ });
82
+ }
83
+ else if (input instanceof Error)
84
+ throw input;
85
+ return [4, this.callback(__assign(__assign({}, callContext), { input: input }))];
86
+ case 1:
87
+ rawResponse_1 = _g.sent();
88
+ response = _.attempt(function () { return _this.options.outputSchema.parse(rawResponse_1); });
89
+ if (response instanceof zod_1.ZodError) {
90
+ throw new common_1.BadRequestException({
91
+ from: 'response',
92
+ 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 : [],
93
+ });
94
+ }
95
+ else if (response instanceof Error)
96
+ throw response;
97
+ return [2, response];
98
+ case 2:
99
+ error_1 = _g.sent();
100
+ throw error_1;
101
+ case 3: return [2];
102
+ }
103
+ });
104
+ });
105
+ };
106
+ return MethodConfig;
107
+ }());
108
+ var MethodPool = (function () {
109
+ function MethodPool() {
110
+ this.methods = new Map();
111
+ }
112
+ MethodPool.prototype.registerMethod = function (name, options, callback) {
113
+ if (utilities_1.StringUtil.isFalsyString(name))
114
+ return;
115
+ if (name.includes('/'))
116
+ throw new Error("Method name cannot contain slashes: ".concat(name));
117
+ this.methods.set(name, new MethodConfig(name, options, callback));
118
+ };
119
+ 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;
123
+ };
124
+ MethodPool.prototype.getOpenAPIPathsObject = function (group) {
125
+ var result = {};
126
+ Array.from(this.methods.entries()).forEach(function (_a) {
127
+ var _b;
128
+ var name = _a[0], config = _a[1];
129
+ var defaultGroupName = "".concat(Date.now(), "_").concat(Math.random().toString(16).slice(2));
130
+ var currentGroupName = utilities_1.StringUtil.isFalsyString(group) ? defaultGroupName : group;
131
+ var clientGroups = typeof config.options.clientGroups === 'function'
132
+ ? config.options.clientGroups(defaultGroupName)
133
+ : (_b = config === null || config === void 0 ? void 0 : config.options) === null || _b === void 0 ? void 0 : _b.clientGroups;
134
+ if (Array.isArray(clientGroups) && !clientGroups.includes(currentGroupName))
135
+ return;
136
+ result["/".concat(name)] = {
137
+ post: {
138
+ requestBody: {
139
+ description: 'Request body for method ' + name,
140
+ required: true,
141
+ content: {
142
+ 'application/json': {
143
+ schema: (0, zod_openapi_1.createSchema)(config.options.inputSchema).schema,
144
+ },
145
+ },
146
+ },
147
+ responses: {
148
+ '200': {
149
+ description: 'Response for method ' + name,
150
+ content: {
151
+ 'application/json': (0, zod_openapi_1.createSchema)(zod_1.z.object({
152
+ data: config.options.outputSchema,
153
+ token: zod_1.z.string().nullable(),
154
+ })),
155
+ },
156
+ },
157
+ },
158
+ },
159
+ };
160
+ });
161
+ return result;
162
+ };
163
+ return MethodPool;
164
+ }());
165
+ function Method() {
166
+ var parameters = [];
167
+ for (var _i = 0; _i < arguments.length; _i++) {
168
+ parameters[_i] = arguments[_i];
169
+ }
170
+ return function (target) {
171
+ var pool = Reflect.getMetadata(METHOD_POOL, target.prototype);
172
+ if (!(pool instanceof MethodPool)) {
173
+ pool = new MethodPool();
174
+ Reflect.defineMetadata(METHOD_POOL, pool, target.prototype);
175
+ }
176
+ pool.registerMethod.apply(pool, parameters);
177
+ };
178
+ }
179
+ exports.Method = Method;
180
+ Method.getPool = function (targetPrototype) {
181
+ var pool = Reflect.getMetadata(METHOD_POOL, targetPrototype);
182
+ if (!(pool instanceof MethodPool))
183
+ return null;
184
+ return pool;
185
+ };
@@ -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
+ };
package/dist/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export * from './constants';
8
8
  export * from './decorators';
9
9
  export * from './utilities';
10
10
  export * from '@nestjs/sequelize';
11
+ export * from './clients';
11
12
  export { Response } from 'express';
12
13
  export { Request } from './types';
13
14
  export { LoggerService } from './modules';
package/dist/index.js CHANGED
@@ -25,5 +25,6 @@ __exportStar(require("./constants"), exports);
25
25
  __exportStar(require("./decorators"), exports);
26
26
  __exportStar(require("./utilities"), exports);
27
27
  __exportStar(require("@nestjs/sequelize"), exports);
28
+ __exportStar(require("./clients"), exports);
28
29
  var modules_1 = require("./modules");
29
30
  Object.defineProperty(exports, "LoggerService", { enumerable: true, get: function () { return modules_1.LoggerService; } });
@@ -1,14 +1,10 @@
1
- import { BelongsToOptions, ModelAttributeColumnOptions } from 'sequelize';
1
+ import { BelongsToOptions, IndexesOptions, ModelAttributeColumnOptions } from 'sequelize';
2
2
  import { Model, TableOptions as SequelizeTableOptions, ModelClassGetter } from 'sequelize-typescript';
3
3
  import { Constructor } from 'type-fest';
4
4
  export * from 'sequelize-typescript';
5
- export interface TableOptions<M extends Model = Model> extends SequelizeTableOptions<M> {
6
- standaloneIndexes?: Array<string | {
7
- field: string;
8
- name?: string;
9
- unique?: string;
10
- }>;
5
+ export interface TableOptions<M extends Model = Model> extends Omit<SequelizeTableOptions<M>, 'indexes'> {
6
+ indexes?: Array<IndexesOptions | string>;
11
7
  }
12
- export declare function Table<M extends Model = Model>({ standaloneIndexes, ...options }: TableOptions<M>): (target: Constructor<M>) => void;
8
+ export declare function Table<M extends Model = Model>({ indexes, ...options }: TableOptions<M>): (target: Constructor<M>) => void;
13
9
  export declare function BelongsTo(associatedClassGetter: ModelClassGetter<{}, {}>, options?: BelongsToOptions): Function;
14
10
  export declare function DateColumn(options: Partial<ModelAttributeColumnOptions>): Function;
@@ -37,44 +37,25 @@ var __rest = (this && this.__rest) || function (s, e) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.DateColumn = exports.BelongsTo = exports.Table = void 0;
40
- var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
41
40
  var sequelize_typescript_1 = require("sequelize-typescript");
42
41
  __exportStar(require("sequelize-typescript"), exports);
43
42
  function Table(_a) {
44
- var standaloneIndexes = _a.standaloneIndexes, options = __rest(_a, ["standaloneIndexes"]);
43
+ var indexes = _a.indexes, options = __rest(_a, ["indexes"]);
45
44
  return function (target) {
46
45
  var newOptions = !options ? {} : options;
47
- if (!Array.isArray(newOptions === null || newOptions === void 0 ? void 0 : newOptions.indexes))
48
- newOptions.indexes = [];
49
- newOptions.indexes = Array.from(newOptions.indexes)
50
- .concat({
46
+ newOptions.indexes = [];
47
+ newOptions.indexes = Array.from(Array.isArray(indexes) ? indexes : []).map(function (item, index) {
48
+ if (typeof item === 'string') {
49
+ return {
50
+ name: "sidx__".concat(index),
51
+ fields: [item],
52
+ };
53
+ }
54
+ return item;
55
+ }).concat({
51
56
  name: 'pagination',
52
57
  fields: ['id', 'created_at'],
53
- })
54
- .concat(Array.isArray(standaloneIndexes)
55
- ? standaloneIndexes
56
- .map(function (item, index) {
57
- if (typeof item === 'string') {
58
- if (item.length === 0)
59
- return null;
60
- return {
61
- name: "idx_standalone__".concat(index),
62
- fields: [item],
63
- };
64
- }
65
- else if (typeof (item === null || item === void 0 ? void 0 : item.field) === 'string' && item.field.length > 0) {
66
- return {
67
- fields: [item.field],
68
- name: string_util_class_1.StringUtil.isFalsyString(item === null || item === void 0 ? void 0 : item.name) ? "idx_standalone__".concat(index) : item.name,
69
- unique: typeof (item === null || item === void 0 ? void 0 : item.unique) === 'boolean' ? item.unique : false,
70
- };
71
- }
72
- else {
73
- return null;
74
- }
75
- })
76
- .filter(function (item) { return item !== null; })
77
- : []);
58
+ });
78
59
  (0, sequelize_typescript_1.Table)(__assign(__assign({}, newOptions), { tableName: newOptions === null || newOptions === void 0 ? void 0 : newOptions.modelName, modelName: target.name }))(target);
79
60
  };
80
61
  }
@@ -1,3 +1,3 @@
1
1
  import * as ts from 'typescript';
2
2
  export declare const DECORATOR_NAME_PREFIX = "\u03A6nt:method:";
3
- export default function transformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile>;
3
+ export declare function transformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile>;
@@ -9,7 +9,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
9
9
  return to.concat(ar || Array.prototype.slice.call(from));
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.DECORATOR_NAME_PREFIX = void 0;
12
+ exports.transformer = exports.DECORATOR_NAME_PREFIX = void 0;
13
13
  var ts = require("typescript");
14
14
  exports.DECORATOR_NAME_PREFIX = 'Φnt:method:';
15
15
  function transformer(program) {
@@ -60,4 +60,4 @@ function transformer(program) {
60
60
  };
61
61
  };
62
62
  }
63
- exports.default = transformer;
63
+ exports.transformer = transformer;
@@ -1,10 +1,13 @@
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
+ import { ModuleRef } from '@nestjs/core';
5
+ export interface RequestContext {
5
6
  methodName: string;
7
+ moduleRef: ModuleRef;
6
8
  rawBody: string | null;
7
9
  traceId: string;
8
10
  authenticateResult?: AuthenticateResult;
9
11
  transaction?: Transaction;
10
- };
12
+ }
13
+ export type Request = ExpressRequest & RequestContext;
@@ -2,6 +2,7 @@ import 'reflect-metadata';
2
2
  import { Request as ExpressRequest } from 'express';
3
3
  import { Constructor } from 'type-fest';
4
4
  export declare function isHerbalController(target: Function): boolean;
5
+ export declare function getControllerName(target: Function): any;
5
6
  export interface HerbalControllerOptions {
6
7
  prefix?: string;
7
8
  useHeadGuards?: Constructor<any>[];
@@ -16,5 +17,6 @@ export declare class ControllerUtil {
16
17
  static create(createOptions?: ControllerUtilCreateOptions): {
17
18
  (options?: HerbalControllerOptions): ClassDecorator;
18
19
  isHerbalController: typeof isHerbalController;
20
+ getControllerName: typeof getControllerName;
19
21
  };
20
22
  }