Forráskód Böngészése

♻️ refactor(crud): add generic type parameters to crud options and routes

- 为CrudOptions接口添加泛型类型参数CreateSchema、UpdateSchema、GetSchema和ListSchema
- 修改createCrudRoutes函数定义,使其支持泛型模式参数
- 将schema属性类型从z.ZodSchema改为对应的泛型参数,提高类型安全性和代码提示能力
yourname 5 hónapja
szülő
commit
659475f5fe

+ 7 - 1
src/server/utils/generic-crud.routes.ts

@@ -6,7 +6,13 @@ import { AuthContext } from '../types/context';
 import { ObjectLiteral } from 'typeorm';
 import { AppDataSource } from '../data-source';
 
-export function createCrudRoutes<T extends ObjectLiteral>(options: CrudOptions<T>) {
+export function createCrudRoutes<
+  T extends ObjectLiteral,
+  CreateSchema extends z.ZodSchema = z.ZodSchema,
+  UpdateSchema extends z.ZodSchema = z.ZodSchema,
+  GetSchema extends z.ZodSchema = z.ZodSchema,
+  ListSchema extends z.ZodSchema = z.ZodSchema
+>(options: CrudOptions<T, CreateSchema, UpdateSchema, GetSchema, ListSchema>) {
   const { entity, createSchema, updateSchema, getSchema, listSchema, searchFields, middleware = [] } = options;
   
   // 创建CRUD服务实例

+ 11 - 5
src/server/utils/generic-crud.service.ts

@@ -99,12 +99,18 @@ export abstract class GenericCrudService<T extends ObjectLiteral> {
   }
 }
 
-export type CrudOptions<T extends ObjectLiteral> = {
+export type CrudOptions<
+  T extends ObjectLiteral,
+  CreateSchema extends z.ZodSchema = z.ZodSchema,
+  UpdateSchema extends z.ZodSchema = z.ZodSchema,
+  GetSchema extends z.ZodSchema = z.ZodSchema,
+  ListSchema extends z.ZodSchema = z.ZodSchema
+> = {
   entity: new () => T;
-  createSchema: z.ZodSchema;
-  updateSchema: z.ZodSchema;
-  getSchema: z.ZodSchema;
-  listSchema: z.ZodSchema;
+  createSchema: CreateSchema;
+  updateSchema: UpdateSchema;
+  getSchema: GetSchema;
+  listSchema: ListSchema;
   searchFields?: string[];
   middleware?: any[];
 };