imean-service-engine-htmx-plugin 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -82,24 +82,31 @@ interface FieldMetadata {
82
82
  searchable?: boolean;
83
83
  /** 是否可排序 */
84
84
  sortable?: boolean;
85
+ /** 是否可用于筛选(默认 true,设置为 false 时不会出现在筛选表单中) */
86
+ filterable?: boolean;
85
87
  /** 自定义渲染函数 */
86
88
  render?: (value: any, item: any) => any;
87
89
  }
88
90
  /**
89
- * Model Schema 接口
90
- * 类型通过 schema z.infer 推断,无需显式传递泛型
91
- */
92
- interface ModelSchema {
93
- /** Zod Schema(用于数据验证和类型推断) */
94
- schema: z.ZodObject<any>;
95
- /** 字段元数据(可选,用于自定义字段标签、描述等) */
96
- fields?: Record<string, FieldMetadata>;
97
- /** 列表显示字段(可选,用于列表页显示哪些字段) */
98
- listFields?: string[];
99
- /** 详情显示字段(可选,用于详情页显示哪些字段) */
100
- detailFields?: string[];
101
- /** 表单字段(可选,用于表单页显示哪些字段,默认排除 id、createdAt、updatedAt) */
102
- formFields?: string[];
91
+ * 模型字段信息
92
+ * Zod Schema 一次性解析出的字段元数据
93
+ */
94
+ interface ModelField {
95
+ /** 字段名 */
96
+ name: string;
97
+ /** 字段标签(从 schema description 提取) */
98
+ label: string;
99
+ /** 字段类型 */
100
+ type: FormField["type"];
101
+ /** 是否必填 */
102
+ required: boolean;
103
+ /** 选项(用于 select 类型) */
104
+ options?: Array<{
105
+ value: string | number;
106
+ label: string;
107
+ }>;
108
+ /** 原始 Zod Schema(用于验证等) */
109
+ schema: any;
103
110
  }
104
111
  /**
105
112
  * Feature 类型
@@ -119,7 +126,7 @@ interface Notification {
119
126
  }
120
127
  /**
121
128
  * Feature 上下文
122
- * 类型通过 model.modelSchema.schema 的 z.infer 推断
129
+ * 类型通过 model.schema 的 z.infer 推断
123
130
  */
124
131
  interface FeatureContext {
125
132
  /** PageModel 实例 */
@@ -167,7 +174,7 @@ interface FeatureContext {
167
174
  }
168
175
  /**
169
176
  * Feature 接口
170
- * 类型通过 context.model.modelSchema.schema 的 z.infer 推断
177
+ * 类型通过 context.model.schema 的 z.infer 推断
171
178
  */
172
179
  interface Feature {
173
180
  /** Feature 名称 */
@@ -313,7 +320,11 @@ interface FormField {
313
320
 
314
321
  /**
315
322
  * Feature 注册表
316
- * 类型通过 model.modelSchema.schema 的 z.infer 推断
323
+ */
324
+
325
+ /**
326
+ * Feature 注册表
327
+ * 类型通过 model.schema 的 z.infer 推断
317
328
  */
318
329
  declare class FeatureRegistry {
319
330
  private features;
@@ -347,70 +358,6 @@ declare class FeatureRegistry {
347
358
  * 快捷方法:注册自定义 Feature
348
359
  */
349
360
  custom(name: string, feature: Feature): void;
350
- /**
351
- * 一键启用所有 CRUD Feature
352
- * 类型通过 model.modelSchema.schema 的 z.infer 推断
353
- */
354
- crud<T extends {
355
- id: string | number;
356
- } = any>(options: {
357
- /** 权限前缀 */
358
- permissionPrefix: string;
359
- /** 获取列表数据的函数 */
360
- getList: (params: ListParams) => Promise<ListResult<T>>;
361
- /** 获取单条数据的函数 */
362
- getItem: (id: string | number) => Promise<T | null>;
363
- /** 创建数据的函数 */
364
- createItem: (data: Partial<T>) => Promise<T>;
365
- /** 更新数据的函数 */
366
- updateItem: (id: string | number, data: Partial<T>) => Promise<T | null>;
367
- /** 删除数据的函数(可选) */
368
- deleteItem?: (id: string | number) => Promise<boolean>;
369
- /** 自定义权限(可选) */
370
- permissions?: {
371
- list?: string;
372
- read?: string;
373
- create?: string;
374
- edit?: string;
375
- delete?: string;
376
- };
377
- /** 是否启用某些 Feature(可选) */
378
- features?: {
379
- list?: boolean;
380
- detail?: boolean;
381
- create?: boolean;
382
- edit?: boolean;
383
- delete?: boolean;
384
- };
385
- /** 弹窗大小配置(可选) */
386
- dialogSizes?: {
387
- list?: DialogSize;
388
- detail?: DialogSize;
389
- create?: DialogSize;
390
- edit?: DialogSize;
391
- delete?: DialogSize;
392
- };
393
- /** 是否允许点击遮罩关闭配置(可选) */
394
- closeOnBackdropClick?: {
395
- list?: boolean;
396
- detail?: boolean;
397
- create?: boolean;
398
- edit?: boolean;
399
- delete?: boolean;
400
- };
401
- /** 动态标题配置(可选) */
402
- getTitles?: {
403
- detail?: (item: T, context: FeatureContext) => string | Promise<string>;
404
- edit?: (item: T, context: FeatureContext) => string | Promise<string>;
405
- create?: (context: FeatureContext) => string | Promise<string>;
406
- };
407
- /** 动态描述配置(可选) */
408
- getDescriptions?: {
409
- detail?: (item: T, context: FeatureContext) => string | Promise<string> | undefined;
410
- edit?: (item: T, context: FeatureContext) => string | Promise<string> | undefined;
411
- create?: (context: FeatureContext) => string | Promise<string> | undefined;
412
- };
413
- }): void;
414
361
  /**
415
362
  * 获取所有注册的 Feature
416
363
  */
@@ -428,81 +375,24 @@ declare class FeatureRegistry {
428
375
 
429
376
  /**
430
377
  * PageModel 基类
431
- * 类型通过 modelSchema.schema z.infer 推断,无需显式传递泛型
378
+ * 不再包含 schema,schema 由各个 Feature 指定
432
379
  */
433
380
  declare abstract class PageModel {
434
381
  readonly modelName: string;
435
382
  readonly features: FeatureRegistry;
436
- protected modelSchema?: ModelSchema;
437
383
  private metadata;
438
384
  /**
439
385
  * 构造函数
440
386
  * @param modelName 模型/页面名称(用于路由和权限)
441
- * @param schema 模型 Schema(可选,不提供则为普通页面)
442
387
  * @param metadata 页面元数据(可选,如果提供则不需要实现 getMetadata)
443
388
  */
444
- constructor(modelName: string, schema?: ModelSchema, metadata?: PageMetadata);
445
- /**
446
- * 获取推断的类型(如果有 schema)
447
- * 使用方式:type ModelType = z.infer<typeof pageModel.getSchema()!.schema>
448
- */
449
- getInferredType(): z.ZodObject<any> | undefined;
389
+ constructor(modelName: string, metadata?: PageMetadata);
450
390
  /**
451
391
  * 获取页面元数据
452
392
  * 如果构造函数提供了 metadata,直接返回
453
393
  * 否则调用子类实现
454
394
  */
455
395
  getMetadata(): PageMetadata;
456
- /**
457
- * 判断是否有数据模型(有 Schema)
458
- */
459
- hasModel(): boolean;
460
- /**
461
- * 获取模型 Schema(可选)
462
- */
463
- getSchema?(): ModelSchema | undefined;
464
- /**
465
- * 验证数据(仅用于有数据模型的场景)
466
- */
467
- validate(data: unknown): {
468
- success: true;
469
- data: any;
470
- } | {
471
- success: false;
472
- error: string;
473
- };
474
- /**
475
- * 获取表单字段(仅用于有数据模型的场景)
476
- */
477
- getFormFields(): FormField[];
478
- /**
479
- * 创建表单字段
480
- */
481
- private createFormField;
482
- /**
483
- * 从 Zod enum schema 中提取选项
484
- */
485
- private extractEnumOptions;
486
- /**
487
- * 判断字段是否为必填
488
- */
489
- private isRequiredField;
490
- /**
491
- * 推断字段类型
492
- */
493
- private inferFieldType;
494
- /**
495
- * 获取列表字段(仅用于有数据模型的场景)
496
- */
497
- getListFields(): string[];
498
- /**
499
- * 获取详情字段(仅用于有数据模型的场景)
500
- */
501
- getDetailFields(): string[];
502
- /**
503
- * 获取字段标签
504
- */
505
- getFieldLabel(fieldName: string): string;
506
396
  }
507
397
 
508
398
  /**
@@ -525,6 +415,10 @@ declare abstract class BaseFeature implements Feature {
525
415
  dialogSize?: DialogSize;
526
416
  /** 是否允许点击遮罩区域关闭弹窗(默认 true,设置为 false 时只能通过关闭按钮关闭) */
527
417
  closeOnBackdropClick?: boolean;
418
+ /** Schema(可选,由子类在构造函数中设置) */
419
+ protected schema?: z.ZodObject<any>;
420
+ /** 解析后的字段列表(可选,由子类在构造函数中设置) */
421
+ protected fields?: ModelField[];
528
422
  constructor(options: {
529
423
  name: string;
530
424
  type: FeatureType;
@@ -638,6 +532,7 @@ declare abstract class BaseFormFeature<T extends {
638
532
  * 处理请求
639
533
  */
640
534
  handle(context: FeatureContext): Promise<any>;
535
+ protected formFieldNames?: string[];
641
536
  /**
642
537
  * 渲染表单页面
643
538
  */
@@ -694,13 +589,15 @@ declare class CustomFeature extends BaseFeature {
694
589
 
695
590
  /**
696
591
  * 默认创建 Feature
697
- * 类型通过 context.model.modelSchema.schema 的 z.infer 推断
592
+ * 类型通过 schema 的 z.infer 推断
698
593
  */
699
594
  declare class DefaultCreateFeature<T extends {
700
595
  id: string | number;
701
596
  } = any> extends BaseFormFeature<T> {
702
597
  private createItem;
703
598
  constructor(options: {
599
+ /** 表单数据的 Schema(用于验证和字段推断) */
600
+ schema: z.ZodObject<any>;
704
601
  createItem: (data: Partial<T>) => Promise<T>;
705
602
  permissionPrefix: string;
706
603
  permission?: string;
@@ -710,6 +607,8 @@ declare class DefaultCreateFeature<T extends {
710
607
  getTitle?: (context: FeatureContext) => string | Promise<string>;
711
608
  /** 获取动态描述(可选,接收 context,返回描述字符串) */
712
609
  getDescription?: (context: FeatureContext) => string | Promise<string> | undefined;
610
+ /** 表单字段名(可选,不提供则显示 schema 中的所有字段) */
611
+ formFieldNames?: string[];
713
612
  });
714
613
  getFormAction(): "create";
715
614
  getRoutes(): ({
@@ -732,7 +631,7 @@ declare class DefaultCreateFeature<T extends {
732
631
 
733
632
  /**
734
633
  * 默认删除 Feature
735
- * 类型通过 context.model.modelSchema.schema 的 z.infer 推断
634
+ * 类型通过 context.model.schema 的 z.infer 推断
736
635
  */
737
636
  declare class DefaultDeleteFeature<T extends {
738
637
  id: string | number;
@@ -756,7 +655,7 @@ declare class DefaultDeleteFeature<T extends {
756
655
 
757
656
  /**
758
657
  * 默认详情 Feature
759
- * 类型通过 context.model.modelSchema.schema 的 z.infer 推断
658
+ * 类型通过 schema 的 z.infer 推断
760
659
  */
761
660
  declare class DefaultDetailFeature<T extends {
762
661
  id: string | number;
@@ -765,7 +664,10 @@ declare class DefaultDetailFeature<T extends {
765
664
  private deleteItem?;
766
665
  private titleGetter?;
767
666
  private descriptionGetter?;
667
+ private detailFieldNames?;
768
668
  constructor(options: {
669
+ /** 详情数据的 Schema(用于字段推断和显示) */
670
+ schema: z.ZodObject<any>;
769
671
  getItem: (id: string | number) => Promise<T | null>;
770
672
  deleteItem?: (id: string | number) => Promise<boolean>;
771
673
  permissionPrefix: string;
@@ -776,6 +678,8 @@ declare class DefaultDetailFeature<T extends {
776
678
  getTitle?: (item: T, context: FeatureContext) => string | Promise<string>;
777
679
  /** 获取动态描述(可选,接收 item 和 context,返回描述字符串) */
778
680
  getDescription?: (item: T, context: FeatureContext) => string | Promise<string> | undefined;
681
+ /** 详情显示字段名(可选,不提供则显示 schema 中的所有字段) */
682
+ detailFieldNames?: string[];
779
683
  });
780
684
  getTitle(context: FeatureContext): Promise<string>;
781
685
  getDescription(context: FeatureContext): Promise<string | undefined>;
@@ -793,7 +697,7 @@ declare class DefaultDetailFeature<T extends {
793
697
 
794
698
  /**
795
699
  * 默认编辑 Feature
796
- * 类型通过 context.model.modelSchema.schema 的 z.infer 推断
700
+ * 类型通过 schema 的 z.infer 推断
797
701
  */
798
702
  declare class DefaultEditFeature<T extends {
799
703
  id: string | number;
@@ -801,6 +705,8 @@ declare class DefaultEditFeature<T extends {
801
705
  private getItem;
802
706
  private updateItem;
803
707
  constructor(options: {
708
+ /** 表单数据的 Schema(用于验证和字段推断) */
709
+ schema: z.ZodObject<any>;
804
710
  getItem: (id: string | number) => Promise<T | null>;
805
711
  updateItem: (id: string | number, data: Partial<T>) => Promise<T | null>;
806
712
  permissionPrefix: string;
@@ -811,6 +717,8 @@ declare class DefaultEditFeature<T extends {
811
717
  getTitle?: (item: T, context: FeatureContext) => string | Promise<string>;
812
718
  /** 获取动态描述(可选,接收 item 和 context,返回描述字符串) */
813
719
  getDescription?: (item: T, context: FeatureContext) => string | Promise<string> | undefined;
720
+ /** 表单字段名(可选,不提供则显示 schema 中的所有字段) */
721
+ formFieldNames?: string[];
814
722
  });
815
723
  getFormAction(): "edit";
816
724
  getRoutes(): ({
@@ -837,14 +745,18 @@ declare class DefaultEditFeature<T extends {
837
745
 
838
746
  /**
839
747
  * 默认列表 Feature
840
- * 类型通过 context.model.modelSchema.schema 的 z.infer 推断
748
+ * 类型通过 schema 的 z.infer 推断
841
749
  */
842
750
  declare class DefaultListFeature<T extends {
843
751
  id: string | number;
844
752
  } = any> extends BaseFeature {
845
753
  private getList;
846
754
  private deleteItem?;
755
+ private listFieldNames?;
756
+ private filterSchema?;
847
757
  constructor(options: {
758
+ /** 列表数据的 Schema(用于字段推断和显示) */
759
+ schema: z.ZodObject<any>;
848
760
  /** 获取列表数据的函数 */
849
761
  getList: (params: ListParams) => Promise<ListResult<T>>;
850
762
  /** 删除数据的函数(可选,不提供则列表不显示删除操作) */
@@ -857,6 +769,10 @@ declare class DefaultListFeature<T extends {
857
769
  dialogSize?: DialogSize;
858
770
  /** 是否允许点击遮罩区域关闭弹窗 */
859
771
  closeOnBackdropClick?: boolean;
772
+ /** 列表显示字段名(可选,不提供则显示 schema 中的所有字段) */
773
+ listFieldNames?: string[];
774
+ /** 筛选表单的 Schema(可选,不提供则不显示筛选表单) */
775
+ filterSchema?: z.ZodObject<any>;
860
776
  });
861
777
  getRoutes(): {
862
778
  method: "get";
@@ -989,4 +905,4 @@ declare function ErrorAlert(props: ErrorAlertProps): hono_jsx_jsx_dev_runtime.JS
989
905
  */
990
906
  declare function LoadingBar(): hono_jsx_jsx_dev_runtime.JSX.Element;
991
907
 
992
- export { type AuthProvider, BaseFeature, CustomFeature, DefaultCreateFeature, DefaultDeleteFeature, DefaultDetailFeature, DefaultEditFeature, DefaultListFeature, Dialog, type DialogProps, type DialogSize, ErrorAlert, type ErrorAlertProps, type Feature, type FeatureContext, type FeatureType, type FieldMetadata, HtmxAdminPlugin, type HtmxAdminPluginOptions, type ListParams, type ListResult, LoadingBar, type ModelSchema, type NavItemConfig, type Notification, type NotificationType$1 as NotificationType, type PageMetadata, PageModel, type PermissionResult, type UserInfo, checkUserPermission, getUserInfo, modelNameToPath, parseListParams };
908
+ export { type AuthProvider, BaseFeature, CustomFeature, DefaultCreateFeature, DefaultDeleteFeature, DefaultDetailFeature, DefaultEditFeature, DefaultListFeature, Dialog, type DialogProps, type DialogSize, ErrorAlert, type ErrorAlertProps, type Feature, type FeatureContext, type FeatureType, type FieldMetadata, HtmxAdminPlugin, type HtmxAdminPluginOptions, type ListParams, type ListResult, LoadingBar, type NavItemConfig, type Notification, type NotificationType$1 as NotificationType, type PageMetadata, PageModel, type PermissionResult, type UserInfo, checkUserPermission, getUserInfo, modelNameToPath, parseListParams };