feat: update adapter to use globalThis for origin resolution fix: remove unused ClientQuery export from query.ts chore: update tsconfig to include test files and set rootDir feat: add create-query functionality for dynamic API generation feat: implement QueryApi with enhanced type inference from JSON Schema test: add comprehensive API tests for QueryApi functionality test: create demo routes and schemas for testing purposes docs: add type inference demo for QueryApi usage
68 lines
1.1 KiB
TypeScript
68 lines
1.1 KiB
TypeScript
import { QueryApi } from "../src/query-api";
|
||
|
||
const api = new QueryApi();
|
||
|
||
// 示例 1: args 的 value 是 string 类型
|
||
api.post(
|
||
{
|
||
path: "/users",
|
||
metadata: {
|
||
args: {
|
||
name: "John",
|
||
email: "john@example.com"
|
||
}
|
||
}
|
||
},
|
||
{
|
||
pos: "someValue", // ✅ TypeScript 会推断 pos 应该是 string 类型
|
||
other: "data"
|
||
}
|
||
);
|
||
|
||
// 示例 2: args 的 value 是 number 类型
|
||
api.post(
|
||
{
|
||
path: "/products",
|
||
metadata: {
|
||
args: {
|
||
id: 123,
|
||
price: 99.99
|
||
}
|
||
}
|
||
},
|
||
{
|
||
pos: 456, // ✅ TypeScript 会推断 pos 应该是 number 类型
|
||
other: "data"
|
||
}
|
||
);
|
||
|
||
// 示例 3: args 的 value 是混合类型
|
||
api.post(
|
||
{
|
||
path: "/orders",
|
||
metadata: {
|
||
args: {
|
||
orderId: 123,
|
||
status: "pending",
|
||
total: 99.99
|
||
}
|
||
}
|
||
},
|
||
{
|
||
pos: "status", // ✅ TypeScript 会推断 pos 可以是 string 或 number
|
||
// pos: 999, // 这也是合法的
|
||
other: "data"
|
||
}
|
||
);
|
||
|
||
// 示例 4: 没有 metadata 或 args
|
||
api.post(
|
||
{
|
||
path: "/test"
|
||
},
|
||
{
|
||
pos: undefined, // ✅ pos 类型为 never,只能是 undefined
|
||
other: "data"
|
||
}
|
||
);
|