@open-norantec/herbal 1.0.2-alpha.2 → 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.
- 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 +45 -0
- package/dist/decorators/method.decorator.js +193 -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 +126 -82
- package/package.json +8 -12
- package/dist/create.js +0 -154
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { Constructor } from 'type-fest';
|
|
3
|
+
import { OpenAPIObject } from 'zod-openapi/dist/openapi3-ts/dist/model/openapi31';
|
|
4
|
+
export interface CreateClientOptions {
|
|
5
|
+
Module: Constructor<any>;
|
|
6
|
+
}
|
|
7
|
+
export declare abstract class Client {
|
|
8
|
+
readonly options: CreateClientOptions;
|
|
9
|
+
protected document: OpenAPIObject;
|
|
10
|
+
constructor(options: CreateClientOptions);
|
|
11
|
+
abstract generateClientSourceFile(): string;
|
|
12
|
+
createSchema(group?: string): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Client = void 0;
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
var nest_util_class_1 = require("../utilities/nest-util.class");
|
|
6
|
+
var string_util_class_1 = require("@open-norantec/utilities/dist/string-util.class");
|
|
7
|
+
var controller_util_class_1 = require("../utilities/controller-util.class");
|
|
8
|
+
var method_decorator_1 = require("../decorators/method.decorator");
|
|
9
|
+
var Client = (function () {
|
|
10
|
+
function Client(options) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
this.document = {
|
|
13
|
+
openapi: '3.1.0',
|
|
14
|
+
info: {
|
|
15
|
+
title: 'API Documentation',
|
|
16
|
+
version: '1.0.0',
|
|
17
|
+
},
|
|
18
|
+
paths: {},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
Client.prototype.createSchema = function (group) {
|
|
22
|
+
var _this = this;
|
|
23
|
+
if (string_util_class_1.StringUtil.isFalsyString(group) && typeof group !== 'undefined')
|
|
24
|
+
return;
|
|
25
|
+
nest_util_class_1.NestUtil.getControllerClasses(this.options.Module).forEach(function (Class) {
|
|
26
|
+
if (string_util_class_1.StringUtil.isFalsyString(Class === null || Class === void 0 ? void 0 : Class.name) || !(0, controller_util_class_1.isHerbalController)(Class))
|
|
27
|
+
return;
|
|
28
|
+
var controllerName = (0, controller_util_class_1.getControllerName)(Class);
|
|
29
|
+
var pool = method_decorator_1.Method.getPool(Class.prototype);
|
|
30
|
+
if (string_util_class_1.StringUtil.isFalsyString(controllerName) || pool === null)
|
|
31
|
+
return;
|
|
32
|
+
Object.entries(pool.getOpenAPIPathsObject(group)).forEach(function (_a) {
|
|
33
|
+
var pathname = _a[0], schemas = _a[1];
|
|
34
|
+
_this.document.paths[["/".concat(controllerName), pathname].join('')] = schemas;
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
return Client;
|
|
39
|
+
}());
|
|
40
|
+
exports.Client = Client;
|
package/dist/abstracts/index.js
CHANGED
|
@@ -15,3 +15,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./auth-adapter.abstract.class"), exports);
|
|
18
|
+
__exportStar(require("./client.abstract.class"), exports);
|
package/dist/cli/herbal.js
CHANGED
|
@@ -3,16 +3,38 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
var commander_1 = require("commander");
|
|
5
5
|
var forge_1 = require("@open-norantec/forge");
|
|
6
|
+
var _ = require("lodash");
|
|
7
|
+
var fs = require("fs-extra");
|
|
8
|
+
var path = require("node:path");
|
|
9
|
+
var requireFromString = require("require-from-string");
|
|
10
|
+
var reflect_declaration_1 = require("../transformers/reflect-declaration");
|
|
6
11
|
var command = new commander_1.Command('herbal');
|
|
7
|
-
var
|
|
8
|
-
var
|
|
12
|
+
var log = function (level) {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
var messages = [];
|
|
15
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
16
|
+
messages[_i - 1] = arguments[_i];
|
|
17
|
+
}
|
|
18
|
+
console.log("[".concat(new Date().toISOString(), "] -").concat(level, "- ").concat((_b = (_a = messages === null || messages === void 0 ? void 0 : messages.join) === null || _a === void 0 ? void 0 : _a.call(messages, ' ')) !== null && _b !== void 0 ? _b : ''));
|
|
19
|
+
switch (level) {
|
|
20
|
+
case 'error':
|
|
21
|
+
process.exit(1);
|
|
22
|
+
default:
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var handleGetVirtualEntryFileContent = function (buildEntryFilePath) {
|
|
9
27
|
return [
|
|
10
28
|
"import 'reflect-metadata';",
|
|
11
|
-
"import { ModelUtil, NestFactory } from '@open-norantec/herbal';",
|
|
29
|
+
"import { ModelUtil, NestFactory, isApplication } from '@open-norantec/herbal';",
|
|
12
30
|
"import { LoggerService } from '@open-norantec/herbal/dist/modules/logger/logger.service';",
|
|
13
31
|
"import { Worker, isMainThread, workerData } from 'node:worker_threads';",
|
|
14
|
-
"import ENTRY from '".concat(
|
|
32
|
+
"import ENTRY from '".concat(buildEntryFilePath, "';"),
|
|
15
33
|
'\nasync function bootstrap() {',
|
|
34
|
+
' if (!isApplication(ENTRY)) {',
|
|
35
|
+
' console.log(`The entry file must export an application or a function that returns an application.`);',
|
|
36
|
+
' process.exit(1);',
|
|
37
|
+
' }',
|
|
16
38
|
' const entryOptions = ENTRY?.options;',
|
|
17
39
|
' await entryOptions?.onBeforeBootstrap?.();',
|
|
18
40
|
"\n if (!!workerData?.['__herbal_worker']) {",
|
|
@@ -67,74 +89,110 @@ var getEntryFileContent = function (_a) {
|
|
|
67
89
|
'\nbootstrap();',
|
|
68
90
|
].join('\n');
|
|
69
91
|
};
|
|
70
|
-
var
|
|
71
|
-
var
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
"import * as fs from 'node:fs';",
|
|
76
|
-
"import * as path from 'node:path';",
|
|
77
|
-
'\nasync function bootstrap() {',
|
|
78
|
-
" const outputDirPath = '".concat(outputPath, "';"),
|
|
79
|
-
" const outputFilePath = path.resolve(outputDirPath, '".concat(options.outputName, ".ts');"),
|
|
80
|
-
' await ENTRY?.options?.onBeforeBootstrap?.();',
|
|
81
|
-
' try {',
|
|
82
|
-
' fs.rmSync(outputFilePath, {',
|
|
83
|
-
' recursive: true,',
|
|
84
|
-
' force: true,',
|
|
85
|
-
' });',
|
|
86
|
-
' } catch {}',
|
|
87
|
-
' try {',
|
|
88
|
-
' if (!fs.statSync(path.dirname(outputDirPath)).isDirectory()) {',
|
|
89
|
-
' fs.rmSync(path.dirname(outputDirPath), {',
|
|
90
|
-
' recursive: true,',
|
|
91
|
-
' force: true,',
|
|
92
|
-
' });',
|
|
93
|
-
' }',
|
|
94
|
-
' } catch {}',
|
|
95
|
-
' try {',
|
|
96
|
-
' fs.mkdirSync(outputDirPath, { recursive: true });',
|
|
97
|
-
' } catch {}',
|
|
98
|
-
' fs.writeFileSync(',
|
|
99
|
-
' outputFilePath,',
|
|
100
|
-
' ENTRY?.generateClientSourceFile?.(),',
|
|
101
|
-
' );',
|
|
102
|
-
'}',
|
|
103
|
-
'\nbootstrap();',
|
|
104
|
-
].join('\n');
|
|
105
|
-
};
|
|
106
|
-
var handleLog = function (level, message) {
|
|
107
|
-
console.log("[".concat(new Date().toISOString(), "] [").concat(level, "] ").concat(message));
|
|
108
|
-
switch (level) {
|
|
109
|
-
case 'error':
|
|
110
|
-
process.exit(1);
|
|
111
|
-
default:
|
|
112
|
-
break;
|
|
92
|
+
var handleGetFileContent = function (filePath) {
|
|
93
|
+
var content = _.attempt(function () { return fs.readFileSync(filePath, 'utf-8'); });
|
|
94
|
+
if (content instanceof Error) {
|
|
95
|
+
log('error', "Failed to read file content for ".concat(filePath, ":"), content.message);
|
|
96
|
+
return '';
|
|
113
97
|
}
|
|
98
|
+
return content;
|
|
99
|
+
};
|
|
100
|
+
var handleGetWatcher = function (callback) {
|
|
101
|
+
var watcher = fs.watch(process.cwd(), { recursive: true }, function (eventType, filename) {
|
|
102
|
+
callback(path.resolve(filename));
|
|
103
|
+
});
|
|
104
|
+
return { close: watcher.close.bind(watcher) };
|
|
114
105
|
};
|
|
106
|
+
var createHandleOutputFile = function (disableWriteFile) { return function (filePath, content) {
|
|
107
|
+
if (!disableWriteFile) {
|
|
108
|
+
var dir_1 = path.dirname(filePath);
|
|
109
|
+
if (!fs.existsSync(dir_1) || !fs.statSync(dir_1).isDirectory()) {
|
|
110
|
+
_.attempt(function () { return fs.removeSync(dir_1); });
|
|
111
|
+
_.attempt(function () { return fs.mkdirpSync(dir_1); });
|
|
112
|
+
}
|
|
113
|
+
_.attempt(function () { return fs.writeFileSync(filePath, content, 'utf-8'); });
|
|
114
|
+
log('info', "Generated file: ".concat(filePath));
|
|
115
|
+
}
|
|
116
|
+
}; };
|
|
115
117
|
command
|
|
116
|
-
.addCommand((0, forge_1.
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
118
|
+
.addCommand((0, forge_1.createCommand)('build', {
|
|
119
|
+
hiddenOptions: ['--watch', '--execute-after-build'],
|
|
120
|
+
onLog: log,
|
|
121
|
+
defaultOptions: function (source, output, options) { return ({
|
|
122
|
+
getWatcher: handleGetWatcher,
|
|
123
|
+
getVirtualEntryFileContent: handleGetVirtualEntryFileContent,
|
|
124
|
+
onGetFileContent: handleGetFileContent,
|
|
125
|
+
onOutputFile: createHandleOutputFile(!!(options === null || options === void 0 ? void 0 : options.disableWriteFile)),
|
|
126
|
+
}); },
|
|
127
|
+
}))
|
|
128
|
+
.addCommand((0, forge_1.createCommand)('watch', {
|
|
129
|
+
hiddenOptions: [
|
|
130
|
+
'--watch',
|
|
131
|
+
'--execute-after-build',
|
|
132
|
+
'--obfuscate',
|
|
133
|
+
'--obfuscator-config-file <string>',
|
|
134
|
+
'--disable-write-file',
|
|
135
|
+
],
|
|
136
|
+
onLog: log,
|
|
137
|
+
defaultOptions: function () { return ({
|
|
138
|
+
watch: true,
|
|
139
|
+
executeAfterBuild: true,
|
|
140
|
+
obfuscate: false,
|
|
141
|
+
getWatcher: handleGetWatcher,
|
|
142
|
+
getVirtualEntryFileContent: handleGetVirtualEntryFileContent,
|
|
143
|
+
onGetFileContent: handleGetFileContent,
|
|
144
|
+
onOutputFile: createHandleOutputFile(true),
|
|
145
|
+
}); },
|
|
146
|
+
}))
|
|
147
|
+
.addCommand((0, forge_1.createCommand)('generate-client', function (_a) {
|
|
148
|
+
var addOption = _a.addOption;
|
|
149
|
+
addOption('--group <name>', 'Client group name to generate');
|
|
150
|
+
return {
|
|
151
|
+
onLog: log,
|
|
152
|
+
hiddenOptions: [
|
|
153
|
+
'--watch',
|
|
154
|
+
'--execute-after-build',
|
|
155
|
+
'--obfuscate',
|
|
156
|
+
'--obfuscator-config-file <string>',
|
|
157
|
+
'--disable-write-file',
|
|
158
|
+
],
|
|
159
|
+
defaultOptions: function (source, output, options) { return ({
|
|
160
|
+
watch: false,
|
|
161
|
+
executeAfterBuild: false,
|
|
162
|
+
obfuscate: false,
|
|
163
|
+
customTransformers: function (program) {
|
|
164
|
+
return {
|
|
165
|
+
before: [(0, reflect_declaration_1.transformer)(program)],
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
getWatcher: handleGetWatcher,
|
|
169
|
+
onGetFileContent: handleGetFileContent,
|
|
170
|
+
onOutputFile: createHandleOutputFile(false),
|
|
171
|
+
getVirtualEntryFileContent: function (buildEntryFilePath) {
|
|
172
|
+
return [
|
|
173
|
+
"const entry = require('".concat(buildEntryFilePath, "')"),
|
|
174
|
+
"const { isClient } = require(\'@open-norantec/herbal\')",
|
|
175
|
+
'module.exports = (context) => {',
|
|
176
|
+
' let client = entry;',
|
|
177
|
+
' if (!isClient(client)) { client = entry?.default; }',
|
|
178
|
+
" if (!isClient(client)) return '';",
|
|
179
|
+
' client.instance.createSchema(context?.group);',
|
|
180
|
+
" return client.instance.generateClientSourceFile() ?? '';",
|
|
181
|
+
'};',
|
|
182
|
+
].join('\n');
|
|
183
|
+
},
|
|
184
|
+
rewriteOutputFile: function (code) {
|
|
185
|
+
try {
|
|
186
|
+
var generateCodeMethod = requireFromString(code);
|
|
187
|
+
if (typeof generateCodeMethod !== 'function')
|
|
188
|
+
return '';
|
|
189
|
+
return generateCodeMethod({ group: options === null || options === void 0 ? void 0 : options.group });
|
|
190
|
+
}
|
|
191
|
+
catch (_a) {
|
|
192
|
+
return '';
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
}); },
|
|
196
|
+
};
|
|
197
|
+
}));
|
|
140
198
|
command.parse(process.argv);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './typescript-client.class';
|
|
@@ -0,0 +1,17 @@
|
|
|
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("./typescript-client.class"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { SchemaObject } from 'zod-openapi/dist/openapi3-ts/dist/model/openapi31';
|
|
3
|
+
import { Client, CreateClientOptions } from '../abstracts/client.abstract.class';
|
|
4
|
+
declare namespace OpenApiToTypescript {
|
|
5
|
+
interface Options {
|
|
6
|
+
useInterface?: boolean;
|
|
7
|
+
export?: boolean;
|
|
8
|
+
generateJSDoc?: boolean;
|
|
9
|
+
preferUnknown?: boolean;
|
|
10
|
+
singleLine?: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface TypeResult {
|
|
13
|
+
code: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export declare function convertOpenApiToTypescript(schema: SchemaObject, name?: string, options?: OpenApiToTypescript.Options): OpenApiToTypescript.TypeResult;
|
|
18
|
+
export declare class TypeScriptClient extends Client implements Client {
|
|
19
|
+
constructor(options: CreateClientOptions);
|
|
20
|
+
generateClientSourceFile(): string;
|
|
21
|
+
}
|
|
22
|
+
export {};
|