vona-module-a-openapiutils 5.0.17 → 5.1.1

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 (112) hide show
  1. package/LICENSE +0 -0
  2. package/dist/.metadata/index.d.ts +1 -0
  3. package/dist/.metadata/index.d.ts.map +1 -0
  4. package/dist/.metadata/locales.d.ts +1 -0
  5. package/dist/.metadata/locales.d.ts.map +1 -0
  6. package/dist/.metadata/this.d.ts +1 -0
  7. package/dist/.metadata/this.d.ts.map +1 -0
  8. package/dist/config/locale/en-us.d.ts +1 -0
  9. package/dist/config/locale/en-us.d.ts.map +1 -0
  10. package/dist/config/locale/zh-cn.d.ts +1 -0
  11. package/dist/config/locale/zh-cn.d.ts.map +1 -0
  12. package/dist/index.d.ts +1 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +137 -79
  15. package/dist/index.js.map +1 -0
  16. package/dist/lib/const/database.d.ts +1 -0
  17. package/dist/lib/const/database.d.ts.map +1 -0
  18. package/dist/lib/const/decorator.d.ts +1 -0
  19. package/dist/lib/const/decorator.d.ts.map +1 -0
  20. package/dist/lib/const/index.d.ts +1 -0
  21. package/dist/lib/const/index.d.ts.map +1 -0
  22. package/dist/lib/decorator/api.d.ts +1 -0
  23. package/dist/lib/decorator/api.d.ts.map +1 -0
  24. package/dist/lib/decorator/field.d.ts +1 -0
  25. package/dist/lib/decorator/field.d.ts.map +1 -0
  26. package/dist/lib/decorator/index.d.ts +1 -0
  27. package/dist/lib/decorator/index.d.ts.map +1 -0
  28. package/dist/lib/decorator/resource.d.ts +1 -0
  29. package/dist/lib/decorator/resource.d.ts.map +1 -0
  30. package/dist/lib/index.d.ts +1 -0
  31. package/dist/lib/index.d.ts.map +1 -0
  32. package/dist/lib/schema/bodySchemaWrapper.d.ts +1 -0
  33. package/dist/lib/schema/bodySchemaWrapper.d.ts.map +1 -0
  34. package/dist/lib/schema/index.d.ts +2 -0
  35. package/dist/lib/schema/index.d.ts.map +1 -0
  36. package/dist/lib/schema/makeMetadata.d.ts +4 -0
  37. package/dist/lib/schema/makeMetadata.d.ts.map +1 -0
  38. package/dist/lib/schema/makeSchemaLikes.d.ts +3 -2
  39. package/dist/lib/schema/makeSchemaLikes.d.ts.map +1 -0
  40. package/dist/lib/schema/schemaDynamic.d.ts +1 -0
  41. package/dist/lib/schema/schemaDynamic.d.ts.map +1 -0
  42. package/dist/lib/schema/v/component.d.ts +5 -0
  43. package/dist/lib/schema/v/component.d.ts.map +1 -0
  44. package/dist/lib/schema/v/filter.d.ts +1 -0
  45. package/dist/lib/schema/v/filter.d.ts.map +1 -0
  46. package/dist/lib/schema/v/helpers.d.ts +1 -2
  47. package/dist/lib/schema/v/helpers.d.ts.map +1 -0
  48. package/dist/lib/schema/v/openapi.d.ts +1 -0
  49. package/dist/lib/schema/v/openapi.d.ts.map +1 -0
  50. package/dist/lib/schema/v/rest.d.ts +4 -4
  51. package/dist/lib/schema/v/rest.d.ts.map +1 -0
  52. package/dist/lib/schema/v/serializer.d.ts +1 -0
  53. package/dist/lib/schema/v/serializer.d.ts.map +1 -0
  54. package/dist/lib/schema/v/system.d.ts +2 -1
  55. package/dist/lib/schema/v/system.d.ts.map +1 -0
  56. package/dist/lib/schema/v/utils.d.ts +4 -0
  57. package/dist/lib/schema/v/utils.d.ts.map +1 -0
  58. package/dist/lib/schema/v/zod.d.ts +1 -0
  59. package/dist/lib/schema/v/zod.d.ts.map +1 -0
  60. package/dist/lib/schema/v.d.ts +7 -5
  61. package/dist/lib/schema/v.d.ts.map +1 -0
  62. package/dist/lib/utils.d.ts +4 -1
  63. package/dist/lib/utils.d.ts.map +1 -0
  64. package/dist/lib/web.d.ts +2 -2
  65. package/dist/lib/web.d.ts.map +1 -0
  66. package/dist/types/api.d.ts +1 -0
  67. package/dist/types/api.d.ts.map +1 -0
  68. package/dist/types/component.d.ts +12 -0
  69. package/dist/types/component.d.ts.map +1 -0
  70. package/dist/types/decorator.d.ts +1 -0
  71. package/dist/types/decorator.d.ts.map +1 -0
  72. package/dist/types/index.d.ts +2 -0
  73. package/dist/types/index.d.ts.map +1 -0
  74. package/dist/types/order.d.ts +1 -0
  75. package/dist/types/order.d.ts.map +1 -0
  76. package/package.json +21 -16
  77. package/src/.metadata/index.ts +35 -0
  78. package/src/.metadata/locales.ts +13 -0
  79. package/src/.metadata/this.ts +2 -0
  80. package/src/config/locale/en-us.ts +3 -0
  81. package/src/config/locale/zh-cn.ts +3 -0
  82. package/src/index.ts +4 -0
  83. package/src/lib/const/database.ts +10 -0
  84. package/src/lib/const/decorator.ts +6 -0
  85. package/src/lib/const/index.ts +2 -0
  86. package/src/lib/decorator/api.ts +107 -0
  87. package/src/lib/decorator/field.ts +19 -0
  88. package/src/lib/decorator/index.ts +2 -0
  89. package/src/lib/decorator/resource.ts +10 -0
  90. package/src/lib/index.ts +6 -0
  91. package/src/lib/schema/bodySchemaWrapper.ts +9 -0
  92. package/src/lib/schema/index.ts +5 -0
  93. package/src/lib/schema/makeMetadata.ts +12 -0
  94. package/src/lib/schema/makeSchemaLikes.ts +107 -0
  95. package/src/lib/schema/schemaDynamic.ts +17 -0
  96. package/src/lib/schema/v/component.ts +78 -0
  97. package/src/lib/schema/v/filter.tsx +40 -0
  98. package/src/lib/schema/v/helpers.ts +110 -0
  99. package/src/lib/schema/v/openapi.ts +29 -0
  100. package/src/lib/schema/v/rest.ts +50 -0
  101. package/src/lib/schema/v/serializer.ts +86 -0
  102. package/src/lib/schema/v/system.ts +72 -0
  103. package/src/lib/schema/v/utils.ts +6 -0
  104. package/src/lib/schema/v/zod.ts +36 -0
  105. package/src/lib/schema/v.ts +88 -0
  106. package/src/lib/utils.ts +100 -0
  107. package/src/lib/web.ts +11 -0
  108. package/src/types/api.ts +99 -0
  109. package/src/types/component.ts +19 -0
  110. package/src/types/decorator.ts +7 -0
  111. package/src/types/index.ts +4 -0
  112. package/src/types/order.ts +9 -0
@@ -0,0 +1,88 @@
1
+ import { schemaComponent } from './v/component.ts';
2
+ import { schemaFilter, schemaFilterDateRange, schemaFilterTransform } from './v/filter.tsx';
3
+ import {
4
+ schemaEmail,
5
+ schemaIPv4,
6
+ schemaIPv6,
7
+ schemaLowercase,
8
+ schemaMax,
9
+ schemaMin,
10
+ schemaRegex,
11
+ schemaTableIdentity,
12
+ schemaToLowerCase,
13
+ schemaToUpperCase,
14
+ schemaTrim,
15
+ schemaUppercase,
16
+ schemaUrl,
17
+ schemaUuid,
18
+ } from './v/helpers.ts';
19
+ import { schemaDescription, schemaExample, schemaOpenapi, schemaTitle } from './v/openapi.ts';
20
+ import { schemaCustomKey, schemaOrder, schemaRender, schemaRest, schemaVisible } from './v/rest.ts';
21
+ import {
22
+ schemaSerializerCustom,
23
+ schemaSerializerExclude,
24
+ schemaSerializerGetter,
25
+ schemaSerializerReplace,
26
+ schemaSerializerTransform,
27
+ } from './v/serializer.ts';
28
+ import {
29
+ schemaArray,
30
+ schemaDefault,
31
+ schemaLazy,
32
+ schemaLooseObject,
33
+ schemaObject,
34
+ schemaOptional,
35
+ schemaRequired,
36
+ schemaStrictObject,
37
+ } from './v/system.ts';
38
+ import { schemaZodRefine, schemaZodTransform } from './v/zod.ts';
39
+
40
+ export const v = {
41
+ required: schemaRequired,
42
+ optional: schemaOptional,
43
+ default: schemaDefault,
44
+ object: schemaObject,
45
+ strictObject: schemaStrictObject,
46
+ looseObject: schemaLooseObject,
47
+ array: schemaArray,
48
+ lazy: schemaLazy,
49
+ // helpers
50
+ email: schemaEmail,
51
+ url: schemaUrl,
52
+ uuid: schemaUuid,
53
+ ipv4: schemaIPv4,
54
+ ipv6: schemaIPv6,
55
+ min: schemaMin,
56
+ max: schemaMax,
57
+ trim: schemaTrim,
58
+ toLowerCase: schemaToLowerCase,
59
+ toUpperCase: schemaToUpperCase,
60
+ lowercase: schemaLowercase,
61
+ uppercase: schemaUppercase,
62
+ regex: schemaRegex,
63
+ tableIdentity: schemaTableIdentity,
64
+ serializerExclude: schemaSerializerExclude,
65
+ serializerTransform: schemaSerializerTransform,
66
+ serializerReplace: schemaSerializerReplace,
67
+ serializerGetter: schemaSerializerGetter,
68
+ serializerCustom: schemaSerializerCustom,
69
+ filter: schemaFilter,
70
+ filterTransform: schemaFilterTransform,
71
+ filterDateRange: schemaFilterDateRange,
72
+ // openapi
73
+ openapi: schemaOpenapi,
74
+ title: schemaTitle,
75
+ description: schemaDescription,
76
+ example: schemaExample,
77
+ // rest
78
+ rest: schemaRest,
79
+ customKey: schemaCustomKey,
80
+ order: schemaOrder,
81
+ render: schemaRender,
82
+ visible: schemaVisible,
83
+ // component
84
+ component: schemaComponent,
85
+ // zod
86
+ refine: schemaZodRefine,
87
+ transform: schemaZodTransform,
88
+ };
@@ -0,0 +1,100 @@
1
+ import type { Constructable, ILocaleMagic } from 'vona';
2
+ import type { TypeOpenapiMetadata } from 'vona-module-a-openapi';
3
+
4
+ import { isClass, isEmptyObject } from '@cabloy/utils';
5
+ import { ZodMetadata } from '@cabloy/zod-openapi';
6
+ import { appMetadata, appResource, cast, deepExtend, registerMappedClassMetadataKey } from 'vona';
7
+ import { z } from 'zod';
8
+
9
+ import type { TypeDecoratorRules } from '../types/decorator.ts';
10
+ import type { TypeSchemaOrderLevel } from '../types/order.ts';
11
+
12
+ import { OrderLevelBaseMap } from './const/database.ts';
13
+ import { SymbolDecoratorRule } from './const/decorator.ts';
14
+
15
+ export function getTargetDecoratorRules(target: object, disableRegisterMetadata?: boolean): TypeDecoratorRules {
16
+ if (!disableRegisterMetadata) {
17
+ registerMappedClassMetadataKey(target, SymbolDecoratorRule, {
18
+ partialClass: (meta: z.ZodType) => {
19
+ return meta.optional();
20
+ },
21
+ });
22
+ }
23
+ return appMetadata.getOwnMetadataMap(true, SymbolDecoratorRule, target);
24
+ }
25
+
26
+ export function getTargetDecoratorRuleColumns(target: object): string[] {
27
+ const rules = getTargetDecoratorRules(target, true);
28
+ return Object.keys(rules);
29
+ }
30
+
31
+ export function getTargetDecoratorRuleColumnsMap(target: object): Record<string, string> {
32
+ const columns = getTargetDecoratorRuleColumns(target);
33
+ const map = {};
34
+ for (const column of columns) {
35
+ map[column] = column;
36
+ }
37
+ return map;
38
+ }
39
+
40
+ export function mergeFieldsOpenapiMetadata(target: Constructable) {
41
+ // beanOptions
42
+ const beanOptions = appResource.getBean(target);
43
+ const fields = cast(beanOptions?.options)?.fields;
44
+ if (!fields) return;
45
+ for (const key in fields) {
46
+ const field: TypeOpenapiMetadata | z.ZodType = fields[key];
47
+ if (!field) continue;
48
+ mergeFieldOpenapiMetadata(target.prototype, key, field);
49
+ }
50
+ }
51
+
52
+ // fieldRule maybe undefined
53
+ export function mergeFieldOpenapiMetadata(target: object, prop: string, fieldRule?: TypeOpenapiMetadata | z.ZodType): TypeDecoratorRules {
54
+ // rules
55
+ const rules = getTargetDecoratorRules(target);
56
+ // rule
57
+ const schemaCurrent = rules[prop];
58
+ const metadataCurrent = schemaCurrent ? ZodMetadata.getOpenapiMetadata(schemaCurrent) : undefined;
59
+ // merge
60
+ if (Object.prototype.hasOwnProperty.call(fieldRule, 'parseAsync')) {
61
+ const schema: z.ZodType = fieldRule as any;
62
+ if (isEmptyObject(metadataCurrent)) {
63
+ rules[prop] = schema;
64
+ } else {
65
+ const metadataCustom = ZodMetadata.getOpenapiMetadata(schema);
66
+ rules[prop] = schema.openapi(deepExtend({}, metadataCurrent, metadataCustom));
67
+ }
68
+ } else {
69
+ if (schemaCurrent) {
70
+ if (!isEmptyObject(fieldRule)) {
71
+ rules[prop] = schemaCurrent.openapi(deepExtend({}, metadataCurrent, fieldRule));
72
+ }
73
+ } else {
74
+ if (isEmptyObject(fieldRule)) {
75
+ rules[prop] = z.any();
76
+ } else {
77
+ rules[prop] = z.any().openapi(fieldRule as any);
78
+ }
79
+ }
80
+ }
81
+ return rules;
82
+ }
83
+
84
+ export function prepareClassType<T>(classType: (() => Constructable<T>) | Constructable<T>): Constructable<T> {
85
+ return isClass(classType) ? (classType as Constructable<T>) : cast(classType)();
86
+ }
87
+
88
+ export function normalizeErrorParams(params?: string | ILocaleMagic | any, errorDefault?: any) {
89
+ const params2 = params as any;
90
+ if (!params2) return errorDefault;
91
+ if (params2.toJSON) {
92
+ return { error: () => params2 };
93
+ }
94
+ return z.util.normalizeParams(params2);
95
+ }
96
+
97
+ export function $order(order: number, level?: TypeSchemaOrderLevel) {
98
+ const levelBase = OrderLevelBaseMap[level ?? 'business'];
99
+ return levelBase + order;
100
+ }
package/src/lib/web.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { IApiPathRecord } from 'vona-module-a-web';
2
+
3
+ import { combineParamsAndQuery } from '@cabloy/utils';
4
+
5
+ export function $apiPath<K extends keyof IApiPathRecord>(path: K): K {
6
+ return path;
7
+ }
8
+
9
+ export function $apiPathAndCombineParamsAndQuery<K extends keyof IApiPathRecord>(path: K, options?: { params?: object; query?: object }): string {
10
+ return combineParamsAndQuery(path, options);
11
+ }
@@ -0,0 +1,99 @@
1
+ import type { OpenAPIObject as OpenAPIObject30, SchemaObject as SchemaObject30 } from 'openapi3-ts/oas30';
2
+ import type { OpenAPIObject as OpenAPIObject31, SchemaObject as SchemaObject31 } from 'openapi3-ts/oas31';
3
+ import type { ILocaleMagic, VonaApplication } from 'vona';
4
+ import type z from 'zod';
5
+
6
+ import type { SchemaLikeCreate } from './decorator.ts';
7
+
8
+ export interface IOpenapiHeader {
9
+ name: string;
10
+ description?: string;
11
+ }
12
+
13
+ export interface IResponseHeaders {
14
+ [key: string]: string | string[];
15
+ }
16
+
17
+ export type TypeGenerateJsonScene = 'api' | 'rest';
18
+
19
+ export type TypeOpenApiVersion = 'V30' | 'V31';
20
+ export interface IOpenapiObject {
21
+ V30: OpenAPIObject30;
22
+ V31: OpenAPIObject31;
23
+ }
24
+ export interface IOpenapiSchemaObject {
25
+ V30: SchemaObject30;
26
+ V31: SchemaObject31;
27
+ }
28
+
29
+ export type TypeSecuritySchemes = Record<string, ((this: VonaApplication) => object) | object>;
30
+
31
+ export interface IOpenapiOptions {
32
+ public?: boolean;
33
+ description?: string | ILocaleMagic;
34
+ summary?: string | ILocaleMagic;
35
+ httpCode?: number;
36
+ contentType?: TypeResponseContentType;
37
+ bodySchema?: z.ZodType;
38
+ bodySchemaWrapper?: SchemaLikeCreate | false;
39
+ exclude?: boolean;
40
+ tags?: string[];
41
+ operationId?: string;
42
+ headers?: IOpenapiHeader[];
43
+ setHeaders?: IResponseHeaders;
44
+ }
45
+
46
+ export type TypeResponseContentType =
47
+ | 'application/json'
48
+ | 'application/xml'
49
+ | 'application/x-www-form-urlencoded'
50
+ | 'multipart/form-data'
51
+ | 'text/plain'
52
+ | 'text/html'
53
+ | 'application/octet-stream'
54
+ | 'application/pdf'
55
+ | 'image/png'
56
+ | 'image/jpeg'
57
+ | 'image/gif'
58
+ | 'image/webp'
59
+ | 'image/svg+xml'
60
+ | 'image/bmp'
61
+ | 'image/tiff'
62
+ | 'image/vnd.microsoft.icon'
63
+ | 'image/vnd.wap.wbmp'
64
+ | 'image/x-icon'
65
+ | 'image/x-jng'
66
+ | 'image/x-ms-bmp'
67
+ | 'image/x-portable-bitmap'
68
+ | 'image/x-portable-graymap'
69
+ | 'image/x-portable-pixmap'
70
+ | 'image/x-xbitmap'
71
+ | 'image/x-xpixmap'
72
+ | 'image/x-xwindowdump'
73
+ | 'image/bmp'
74
+ | 'image/x-bmp'
75
+ | 'image/x-xbitmap'
76
+ | 'image/x-win-bitmap'
77
+ | 'image/x-windows-bmp'
78
+ | 'image/ms-bmp'
79
+ | 'image/x-ms-bmp'
80
+ | 'image/x-icon'
81
+ | 'image/x-ico'
82
+ | 'image/vnd.microsoft.icon'
83
+ | 'image/ico'
84
+ | 'image/icon'
85
+ | 'text/css'
86
+ | 'text/csv'
87
+ | 'text/html'
88
+ | 'text/javascript'
89
+ | 'text/plain'
90
+ | 'text/xml'
91
+ | 'application/javascript'
92
+ | 'application/ecmascript'
93
+ | 'application/json'
94
+ | 'application/ld+json'
95
+ | 'application/manifest+json'
96
+ | 'application/javascript'
97
+ | 'application/x-javascript'
98
+ | 'application/x-json'
99
+ | 'application/x-ms-application';
@@ -0,0 +1,19 @@
1
+ import type { CurrencyOptions } from '@zhennann/currency';
2
+ import type {
3
+ ICaptchaOptions,
4
+ IResourcePickerOptions,
5
+ ISelectOptions,
6
+ ITextareaOptions,
7
+ IToggleOptions,
8
+ TypeDateFormat,
9
+ } from 'vona-module-a-openapi';
10
+
11
+ export interface ISchemaComponentRecord {
12
+ currency: CurrencyOptions;
13
+ date: TypeDateFormat;
14
+ captcha: ICaptchaOptions;
15
+ toggle: IToggleOptions;
16
+ select: ISelectOptions;
17
+ resourcePicker: IResourcePickerOptions;
18
+ textarea: ITextareaOptions;
19
+ }
@@ -0,0 +1,7 @@
1
+ import type { Constructable } from 'vona';
2
+ import type z from 'zod';
3
+
4
+ export type SchemaLikeCreate = (schema: any) => any; // not use z.ZodType
5
+ export type SchemaLike<T = unknown> = SchemaLikeCreate | z.ZodType<T> | Constructable;
6
+
7
+ export type TypeDecoratorRules = Record<string, z.ZodType | undefined>;
@@ -0,0 +1,4 @@
1
+ export * from './api.ts';
2
+ export * from './component.ts';
3
+ export * from './decorator.ts';
4
+ export * from './order.ts';
@@ -0,0 +1,9 @@
1
+ import type { TypeSchemaScene } from 'vona-module-a-openapi';
2
+
3
+ export type TypeSchemaOrderLevel = 'core' | 'business' | 'max';
4
+
5
+ export interface ISchemaOrderParams {
6
+ order: number;
7
+ level?: TypeSchemaOrderLevel;
8
+ scene?: TypeSchemaScene;
9
+ }