vona-module-a-openapi 5.0.38 → 5.0.40
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.
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
/** bean: begin */
|
|
2
|
+
export * from '../bean/bean.openapi.ts';
|
|
3
|
+
import 'vona';
|
|
4
|
+
declare module 'vona' {
|
|
5
|
+
}
|
|
6
|
+
declare module 'vona-module-a-openapi' {
|
|
7
|
+
interface BeanOpenapi {
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/** bean: end */
|
|
11
|
+
/** bean: begin */
|
|
12
|
+
import type { BeanOpenapi } from '../bean/bean.openapi.ts';
|
|
13
|
+
import 'vona';
|
|
14
|
+
declare module 'vona' {
|
|
15
|
+
interface IBeanRecordGlobal {
|
|
16
|
+
'openapi': BeanOpenapi;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** bean: end */
|
|
1
20
|
/** service: begin */
|
|
2
21
|
export * from '../service/openapi.ts';
|
|
3
22
|
import 'vona';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Constructable } from 'vona';
|
|
2
|
+
import type { IOpenapiObject } from 'vona-module-a-openapiutils';
|
|
3
|
+
import { BeanBase } from 'vona';
|
|
4
|
+
export declare class BeanOpenapi extends BeanBase {
|
|
5
|
+
generateJsonOfClass<K extends keyof IOpenapiObject>(schemaClass: Constructable, version?: K): Promise<IOpenapiObject[K]>;
|
|
6
|
+
generateJsonOfClasses<K extends keyof IOpenapiObject>(schemaClasses: Constructable[], version?: K): Promise<IOpenapiObject[K]>;
|
|
7
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,36 +1,20 @@
|
|
|
1
1
|
import { registerMappedClassMetadataKey, appMetadata, appResource, cast, deepExtend, BeanInfo, BeanBase, LocaleModuleNameSeparator, HttpStatus, BeanSimple, BeanScopeBase, useApp, beanFullNameFromOnionName } from 'vona';
|
|
2
|
-
import
|
|
2
|
+
import { OpenAPIRegistry, OpenApiGeneratorV3, OpenApiGeneratorV31 } from '@cabloy/zod-to-openapi';
|
|
3
|
+
import { Bean, Service, Scope } from 'vona-module-a-bean';
|
|
4
|
+
import { SymbolDecoratorRule, SymbolOpenApiOptions } from 'vona-module-a-openapiutils';
|
|
5
|
+
import { z } from 'zod';
|
|
3
6
|
import { isEmptyObject, isClass, isNil } from '@cabloy/utils';
|
|
7
|
+
import { ZodMetadata } from '@cabloy/zod-openapi';
|
|
8
|
+
import * as ModuleInfo from '@cabloy/module-info';
|
|
4
9
|
import { toUpperCaseFirstChar } from '@cabloy/word-utils';
|
|
10
|
+
import { translateError } from '@cabloy/zod-errors-custom';
|
|
5
11
|
import { getInnerTypeName, coerceWithNil } from '@cabloy/zod-query';
|
|
6
|
-
import { OpenApiGeneratorV3, OpenApiGeneratorV31, OpenAPIRegistry } from '@cabloy/zod-to-openapi';
|
|
7
|
-
import { Service, Scope } from 'vona-module-a-bean';
|
|
8
12
|
import { Caching } from 'vona-module-a-caching';
|
|
9
|
-
import { SymbolDecoratorRule, SymbolOpenApiOptions } from 'vona-module-a-openapiutils';
|
|
10
13
|
import { SymbolRequestMappingHandler } from 'vona-module-a-web';
|
|
11
|
-
import { z } from 'zod';
|
|
12
|
-
import { ZodMetadata } from '@cabloy/zod-openapi';
|
|
13
14
|
import { SummerCache, BeanSummerCacheBase } from 'vona-module-a-summer';
|
|
14
15
|
import 'openapi3-ts/oas30';
|
|
15
16
|
import 'openapi3-ts/oas31';
|
|
16
17
|
|
|
17
|
-
function _applyDecoratedDescriptor(i, e, r, n, l) {
|
|
18
|
-
var a = {};
|
|
19
|
-
return Object.keys(n).forEach(function (i) {
|
|
20
|
-
a[i] = n[i];
|
|
21
|
-
}), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = true), a = r.slice().reverse().reduce(function (r, n) {
|
|
22
|
-
return n(i, e, r) || r;
|
|
23
|
-
}, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function bodySchemaWrapperDefault(bodySchema) {
|
|
27
|
-
return z.object({
|
|
28
|
-
code: z.string(),
|
|
29
|
-
message: z.string(),
|
|
30
|
-
data: bodySchema
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
18
|
function getTargetDecoratorRules(target, disableRegisterMetadata) {
|
|
35
19
|
if (!disableRegisterMetadata) {
|
|
36
20
|
registerMappedClassMetadataKey(target, SymbolDecoratorRule, {
|
|
@@ -191,6 +175,52 @@ function _createSchemaObject(rules, options) {
|
|
|
191
175
|
return schema;
|
|
192
176
|
}
|
|
193
177
|
|
|
178
|
+
var _dec$3, _dec2$3, _class$3;
|
|
179
|
+
let BeanOpenapi = (_dec$3 = Bean(), _dec2$3 = BeanInfo({
|
|
180
|
+
module: "a-openapi"
|
|
181
|
+
}), _dec$3(_class$3 = _dec2$3(_class$3 = class BeanOpenapi extends BeanBase {
|
|
182
|
+
// need not cache
|
|
183
|
+
async generateJsonOfClass(schemaClass, version = 'V31') {
|
|
184
|
+
return await this.generateJsonOfClasses([schemaClass], version);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// need not cache
|
|
188
|
+
async generateJsonOfClasses(schemaClasses, version = 'V31') {
|
|
189
|
+
const registry = new OpenAPIRegistry();
|
|
190
|
+
// schema: independent
|
|
191
|
+
let counter = 0;
|
|
192
|
+
for (const schemaClass of schemaClasses) {
|
|
193
|
+
const schema = $schema(schemaClass);
|
|
194
|
+
let beanFullName = appResource.getBeanFullName(schemaClass);
|
|
195
|
+
if (!beanFullName) {
|
|
196
|
+
beanFullName = `${schemaClass.name}_${counter++}`;
|
|
197
|
+
}
|
|
198
|
+
registry.register(beanFullName, schema);
|
|
199
|
+
}
|
|
200
|
+
const generator = version === 'V30' ? new OpenApiGeneratorV3(registry.definitions) : new OpenApiGeneratorV31(registry.definitions);
|
|
201
|
+
const apiObj = generator.generateDocument(this.scope.config.generateDocument[version]);
|
|
202
|
+
this.scope.service.openapi.translate(apiObj, 'rest');
|
|
203
|
+
return apiObj;
|
|
204
|
+
}
|
|
205
|
+
}) || _class$3) || _class$3);
|
|
206
|
+
|
|
207
|
+
function _applyDecoratedDescriptor(i, e, r, n, l) {
|
|
208
|
+
var a = {};
|
|
209
|
+
return Object.keys(n).forEach(function (i) {
|
|
210
|
+
a[i] = n[i];
|
|
211
|
+
}), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = true), a = r.slice().reverse().reduce(function (r, n) {
|
|
212
|
+
return n(i, e, r) || r;
|
|
213
|
+
}, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function bodySchemaWrapperDefault(bodySchema) {
|
|
217
|
+
return z.object({
|
|
218
|
+
code: z.string(),
|
|
219
|
+
message: z.string(),
|
|
220
|
+
data: bodySchema
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
194
224
|
const SymbolRouteHandlersArgumentsMeta = Symbol('SymbolRouteHandlersArgumentsMeta');
|
|
195
225
|
const SymbolRouteHandlersArgumentsValue = Symbol('SymbolRouteHandlersArgumentsValue');
|
|
196
226
|
|
|
@@ -221,7 +251,7 @@ let ServiceOpenapi = (_dec$2 = Service(), _dec2$2 = BeanInfo({
|
|
|
221
251
|
const registry = this._collectRegistry();
|
|
222
252
|
const generator = version === 'V30' ? new OpenApiGeneratorV3(registry.definitions) : new OpenApiGeneratorV31(registry.definitions);
|
|
223
253
|
const apiObj = generator.generateDocument(this.scope.config.generateDocument[version]);
|
|
224
|
-
this.
|
|
254
|
+
this.translate(apiObj, 'api');
|
|
225
255
|
return apiObj;
|
|
226
256
|
}
|
|
227
257
|
async generateJsonOfControllerAction(controller, actionKey, version = 'V31') {
|
|
@@ -231,34 +261,10 @@ let ServiceOpenapi = (_dec$2 = Service(), _dec2$2 = BeanInfo({
|
|
|
231
261
|
this._collectController(registry, beanOptions.module, controller, actionKey);
|
|
232
262
|
const generator = version === 'V30' ? new OpenApiGeneratorV3(registry.definitions) : new OpenApiGeneratorV31(registry.definitions);
|
|
233
263
|
const apiObj = generator.generateDocument(this.scope.config.generateDocument[version]);
|
|
234
|
-
this.
|
|
264
|
+
this.translate(apiObj, 'rest');
|
|
235
265
|
return apiObj;
|
|
236
266
|
}
|
|
237
|
-
|
|
238
|
-
// need not cache
|
|
239
|
-
async generateJsonOfClass(schemaClass, version = 'V31') {
|
|
240
|
-
return await this.generateJsonOfClasses([schemaClass], version);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// need not cache
|
|
244
|
-
async generateJsonOfClasses(schemaClasses, version = 'V31') {
|
|
245
|
-
const registry = new OpenAPIRegistry();
|
|
246
|
-
// schema: independent
|
|
247
|
-
let counter = 0;
|
|
248
|
-
for (const schemaClass of schemaClasses) {
|
|
249
|
-
const schema = $schema(schemaClass);
|
|
250
|
-
let beanFullName = appResource.getBeanFullName(schemaClass);
|
|
251
|
-
if (!beanFullName) {
|
|
252
|
-
beanFullName = `${schemaClass.name}_${counter++}`;
|
|
253
|
-
}
|
|
254
|
-
registry.register(beanFullName, schema);
|
|
255
|
-
}
|
|
256
|
-
const generator = version === 'V30' ? new OpenApiGeneratorV3(registry.definitions) : new OpenApiGeneratorV31(registry.definitions);
|
|
257
|
-
const apiObj = generator.generateDocument(this.scope.config.generateDocument[version]);
|
|
258
|
-
this._translate(apiObj, 'rest');
|
|
259
|
-
return apiObj;
|
|
260
|
-
}
|
|
261
|
-
_translate(apiObj, generateJsonScene) {
|
|
267
|
+
translate(apiObj, generateJsonScene) {
|
|
262
268
|
// paths
|
|
263
269
|
if (apiObj.paths) {
|
|
264
270
|
for (const key in apiObj.paths) {
|
|
@@ -294,6 +300,8 @@ let ServiceOpenapi = (_dec$2 = Service(), _dec2$2 = BeanInfo({
|
|
|
294
300
|
schema.description = schema.title;
|
|
295
301
|
delete schema.title;
|
|
296
302
|
}
|
|
303
|
+
// errorMessage
|
|
304
|
+
this._translateErrorMessages(schema);
|
|
297
305
|
// properties
|
|
298
306
|
const properties = cast(schema).properties;
|
|
299
307
|
if (properties && typeof properties === 'object') {
|
|
@@ -308,6 +316,23 @@ let ServiceOpenapi = (_dec$2 = Service(), _dec2$2 = BeanInfo({
|
|
|
308
316
|
this._translateSchema(items, generateJsonScene);
|
|
309
317
|
}
|
|
310
318
|
}
|
|
319
|
+
_translateErrorMessages(obj) {
|
|
320
|
+
if (!obj.errorMessage) return;
|
|
321
|
+
for (const key in obj.errorMessage) {
|
|
322
|
+
let error = obj.errorMessage[key];
|
|
323
|
+
if (typeof error === 'function') {
|
|
324
|
+
error = error();
|
|
325
|
+
}
|
|
326
|
+
const scope = Object.assign({}, obj);
|
|
327
|
+
if (!isNil(obj.minLength)) scope.minimum = obj.minLength;
|
|
328
|
+
if (!isNil(obj.exclusiveMinimum)) scope.minimum = obj.exclusiveMinimum;
|
|
329
|
+
if (!isNil(obj.maxLength)) scope.maximum = obj.maxLength;
|
|
330
|
+
if (!isNil(obj.exclusiveMaximum)) scope.maximum = obj.exclusiveMaximum;
|
|
331
|
+
obj.errorMessage[key] = translateError((text, ...args) => {
|
|
332
|
+
return this.app.meta.text(text, ...args);
|
|
333
|
+
}, error, scope);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
311
336
|
_translateStrings(obj, keys) {
|
|
312
337
|
for (const key of keys) {
|
|
313
338
|
this._translateString(obj, key);
|
|
@@ -741,21 +766,18 @@ function schemaRegex(regex, params) {
|
|
|
741
766
|
function schemaTableIdentity() {
|
|
742
767
|
const app = useApp();
|
|
743
768
|
const ormConfig = app.util.getModuleConfigRaw('a-orm');
|
|
744
|
-
const _identityType = ormConfig?.table?.identityType ?? '
|
|
769
|
+
const _identityType = ormConfig?.table?.identityType ?? 'bigint';
|
|
745
770
|
return function (_schema) {
|
|
746
771
|
if (_identityType === 'string') {
|
|
747
772
|
return z.string();
|
|
748
773
|
} else if (_identityType === 'number') {
|
|
749
774
|
return z.number();
|
|
775
|
+
} else if (_identityType === 'bigint') {
|
|
776
|
+
return z.string().regex(/^\d+$/);
|
|
750
777
|
}
|
|
751
778
|
throw new Error('not support');
|
|
752
779
|
};
|
|
753
780
|
}
|
|
754
|
-
function schemaBigNumber() {
|
|
755
|
-
return function (_schema) {
|
|
756
|
-
return z.union([z.string(), z.number()]);
|
|
757
|
-
};
|
|
758
|
-
}
|
|
759
781
|
function schemaCaptcha(options) {
|
|
760
782
|
return function (schema) {
|
|
761
783
|
return schema.openapi({
|
|
@@ -926,7 +948,6 @@ const v = {
|
|
|
926
948
|
uppercase: schemaUppercase,
|
|
927
949
|
regex: schemaRegex,
|
|
928
950
|
tableIdentity: schemaTableIdentity,
|
|
929
|
-
bigNumber: schemaBigNumber,
|
|
930
951
|
captcha: schemaCaptcha,
|
|
931
952
|
serializerExclude: schemaSerializerExclude,
|
|
932
953
|
serializerTransform: schemaSerializerTransform,
|
|
@@ -1050,4 +1071,4 @@ const OrderBusinessBase = 1000;
|
|
|
1050
1071
|
const OrderUnknownBase = 10000;
|
|
1051
1072
|
const OrderMaxBase = 100000;
|
|
1052
1073
|
|
|
1053
|
-
export { $locale, $schema, $schemaLazy, Api, Main, OrderBusinessBase, OrderCoreBase, OrderMaxBase, OrderUnknownBase, ScopeModuleAOpenapi, ServiceOpenapi, SummerCacheJson, SymbolRouteHandlersArgumentsMeta, SymbolRouteHandlersArgumentsValue, SymbolSchemaDynamicRefId, addSchemaDynamic, bodySchemaWrapperDefault, config, getSchemaDynamic, getSchemasDynamic, getTargetDecoratorRuleColumns, getTargetDecoratorRuleColumnsMap, getTargetDecoratorRules, locales, makeSchemaLike, makeSchemaLikes, mergeFieldOpenapiMetadata, mergeFieldsOpenapiMetadata, prepareClassType, schemaRefCustomAdapter, v };
|
|
1074
|
+
export { $locale, $schema, $schemaLazy, Api, BeanOpenapi, Main, OrderBusinessBase, OrderCoreBase, OrderMaxBase, OrderUnknownBase, ScopeModuleAOpenapi, ServiceOpenapi, SummerCacheJson, SymbolRouteHandlersArgumentsMeta, SymbolRouteHandlersArgumentsValue, SymbolSchemaDynamicRefId, addSchemaDynamic, bodySchemaWrapperDefault, config, getSchemaDynamic, getSchemasDynamic, getTargetDecoratorRuleColumns, getTargetDecoratorRuleColumnsMap, getTargetDecoratorRules, locales, makeSchemaLike, makeSchemaLikes, mergeFieldOpenapiMetadata, mergeFieldsOpenapiMetadata, prepareClassType, schemaRefCustomAdapter, v };
|
|
@@ -16,7 +16,6 @@ export declare function schemaLowercase(params?: string | z.core.$ZodCheckLowerC
|
|
|
16
16
|
export declare function schemaUppercase(params?: string | z.core.$ZodCheckUpperCaseParams): (schema: z.ZodString) => z.ZodString;
|
|
17
17
|
export declare function schemaRegex(regex: RegExp, params?: string | z.core.$ZodCheckRegexParams): (schema: z.ZodString) => z.ZodString;
|
|
18
18
|
export declare function schemaTableIdentity(): (_schema?: any) => z.ZodString | z.ZodNumber;
|
|
19
|
-
export declare function schemaBigNumber(): (_schema: any) => z.ZodType;
|
|
20
19
|
export declare function schemaCaptcha(options: ISchemaObjectExtensionFieldCaptcha): (schema: z.ZodType) => z.ZodType;
|
|
21
20
|
export declare function schemaSerializerExclude(): (schema: z.ZodType) => z.ZodType;
|
|
22
21
|
export declare function schemaSerializerTransform<T extends keyof ISerializerTransformRecord>(serializerTransformName: T, options?: Partial<ISerializerTransformRecord[T]>): (schema: z.ZodType) => z.ZodType;
|
package/dist/lib/schema/v.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { schemaCaptcha, schemaEmail, schemaIPv4, schemaIPv6, schemaLowercase, schemaMax, schemaMin, schemaRegex, schemaRequired, schemaSerializerExclude, schemaSerializerGetter, schemaSerializerSensitive, schemaSerializerTransform, schemaTableIdentity, schemaToLowerCase, schemaToUpperCase, schemaTrim, schemaUppercase, schemaUrl, schemaUuid } from './v/helpers.ts';
|
|
2
2
|
import { schemaDescription, schemaExample, schemaOpenapi, schemaTitle } from './v/openapi.ts';
|
|
3
3
|
import { schemaArray, schemaDefault, schemaLazy, schemaLooseObject, schemaObject, schemaOptional, schemaStrictObject } from './v/system.ts';
|
|
4
4
|
import { schemaZodRefine, schemaZodTransform } from './v/zod.ts';
|
|
@@ -25,7 +25,6 @@ export declare const v: {
|
|
|
25
25
|
uppercase: typeof schemaUppercase;
|
|
26
26
|
regex: typeof schemaRegex;
|
|
27
27
|
tableIdentity: typeof schemaTableIdentity;
|
|
28
|
-
bigNumber: typeof schemaBigNumber;
|
|
29
28
|
captcha: typeof schemaCaptcha;
|
|
30
29
|
serializerExclude: typeof schemaSerializerExclude;
|
|
31
30
|
serializerTransform: typeof schemaSerializerTransform;
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
+
import type { OpenAPIObject as OpenAPIObject30 } from 'openapi3-ts/oas30';
|
|
2
|
+
import type { OpenAPIObject as OpenAPIObject31 } from 'openapi3-ts/oas31';
|
|
1
3
|
import type { Constructable } from 'vona';
|
|
2
|
-
import type { IOpenapiObject } from 'vona-module-a-openapiutils';
|
|
4
|
+
import type { IOpenapiObject, TypeGenerateJsonScene } from 'vona-module-a-openapiutils';
|
|
3
5
|
import { BeanBase } from 'vona';
|
|
4
6
|
export declare class ServiceOpenapi extends BeanBase {
|
|
5
7
|
protected generateJsonCacheKey(args: any[], prop: string): string;
|
|
6
8
|
protected generateJsonOfControllerActionCacheKey(args: any[], prop: string): string;
|
|
7
9
|
generateJson<K extends keyof IOpenapiObject>(version?: K): Promise<IOpenapiObject[K]>;
|
|
8
10
|
generateJsonOfControllerAction<K extends keyof IOpenapiObject>(controller: Constructable, actionKey: string, version?: K): Promise<IOpenapiObject[K]>;
|
|
9
|
-
|
|
10
|
-
generateJsonOfClasses<K extends keyof IOpenapiObject>(schemaClasses: Constructable[], version?: K): Promise<IOpenapiObject[K]>;
|
|
11
|
-
private _translate;
|
|
11
|
+
translate(apiObj: OpenAPIObject30 | OpenAPIObject31, generateJsonScene: TypeGenerateJsonScene): void;
|
|
12
12
|
private _translateSchema;
|
|
13
|
+
private _translateErrorMessages;
|
|
13
14
|
private _translateStrings;
|
|
14
15
|
private _translateString;
|
|
15
16
|
private _collectRegistry;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vona-module-a-openapi",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "5.0.
|
|
4
|
+
"version": "5.0.40",
|
|
5
5
|
"title": "a-openapi",
|
|
6
6
|
"vonaModule": {
|
|
7
7
|
"capabilities": {
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
"static"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@cabloy/zod-openapi": "^1.0.
|
|
38
|
+
"@cabloy/zod-openapi": "^1.0.3",
|
|
39
39
|
"@cabloy/zod-query": "^2.0.1",
|
|
40
|
-
"@cabloy/zod-to-openapi": "^8.1.
|
|
40
|
+
"@cabloy/zod-to-openapi": "^8.1.4",
|
|
41
41
|
"@zhennann/currency": "^2.0.0",
|
|
42
42
|
"openapi3-ts": "^4.5.0",
|
|
43
43
|
"zod": "^4.1.5"
|