chore: bump version to 0.0.50, update dependencies, and enhance createQueryByRoutes with options
This commit is contained in:
@@ -22,8 +22,14 @@ type RouteInfo = {
|
||||
}
|
||||
};
|
||||
}
|
||||
const removeViewItemFromRoutes = (list: RouteInfo[]) => {
|
||||
const removeViewItemCotnextFromRoutes = (list: RouteInfo[], options?: CreateQueryOptions) => {
|
||||
for (const route of list) {
|
||||
if (options?.removeViewItem) {
|
||||
if (route.metadata?.viewItem) {
|
||||
delete route.metadata.viewItem;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (route.metadata?.viewItem) {
|
||||
if (route.metadata.viewItem?.api?.query) {
|
||||
delete route.metadata.viewItem.api.query;
|
||||
@@ -38,9 +44,14 @@ const removeViewItemFromRoutes = (list: RouteInfo[]) => {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
export const createQueryByRoutes = (list: RouteInfo[]) => {
|
||||
type CreateQueryOptions = {
|
||||
removeViewItem?: boolean,
|
||||
before?: string,
|
||||
after?: string,
|
||||
}
|
||||
export const createQueryByRoutes = (list: RouteInfo[], options?: CreateQueryOptions) => {
|
||||
const obj: any = {};
|
||||
list = removeViewItemFromRoutes(list);
|
||||
list = removeViewItemCotnextFromRoutes(list, options);
|
||||
for (const route of list) {
|
||||
if (!obj[route.path]) {
|
||||
obj[route.path] = {};
|
||||
@@ -55,11 +66,12 @@ export const createQueryByRoutes = (list: RouteInfo[]) => {
|
||||
}
|
||||
obj[route.path][route.key] = route;
|
||||
}
|
||||
const code = `
|
||||
import { createQueryApi } from '@kevisual/query/api';
|
||||
const before = options?.before || `import { createQueryApi } from '@kevisual/query/api';`;
|
||||
const after = options?.after || `export { queryApi };`;
|
||||
const code = `${before}
|
||||
const api = ${generateApiCode(obj)} as const;
|
||||
const queryApi = createQueryApi({ api });
|
||||
export { queryApi };
|
||||
${after}
|
||||
`
|
||||
return code;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DataOpts, Query } from "./query.ts";
|
||||
import { DataOpts, Query, Result } from "./query.ts";
|
||||
import { z } from "zod";
|
||||
import { createQueryByRoutes } from "./create-query/index.ts";
|
||||
import { pick } from 'es-toolkit'
|
||||
@@ -71,22 +71,23 @@ type IsOptional<T> = T extends { optional: true } ? true : false;
|
||||
// 根据 optional 字段分离必需字段和可选字段
|
||||
type ExtractArgsFromMetadata<T> = T extends { metadata?: { args?: infer A } }
|
||||
? A extends Record<string, any>
|
||||
? (
|
||||
// 必需字段(没有 optional: true)
|
||||
{ [K in keyof A as IsOptional<A[K]> extends true ? never : K]: InferType<A[K]> } &
|
||||
// 可选字段(有 optional: true)
|
||||
{ [K in keyof A as IsOptional<A[K]> extends true ? K : never]?: InferType<A[K]> }
|
||||
)
|
||||
: never
|
||||
? (
|
||||
// 必需字段(没有 optional: true)
|
||||
{ [K in keyof A as IsOptional<A[K]> extends true ? never : K]: InferType<A[K]> } &
|
||||
// 可选字段(有 optional: true)
|
||||
{ [K in keyof A as IsOptional<A[K]> extends true ? K : never]?: InferType<A[K]> }
|
||||
)
|
||||
: never
|
||||
: never;
|
||||
|
||||
|
||||
// 类型映射:将 API 配置转换为方法签名
|
||||
type ApiMethods<P extends { [path: string]: { [key: string]: Pos } }> = {
|
||||
[Path in keyof P]: {
|
||||
[Key in keyof P[Path]]: (
|
||||
[Key in keyof P[Path]]: <T = any>(
|
||||
data?: ExtractArgsFromMetadata<P[Path][Key]>,
|
||||
opts?: DataOpts
|
||||
) => ReturnType<Query['post']>
|
||||
) => Promise<Result<T>>
|
||||
}
|
||||
}
|
||||
type QueryApiOpts<P extends { [path: string]: { [key: string]: Pos } } = {}> = {
|
||||
|
||||
Reference in New Issue
Block a user