zova-module-a-openapi 5.0.0 → 5.1.0
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/LICENSE +21 -0
- package/dist/index.js +470 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -23
- package/src/.metadata/index.ts +102 -0
- package/src/.metadata/this.ts +2 -0
- package/src/bean/sys.sdk.ts +156 -0
- package/src/config/config.ts +44 -0
- package/src/index.ts +3 -0
- package/src/lib/index.ts +1 -0
- package/src/lib/schema.ts +114 -0
- package/src/model/sdk.ts +226 -0
- package/src/monkey.ts +52 -0
- package/src/types/action.ts +26 -0
- package/src/types/actions.ts +18 -0
- package/src/types/captcha.ts +16 -0
- package/src/types/database.ts +4 -0
- package/src/types/date.ts +25 -0
- package/src/types/index.ts +16 -0
- package/src/types/jsx.ts +10 -0
- package/src/types/permissions.ts +18 -0
- package/src/types/resourceMeta.ts +37 -0
- package/src/types/resourcePicker.ts +10 -0
- package/src/types/rest.ts +107 -0
- package/src/types/schema.ts +24 -0
- package/src/types/sdk.ts +26 -0
- package/src/types/select.ts +16 -0
- package/src/types/table.ts +27 -0
- package/src/types/textarea.ts +10 -0
- package/src/types/toggle.ts +3 -0
- package/README.md +0 -1
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// eslint-disable
|
|
2
|
+
/** model: begin */
|
|
3
|
+
export * from '../model/sdk.js';
|
|
4
|
+
import { IModelOptionsSdk } from '../model/sdk.js';
|
|
5
|
+
import 'zova-module-a-model';
|
|
6
|
+
declare module 'zova-module-a-model' {
|
|
7
|
+
|
|
8
|
+
export interface IModelRecord {
|
|
9
|
+
'a-openapi:sdk': IModelOptionsSdk;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
declare module 'zova-module-a-openapi' {
|
|
15
|
+
|
|
16
|
+
export interface ModelSdk {
|
|
17
|
+
/** @internal */
|
|
18
|
+
get scope(): ScopeModuleAOpenapi;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ModelSdk {
|
|
22
|
+
get $beanFullName(): 'a-openapi.model.sdk';
|
|
23
|
+
get $onionName(): 'a-openapi:sdk';
|
|
24
|
+
get $onionOptions(): IModelOptionsSdk;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** model: end */
|
|
28
|
+
/** model: begin */
|
|
29
|
+
import { ModelSdk } from '../model/sdk.js';
|
|
30
|
+
import 'zova';
|
|
31
|
+
declare module 'zova' {
|
|
32
|
+
export interface IBeanRecordGeneral {
|
|
33
|
+
'a-openapi.model.sdk': ModelSdk;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** model: end */
|
|
37
|
+
/** sys: begin */
|
|
38
|
+
export * from '../bean/sys.sdk.js';
|
|
39
|
+
|
|
40
|
+
import 'zova';
|
|
41
|
+
declare module 'zova' {
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
declare module 'zova-module-a-openapi' {
|
|
46
|
+
|
|
47
|
+
export interface SysSdk {
|
|
48
|
+
/** @internal */
|
|
49
|
+
get scope(): ScopeModuleAOpenapi;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface SysSdk {
|
|
53
|
+
get $beanFullName(): 'a-openapi.sys.sdk';
|
|
54
|
+
get $onionName(): 'a-openapi:sdk';
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** sys: end */
|
|
59
|
+
/** sys: begin */
|
|
60
|
+
import { SysSdk } from '../bean/sys.sdk.js';
|
|
61
|
+
import 'zova';
|
|
62
|
+
declare module 'zova' {
|
|
63
|
+
export interface IBeanRecordGeneral {
|
|
64
|
+
'a-openapi.sys.sdk': SysSdk;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** sys: end */
|
|
68
|
+
/** config: begin */
|
|
69
|
+
export * from '../config/config.js';
|
|
70
|
+
import { config } from '../config/config.js';
|
|
71
|
+
/** config: end */
|
|
72
|
+
/** monkey: begin */
|
|
73
|
+
export * from '../monkey.js';
|
|
74
|
+
/** monkey: end */
|
|
75
|
+
/** scope: begin */
|
|
76
|
+
import { BeanScopeBase, type BeanScopeUtil, TypeModuleConfig } from 'zova';
|
|
77
|
+
import { Scope } from 'zova-module-a-bean';
|
|
78
|
+
|
|
79
|
+
@Scope()
|
|
80
|
+
export class ScopeModuleAOpenapi extends BeanScopeBase {}
|
|
81
|
+
|
|
82
|
+
export interface ScopeModuleAOpenapi {
|
|
83
|
+
util: BeanScopeUtil;
|
|
84
|
+
config: TypeModuleConfig<typeof config>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
import 'zova';
|
|
88
|
+
declare module 'zova' {
|
|
89
|
+
export interface IBeanScopeRecord {
|
|
90
|
+
'a-openapi': ScopeModuleAOpenapi;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface IBeanScopeConfig {
|
|
94
|
+
'a-openapi': ReturnType<typeof config>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** scope: end */
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import type { SchemaObject } from 'openapi3-ts/oas31';
|
|
2
|
+
|
|
3
|
+
import { shallowReactive } from 'vue';
|
|
4
|
+
import { BeanBase, IApiActionConfigPrepareOptions, ILocaleRecord, TypeEventOff } from 'zova';
|
|
5
|
+
import { IApiSchemaOptions } from 'zova-module-a-api';
|
|
6
|
+
import { Sys } from 'zova-module-a-bean';
|
|
7
|
+
import { BeanFetch } from 'zova-module-a-fetch';
|
|
8
|
+
|
|
9
|
+
import { IOpenapiSchema } from '../types/schema.js';
|
|
10
|
+
import { IOpenapiSdkBootstrap, IOpenapiSdkItem, SymbolOpenapiSchemaName, TypeRequestMethod } from '../types/sdk.js';
|
|
11
|
+
|
|
12
|
+
// const PATH_PARAM_RE = /\{([^{}/]+)\}/g;
|
|
13
|
+
|
|
14
|
+
@Sys()
|
|
15
|
+
export class SysSdk extends BeanBase {
|
|
16
|
+
private locale: keyof ILocaleRecord;
|
|
17
|
+
bootstraps: Record<string, IOpenapiSdkBootstrap>;
|
|
18
|
+
schemas: Record<string, SchemaObject>;
|
|
19
|
+
sdks: Record<string, Record<string, IOpenapiSdkItem>>;
|
|
20
|
+
private _eventSsrHmrReload: TypeEventOff;
|
|
21
|
+
private _fetch: BeanFetch;
|
|
22
|
+
|
|
23
|
+
protected async __init__(locale: keyof ILocaleRecord) {
|
|
24
|
+
this.locale = locale;
|
|
25
|
+
this.bootstraps = shallowReactive({});
|
|
26
|
+
this.schemas = shallowReactive({});
|
|
27
|
+
this.sdks = shallowReactive({});
|
|
28
|
+
// event
|
|
29
|
+
if (this.sys.env.SSR_HMR === 'true') {
|
|
30
|
+
this._eventSsrHmrReload = this.sys.meta.event.on('a-ssrhmr:reload', async (_data, next) => {
|
|
31
|
+
await this.reload();
|
|
32
|
+
return next();
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
protected __dispose__() {
|
|
38
|
+
if (this._eventSsrHmrReload) {
|
|
39
|
+
this._eventSsrHmrReload();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private async reload() {
|
|
44
|
+
// server
|
|
45
|
+
if (process.env.SERVER) {
|
|
46
|
+
this.bootstraps = shallowReactive({});
|
|
47
|
+
this.schemas = shallowReactive({});
|
|
48
|
+
this.sdks = shallowReactive({});
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
// client
|
|
52
|
+
// bootstraps
|
|
53
|
+
const bootstraps = this.bootstraps;
|
|
54
|
+
this.bootstraps = shallowReactive({});
|
|
55
|
+
for (const resource in bootstraps) {
|
|
56
|
+
await this.loadBootstrap(this._fetch, resource);
|
|
57
|
+
}
|
|
58
|
+
// sdks
|
|
59
|
+
const sdks = this.sdks;
|
|
60
|
+
this.sdks = shallowReactive({});
|
|
61
|
+
for (const api in sdks) {
|
|
62
|
+
for (const apiMethod in sdks[api]) {
|
|
63
|
+
await this.loadSdk(this._fetch, api, apiMethod as any, { authToken: false });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getBootstrap(resource: string): IOpenapiSdkBootstrap | undefined {
|
|
69
|
+
return this.bootstraps[resource];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getSdk(api: string | undefined, apiMethod: string | undefined): IOpenapiSdkItem | undefined {
|
|
73
|
+
if (!api) return;
|
|
74
|
+
const [api2, apiMethod2] = this.prepareApiMeta(api, apiMethod);
|
|
75
|
+
return this.sdks[api2]?.[apiMethod2];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
getSchema(schemaName: string): SchemaObject {
|
|
79
|
+
return this.schemas[schemaName];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async loadBootstrap($fetch: BeanFetch, resource: string): Promise<IOpenapiSdkBootstrap> {
|
|
83
|
+
if (process.env.CLIENT) {
|
|
84
|
+
this._fetch = $fetch;
|
|
85
|
+
}
|
|
86
|
+
if (!this.bootstraps[resource]) {
|
|
87
|
+
this.bootstraps[resource] = await $fetch.get(
|
|
88
|
+
this.sys.util.apiActionPathTranslate(this.scope.config.api.bootstrap, { resource }),
|
|
89
|
+
this.sys.util.apiActionConfigPrepare(undefined, undefined, false),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
return this.bootstraps[resource];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async loadSdk(
|
|
96
|
+
$fetch: BeanFetch,
|
|
97
|
+
api?: string,
|
|
98
|
+
apiMethod?: TypeRequestMethod,
|
|
99
|
+
apiOptions?: IApiSchemaOptions,
|
|
100
|
+
): Promise<IOpenapiSdkItem | undefined> {
|
|
101
|
+
if (process.env.CLIENT) {
|
|
102
|
+
this._fetch = $fetch;
|
|
103
|
+
}
|
|
104
|
+
if (!api) return;
|
|
105
|
+
const [api2, apiMethod2] = this.prepareApiMeta(api, apiMethod);
|
|
106
|
+
if (this.sdks[api2]?.[apiMethod2]) return this.sdks[api2][apiMethod2];
|
|
107
|
+
// params
|
|
108
|
+
const params: any[] = [this.sys.util.apiActionPathTranslate(api2)];
|
|
109
|
+
// body
|
|
110
|
+
if (!['get', 'delete'].includes(apiMethod2)) {
|
|
111
|
+
params.push(undefined);
|
|
112
|
+
}
|
|
113
|
+
// options
|
|
114
|
+
const options: IApiActionConfigPrepareOptions = { authToken: apiOptions?.authToken, openapiSchema: true, headers: {} };
|
|
115
|
+
const localeKey = this.sys.env.APP_LOCALE_HEADER_KEY;
|
|
116
|
+
if (localeKey) {
|
|
117
|
+
options.headers![localeKey] = this.locale;
|
|
118
|
+
}
|
|
119
|
+
params.push(this.sys.util.apiActionConfigPrepare(undefined, options));
|
|
120
|
+
const data: IOpenapiSchema = await $fetch[apiMethod2](...params);
|
|
121
|
+
// schemas
|
|
122
|
+
const schemaNames: string[] = [];
|
|
123
|
+
const schemas = data.doc.components?.schemas;
|
|
124
|
+
if (schemas) {
|
|
125
|
+
for (const key in schemas) {
|
|
126
|
+
const schema = schemas[key] as unknown as SchemaObject;
|
|
127
|
+
if (!schema[SymbolOpenapiSchemaName]) {
|
|
128
|
+
schema[SymbolOpenapiSchemaName] = key;
|
|
129
|
+
}
|
|
130
|
+
this.schemas[key] = schema;
|
|
131
|
+
schemaNames.push(key);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// paths
|
|
135
|
+
const paths = data.doc.paths;
|
|
136
|
+
if (paths) {
|
|
137
|
+
for (const key in paths) {
|
|
138
|
+
// const path = key.replace(PATH_PARAM_RE, ':$1');
|
|
139
|
+
if (!this.sdks[api2]) this.sdks[api2] = shallowReactive({});
|
|
140
|
+
for (const method in paths[key]) {
|
|
141
|
+
this.sdks[api2][method] = {
|
|
142
|
+
schemas: schemaNames,
|
|
143
|
+
operationObject: paths[key][method],
|
|
144
|
+
meta: data.meta,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// ok
|
|
150
|
+
return this.sdks[api2][apiMethod2];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
prepareApiMeta(api: string, apiMethod?: string) {
|
|
154
|
+
return [api, apiMethod ?? 'get'];
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ZovaSys } from 'zova';
|
|
2
|
+
|
|
3
|
+
import type { IOpenapiOptionsResourceMeta } from '../types/resourceMeta.js';
|
|
4
|
+
|
|
5
|
+
export const config = (_sys: ZovaSys) => {
|
|
6
|
+
return {
|
|
7
|
+
base: {
|
|
8
|
+
provider: {
|
|
9
|
+
components: {
|
|
10
|
+
form: 'a-form:form',
|
|
11
|
+
table: 'a-table:table',
|
|
12
|
+
},
|
|
13
|
+
actions: {
|
|
14
|
+
actionLog: 'a-actions:log',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
form: {
|
|
18
|
+
provider: {
|
|
19
|
+
components: {
|
|
20
|
+
formField: 'a-form:formField',
|
|
21
|
+
text: 'input',
|
|
22
|
+
password: 'input',
|
|
23
|
+
currency: 'a-currency:formFieldCurrency',
|
|
24
|
+
},
|
|
25
|
+
behaviors: {},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
table: {
|
|
29
|
+
provider: {
|
|
30
|
+
components: {
|
|
31
|
+
currency: 'a-currency.tableCell.currency',
|
|
32
|
+
date: 'a-date.tableCell.date',
|
|
33
|
+
},
|
|
34
|
+
actions: {},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
} as IOpenapiOptionsResourceMeta,
|
|
38
|
+
resourceMeta: {} as IOpenapiOptionsResourceMeta,
|
|
39
|
+
api: {
|
|
40
|
+
bootstrap: '/api/openapischema/resource/bootstrap/:resource',
|
|
41
|
+
permissions: '/api/home/base/permission/:resource',
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
};
|
package/src/index.ts
ADDED
package/src/lib/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './schema.js';
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { OperationObject, ParameterObject, RequestBodyObject, SchemaObject } from 'openapi3-ts/oas31';
|
|
2
|
+
|
|
3
|
+
import jsonSchemaToZod from '@cabloy/json-schema-to-zod';
|
|
4
|
+
import { evaluateSimple } from '@cabloy/utils';
|
|
5
|
+
import { toRaw } from 'vue';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
import { cast, deepExtend } from 'zova';
|
|
8
|
+
|
|
9
|
+
import type { TypeSchemaScene } from '../types/rest.js';
|
|
10
|
+
|
|
11
|
+
import { OrderUnknownBase } from '../types/database.js';
|
|
12
|
+
|
|
13
|
+
const __FilterColumnsIgnore = ['columns', 'where', 'orders', 'pageNo', 'pageSize'];
|
|
14
|
+
|
|
15
|
+
export function loadSchemaProperties(
|
|
16
|
+
schema: SchemaObject | undefined,
|
|
17
|
+
onGetSchema: (schemaName: string) => SchemaObject | undefined,
|
|
18
|
+
scene?: TypeSchemaScene,
|
|
19
|
+
): SchemaObject[] | undefined {
|
|
20
|
+
if (!schema) return;
|
|
21
|
+
const properties = schema.properties!;
|
|
22
|
+
const result: SchemaObject[] = [];
|
|
23
|
+
// filter
|
|
24
|
+
for (let key in properties) {
|
|
25
|
+
let property = properties[key] as SchemaObject;
|
|
26
|
+
if (property.$ref) {
|
|
27
|
+
property = onGetSchema(property.$ref)!;
|
|
28
|
+
}
|
|
29
|
+
if (!property) continue;
|
|
30
|
+
const customKey = property.rest?.customKey;
|
|
31
|
+
if (customKey) {
|
|
32
|
+
const parts = customKey.split('.');
|
|
33
|
+
const propertyParent: any = parts[0] === key ? property : result.find(item => item.key === parts[0]);
|
|
34
|
+
property = propertyParent?.properties[parts[1]];
|
|
35
|
+
key = customKey;
|
|
36
|
+
}
|
|
37
|
+
if (!property) continue;
|
|
38
|
+
property = deepExtend({ key }, property, scene ? { rest: property.rest?.[scene] ?? {} } : undefined);
|
|
39
|
+
result.push(property);
|
|
40
|
+
}
|
|
41
|
+
// sort
|
|
42
|
+
result.sort((a, b) => {
|
|
43
|
+
return (a.rest?.order ?? OrderUnknownBase) - (b.rest?.order ?? OrderUnknownBase);
|
|
44
|
+
});
|
|
45
|
+
// ok
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function schemaToZodSchema<T extends z.ZodType = z.ZodType>(
|
|
50
|
+
schema: SchemaObject,
|
|
51
|
+
onGetSchema: (schemaName: string) => SchemaObject | undefined,
|
|
52
|
+
): T {
|
|
53
|
+
const schemaNormalize = _normalizeSchema(toRaw(schema), onGetSchema);
|
|
54
|
+
const code = jsonSchemaToZod(schemaNormalize);
|
|
55
|
+
return evaluateSimple(code, { z });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function _normalizeSchema(schema: SchemaObject, onGetSchema: (schemaName: string) => SchemaObject | undefined) {
|
|
59
|
+
if (!schema.properties) return schema;
|
|
60
|
+
const schemaNew = Object.assign({}, schema, { properties: {} });
|
|
61
|
+
for (const key in schema.properties) {
|
|
62
|
+
let property = schema.properties[key] as SchemaObject | undefined;
|
|
63
|
+
if (property?.$ref) {
|
|
64
|
+
property = onGetSchema(property.$ref);
|
|
65
|
+
}
|
|
66
|
+
if (!property) continue;
|
|
67
|
+
schemaNew.properties[key] = _normalizeSchema(property, onGetSchema);
|
|
68
|
+
}
|
|
69
|
+
return schemaNew;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function getSchemaOfRequestBody(operationObject?: OperationObject): SchemaObject | undefined {
|
|
73
|
+
return cast<RequestBodyObject>(operationObject?.requestBody)?.content?.['application/json']?.schema as any;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function getSchemaOfResponseBody(operationObject?: OperationObject): SchemaObject | undefined {
|
|
77
|
+
return operationObject?.responses?.['200']?.content?.['application/json']?.schema;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function getSchemaOfRequestQuery(operationObject?: OperationObject): SchemaObject | undefined {
|
|
81
|
+
const parameters = operationObject?.parameters;
|
|
82
|
+
if (!parameters) return;
|
|
83
|
+
const schema: SchemaObject = { type: 'object', required: [], properties: {} };
|
|
84
|
+
for (const _parameter of parameters) {
|
|
85
|
+
const parameter = _parameter as ParameterObject;
|
|
86
|
+
if (parameter.in !== 'query') continue;
|
|
87
|
+
const name = parameter.name;
|
|
88
|
+
const fieldSchema = parameter.schema! as SchemaObject;
|
|
89
|
+
schema.properties![name] = fieldSchema;
|
|
90
|
+
if (parameter.required) schema.required!.push(name);
|
|
91
|
+
}
|
|
92
|
+
return schema;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function getSchemaOfRequestQueryFilter(operationObject?: OperationObject, options?: { where?: boolean; order?: boolean }) {
|
|
96
|
+
const parameters = operationObject?.parameters;
|
|
97
|
+
if (!parameters) return;
|
|
98
|
+
const schema: SchemaObject = { type: 'object', required: [], properties: {} };
|
|
99
|
+
for (const _parameter of parameters) {
|
|
100
|
+
const parameter = _parameter as ParameterObject;
|
|
101
|
+
if (parameter.in !== 'query') continue;
|
|
102
|
+
const name = parameter.name;
|
|
103
|
+
if (__FilterColumnsIgnore.includes(name)) continue;
|
|
104
|
+
const fieldSchema = parameter.schema! as SchemaObject;
|
|
105
|
+
if (
|
|
106
|
+
(options?.where === true && fieldSchema.filter?.capabilities?.where !== false) ||
|
|
107
|
+
(options?.order === true && fieldSchema.filter?.capabilities?.order !== false)
|
|
108
|
+
) {
|
|
109
|
+
schema.properties![name] = fieldSchema;
|
|
110
|
+
if (parameter.required) schema.required!.push(name);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return schema;
|
|
114
|
+
}
|
package/src/model/sdk.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { isNil } from '@cabloy/utils';
|
|
2
|
+
import { SchemaObject } from 'openapi3-ts/oas31';
|
|
3
|
+
import z from 'zod';
|
|
4
|
+
import { cast, ILocaleRecord, TypeEventOff, Use, usePrepareArg } from 'zova';
|
|
5
|
+
import { IApiSchemaOptions } from 'zova-module-a-api';
|
|
6
|
+
import { $QueryAutoLoad, BeanModelBase, IDecoratorModelOptions, Model } from 'zova-module-a-model';
|
|
7
|
+
|
|
8
|
+
import { SysSdk } from '../bean/sys.sdk.js';
|
|
9
|
+
import {
|
|
10
|
+
getSchemaOfRequestBody,
|
|
11
|
+
getSchemaOfRequestQuery,
|
|
12
|
+
getSchemaOfRequestQueryFilter,
|
|
13
|
+
getSchemaOfResponseBody,
|
|
14
|
+
loadSchemaProperties,
|
|
15
|
+
schemaToZodSchema,
|
|
16
|
+
} from '../lib/schema.js';
|
|
17
|
+
import { TypeOpenapiPermissions } from '../types/resourceMeta.js';
|
|
18
|
+
import { TypeSchemaScene } from '../types/rest.js';
|
|
19
|
+
import { IOpenapiSchemas, TypeOpenapiSchemasSdk } from '../types/schema.js';
|
|
20
|
+
import { TypeRequestMethod } from '../types/sdk.js';
|
|
21
|
+
|
|
22
|
+
const __schemaRefPrefix = '#/components/schemas/';
|
|
23
|
+
|
|
24
|
+
export interface IModelOptionsSdk extends IDecoratorModelOptions {}
|
|
25
|
+
|
|
26
|
+
@Model<IModelOptionsSdk>({
|
|
27
|
+
enableSelector: true,
|
|
28
|
+
})
|
|
29
|
+
export class ModelSdk extends BeanModelBase {
|
|
30
|
+
private _eventSsrHmrReload: TypeEventOff;
|
|
31
|
+
|
|
32
|
+
@Use({ beanFullName: 'a-openapi.sys.sdk' })
|
|
33
|
+
get $$sysSdk(): SysSdk {
|
|
34
|
+
return usePrepareArg(this.selector, true);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
protected async __init__(locale: keyof ILocaleRecord) {
|
|
38
|
+
if (!locale) throw new Error('locale not specified');
|
|
39
|
+
await super.__init__(locale);
|
|
40
|
+
// event
|
|
41
|
+
if (process.env.CLIENT && this.sys.env.SSR_HMR === 'true') {
|
|
42
|
+
this._eventSsrHmrReload = this.sys.meta.event.on('a-ssrhmr:reload', async (_data, next) => {
|
|
43
|
+
await this.$refetchQueries({ queryKey: ['bootstrap'] });
|
|
44
|
+
await this.$refetchQueries({ queryKey: ['sdk'] });
|
|
45
|
+
return next();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
protected __dispose__() {
|
|
51
|
+
if (this._eventSsrHmrReload) {
|
|
52
|
+
this._eventSsrHmrReload();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getBootstrap(resource: string) {
|
|
57
|
+
return this.$useStateData({
|
|
58
|
+
queryKey: ['bootstrap', resource],
|
|
59
|
+
queryFn: async () => {
|
|
60
|
+
const bootstrap = await this.$$sysSdk.loadBootstrap(this.$fetch, resource);
|
|
61
|
+
if (!bootstrap) throw new Error('load bootstrap error');
|
|
62
|
+
if (process.env.SERVER) {
|
|
63
|
+
await $QueryAutoLoad(() => this.getPermissions(resource));
|
|
64
|
+
} else {
|
|
65
|
+
await this.$refetchQueries({ queryKey: ['permissions', resource] });
|
|
66
|
+
}
|
|
67
|
+
return bootstrap ?? null;
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getPermissions(resource: string) {
|
|
73
|
+
return this.$useStateData({
|
|
74
|
+
queryKey: ['permissions', resource],
|
|
75
|
+
queryFn: async () => {
|
|
76
|
+
// should not use this.getBootstrap(resource);
|
|
77
|
+
const bootstrap = await this.$$sysSdk.loadBootstrap(this.$fetch, resource);
|
|
78
|
+
let permissions = bootstrap.resourceMeta.permissions;
|
|
79
|
+
if (!isNil(permissions)) return permissions;
|
|
80
|
+
permissions = (await this.$fetch.get(
|
|
81
|
+
this.sys.util.apiActionPathTranslate(this.scope.config.api.permissions, { resource }),
|
|
82
|
+
this.sys.util.apiActionConfigPrepare(),
|
|
83
|
+
)) as TypeOpenapiPermissions;
|
|
84
|
+
return permissions ?? null;
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
getSdk(api: string, apiMethod?: TypeRequestMethod, apiOptions?: IApiSchemaOptions): TypeOpenapiSchemasSdk {
|
|
90
|
+
if (!api) throw new Error('should specify api');
|
|
91
|
+
const [api2, apiMethod2] = this.$$sysSdk.prepareApiMeta(api, apiMethod);
|
|
92
|
+
return this.$useStateData({
|
|
93
|
+
queryKey: ['sdk', api2, apiMethod2],
|
|
94
|
+
queryFn: async () => {
|
|
95
|
+
const sdk = await this.$$sysSdk.loadSdk(this.$fetch, api, apiMethod, apiOptions);
|
|
96
|
+
if (!sdk) throw new Error('load sdk error');
|
|
97
|
+
for (const schemaName of sdk.schemas) {
|
|
98
|
+
if (process.env.SERVER) {
|
|
99
|
+
await $QueryAutoLoad(() => this.getSchema(schemaName));
|
|
100
|
+
} else {
|
|
101
|
+
await this.$refetchQueries({ queryKey: ['schema', schemaName] });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return sdk;
|
|
105
|
+
},
|
|
106
|
+
}) as TypeOpenapiSchemasSdk;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getSchema(schemaName: string) {
|
|
110
|
+
if (schemaName.startsWith(__schemaRefPrefix)) {
|
|
111
|
+
schemaName = schemaName.substring(__schemaRefPrefix.length);
|
|
112
|
+
}
|
|
113
|
+
return this.$useStateData({
|
|
114
|
+
queryKey: ['schema', schemaName],
|
|
115
|
+
queryFn: async () => {
|
|
116
|
+
const schema = this.$$sysSdk.getSchema(schemaName);
|
|
117
|
+
return schema ?? null;
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
getZodSchema(schemaName: string) {
|
|
123
|
+
return this.$useStateComputed({
|
|
124
|
+
queryKey: ['zodSchema', schemaName],
|
|
125
|
+
queryFn: () => {
|
|
126
|
+
const querySchema = this.getSchema(schemaName);
|
|
127
|
+
if (!querySchema.data) return null;
|
|
128
|
+
const zodSchema = this.schemaToZodSchema(querySchema.data);
|
|
129
|
+
return zodSchema;
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
getSchemaDefaultValue(schemaName: string) {
|
|
135
|
+
return this.$useStateComputed({
|
|
136
|
+
queryKey: ['schemaDefaultValue', schemaName],
|
|
137
|
+
queryFn: () => {
|
|
138
|
+
const querySchema = this.getSchema(schemaName);
|
|
139
|
+
if (!querySchema.data) return null;
|
|
140
|
+
const schema = querySchema.data;
|
|
141
|
+
// const schema = this.$$sysSdk.getSchema(schemaName);
|
|
142
|
+
// if (!schema) return null;
|
|
143
|
+
const defaultValues = {};
|
|
144
|
+
for (const key in schema.properties) {
|
|
145
|
+
const property = schema.properties[key] as any;
|
|
146
|
+
if (!isNil(property.default)) {
|
|
147
|
+
defaultValues[key] = property.default;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return defaultValues;
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
public createApiSchemas(api: string, apiMethod?: TypeRequestMethod, apiOptions?: IApiSchemaOptions): IOpenapiSchemas {
|
|
156
|
+
const self = this;
|
|
157
|
+
const sdk = this.getSdk(api, apiMethod, apiOptions);
|
|
158
|
+
return {
|
|
159
|
+
get sdk() {
|
|
160
|
+
return sdk;
|
|
161
|
+
},
|
|
162
|
+
get query() {
|
|
163
|
+
const operationObject = sdk.data?.operationObject;
|
|
164
|
+
return getSchemaOfRequestQuery(operationObject);
|
|
165
|
+
},
|
|
166
|
+
get filter() {
|
|
167
|
+
const operationObject = sdk.data?.operationObject;
|
|
168
|
+
return getSchemaOfRequestQueryFilter(operationObject, { where: true });
|
|
169
|
+
},
|
|
170
|
+
get requestBody() {
|
|
171
|
+
const operationObject = sdk.data?.operationObject;
|
|
172
|
+
const schemaBody = getSchemaOfRequestBody(operationObject);
|
|
173
|
+
const schemaName = cast(schemaBody)?.$ref;
|
|
174
|
+
if (!schemaName) return;
|
|
175
|
+
return self.getSchema(schemaName).data;
|
|
176
|
+
},
|
|
177
|
+
get responseBody() {
|
|
178
|
+
const operationObject = sdk.data?.operationObject;
|
|
179
|
+
const schemaBody = getSchemaOfResponseBody(operationObject);
|
|
180
|
+
const schemaName = cast(schemaBody?.properties?.data)?.$ref;
|
|
181
|
+
if (!schemaName) return;
|
|
182
|
+
return self.getSchema(schemaName).data;
|
|
183
|
+
},
|
|
184
|
+
get paged() {
|
|
185
|
+
const operationObject = sdk.data?.operationObject;
|
|
186
|
+
const schemaBody = getSchemaOfResponseBody(operationObject);
|
|
187
|
+
if (!schemaBody) return;
|
|
188
|
+
const schemaName = cast(schemaBody?.properties?.data)?.items?.$ref;
|
|
189
|
+
if (schemaName) return;
|
|
190
|
+
// pages
|
|
191
|
+
return this.responseBody;
|
|
192
|
+
},
|
|
193
|
+
get row() {
|
|
194
|
+
const operationObject = sdk.data?.operationObject;
|
|
195
|
+
const schemaBody = getSchemaOfResponseBody(operationObject);
|
|
196
|
+
if (!schemaBody) return;
|
|
197
|
+
// schemaData
|
|
198
|
+
let schemaData;
|
|
199
|
+
if (schemaBody?.properties?.data.$ref) {
|
|
200
|
+
schemaData = self.getSchema(schemaBody?.properties?.data.$ref).data;
|
|
201
|
+
} else {
|
|
202
|
+
schemaData = schemaBody?.properties?.data;
|
|
203
|
+
}
|
|
204
|
+
// entry
|
|
205
|
+
const schemaName = cast(schemaData)?.items?.$ref;
|
|
206
|
+
if (schemaName) {
|
|
207
|
+
return self.getSchema(schemaName).data;
|
|
208
|
+
}
|
|
209
|
+
// pages
|
|
210
|
+
const schemaRow = cast(schemaData?.properties?.list)?.items;
|
|
211
|
+
if (schemaRow?.$ref) {
|
|
212
|
+
return self.getSchema(schemaRow?.$ref).data;
|
|
213
|
+
}
|
|
214
|
+
return schemaRow;
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
public loadSchemaProperties(schema: SchemaObject | undefined, scene?: TypeSchemaScene): SchemaObject[] | undefined {
|
|
220
|
+
return loadSchemaProperties(schema, schemaName => this.getSchema(schemaName).data, scene);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
public schemaToZodSchema<T extends z.ZodType = z.ZodType>(schema: SchemaObject): T {
|
|
224
|
+
return schemaToZodSchema(schema, schemaName => this.getSchema(schemaName).data);
|
|
225
|
+
}
|
|
226
|
+
}
|