@robsun/create-keystone-app 0.2.11 → 0.2.13

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/package.json CHANGED
@@ -1,23 +1,23 @@
1
- {
2
- "name": "@robsun/create-keystone-app",
3
- "version": "0.2.11",
4
- "scripts": {
5
- "build": "node scripts/build.js",
6
- "prepublishOnly": "node scripts/build.js && node scripts/prune-template-deps.js"
7
- },
8
- "publishConfig": {
9
- "access": "public"
10
- },
11
- "bin": {
12
- "create-keystone-app": "dist/create-keystone-app.js",
13
- "create-keystone-module": "dist/create-module.js"
14
- },
15
- "files": [
16
- "dist",
17
- "template",
18
- "README.md"
19
- ],
20
- "engines": {
21
- "node": ">=18"
22
- }
23
- }
1
+ {
2
+ "name": "@robsun/create-keystone-app",
3
+ "version": "0.2.13",
4
+ "scripts": {
5
+ "build": "node scripts/build.js",
6
+ "prepublishOnly": "node scripts/build.js && node scripts/prune-template-deps.js"
7
+ },
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "bin": {
12
+ "create-keystone-app": "dist/create-keystone-app.js",
13
+ "create-keystone-module": "dist/create-module.js"
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "template",
18
+ "README.md"
19
+ ],
20
+ "engines": {
21
+ "node": ">=18"
22
+ }
23
+ }
@@ -50,11 +50,58 @@ approval:flow:view|create|edit|delete
50
50
  approval:instance:view|approve|cancel
51
51
  approval:record:view|export
52
52
 
53
+ ## 流程版本控制
54
+
55
+ 审批流程支持版本管理,确保活跃实例不受流程修改影响:
56
+
57
+ **状态生命周期**:`draft` → `published` → `deprecated`
58
+
59
+ - **draft**: 草稿状态,可编辑,不能创建实例
60
+ - **published**: 已发布,不可编辑,可创建实例
61
+ - **deprecated**: 已废弃,不可编辑,不可创建新实例
62
+
63
+ **操作方式**:
64
+ ```go
65
+ // 发布流程(draft → published)
66
+ approvalService.PublishFlow(ctx, tenantID, flowID)
67
+
68
+ // 创建新版本(基于已发布流程)
69
+ newFlow, err := approvalService.CreateNewVersion(ctx, tenantID, flowID)
70
+ // 新版本为 draft 状态,ParentFlowID 指向原流程
71
+
72
+ // 废弃旧版本(自动在发布新版本时执行)
73
+ approvalService.DeprecateFlow(ctx, tenantID, flowID)
74
+ ```
75
+
76
+ ## 回调重试机制
77
+
78
+ 审批回调(OnApproved/OnRejected)支持自动重试:
79
+
80
+ ```go
81
+ import approval "github.com/robsuncn/keystone/domain/approval/service"
82
+
83
+ // 配置重试策略
84
+ config := approval.RetryConfig{
85
+ MaxRetries: 3, // 最大重试次数
86
+ InitialBackoff: time.Second, // 初始退避时间
87
+ MaxBackoff: 30 * time.Second, // 最大退避时间
88
+ BackoffFactor: 2.0, // 退避倍数(指数退避)
89
+ }
90
+
91
+ // 包装回调
92
+ retryable := approval.NewRetryableCallback(myCallback, config)
93
+
94
+ // 或使用异步执行器
95
+ executor := approval.NewAsyncCallbackExecutor(registry, config, 100)
96
+ ```
97
+
53
98
  ## 集成自检
54
99
  - Flow code <= 50 且使用大写;business type 用短字符串。
55
100
  - approve/reject/cancel 使用 version 做乐观锁。
56
101
  - 变更接口时更新 OpenAPI 合同。
57
102
  - 新增默认流程时补迁移或 seed。
103
+ - **新流程从 draft 开始,发布后才能创建实例**。
104
+ - **使用 RetryableCallback 包装业务回调以增强可靠性**。
58
105
 
59
106
  测试清单见 [testing.md](testing.md)。
60
107
 
@@ -1,206 +1,261 @@
1
- # Keystone 包完整能力清单
2
-
3
- ## 一、前端组件 (@robsun/keystone-web-core)
4
-
5
- ### 1. 应用与路由
6
- ```typescript
7
- import { KeystoneApp, createKeystoneRouter } from '@robsun/keystone-web-core'
8
- import { registerModule, registerRoutes, registerMenus, registerPermissions } from '@robsun/keystone-web-core'
9
- ```
10
-
11
- ### 2. 高级 UI 组件
12
- ```typescript
13
- // 高级表格 - 过滤、分页、行选择、导出
14
- import { ProTable } from '@robsun/keystone-web-core'
15
-
16
- // 智能表单 - 自动验证、加载状态
17
- import { ProForm } from '@robsun/keystone-web-core'
18
-
19
- // 文件上传 - 拖拽、预览、多文件
20
- import { FileUpload } from '@robsun/keystone-web-core'
21
-
22
- // 数据导入向导
23
- import { DataImporter, ImportWizard, ColumnMapper, ValidationResult } from '@robsun/keystone-web-core'
24
-
25
- // 数据导出向导
26
- import { DataExporter, ExportWizard } from '@robsun/keystone-web-core'
27
-
28
- // 权限守卫
29
- import { PermissionGuard } from '@robsun/keystone-web-core'
30
- ```
31
-
32
- ### 3. 审批流系统
33
- ```typescript
34
- import {
35
- ApprovalFlowEditor, // 流程编辑器
36
- ApprovalFlowCanvas, // 可视化画布
37
- ApprovalNodeEditor, // 节点编辑
38
- ConditionEditor, // 条件编辑
39
- ApprovalStatus, // 状态展示
40
- ApprovalAction, // 审批按钮
41
- ApprovalHistory // 审批历史
42
- } from '@robsun/keystone-web-core'
43
- ```
44
- 审批流程与接入说明见 [approval.md](approval.md)。
45
-
46
- ### 4. 帮助系统
47
- ```typescript
48
- import {
49
- HelpButton, HelpDrawer, HelpSearch, HelpContent,
50
- useCurrentHelpKey, useHelpSearch
51
- } from '@robsun/keystone-web-core'
52
- ```
53
-
54
- ### 5. 布局与主题
55
- ```typescript
56
- import { MainLayout, antdTheme, useLayoutStore } from '@robsun/keystone-web-core'
57
- ```
58
-
59
- ### 6. 认证授权
60
- ```typescript
61
- import { useAuthStore, useAuth, matchesPermission } from '@robsun/keystone-web-core'
62
- ```
63
-
64
- ### 7. 运行时服务
65
- ```typescript
66
- import { api, setApiBaseUrl } from '@robsun/keystone-web-core'
67
- import { createExportJob, createImportJob } from '@robsun/keystone-web-core'
68
- import { listNotifications, markNotificationRead, fetchUnreadCount } from '@robsun/keystone-web-core'
69
- import { getJob, listJobs } from '@robsun/keystone-web-core'
70
- ```
71
-
72
- ### 8. 多语言 (i18n)
73
- ```typescript
74
- // 翻译 Hook
75
- import { useTranslation } from 'react-i18next'
76
-
77
- // 使用示例
78
- const { t } = useTranslation()
79
- const title = t('module:key') // 命名空间:键
80
- const description = t('common:actions.save')
81
-
82
- // 平台多语言配置
83
- import { getKeystoneConfig } from '@robsun/keystone-web-core'
84
- const locale = getKeystoneConfig().ui?.i18n?.defaultLocale // 'zh-CN' | 'en-US'
85
-
86
- // dayjs 本地化同步(自动完成)
87
- import dayjs from 'dayjs'
88
- dayjs.locale(getKeystoneConfig().ui?.locale?.dayjs ?? 'zh-cn')
89
- ```
90
-
91
- **翻译文件组织**:
92
- ```
93
- src/modules/{module}/locales/
94
- ├── zh-CN/
95
- │ └── {namespace}.json
96
- └── en-US/
97
- └── {namespace}.json
98
- ```
99
-
100
- **命名空间约定**:
101
- - `common` - 通用文案(按钮、操作、状态等)
102
- - `auth` - 认证相关
103
- - `system` - 系统管理
104
- - `{module}` - 业务模块专属翻译
105
-
106
- ### 9. Hooks
107
- ```typescript
108
- import { useListState } from '@robsun/keystone-web-core'
109
- ```
110
-
111
- ### 10. 类型
112
- ```typescript
113
- import type { ApiResponse, PaginatedData, PaginatedResponse } from '@robsun/keystone-web-core'
114
- import type { Permission, ModuleName, Action } from '@robsun/keystone-web-core'
115
- ```
116
-
117
- ## 二、后端能力 (Go)
118
-
119
- ### 1. 模块系统
120
- ```go
121
- type Module interface {
122
- Name() string
123
- RegisterRoutes(r *gin.RouterGroup)
124
- RegisterPermissions() []Permission
125
- Migrate(db *gorm.DB) error
126
- Seed(db *gorm.DB) error
127
- }
128
- ```
129
-
130
- ### 2. 审批引擎
131
- ```go
132
- approvalService.CreateInstance(...)
133
- approvalService.Approve(...)
134
- approvalService.Reject(...)
135
- approvalService.Cancel(...)
136
- ```
137
-
138
- ### 3. Job 队列
139
- ```go
140
- jobService.CreateJob(...)
141
- jobService.GetJob(...)
142
- jobService.ListJobs(...)
143
- ```
144
-
145
- ### 4. 通知服务
146
- ```go
147
- notificationService.SendNotification(...)
148
- notificationService.ListNotifications(...)
149
- notificationService.MarkRead(...)
150
- ```
151
-
152
- ### 5. 权限中间件
153
- ```go
154
- middleware.RequirePermission("module:resource:action")
155
- ```
156
-
157
- ### 6. 存储服务
158
- ```go
159
- storageService.Upload(...)
160
- storageService.Download(...)
161
- storageService.Delete(...)
162
- ```
163
-
164
- ### 7. 多语言 (i18n)
165
- ```go
166
- // 获取当前请求语言环境
167
- import "github.com/robsuncn/keystone/infra/i18n"
168
-
169
- locale := i18n.GetLocale(c) // 从请求头或参数获取
170
-
171
- // 翻译函数
172
- i18n.T(c, "module.key") // 基础翻译
173
- i18n.T(c, "module.greeting", "name", "张三") // 带变量
174
- i18n.Tf(c, "module.count", count) // 复数形式
175
-
176
- // 错误消息翻译
177
- return errors.New(i18n.T(c, "module.errors.notFound"))
178
- ```
179
-
180
- **翻译文件组织**:
181
- ```
182
- internal/modules/{module}/i18n/
183
- ├── i18n.go # 初始化
184
- ├── keys.go # 翻译键常量
185
- └── locales/
186
- ├── zh-CN.json
187
- └── en-US.json
188
- ```
189
-
190
- **翻译键常量定义**:
191
- ```go
192
- package i18n
193
-
194
- const (
195
- KeyItemCreated = "example.item.created"
196
- KeyItemNotFound = "example.item.notFound"
197
- KeyItemInvalid = "example.item.invalid"
198
- )
199
- ```
200
-
201
- **模块初始化**:
202
- ```go
203
- func init() {
204
- i18n.MustLoadModuleTranslations("example", Translations)
205
- }
206
- ```
1
+ # Keystone 包完整能力清单
2
+
3
+ ## 一、前端组件 (@robsun/keystone-web-core)
4
+
5
+ ### 1. 应用与路由
6
+ ```typescript
7
+ import { KeystoneApp, createKeystoneRouter } from '@robsun/keystone-web-core'
8
+ import { registerModule, registerRoutes, registerMenus, registerPermissions } from '@robsun/keystone-web-core'
9
+ ```
10
+
11
+ ### 2. 高级 UI 组件
12
+ ```typescript
13
+ // 高级表格 - 过滤、分页、行选择、导出
14
+ import { ProTable } from '@robsun/keystone-web-core'
15
+
16
+ // 智能表单 - 自动验证、加载状态
17
+ import { ProForm } from '@robsun/keystone-web-core'
18
+
19
+ // 文件上传 - 拖拽、预览、多文件
20
+ import { FileUpload } from '@robsun/keystone-web-core'
21
+
22
+ // 数据导入向导
23
+ import { DataImporter, ImportWizard, ColumnMapper, ValidationResult } from '@robsun/keystone-web-core'
24
+
25
+ // 数据导出向导
26
+ import { DataExporter, ExportWizard } from '@robsun/keystone-web-core'
27
+
28
+ // 权限守卫
29
+ import { PermissionGuard } from '@robsun/keystone-web-core'
30
+ ```
31
+
32
+ ### 3. 审批流系统
33
+ ```typescript
34
+ import {
35
+ ApprovalFlowEditor, // 流程编辑器
36
+ ApprovalFlowCanvas, // 可视化画布
37
+ ApprovalNodeEditor, // 节点编辑
38
+ ConditionEditor, // 条件编辑
39
+ ApprovalStatus, // 状态展示
40
+ ApprovalAction, // 审批按钮
41
+ ApprovalHistory // 审批历史
42
+ } from '@robsun/keystone-web-core'
43
+ ```
44
+ 审批流程与接入说明见 [approval.md](approval.md)。
45
+
46
+ ### 4. 帮助系统
47
+ ```typescript
48
+ import {
49
+ HelpButton, HelpDrawer, HelpSearch, HelpContent,
50
+ useCurrentHelpKey, useHelpSearch
51
+ } from '@robsun/keystone-web-core'
52
+ ```
53
+
54
+ ### 5. 布局与主题
55
+ ```typescript
56
+ import { MainLayout, antdTheme, useLayoutStore } from '@robsun/keystone-web-core'
57
+ ```
58
+
59
+ ### 6. 认证授权
60
+ ```typescript
61
+ import { useAuthStore, useAuth, matchesPermission } from '@robsun/keystone-web-core'
62
+ ```
63
+
64
+ ### 7. 运行时服务
65
+ ```typescript
66
+ import { api, setApiBaseUrl } from '@robsun/keystone-web-core'
67
+ import { createExportJob, createImportJob } from '@robsun/keystone-web-core'
68
+ import { listNotifications, markNotificationRead, fetchUnreadCount } from '@robsun/keystone-web-core'
69
+ import { getJob, listJobs } from '@robsun/keystone-web-core'
70
+ ```
71
+
72
+ ### 8. 多语言 (i18n)
73
+ ```typescript
74
+ // 翻译 Hook
75
+ import { useTranslation } from 'react-i18next'
76
+
77
+ // 使用示例
78
+ const { t } = useTranslation()
79
+ const title = t('module:key') // 命名空间:键
80
+ const description = t('common:actions.save')
81
+
82
+ // 平台多语言配置
83
+ import { getKeystoneConfig } from '@robsun/keystone-web-core'
84
+ const locale = getKeystoneConfig().ui?.i18n?.defaultLocale // 'zh-CN' | 'en-US'
85
+
86
+ // dayjs 本地化同步(自动完成)
87
+ import dayjs from 'dayjs'
88
+ dayjs.locale(getKeystoneConfig().ui?.locale?.dayjs ?? 'zh-cn')
89
+ ```
90
+
91
+ **翻译文件组织**:
92
+ ```
93
+ src/modules/{module}/locales/
94
+ ├── zh-CN/
95
+ │ └── {namespace}.json
96
+ └── en-US/
97
+ └── {namespace}.json
98
+ ```
99
+
100
+ **命名空间约定**:
101
+ - `common` - 通用文案(按钮、操作、状态等)
102
+ - `auth` - 认证相关
103
+ - `system` - 系统管理
104
+ - `{module}` - 业务模块专属翻译
105
+
106
+ ### 9. Hooks
107
+ ```typescript
108
+ import { useListState } from '@robsun/keystone-web-core'
109
+ ```
110
+
111
+ ### 10. 类型
112
+ ```typescript
113
+ import type { ApiResponse, PaginatedData, PaginatedResponse } from '@robsun/keystone-web-core'
114
+ import type { Permission, ModuleName, Action } from '@robsun/keystone-web-core'
115
+ ```
116
+
117
+ ## 二、后端能力 (Go)
118
+
119
+ ### 1. 模块系统
120
+ ```go
121
+ type Module interface {
122
+ Name() string
123
+ RegisterRoutes(r *gin.RouterGroup)
124
+ RegisterPermissions() []Permission
125
+ Migrate(db *gorm.DB) error
126
+ Seed(db *gorm.DB) error
127
+ }
128
+ ```
129
+
130
+ ### 2. 审批引擎
131
+ ```go
132
+ // 基础操作
133
+ approvalService.CreateInstance(...)
134
+ approvalService.Approve(...)
135
+ approvalService.Reject(...)
136
+ approvalService.Cancel(...)
137
+
138
+ // 流程版本控制
139
+ approvalService.PublishFlow(...) // draft → published
140
+ approvalService.DeprecateFlow(...) // published → deprecated
141
+ approvalService.CreateNewVersion(...) // 基于现有流程创建新版本
142
+ ```
143
+
144
+ **流程状态生命周期**:`draft` → `published` → `deprecated`
145
+
146
+ ### 3. 审批回调重试
147
+ ```go
148
+ import approval "github.com/robsuncn/keystone/domain/approval/service"
149
+
150
+ // 带重试的回调包装
151
+ config := approval.RetryConfig{
152
+ MaxRetries: 3,
153
+ InitialBackoff: time.Second,
154
+ MaxBackoff: 30 * time.Second,
155
+ BackoffFactor: 2.0,
156
+ }
157
+ retryable := approval.NewRetryableCallback(callback, config)
158
+
159
+ // 异步回调执行器
160
+ executor := approval.NewAsyncCallbackExecutor(registry, config, 100)
161
+ executor.ExecuteApproved(ctx, businessType, tenantID, businessID, approverID)
162
+
163
+ // 监听结果
164
+ for result := range executor.Results() {
165
+ if !result.Success {
166
+ log.Error("callback failed", "error", result.Error)
167
+ }
168
+ }
169
+ ```
170
+
171
+ ### 4. Job 队列
172
+ ```go
173
+ jobService.CreateJob(...)
174
+ jobService.GetJob(...)
175
+ jobService.ListJobs(...)
176
+ ```
177
+
178
+ ### 5. 通知服务
179
+ ```go
180
+ notificationService.SendNotification(...)
181
+ notificationService.ListNotifications(...)
182
+ notificationService.MarkRead(...)
183
+ ```
184
+
185
+ ### 6. 权限中间件
186
+ ```go
187
+ middleware.RequirePermission("module:resource:action")
188
+ ```
189
+
190
+ ### 7. 存储服务
191
+ ```go
192
+ storageService.Upload(...)
193
+ storageService.Download(...)
194
+ storageService.Delete(...)
195
+ ```
196
+
197
+ ### 8. 统一分页
198
+ ```go
199
+ import "github.com/robsuncn/keystone/infra/pagination"
200
+
201
+ // 解析请求参数
202
+ req := pagination.ParseRequest(c) // 从 Gin context 获取 page, page_size
203
+
204
+ // 方式一:使用 GORM Scope
205
+ db.Scopes(pagination.Scope(req)).Find(&items)
206
+
207
+ // 方式二:一步完成分页查询
208
+ var items []Item
209
+ total, err := pagination.Paginate(db.Where("tenant_id = ?", tenantID), req, &items)
210
+
211
+ // 方式三:带预加载的分页
212
+ total, err := pagination.PaginateWithPreload(db, req, &items, "Category", "Tags")
213
+
214
+ // 构建响应
215
+ resp := pagination.NewResponse(items, total, req)
216
+ // resp.TotalPages(), resp.HasNextPage(), resp.HasPrevPage()
217
+ ```
218
+
219
+ ### 9. 多语言 (i18n)
220
+ ```go
221
+ // 获取当前请求语言环境
222
+ import "github.com/robsuncn/keystone/infra/i18n"
223
+
224
+ locale := i18n.GetLocale(c) // 从请求头或参数获取
225
+
226
+ // 翻译函数
227
+ i18n.T(c, "module.key") // 基础翻译
228
+ i18n.T(c, "module.greeting", "name", "张三") // 带变量
229
+ i18n.Tf(c, "module.count", count) // 复数形式
230
+
231
+ // 错误消息翻译
232
+ return errors.New(i18n.T(c, "module.errors.notFound"))
233
+ ```
234
+
235
+ **翻译文件组织**:
236
+ ```
237
+ internal/modules/{module}/i18n/
238
+ ├── i18n.go # 初始化
239
+ ├── keys.go # 翻译键常量
240
+ └── locales/
241
+ ├── zh-CN.json
242
+ └── en-US.json
243
+ ```
244
+
245
+ **翻译键常量定义**:
246
+ ```go
247
+ package i18n
248
+
249
+ const (
250
+ KeyItemCreated = "example.item.created"
251
+ KeyItemNotFound = "example.item.notFound"
252
+ KeyItemInvalid = "example.item.invalid"
253
+ )
254
+ ```
255
+
256
+ **模块初始化**:
257
+ ```go
258
+ func init() {
259
+ i18n.MustLoadModuleTranslations("example", Translations)
260
+ }
261
+ ```