@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.
- package/dist/abstracts/client.abstract.class.d.ts +13 -0
- package/dist/abstracts/client.abstract.class.js +40 -0
- package/dist/abstracts/index.d.ts +1 -0
- package/dist/abstracts/index.js +1 -0
- package/dist/cli/herbal.js +129 -71
- package/dist/clients/index.d.ts +1 -0
- package/dist/clients/index.js +17 -0
- package/dist/clients/typescript-client.class.d.ts +22 -0
- package/dist/clients/typescript-client.class.js +374 -0
- package/dist/core.d.ts +4 -3
- package/dist/core.js +59 -41
- package/dist/{create.d.ts → create/create-application.d.ts} +8 -8
- package/dist/create/create-application.js +18 -0
- package/dist/create/create-client.d.ts +9 -0
- package/dist/create/create-client.js +17 -0
- package/dist/create/index.d.ts +2 -0
- package/dist/create/index.js +18 -0
- package/dist/decorators/index.d.ts +2 -0
- package/dist/decorators/index.js +2 -0
- package/dist/decorators/method.decorator.d.ts +44 -0
- package/dist/decorators/method.decorator.js +185 -0
- package/dist/decorators/no-transaction.decorator.d.ts +5 -0
- package/dist/decorators/no-transaction.decorator.js +16 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/sequelize-di.d.ts +4 -8
- package/dist/sequelize-di.js +12 -31
- package/dist/transformers/reflect-declaration.d.ts +1 -1
- package/dist/transformers/reflect-declaration.js +2 -2
- package/dist/types/request.type.d.ts +5 -2
- package/dist/utilities/controller-util.class.d.ts +2 -0
- package/dist/utilities/controller-util.class.js +124 -82
- package/package.json +8 -12
- package/dist/create.js +0 -154
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
29
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
30
|
+
if (ar || !(i in from)) {
|
|
31
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
32
|
+
ar[i] = from[i];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.TypeScriptClient = exports.convertOpenApiToTypescript = void 0;
|
|
39
|
+
require("reflect-metadata");
|
|
40
|
+
var client_abstract_class_1 = require("../abstracts/client.abstract.class");
|
|
41
|
+
var nest_util_class_1 = require("../utilities/nest-util.class");
|
|
42
|
+
var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
|
|
43
|
+
var controller_util_class_1 = require("../utilities/controller-util.class");
|
|
44
|
+
var method_decorator_1 = require("../decorators/method.decorator");
|
|
45
|
+
var OpenApiToTypescriptConverter = (function () {
|
|
46
|
+
function OpenApiToTypescriptConverter(options) {
|
|
47
|
+
if (options === void 0) { options = {}; }
|
|
48
|
+
this.options = __assign({ useInterface: false, export: true, generateJSDoc: true, preferUnknown: true, singleLine: true }, options);
|
|
49
|
+
}
|
|
50
|
+
OpenApiToTypescriptConverter.prototype.convert = function (schema, name) {
|
|
51
|
+
var typeString = this.parseSchema(schema, 0, name);
|
|
52
|
+
if (name) {
|
|
53
|
+
var exportKeyword = this.options.export ? 'export ' : '';
|
|
54
|
+
if (this.options.useInterface && schema.type === 'object' && schema.properties) {
|
|
55
|
+
var code_1 = "".concat(exportKeyword).concat(typeString);
|
|
56
|
+
return { code: code_1, name: name };
|
|
57
|
+
}
|
|
58
|
+
var code = "".concat(exportKeyword, "type ").concat(name, " = ").concat(typeString, ";");
|
|
59
|
+
return { code: code, name: name };
|
|
60
|
+
}
|
|
61
|
+
return { code: typeString };
|
|
62
|
+
};
|
|
63
|
+
OpenApiToTypescriptConverter.prototype.generateTypes = function (schemas) {
|
|
64
|
+
var _this = this;
|
|
65
|
+
if (!schemas)
|
|
66
|
+
return '';
|
|
67
|
+
var lines = [];
|
|
68
|
+
Object.entries(schemas).forEach(function (_a) {
|
|
69
|
+
var name = _a[0], schema = _a[1];
|
|
70
|
+
var result = _this.convert(schema, name);
|
|
71
|
+
lines.push(result.code);
|
|
72
|
+
});
|
|
73
|
+
return lines.join('\n');
|
|
74
|
+
};
|
|
75
|
+
OpenApiToTypescriptConverter.fromZodResult = function (result, name, options) {
|
|
76
|
+
var converter = new OpenApiToTypescriptConverter(options);
|
|
77
|
+
return converter.convert(result.schema, name);
|
|
78
|
+
};
|
|
79
|
+
OpenApiToTypescriptConverter.prototype.parseSchema = function (schema, depth, name) {
|
|
80
|
+
if (depth === void 0) { depth = 0; }
|
|
81
|
+
if ('$ref' in schema && typeof schema.$ref === 'string') {
|
|
82
|
+
return this.refToTypeName(schema.$ref);
|
|
83
|
+
}
|
|
84
|
+
var s = schema;
|
|
85
|
+
if (s.oneOf) {
|
|
86
|
+
return this.parseOneOf(s.oneOf, depth);
|
|
87
|
+
}
|
|
88
|
+
if (s.anyOf) {
|
|
89
|
+
return this.parseAnyOf(s.anyOf, depth);
|
|
90
|
+
}
|
|
91
|
+
if (s.allOf) {
|
|
92
|
+
return this.parseAllOf(s.allOf, depth);
|
|
93
|
+
}
|
|
94
|
+
switch (s.type) {
|
|
95
|
+
case 'object':
|
|
96
|
+
return this.parseObject(s, depth, name);
|
|
97
|
+
case 'array':
|
|
98
|
+
return this.parseArray(s, depth);
|
|
99
|
+
case 'string':
|
|
100
|
+
return this.parseString(s);
|
|
101
|
+
case 'integer':
|
|
102
|
+
case 'number':
|
|
103
|
+
return 'number';
|
|
104
|
+
case 'boolean':
|
|
105
|
+
return 'boolean';
|
|
106
|
+
default:
|
|
107
|
+
if (s.enum) {
|
|
108
|
+
return this.parseEnum(s.enum);
|
|
109
|
+
}
|
|
110
|
+
if (Array.isArray(s.type)) {
|
|
111
|
+
return s.type.join(' | ');
|
|
112
|
+
}
|
|
113
|
+
if (Object.keys(s).length === 0 || (s.type === undefined && !s.enum)) {
|
|
114
|
+
return this.options.preferUnknown ? 'unknown' : 'any';
|
|
115
|
+
}
|
|
116
|
+
return this.options.preferUnknown ? 'unknown' : 'any';
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
OpenApiToTypescriptConverter.prototype.parseObject = function (schema, depth, name) {
|
|
120
|
+
var _this = this;
|
|
121
|
+
if (schema.additionalProperties && !schema.properties) {
|
|
122
|
+
if (typeof schema.additionalProperties === 'boolean') {
|
|
123
|
+
return 'Record<string, unknown>';
|
|
124
|
+
}
|
|
125
|
+
var valueType = this.parseSchema(schema.additionalProperties, depth);
|
|
126
|
+
return "Record<string, ".concat(valueType, ">");
|
|
127
|
+
}
|
|
128
|
+
var properties = schema.properties || {};
|
|
129
|
+
var required = schema.required || [];
|
|
130
|
+
if (Object.keys(properties).length === 0) {
|
|
131
|
+
if (schema.additionalProperties) {
|
|
132
|
+
if (typeof schema.additionalProperties === 'boolean') {
|
|
133
|
+
return 'Record<string, unknown>';
|
|
134
|
+
}
|
|
135
|
+
var valueType = this.parseSchema(schema.additionalProperties, depth);
|
|
136
|
+
return "Record<string, ".concat(valueType, ">");
|
|
137
|
+
}
|
|
138
|
+
return 'Record<string, never>';
|
|
139
|
+
}
|
|
140
|
+
if (this.options.useInterface && name && depth === 0) {
|
|
141
|
+
var props_1 = [];
|
|
142
|
+
Object.entries(properties).forEach(function (_a) {
|
|
143
|
+
var key = _a[0], propSchema = _a[1];
|
|
144
|
+
var isRequired = required.includes(key);
|
|
145
|
+
var tsType = _this.parseSchema(propSchema, depth + 1);
|
|
146
|
+
var optional = isRequired ? '' : '?';
|
|
147
|
+
props_1.push("".concat(key).concat(optional, ": ").concat(tsType));
|
|
148
|
+
});
|
|
149
|
+
return "interface ".concat(name, " { ").concat(props_1.join('; '), " }");
|
|
150
|
+
}
|
|
151
|
+
var props = [];
|
|
152
|
+
Object.entries(properties).forEach(function (_a) {
|
|
153
|
+
var key = _a[0], propSchema = _a[1];
|
|
154
|
+
var isRequired = required.includes(key);
|
|
155
|
+
var tsType = _this.parseSchema(propSchema, depth + 1);
|
|
156
|
+
var optional = isRequired ? '' : '?';
|
|
157
|
+
props.push("".concat(key).concat(optional, ": ").concat(tsType));
|
|
158
|
+
});
|
|
159
|
+
return "{ ".concat(props.join('; '), " }");
|
|
160
|
+
};
|
|
161
|
+
OpenApiToTypescriptConverter.prototype.parseArray = function (schema, depth) {
|
|
162
|
+
var _this = this;
|
|
163
|
+
var items = schema.items;
|
|
164
|
+
if (!items) {
|
|
165
|
+
return 'unknown[]';
|
|
166
|
+
}
|
|
167
|
+
if (Array.isArray(items)) {
|
|
168
|
+
var types = items.map(function (item) { return _this.parseSchema(item, depth); });
|
|
169
|
+
return "[".concat(types.join(', '), "]");
|
|
170
|
+
}
|
|
171
|
+
var itemType = this.parseSchema(items, depth);
|
|
172
|
+
if (itemType.includes('|') || itemType.includes('&')) {
|
|
173
|
+
return "(".concat(itemType, ")[]");
|
|
174
|
+
}
|
|
175
|
+
return "".concat(itemType, "[]");
|
|
176
|
+
};
|
|
177
|
+
OpenApiToTypescriptConverter.prototype.parseString = function (schema) {
|
|
178
|
+
if (schema.enum) {
|
|
179
|
+
return this.parseEnum(schema.enum);
|
|
180
|
+
}
|
|
181
|
+
return 'string';
|
|
182
|
+
};
|
|
183
|
+
OpenApiToTypescriptConverter.prototype.parseEnum = function (values) {
|
|
184
|
+
var literals = values.map(function (v) {
|
|
185
|
+
if (v === null)
|
|
186
|
+
return 'null';
|
|
187
|
+
if (typeof v === 'string') {
|
|
188
|
+
return "'".concat(v.replace(/'/g, "\\'"), "'");
|
|
189
|
+
}
|
|
190
|
+
return String(v);
|
|
191
|
+
});
|
|
192
|
+
return literals.join(' | ');
|
|
193
|
+
};
|
|
194
|
+
OpenApiToTypescriptConverter.prototype.parseOneOf = function (schemas, depth) {
|
|
195
|
+
var _this = this;
|
|
196
|
+
var types = schemas.map(function (s) { return _this.parseSchema(s, depth); });
|
|
197
|
+
return types.join(' | ');
|
|
198
|
+
};
|
|
199
|
+
OpenApiToTypescriptConverter.prototype.parseAnyOf = function (schemas, depth) {
|
|
200
|
+
var _this = this;
|
|
201
|
+
var types = schemas.map(function (s) { return _this.parseSchema(s, depth); });
|
|
202
|
+
return types.join(' | ');
|
|
203
|
+
};
|
|
204
|
+
OpenApiToTypescriptConverter.prototype.parseAllOf = function (schemas, depth) {
|
|
205
|
+
var _this = this;
|
|
206
|
+
var types = schemas.map(function (s) {
|
|
207
|
+
var parsed = _this.parseSchema(s, depth);
|
|
208
|
+
return parsed;
|
|
209
|
+
});
|
|
210
|
+
var uniqueTypes = __spreadArray([], Array.from(new Set(types)), true);
|
|
211
|
+
if (uniqueTypes.length === 1) {
|
|
212
|
+
return uniqueTypes[0];
|
|
213
|
+
}
|
|
214
|
+
return uniqueTypes.join(' & ');
|
|
215
|
+
};
|
|
216
|
+
OpenApiToTypescriptConverter.prototype.refToTypeName = function (ref) {
|
|
217
|
+
var parts = ref.split('/');
|
|
218
|
+
return parts[parts.length - 1] || 'unknown';
|
|
219
|
+
};
|
|
220
|
+
return OpenApiToTypescriptConverter;
|
|
221
|
+
}());
|
|
222
|
+
function convertOpenApiToTypescript(schema, name, options) {
|
|
223
|
+
var converter = new OpenApiToTypescriptConverter(options);
|
|
224
|
+
return converter.convert(schema, name);
|
|
225
|
+
}
|
|
226
|
+
exports.convertOpenApiToTypescript = convertOpenApiToTypescript;
|
|
227
|
+
var TypeScriptClient = (function (_super) {
|
|
228
|
+
__extends(TypeScriptClient, _super);
|
|
229
|
+
function TypeScriptClient(options) {
|
|
230
|
+
return _super.call(this, options) || this;
|
|
231
|
+
}
|
|
232
|
+
TypeScriptClient.prototype.generateClientSourceFile = function () {
|
|
233
|
+
var _this = this;
|
|
234
|
+
var options = this.options;
|
|
235
|
+
if (!(options === null || options === void 0 ? void 0 : options.Module))
|
|
236
|
+
throw new Error("Parameter 'Module' must be specified");
|
|
237
|
+
var METHOD_TYPE_MAP_NAME = 'MethodTypeMap';
|
|
238
|
+
var METHOD_TYPE_MAP_KEYS_NAME = 'MethodTypeMapKeys';
|
|
239
|
+
var RESPONSE_CALLBACK_DATA_NAME = 'ResponseCallbackData';
|
|
240
|
+
var REQUEST_OPTIONS_NAME = 'RequestOptions';
|
|
241
|
+
var RESULT_TYPE_NAME = 'Result';
|
|
242
|
+
var REQUEST_METHOD_MAP_NAME = 'REQUEST_METHOD_MAP';
|
|
243
|
+
var RESPONSE_CACHE_MAP_NAME = 'RESPONSE_CACHE_MAP';
|
|
244
|
+
var REQUEST_BODY_TYPE_ANNOTATION = "".concat(METHOD_TYPE_MAP_NAME, "[T]['request']");
|
|
245
|
+
var RESULT_TYPE_ANNOTATION = "".concat(RESULT_TYPE_NAME, "<").concat(METHOD_TYPE_MAP_NAME, "[T]['response']>");
|
|
246
|
+
var methodTypeMapCodeLines = nest_util_class_1.NestUtil.getControllerClasses(options.Module)
|
|
247
|
+
.reduce(function (result, Class) {
|
|
248
|
+
var _a;
|
|
249
|
+
if (string_util_class_1.StringUtil.isFalsyString(Class === null || Class === void 0 ? void 0 : Class.name) || !(0, controller_util_class_1.isHerbalController)(Class))
|
|
250
|
+
return result;
|
|
251
|
+
var pool = method_decorator_1.Method.getPool(Class.prototype);
|
|
252
|
+
if (pool === null)
|
|
253
|
+
return result;
|
|
254
|
+
return result.concat(Object.entries((_a = _this.document.paths) !== null && _a !== void 0 ? _a : {})
|
|
255
|
+
.map(function (_a) {
|
|
256
|
+
var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
257
|
+
var pathname = _a[0], schema = _a[1];
|
|
258
|
+
var requestSchema = (_e = (_d = (_c = (_b = schema === null || schema === void 0 ? void 0 : schema.post) === null || _b === void 0 ? void 0 : _b.requestBody) === null || _c === void 0 ? void 0 : _c.content) === null || _d === void 0 ? void 0 : _d['application/json']) === null || _e === void 0 ? void 0 : _e.schema;
|
|
259
|
+
var responseSchema = (_k = (_j = (_h = (_g = (_f = schema === null || schema === void 0 ? void 0 : schema.post) === null || _f === void 0 ? void 0 : _f.responses) === null || _g === void 0 ? void 0 : _g['200']) === null || _h === void 0 ? void 0 : _h.content) === null || _j === void 0 ? void 0 : _j['application/json']) === null || _k === void 0 ? void 0 : _k.schema;
|
|
260
|
+
if (!requestSchema && !responseSchema)
|
|
261
|
+
return null;
|
|
262
|
+
return [
|
|
263
|
+
"'".concat(pathname, "': {"),
|
|
264
|
+
" request: ".concat(((_l = convertOpenApiToTypescript(requestSchema)) === null || _l === void 0 ? void 0 : _l.code) || 'any', ";"),
|
|
265
|
+
" response: ".concat(((_m = convertOpenApiToTypescript(responseSchema)) === null || _m === void 0 ? void 0 : _m.code) || 'any', ";"),
|
|
266
|
+
' };',
|
|
267
|
+
].join('');
|
|
268
|
+
})
|
|
269
|
+
.filter(function (value) { return value !== null; }));
|
|
270
|
+
}, [])
|
|
271
|
+
.map(function (line) { return " ".concat(line); });
|
|
272
|
+
methodTypeMapCodeLines.unshift("export interface ".concat(METHOD_TYPE_MAP_NAME, " {"));
|
|
273
|
+
methodTypeMapCodeLines.push('}');
|
|
274
|
+
return [
|
|
275
|
+
"import * as hash from 'object-hash';",
|
|
276
|
+
"import * as _ from 'lodash';",
|
|
277
|
+
"\n".concat(methodTypeMapCodeLines.join('\n')),
|
|
278
|
+
"\ntype ".concat(METHOD_TYPE_MAP_KEYS_NAME, " = keyof ").concat(METHOD_TYPE_MAP_NAME, ";"),
|
|
279
|
+
"\ntype ".concat(RESPONSE_CALLBACK_DATA_NAME, "<K extends ").concat(METHOD_TYPE_MAP_KEYS_NAME, "> = {"),
|
|
280
|
+
' url: K;',
|
|
281
|
+
" result: ".concat(RESULT_TYPE_NAME, "<").concat(METHOD_TYPE_MAP_NAME, "[K]['response']>;"),
|
|
282
|
+
'};',
|
|
283
|
+
"\nexport interface ".concat(REQUEST_OPTIONS_NAME, " extends RequestInit {"),
|
|
284
|
+
' headers?: Record<string, any>;',
|
|
285
|
+
' ignoreCache?: boolean;',
|
|
286
|
+
' prefix?: string;',
|
|
287
|
+
' timeout?: number;',
|
|
288
|
+
' getAuthorizationCredential?: () => string;',
|
|
289
|
+
" onRequest?: (context: { cached: boolean; id: string; options: Omit<".concat(REQUEST_OPTIONS_NAME, ", 'getAuthorizationCredential' | 'onRequest' | 'onResponse'>; prefix: string | undefined; requestBody: string; url: string; }) => void | Promise<void>;"),
|
|
290
|
+
" onResponse?: <K extends ".concat(METHOD_TYPE_MAP_KEYS_NAME, ">(response: ").concat(RESPONSE_CALLBACK_DATA_NAME, "<K>, id: string) => void | Promise<void>;"),
|
|
291
|
+
'}',
|
|
292
|
+
"\nexport interface ".concat(RESULT_TYPE_NAME, "<T> {"),
|
|
293
|
+
' error: Error | null;',
|
|
294
|
+
" response: T | null;",
|
|
295
|
+
' status: number;',
|
|
296
|
+
' statusText: string;',
|
|
297
|
+
' headers?: Record<string, any>;',
|
|
298
|
+
'}',
|
|
299
|
+
'\nexport class Client {',
|
|
300
|
+
" public constructor(private readonly options: ".concat(REQUEST_OPTIONS_NAME, " = {}) {}"),
|
|
301
|
+
"\n protected readonly ".concat(REQUEST_METHOD_MAP_NAME, " = new Map<keyof ").concat(METHOD_TYPE_MAP_NAME, ", (...params: any[]) => Promise<unknown>>();"),
|
|
302
|
+
"\n protected readonly ".concat(RESPONSE_CACHE_MAP_NAME, " = new Map<string, ").concat(RESULT_TYPE_NAME, "<unknown>>();"),
|
|
303
|
+
"\n public createRequest<T extends keyof ".concat(METHOD_TYPE_MAP_NAME, ">(url: T): (requestBody?: ").concat(REQUEST_BODY_TYPE_ANNOTATION, ", options?: ").concat(REQUEST_OPTIONS_NAME, ") => Promise<").concat(RESULT_TYPE_ANNOTATION, "> {"),
|
|
304
|
+
" if (typeof this.".concat(REQUEST_METHOD_MAP_NAME, ".get(url) !== 'function') {"),
|
|
305
|
+
" this.".concat(REQUEST_METHOD_MAP_NAME, ".set(url, (requestBody?: ").concat(REQUEST_BODY_TYPE_ANNOTATION, ", options?: ").concat(REQUEST_OPTIONS_NAME, ") => this.request.call(this, url, requestBody, options));"),
|
|
306
|
+
' }',
|
|
307
|
+
" return this.".concat(REQUEST_METHOD_MAP_NAME, ".get(url) as (requestBody?: ").concat(REQUEST_BODY_TYPE_ANNOTATION, ", options?: ").concat(REQUEST_OPTIONS_NAME, ") => Promise<").concat(RESULT_TYPE_ANNOTATION, ">;"),
|
|
308
|
+
' }',
|
|
309
|
+
"\n public async request<T extends keyof ".concat(METHOD_TYPE_MAP_NAME, ">(url: T, requestBody?: ").concat(REQUEST_BODY_TYPE_ANNOTATION, ", options?: Omit<").concat(REQUEST_OPTIONS_NAME, ", 'getAuthorizationCredential' | 'onRequest' | 'onResponse'>): Promise<").concat(RESULT_TYPE_ANNOTATION, "> {"),
|
|
310
|
+
' const id = `${Math.random().toString(32).slice(2)}${Date.now().toString(16)}`;',
|
|
311
|
+
' const requestHash = hash(requestBody ?? null);',
|
|
312
|
+
' const requestBodyString = JSON.stringify(requestBody);',
|
|
313
|
+
" const finalOptions = _.merge({}, this?.options, _.omit(options, ['getAuthorizationCredential', 'onRequest', 'onResponse']));",
|
|
314
|
+
" const onRequestOptions = _.omit(finalOptions, ['getAuthorizationCredential', 'onRequest', 'onResponse']);",
|
|
315
|
+
' const { getAuthorizationCredential, onResponse, onRequest, ignoreCache, timeout, prefix, ...requestOptions } = finalOptions;',
|
|
316
|
+
" if (this.".concat(RESPONSE_CACHE_MAP_NAME, ".has(requestHash) && !ignoreCache) {"),
|
|
317
|
+
' onRequest?.({',
|
|
318
|
+
' prefix,',
|
|
319
|
+
' url,',
|
|
320
|
+
' requestBody: requestBodyString,',
|
|
321
|
+
' cached: true,',
|
|
322
|
+
' options: onRequestOptions,',
|
|
323
|
+
' id,',
|
|
324
|
+
' });',
|
|
325
|
+
" return this.".concat(RESPONSE_CACHE_MAP_NAME, ".get(requestHash) as ").concat(RESULT_TYPE_ANNOTATION, ";"),
|
|
326
|
+
' }',
|
|
327
|
+
' onRequest?.({',
|
|
328
|
+
' prefix,',
|
|
329
|
+
' url,',
|
|
330
|
+
' id,',
|
|
331
|
+
' requestBody: requestBodyString,',
|
|
332
|
+
' cached: false,',
|
|
333
|
+
' options: onRequestOptions,',
|
|
334
|
+
' });',
|
|
335
|
+
' const credential = getAuthorizationCredential?.();',
|
|
336
|
+
' const abortController = new AbortController();',
|
|
337
|
+
' if (timeout! > 0) {',
|
|
338
|
+
' setTimeout(() => {',
|
|
339
|
+
' abortController.abort();',
|
|
340
|
+
' }, timeout);',
|
|
341
|
+
' }',
|
|
342
|
+
" const result: ".concat(RESULT_TYPE_ANNOTATION, " = await fetch((prefix ?? '') + url, {"),
|
|
343
|
+
' ...requestOptions,',
|
|
344
|
+
' body: JSON.stringify(requestBody),',
|
|
345
|
+
" method: 'POST',",
|
|
346
|
+
' signal: abortController.signal,',
|
|
347
|
+
' headers: {',
|
|
348
|
+
' ...requestOptions?.headers,',
|
|
349
|
+
" 'Content-Type': 'application/json',",
|
|
350
|
+
" Authorization: (typeof credential === 'string' && credential.length > 0) ? credential : finalOptions?.headers?.Authorization,",
|
|
351
|
+
' },',
|
|
352
|
+
' }).then((response) => {',
|
|
353
|
+
' const status = response?.status;',
|
|
354
|
+
' const statusText = response?.statusText;',
|
|
355
|
+
' const headers = Array.from(response?.headers?.entries?.() ?? []).reduce((result, [key, value]) => {',
|
|
356
|
+
" if (typeof key !== 'string' || key.length === 0) return result;",
|
|
357
|
+
' result[key] = value;',
|
|
358
|
+
' return result;',
|
|
359
|
+
' }, {});',
|
|
360
|
+
' if (!response?.ok) {',
|
|
361
|
+
' return response.text().then((errorText) => ({ error: new Error(errorText), response: null, headers, status, statusText }));',
|
|
362
|
+
' }',
|
|
363
|
+
" return response.json().then((response) => ({ error: null, response, headers, status, statusText } as ".concat(RESULT_TYPE_ANNOTATION, "));"),
|
|
364
|
+
" }).catch((error) => Promise.resolve({ error, response: null, headers: {}, status: 0, statusText: '' }));",
|
|
365
|
+
" onResponse?.({ url, result }, id);",
|
|
366
|
+
" this.".concat(RESPONSE_CACHE_MAP_NAME, ".set(requestHash, result);"),
|
|
367
|
+
' return result;',
|
|
368
|
+
' }',
|
|
369
|
+
'}\n',
|
|
370
|
+
].join('\n');
|
|
371
|
+
};
|
|
372
|
+
return TypeScriptClient;
|
|
373
|
+
}(client_abstract_class_1.Client));
|
|
374
|
+
exports.TypeScriptClient = TypeScriptClient;
|
package/dist/core.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { HeaderUtil } from '@open-norantec/utilities/dist/header-util.class';
|
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import { Request } from './types/request.type';
|
|
5
5
|
export * from '@nestjs/core';
|
|
6
|
-
|
|
6
|
+
interface LegacyMethodContext<IS extends z.Schema<any>> {
|
|
7
7
|
headers: ReturnType<typeof HeaderUtil.parse>;
|
|
8
8
|
input: z.infer<IS>;
|
|
9
9
|
request: Request;
|
|
@@ -13,6 +13,7 @@ export type MethodHandler<IS extends z.Schema<any>, OS extends z.Schema<any>> =
|
|
|
13
13
|
response: z.infer<OS>;
|
|
14
14
|
}>;
|
|
15
15
|
export declare class HerbalController {
|
|
16
|
-
protected registerMethod: <IS extends z.ZodType<any, z.ZodTypeDef, any>, OS extends z.ZodType<any, z.ZodTypeDef, any>>(inputSchema: IS, outputSchema: OS, callback: (context:
|
|
17
|
-
private
|
|
16
|
+
protected registerMethod: <IS extends z.ZodType<any, z.ZodTypeDef, any>, OS extends z.ZodType<any, z.ZodTypeDef, any>>(inputSchema: IS, outputSchema: OS, callback: (context: LegacyMethodContext<IS>) => Promise<z.TypeOf<OS>>) => MethodHandler<IS, OS>;
|
|
17
|
+
private $handleRequest;
|
|
18
|
+
private $call;
|
|
18
19
|
}
|
package/dist/core.js
CHANGED
|
@@ -69,6 +69,8 @@ var header_util_class_1 = require("@open-norantec/utilities/dist/header-util.cla
|
|
|
69
69
|
var zod_1 = require("zod");
|
|
70
70
|
var _ = require("lodash");
|
|
71
71
|
var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
|
|
72
|
+
var decorators_1 = require("./decorators");
|
|
73
|
+
var utilities_1 = require("@open-norantec/utilities");
|
|
72
74
|
__exportStar(require("@nestjs/core"), exports);
|
|
73
75
|
var HerbalController = exports.HerbalController = (function () {
|
|
74
76
|
function HerbalController() {
|
|
@@ -101,54 +103,70 @@ var HerbalController = exports.HerbalController = (function () {
|
|
|
101
103
|
}); };
|
|
102
104
|
};
|
|
103
105
|
}
|
|
104
|
-
HerbalController.prototype
|
|
105
|
-
var _a, _b, _c, _d
|
|
106
|
+
HerbalController.prototype.$handleRequest = function (request) {
|
|
107
|
+
var _a, _b, _c, _d;
|
|
106
108
|
return __awaiter(this, void 0, void 0, function () {
|
|
107
|
-
var
|
|
108
|
-
var
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
var result, error_1;
|
|
110
|
+
var _e;
|
|
111
|
+
var _this = this;
|
|
112
|
+
return __generator(this, function (_f) {
|
|
113
|
+
switch (_f.label) {
|
|
111
114
|
case 0:
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
+
_f.trys.push([0, 3, , 5]);
|
|
116
|
+
_e = {};
|
|
117
|
+
return [4, this.$call(request.methodName, {
|
|
118
|
+
authenticateResult: request.authenticateResult,
|
|
119
|
+
headers: header_util_class_1.HeaderUtil.parse((_a = request.headers) !== null && _a !== void 0 ? _a : {}),
|
|
120
|
+
methodName: request.methodName,
|
|
121
|
+
rawBody: request.rawBody,
|
|
122
|
+
traceId: request.traceId,
|
|
123
|
+
transaction: request.transaction,
|
|
124
|
+
url: request.originalUrl,
|
|
125
|
+
getProvider: function (token) { return request.moduleRef.get(token, { strict: false }); },
|
|
126
|
+
})];
|
|
115
127
|
case 1:
|
|
116
|
-
|
|
117
|
-
|
|
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)
|
|
128
|
+
result = (_e.data = _f.sent(),
|
|
129
|
+
_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
130
|
? null
|
|
124
131
|
: request.authenticateResult.nextToken,
|
|
125
|
-
|
|
126
|
-
_k.label = 3;
|
|
127
|
-
case 3:
|
|
128
|
-
_k.trys.push([3, 5, , 6]);
|
|
132
|
+
_e);
|
|
129
133
|
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))];
|
|
134
|
+
case 2:
|
|
135
|
+
_f.sent();
|
|
136
|
+
return [2, result];
|
|
137
|
+
case 3:
|
|
138
|
+
error_1 = _f.sent();
|
|
139
|
+
return [4, utilities_1.AttemptUtil.execPromise((function () { return __awaiter(_this, void 0, void 0, function () {
|
|
140
|
+
var _a, _b;
|
|
141
|
+
return __generator(this, function (_c) {
|
|
142
|
+
switch (_c.label) {
|
|
143
|
+
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))];
|
|
144
|
+
case 1:
|
|
145
|
+
_c.sent();
|
|
146
|
+
return [2];
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}); })())];
|
|
130
150
|
case 4:
|
|
131
|
-
|
|
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();
|
|
151
|
+
_f.sent();
|
|
150
152
|
throw error_1;
|
|
151
|
-
case
|
|
153
|
+
case 5: return [2];
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
HerbalController.prototype.$call = function (name, context) {
|
|
159
|
+
var _a, _b;
|
|
160
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
161
|
+
var callFn;
|
|
162
|
+
return __generator(this, function (_c) {
|
|
163
|
+
switch (_c.label) {
|
|
164
|
+
case 0:
|
|
165
|
+
callFn = (_b = (_a = decorators_1.Method.getPool(this)) === null || _a === void 0 ? void 0 : _a.getCallFn) === null || _b === void 0 ? void 0 : _b.call(_a, name);
|
|
166
|
+
if (typeof callFn !== 'function')
|
|
167
|
+
throw new common_1.NotFoundException("Method ".concat(name, " not found"));
|
|
168
|
+
return [4, callFn(context)];
|
|
169
|
+
case 1: return [2, _c.sent()];
|
|
152
170
|
}
|
|
153
171
|
});
|
|
154
172
|
});
|
|
@@ -159,6 +177,6 @@ var HerbalController = exports.HerbalController = (function () {
|
|
|
159
177
|
__metadata("design:type", Function),
|
|
160
178
|
__metadata("design:paramtypes", [Object]),
|
|
161
179
|
__metadata("design:returntype", Promise)
|
|
162
|
-
], HerbalController.prototype, "
|
|
180
|
+
], HerbalController.prototype, "$handleRequest", null);
|
|
163
181
|
return HerbalController;
|
|
164
182
|
}());
|
|
@@ -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
|
|
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
|
-
|
|
25
|
-
options:
|
|
26
|
-
|
|
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,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);
|
package/dist/decorators/index.js
CHANGED
|
@@ -16,3 +16,5 @@ 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);
|
|
20
|
+
__exportStar(require("./method.decorator"), exports);
|