@kevisual/query 0.0.47 → 0.0.48
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/query-api.d.ts +10 -5
- package/dist/query-api.js +13757 -0
- package/package.json +2 -2
- package/src/create-query/index.ts +9 -0
- package/src/query-api.ts +14 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kevisual/query",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.48",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "npm run clean && bun run bun.config.ts",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"description": "",
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@kevisual/code-builder": "^0.0.6",
|
|
22
|
-
"@kevisual/router": "^0.0.
|
|
22
|
+
"@kevisual/router": "^0.0.80",
|
|
23
23
|
"@types/node": "^25.2.3",
|
|
24
24
|
"typescript": "^5.9.3",
|
|
25
25
|
"es-toolkit": "^1.44.0",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
|
|
2
|
+
import { toJSONSchema, fromJSONSchema } from '@kevisual/router/browser'
|
|
2
3
|
type RouteInfo = {
|
|
3
4
|
path: string;
|
|
4
5
|
key: string;
|
|
@@ -44,6 +45,14 @@ export const createQueryByRoutes = (list: RouteInfo[]) => {
|
|
|
44
45
|
if (!obj[route.path]) {
|
|
45
46
|
obj[route.path] = {};
|
|
46
47
|
}
|
|
48
|
+
if (route.metadata?.args) {
|
|
49
|
+
const args = route.metadata.args;
|
|
50
|
+
if (args?.$schema) {
|
|
51
|
+
// 将 args 转换为 JSON Schema
|
|
52
|
+
const jsonSchema = fromJSONSchema(args);
|
|
53
|
+
route.metadata.args = toJSONSchema(jsonSchema);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
47
56
|
obj[route.path][route.key] = route;
|
|
48
57
|
}
|
|
49
58
|
const code = `
|
package/src/query-api.ts
CHANGED
|
@@ -64,18 +64,27 @@ type InferType<T> =
|
|
|
64
64
|
T extends { properties: infer P } ? InferFromJSONSchema<T> : // 处理没有 type 但有 properties 的对象
|
|
65
65
|
T;
|
|
66
66
|
|
|
67
|
+
// 检查是否标记为可选
|
|
68
|
+
type IsOptional<T> = T extends { optional: true } ? true : false;
|
|
69
|
+
|
|
67
70
|
// 提取 args 对象,将每个 Zod schema 或 JSON Schema 转换为实际类型
|
|
71
|
+
// 根据 optional 字段分离必需字段和可选字段
|
|
68
72
|
type ExtractArgsFromMetadata<T> = T extends { metadata?: { args?: infer A } }
|
|
69
73
|
? A extends Record<string, any>
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
? (
|
|
75
|
+
// 必需字段(没有 optional: true)
|
|
76
|
+
{ [K in keyof A as IsOptional<A[K]> extends true ? never : K]: InferType<A[K]> } &
|
|
77
|
+
// 可选字段(有 optional: true)
|
|
78
|
+
{ [K in keyof A as IsOptional<A[K]> extends true ? K : never]?: InferType<A[K]> }
|
|
79
|
+
)
|
|
80
|
+
: never
|
|
72
81
|
: never;
|
|
73
82
|
|
|
74
83
|
// 类型映射:将 API 配置转换为方法签名
|
|
75
84
|
type ApiMethods<P extends { [path: string]: { [key: string]: Pos } }> = {
|
|
76
85
|
[Path in keyof P]: {
|
|
77
86
|
[Key in keyof P[Path]]: (
|
|
78
|
-
data?:
|
|
87
|
+
data?: ExtractArgsFromMetadata<P[Path][Key]>,
|
|
79
88
|
opts?: DataOpts
|
|
80
89
|
) => ReturnType<Query['post']>
|
|
81
90
|
}
|
|
@@ -97,7 +106,7 @@ export class QueryApi<P extends { [path: string]: { [key: string]: Pos } } = {}>
|
|
|
97
106
|
// 使用泛型来推断类型
|
|
98
107
|
post<T extends Pos>(
|
|
99
108
|
pos: T,
|
|
100
|
-
data?:
|
|
109
|
+
data?: ExtractArgsFromMetadata<T>,
|
|
101
110
|
opts?: DataOpts
|
|
102
111
|
) {
|
|
103
112
|
const _pos = pick(pos, ['path', 'key', 'id']);
|
|
@@ -121,7 +130,7 @@ export class QueryApi<P extends { [path: string]: { [key: string]: Pos } } = {}>
|
|
|
121
130
|
}
|
|
122
131
|
|
|
123
132
|
for (const [key, pos] of Object.entries(methods)) {
|
|
124
|
-
that[path][key] = (data?:
|
|
133
|
+
that[path][key] = (data?: ExtractArgsFromMetadata<typeof pos>, opts: DataOpts = {}) => {
|
|
125
134
|
const _pos = pick(pos, ['path', 'key', 'id']);
|
|
126
135
|
if (pos.metadata?.viewItem?.api?.url && !opts.url) {
|
|
127
136
|
opts.url = pos.metadata.viewItem.api.url;
|