chore: bump version to 0.0.46 and enhance type inference for JSON Schema required fields
This commit is contained in:
222
test/examples.ts
222
test/examples.ts
@@ -38,15 +38,229 @@ const api = {
|
||||
"url": "/api/router",
|
||||
"source": "query-proxy-api"
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 获取域名列表,支持分页
|
||||
*
|
||||
* @param data - Request parameters
|
||||
* @param data.data - {object}
|
||||
*/
|
||||
"list": {
|
||||
"path": "app_domain_manager",
|
||||
"key": "list",
|
||||
"description": "获取域名列表,支持分页",
|
||||
"metadata": {
|
||||
"args": {
|
||||
"data": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"page": {
|
||||
"type": "number"
|
||||
},
|
||||
"pageSize": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"viewItem": {
|
||||
"api": {
|
||||
"url": "/api/router"
|
||||
},
|
||||
"type": "api",
|
||||
"title": "路由"
|
||||
},
|
||||
"url": "/api/router",
|
||||
"source": "query-proxy-api"
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 更新一个域名的信息
|
||||
*
|
||||
* @param data - Request parameters
|
||||
* @param data.data - {object}
|
||||
*/
|
||||
"update": {
|
||||
"path": "app_domain_manager",
|
||||
"key": "update",
|
||||
"description": "更新一个域名的信息",
|
||||
"metadata": {
|
||||
"args": {
|
||||
"data": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"domain": {
|
||||
"type": "string"
|
||||
},
|
||||
"appId": {
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"active",
|
||||
"inactive"
|
||||
]
|
||||
},
|
||||
"data": {
|
||||
"type": "object",
|
||||
"propertyNames": {
|
||||
"type": "string"
|
||||
},
|
||||
"additionalProperties": {}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"domain"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"viewItem": {
|
||||
"api": {
|
||||
"url": "/api/router"
|
||||
},
|
||||
"type": "api",
|
||||
"title": "路由"
|
||||
},
|
||||
"url": "/api/router",
|
||||
"source": "query-proxy-api"
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 删除一个域名
|
||||
*
|
||||
* @param data - Request parameters
|
||||
* @param data.data - {object}
|
||||
*/
|
||||
"delete": {
|
||||
"path": "app_domain_manager",
|
||||
"key": "delete",
|
||||
"description": "删除一个域名",
|
||||
"metadata": {
|
||||
"args": {
|
||||
"data": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string"
|
||||
},
|
||||
"domain": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
},
|
||||
"viewItem": {
|
||||
"api": {
|
||||
"url": "/api/router"
|
||||
},
|
||||
"type": "api",
|
||||
"title": "路由"
|
||||
},
|
||||
"url": "/api/router",
|
||||
"source": "query-proxy-api"
|
||||
}
|
||||
}
|
||||
},
|
||||
"app": {
|
||||
"getDomainApp": {
|
||||
"path": "app",
|
||||
"key": "getDomainApp",
|
||||
"metadata": {
|
||||
"viewItem": {
|
||||
"api": {
|
||||
"url": "/api/router"
|
||||
},
|
||||
"type": "api",
|
||||
"title": "路由"
|
||||
},
|
||||
"url": "/api/router",
|
||||
"source": "query-proxy-api"
|
||||
}
|
||||
}
|
||||
},
|
||||
"app-domain": {
|
||||
"create": {
|
||||
"path": "app-domain",
|
||||
"key": "create",
|
||||
"metadata": {
|
||||
"viewItem": {
|
||||
"api": {
|
||||
"url": "/api/router"
|
||||
},
|
||||
"type": "api",
|
||||
"title": "路由"
|
||||
},
|
||||
"url": "/api/router",
|
||||
"source": "query-proxy-api"
|
||||
}
|
||||
},
|
||||
"update": {
|
||||
"path": "app-domain",
|
||||
"key": "update",
|
||||
"metadata": {
|
||||
"viewItem": {
|
||||
"api": {
|
||||
"url": "/api/router"
|
||||
},
|
||||
"type": "api",
|
||||
"title": "路由"
|
||||
},
|
||||
"url": "/api/router",
|
||||
"source": "query-proxy-api"
|
||||
}
|
||||
}
|
||||
}
|
||||
} as const;
|
||||
const queryApi = createQueryApi({ api });
|
||||
export { queryApi };
|
||||
|
||||
queryApi.app_domain_manager.get({
|
||||
// ===== 类型推断示例 =====
|
||||
|
||||
// ✅ 示例 1:只传必需字段 domain
|
||||
queryApi.app_domain_manager.update({
|
||||
data: {
|
||||
id: '123',
|
||||
domain: 'example.com'
|
||||
domain: "example.com"
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// ✅ 示例 2:传必需字段 + 可选字段
|
||||
queryApi.app_domain_manager.update({
|
||||
data: {
|
||||
domain: "example.com",
|
||||
id: "123",
|
||||
appId: "app-001",
|
||||
status: "active", // enum 类型,只能是 'active' 或 'inactive'
|
||||
data: { custom: "metadata" }
|
||||
}
|
||||
});
|
||||
|
||||
// ✅ 示例 3:list 方法没有 required 字段,所有参数都是可选的
|
||||
queryApi.app_domain_manager.list({
|
||||
data: {
|
||||
page: 1,
|
||||
pageSize: 10
|
||||
}
|
||||
});
|
||||
|
||||
// ✅ 示例 4:list 方法可以不传参数
|
||||
queryApi.app_domain_manager.list({
|
||||
data: {}
|
||||
});
|
||||
|
||||
// 检查类型推断
|
||||
type UpdateParams = Parameters<typeof queryApi.app_domain_manager.update>[0];
|
||||
// 推断为: { data: { domain: string } & { id?: string, appId?: string, status?: "active" | "inactive", data?: Record<string, any> } }
|
||||
|
||||
type ListParams = Parameters<typeof queryApi.app_domain_manager.list>[0];
|
||||
// 推断为: { data: { page?: number, pageSize?: number } }
|
||||
|
||||
|
||||
@@ -161,4 +161,115 @@ queryApi4.order.updateStatus({
|
||||
// });
|
||||
|
||||
|
||||
// ============ 示例 5: Object 类型 - 带 required 字段 ============
|
||||
const api5 = {
|
||||
"domain_manager": {
|
||||
"update": {
|
||||
"path": "domain_manager",
|
||||
"key": "update",
|
||||
"metadata": {
|
||||
"args": {
|
||||
"data": {
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"id": { "type": "string" },
|
||||
"domain": { "type": "string" },
|
||||
"appId": { "type": "string" },
|
||||
"status": {
|
||||
"type": "string",
|
||||
"enum": ["active", "inactive"] as const
|
||||
},
|
||||
"config": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"ssl": { "type": "boolean" },
|
||||
"cdn": { "type": "boolean" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["domain"] as const, // 只有 domain 是必需的
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"list": {
|
||||
"path": "domain_manager",
|
||||
"key": "list",
|
||||
"metadata": {
|
||||
"args": {
|
||||
"data": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"page": { "type": "number" },
|
||||
"pageSize": { "type": "number" },
|
||||
"status": {
|
||||
"type": "string",
|
||||
"enum": ["active", "inactive"] as const
|
||||
}
|
||||
}
|
||||
// 没有 required 字段,所以所有字段都是可选的
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} as const;
|
||||
|
||||
const queryApi5 = createQueryApi({ api: api5 });
|
||||
|
||||
// ✅ 示例 5.1: 只传必需字段 domain
|
||||
queryApi5.domain_manager.update({
|
||||
data: {
|
||||
domain: "example.com" // domain 是必需的
|
||||
}
|
||||
});
|
||||
|
||||
// ✅ 示例 5.2: 传必需字段 + 可选字段
|
||||
queryApi5.domain_manager.update({
|
||||
data: {
|
||||
domain: "example.com", // 必需
|
||||
id: "123", // 可选
|
||||
appId: "app-001", // 可选
|
||||
status: "active", // 可选,且只能是 "active" | "inactive"
|
||||
config: { // 可选对象
|
||||
ssl: true,
|
||||
cdn: false
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// ❌ 错误:缺少必需字段 domain
|
||||
// queryApi5.domain_manager.update({
|
||||
// data: {
|
||||
// id: "123" // 错误:缺少必需字段 "domain"
|
||||
// }
|
||||
// });
|
||||
|
||||
// ✅ 示例 5.3: list 方法所有字段都是可选的
|
||||
queryApi5.domain_manager.list({
|
||||
data: {
|
||||
page: 1,
|
||||
pageSize: 10
|
||||
}
|
||||
});
|
||||
|
||||
// ✅ 示例 5.4: list 方法可以传空对象
|
||||
queryApi5.domain_manager.list({
|
||||
data: {}
|
||||
});
|
||||
|
||||
// ✅ 示例 5.5: list 方法甚至可以不传参数
|
||||
queryApi5.domain_manager.list();
|
||||
|
||||
|
||||
// ============ 类型检查 ============
|
||||
type UpdateParams = Parameters<typeof queryApi5.domain_manager.update>[0];
|
||||
// 推断为: { data: { domain: string } & { id?: string, appId?: string, status?: "active" | "inactive", config?: {...} } }
|
||||
|
||||
type ListParams = Parameters<typeof queryApi5.domain_manager.list>[0];
|
||||
// 推断为: { query?: { page?: number, pageSize?: number, status?: "active" | "inactive" } }
|
||||
|
||||
|
||||
console.log('✅ 所有类型推断示例运行成功!');
|
||||
|
||||
Reference in New Issue
Block a user