Răsfoiți Sursa

♻️ refactor(files): 修改fullUrl为异步属性

- 将File实体的fullUrl同步getter修改为返回Promise的异步getter
- 注释掉原同步实现代码,保留作为参考
- 调整FileSchema,将fullUrl类型从string更新为Promise<string>

♻️ refactor(crud): 优化通用CRUD路由的数据验证

- 将z.parse替换为z.parseAsync以支持异步schema验证
- 统一Zod错误处理,使用error.message替代error.errors
- 涉及列表、创建、获取、更新和删除等所有CRUD操作路由
yourname 4 luni în urmă
părinte
comite
d5fbb9acd3

+ 16 - 6
src/server/modules/files/file.entity.ts

@@ -20,12 +20,22 @@ export class File {
   path!: string;
 
   // 获取完整的文件URL(包含MINIO_HOST前缀)
-  get fullUrl(): string {
-    const protocol = process.env.MINIO_USE_SSL !== 'false' ? 'https' : 'http';
-    const port = process.env.MINIO_PORT ? `:${process.env.MINIO_PORT}` : '';
-    const host = process.env.MINIO_HOST || 'localhost';
-    const bucketName = process.env.MINIO_BUCKET_NAME || 'd8dai';
-    return `${protocol}://${host}${port}/${bucketName}/${this.path}`;
+  // get fullUrl(): string {
+  //   const protocol = process.env.MINIO_USE_SSL !== 'false' ? 'https' : 'http';
+  //   const port = process.env.MINIO_PORT ? `:${process.env.MINIO_PORT}` : '';
+  //   const host = process.env.MINIO_HOST || 'localhost';
+  //   const bucketName = process.env.MINIO_BUCKET_NAME || 'd8dai';
+  //   return `${protocol}://${host}${port}/${bucketName}/${this.path}`;
+  // }
+  get fullUrl(): Promise<string> {
+    return new Promise((resolve) => {
+      // 这里可以执行异步操作
+      const protocol = process.env.MINIO_USE_SSL !== 'false' ? 'https' : 'http';
+      const port = process.env.MINIO_PORT ? `:${process.env.MINIO_PORT}` : '';
+      const host = process.env.MINIO_HOST || 'localhost';
+      const bucketName = process.env.MINIO_BUCKET_NAME || 'd8dai';
+      resolve(`${protocol}://${host}${port}/${bucketName}/${this.path}`);
+    });
   }
 
   @Column({ name: 'description', type: 'text', nullable: true, comment: '文件描述' })

+ 1 - 1
src/server/modules/files/file.schema.ts

@@ -22,7 +22,7 @@ export const FileSchema = z.object({
     description: '文件存储路径',
     example: '/uploads/documents/2023/project-plan.pdf'
   }),
-  fullUrl: z.string().url().openapi({
+  fullUrl: z.promise(z.url()).openapi({
     description: '完整文件访问URL',
     example: 'https://minio.example.com/d8dai/uploads/documents/2023/project-plan.pdf'
   }),

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

@@ -253,12 +253,12 @@ export function createCrudRoutes<
         );
         
         return c.json({
-          data: z.array(listSchema).parse(data),
+          data: await z.array(listSchema).parseAsync(data),
           pagination: { total, current: page, pageSize }
         }, 200);
       } catch (error) {
         if (error instanceof z.ZodError) {
-          return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
+          return c.json({ code: 400, message: '参数验证失败', errors: error.message }, 400);
         }
         return c.json({
           code: 500,
@@ -274,7 +274,7 @@ export function createCrudRoutes<
         return c.json(result, 201);
       } catch (error) {
         if (error instanceof z.ZodError) {
-          return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
+          return c.json({ code: 400, message: '参数验证失败', errors: error.message }, 400);
         }
         return c.json({
           code: 500,
@@ -291,10 +291,10 @@ export function createCrudRoutes<
           return c.json({ code: 404, message: '资源不存在' }, 404);
         }
         
-        return c.json(getSchema.parse(result), 200);
+        return c.json(await getSchema.parseAsync(result), 200);
       } catch (error) {
         if (error instanceof z.ZodError) {
-          return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
+          return c.json({ code: 400, message: '参数验证失败', errors: error.message }, 400);
         }
         return c.json({
           code: 500,
@@ -316,7 +316,7 @@ export function createCrudRoutes<
         return c.json(result, 200);
       } catch (error) {
         if (error instanceof z.ZodError) {
-          return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
+          return c.json({ code: 400, message: '参数验证失败', errors: error.message }, 400);
         }
         return c.json({
           code: 500,
@@ -336,7 +336,7 @@ export function createCrudRoutes<
         return c.body(null, 204);
       } catch (error) {
         if (error instanceof z.ZodError) {
-          return c.json({ code: 400, message: '参数验证失败', errors: error.errors }, 400);
+          return c.json({ code: 400, message: '参数验证失败', errors: error.message }, 400);
         }
         return c.json({
           code: 500,