dproto 1.0.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/Util.md ADDED
@@ -0,0 +1,233 @@
1
+ # 管理工具
2
+
3
+ 1. Message名重复检查
4
+ 2. Message名转Api Path方案(Api Path关联Message规则)
5
+
6
+ ## ApiPath可逆Message转换方案
7
+
8
+ ### 核心规则
9
+
10
+ ```
11
+ 正向(Path → Message):
12
+ 1. 去除前缀(默认 /api/v1/)
13
+ 2. '/' → '-'
14
+ 3. '-' 转驼峰('_' 保持不变)
15
+ 4. + Request/Response
16
+ 5. ⚠️ Request/Response 必须一对一(每个Path必须同时生成两种消息)
17
+
18
+ 逆向(Message → Path):
19
+ 1. 去除 Request/Response
20
+ 2. 驼峰还原(遇到 '_' 不插入 '-')
21
+ 3. '-' → '/'
22
+ 4. 加前缀
23
+
24
+ 约束条件:
25
+ ✓ 每个API路径必须定义 Request 和 Response 两个Message
26
+ ✓ Request 用于接收请求参数
27
+ ✓ Response 用于返回响应数据
28
+ ✓ 不允许单独定义 Request 或 Response
29
+ ```
30
+
31
+ ### 转换示例
32
+
33
+ ```python
34
+ # Path → Message(必须生成一对 Request/Response)
35
+ /api/v1/users/register → UsersRegisterRequest + UsersRegisterResponse
36
+ /api/v1/user_info/get → UserInfoGetRequest + UserInfoGetResponse
37
+ /api/v1/orders/create → OrdersCreateRequest + OrdersCreateResponse
38
+
39
+ # Message → Path(逆向)
40
+ UsersRegisterRequest → /api/v1/users/register
41
+ UsersRegisterResponse → /api/v1/users/register
42
+ UserInfoGetRequest → /api/v1/user_info/get
43
+ UserInfoGetResponse → /api/v1/user_info/get
44
+ OrdersCreateRequest → /api/v1/orders/create
45
+ OrdersCreateResponse → /api/v1/orders/create
46
+ ```
47
+
48
+ ### 完美可逆验证
49
+
50
+ | Path | Request Message | Response Message | 逆向 Path | 状态 |
51
+ |------|-----------------|------------------|----------|------|
52
+ | `/api/v1/users/register` | `UsersRegisterRequest` | `UsersRegisterResponse` | `/api/v1/users/register` | ✓ 可逆 |
53
+ | `/api/v1/user_info/get` | `UserInfoGetRequest` | `UserInfoGetResponse` | `/api/v1/user_info/get` | ✓ 可逆 |
54
+ | `/api/v1/orders/create` | `OrdersCreateRequest` | `OrdersCreateResponse` | `/api/v1/orders/create` | ✓ 可逆 |
55
+
56
+ **关键特点:**
57
+ - `'_'` 在路径段内部保持不变(如 `user_info` → `User_Info`)
58
+ - 所有转换完美可逆,无歧义
59
+ - 路径段可用 `_` 分隔词组
60
+ - **Request/Response 严格一对一,保证API完整性**
61
+
62
+ ### Python 实现
63
+
64
+ ```python
65
+ def path_to_message(path, prefix='/api/v1/', suffix='Request'):
66
+ """Path → Message"""
67
+ path = path.replace(prefix, '')
68
+ path = path.replace('/', '-')
69
+ parts = path.split('-')
70
+ camel_parts = []
71
+ for part in parts:
72
+ sub_parts = part.split('_')
73
+ camel_sub = '_'.join(p.capitalize() for p in sub_parts)
74
+ camel_parts.append(camel_sub)
75
+ camel = ''.join(camel_parts)
76
+ return camel + suffix
77
+
78
+
79
+ def message_to_path(message, prefix='/api/v1/', suffix='Request'):
80
+ """Message → Path"""
81
+ name = message.replace(suffix, '')
82
+ result = ''
83
+ for i, c in enumerate(name):
84
+ if c == '_':
85
+ result += '_'
86
+ elif c.isupper() and i > 0 and name[i-1] != '_':
87
+ result += '-' + c.lower()
88
+ else:
89
+ result += c.lower()
90
+ path = result.replace('-', '/')
91
+ return prefix + path
92
+
93
+
94
+ # 测试验证
95
+ print(path_to_message('/api/v1/user_info/get'))
96
+ # → UserInfoGetRequest
97
+
98
+ print(message_to_path('UserInfoGetRequest'))
99
+ # → /api/v1/user_info/get ✅ 完美可逆
100
+ ```
101
+
102
+ ### JavaScript 实现
103
+
104
+ ```javascript
105
+ function pathToMessage(path, prefix = '/api/v1/', suffix = 'Request') {
106
+ path = path.replace(prefix, '').replace(/\//g, '-');
107
+ const parts = path.split('-');
108
+ const camelParts = parts.map(part => {
109
+ const subParts = part.split('_');
110
+ return subParts.map(p => p.charAt(0).toUpperCase() + p.slice(1)).join('_');
111
+ });
112
+ return camelParts.join('') + suffix;
113
+ }
114
+
115
+ function messageToPath(message, prefix = '/api/v1/', suffix = 'Request') {
116
+ let name = message.replace(suffix, '');
117
+ let result = '';
118
+ for (let i = 0; i < name.length; i++) {
119
+ const c = name[i];
120
+ if (c === '_') result += '_';
121
+ else if (c === c.toUpperCase() && i > 0 && name[i-1] !== '_')
122
+ result += '-' + c.toLowerCase();
123
+ else result += c.toLowerCase();
124
+ }
125
+ return prefix + result.replace(/-/g, '/');
126
+ }
127
+ ```
128
+
129
+ ### Go 实现
130
+
131
+ ```go
132
+ func PathToMessage(path, prefix, suffix string) string {
133
+ path = strings.Replace(path, prefix, "", 1)
134
+ path = strings.ReplaceAll(path, "/", "-")
135
+ parts := strings.Split(path, "-")
136
+ camelParts := []string{}
137
+ for _, part := range parts {
138
+ subParts := strings.Split(part, "_")
139
+ camelSub := []string{}
140
+ for _, sub := range subParts {
141
+ camelSub = append(camelSub, strings.Title(sub))
142
+ }
143
+ camelParts = append(camelParts, strings.Join(camelSub, "_"))
144
+ }
145
+ return strings.Join(camelParts, "") + suffix
146
+ }
147
+
148
+ func MessageToPath(message, prefix, suffix string) string {
149
+ name := strings.Replace(message, suffix, "", 1)
150
+ result := ""
151
+ for i, c := range name {
152
+ if c == '_' {
153
+ result += "_"
154
+ } else if unicode.IsUpper(c) && i > 0 && name[i-1] != '_' {
155
+ result += "-" + strings.ToLower(string(c))
156
+ } else {
157
+ result += strings.ToLower(string(c))
158
+ }
159
+ }
160
+ return prefix + strings.ReplaceAll(result, "-", "/")
161
+ }
162
+ ```
163
+
164
+ ### API 路径规范
165
+
166
+ | 项目 | 规范 |
167
+ |------|------|
168
+ | **HTTP Method** | 统一 POST |
169
+ | **路径参数** | 无(参数在 Request Body) |
170
+ | **路径分隔符** | `/`(层级分隔) |
171
+ | **路径段命名** | 小写,可用 `_` 分隔词组 |
172
+
173
+ **标准路径格式:**
174
+ ```
175
+ POST /api/v1/{module}/{action}
176
+ POST /api/v1/{module}_{sub_module}/{action}
177
+ ```
178
+
179
+ **示例:**
180
+ ```
181
+ POST /api/v1/users/register ✅
182
+ POST /api/v1/user_info/get ✅
183
+ POST /api/v1/orders/create ✅
184
+ POST /api/v1/password/forgot ✅
185
+ ```
186
+
187
+ ### 工具使用
188
+
189
+ ```bash
190
+ # 单个转换
191
+ python tools/path_message_converter.py path-to-message /api/v1/users/register
192
+ python tools/path_message_converter.py message-to-path UsersRegisterRequest
193
+
194
+ # 批量转换(生成路由表)
195
+ python tools/path_message_converter.py batch proto/*.proto --output proto/routes.yaml
196
+ ```
197
+
198
+ ---
199
+
200
+ ## Message名重复检查
201
+
202
+ ### Proto Package 命名空间
203
+
204
+ 每个 proto 文件应定义独立的 package,避免 Message 名冲突:
205
+
206
+ ```protobuf
207
+ // proto/user.proto
208
+ package user;
209
+
210
+ message RegisterRequest { ... }
211
+ message RegisterResponse { ... }
212
+
213
+
214
+ // proto/order.proto
215
+ package order;
216
+
217
+ message CreateRequest { ... } // 不会与 user.RegisterRequest 冲突
218
+ message CreateResponse { ... }
219
+ ```
220
+
221
+ ### 检查工具
222
+
223
+ ```bash
224
+ # 检查所有 proto 文件的 Message 名重复
225
+ python tools/check_message_duplicates.py proto/*.proto
226
+ ```
227
+
228
+ ### Message 命名建议
229
+
230
+ 1. **使用业务前缀**:`UserRegisterRequest`, `OrderCreateRequest`
231
+ 2. **避免通用名**:不用 `Request`, `Response` 单独命名
232
+ 3. **统一后缀**:Request/Response 保持一致
233
+ 4. **Package 分离**:不同业务模块使用不同 package
package/bin/dproto.js ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { program } = require('../src/index.js');
4
+
5
+ program.parse(process.argv);
package/dproto.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "apiPrefix": "/api/v1/",
3
+ "protoDir": "proto"
4
+ }
@@ -0,0 +1,181 @@
1
+ # Output 目录说明
2
+
3
+ 本目录包含所有 Protobuf 生成的文件,按不同格式分类存储。
4
+
5
+ ## 📁 目录结构
6
+
7
+ ```
8
+ output/
9
+ ├── protoSplit/ ← 多文件方案(推荐大型项目)
10
+ │ ├── user.js 178KB 用户模块
11
+ │ ├── user.d.ts 66KB TypeScript 类型
12
+ │ ├── order.js 255KB 订单模块
13
+ │ ├── order.d.ts 86KB TypeScript 类型
14
+ │ ├── common.js 41KB 公共类型
15
+ │ ├── verification.js 94KB 验证码模块
16
+ │ └── password.js 87KB 密码管理
17
+
18
+ ├── protoJs/ ← 单文件方案(小型项目)
19
+ │ ├── proto.js 490KB CommonJS 格式
20
+ │ ├── proto.es6.js 490KB ES6 格式
21
+ │ └── README.md 使用说明
22
+
23
+ ├── protoJson/ ← JSON 描述文件
24
+ │ └ proto.json 20KB 标准 JSON
25
+
26
+ ├── protoMin/ ← 最小化版本
27
+ │ └ proto.min.json 20KB 生产环境使用
28
+
29
+ ├── protoTs/ ← TypeScript 类型定义
30
+ │ └ proto.d.ts 173KB 完整类型定义
31
+
32
+ └── protoDist/ ← 压缩版本
33
+ └── proto.min.js 130KB 压缩后的静态模块
34
+ ```
35
+
36
+ ## 🎯 使用指南
37
+
38
+ ### 多文件方案(推荐)
39
+
40
+ **适用场景**: 中大型项目 (>100 API)
41
+
42
+ ```bash
43
+ # 生成多文件
44
+ npm run proto:split
45
+ ```
46
+
47
+ **使用方式**:
48
+ ```javascript
49
+ // 按需加载单个模块
50
+ import userProto from '../output/protoSplit/user.js'; // 只加载 178KB
51
+
52
+ // React 懒加载
53
+ const userProto = await import('../output/protoSplit/user.js');
54
+ ```
55
+
56
+ **优势**:
57
+ - ✅ 首屏加载减少 70%
58
+ - ✅ 按需加载
59
+ - ✅ Webpack 自动分割
60
+
61
+ ### 单文件方案
62
+
63
+ **适用场景**: 小型项目 (<50 API)
64
+
65
+ ```bash
66
+ # 生成单文件
67
+ npm run proto:all
68
+ ```
69
+
70
+ **使用方式**:
71
+ ```javascript
72
+ // 加载全部 490KB
73
+ const proto = require('../output/protoJs/proto.js');
74
+ ```
75
+
76
+ ### JSON 动态加载
77
+
78
+ **适用场景**: 需要灵活性的场景
79
+
80
+ ```javascript
81
+ import protobuf from 'protobufjs';
82
+ import protoJson from '../output/protoJson/proto.json';
83
+
84
+ const root = protobuf.Root.fromJSON(protoJson);
85
+ ```
86
+
87
+ ## 📊 文件大小对比
88
+
89
+ | 方案 | 目录 | 文件大小 | 首屏加载 | 适用场景 |
90
+ |------|------|---------|---------|---------|
91
+ | **多文件** | protoSplit/ | 41-255KB | ~150KB | 中大型项目 |
92
+ | **单文件** | protoJs/ | 490KB | 490KB | 小型项目 |
93
+ | **JSON** | protoJson/ | 20KB | 20KB + runtime | 动态场景 |
94
+ | **压缩版** | protoDist/ | 130KB | 130KB | 生产环境 |
95
+
96
+ ## 🔧 生成命令
97
+
98
+ ```bash
99
+ # 多文件方案(推荐)
100
+ npm run proto:split # 生成所有多文件
101
+ npm run proto:split:js # 只生成 JavaScript
102
+ npm run proto:split:ts # 只生成 TypeScript
103
+
104
+ # 单文件方案
105
+ npm run proto:all # 生成所有单文件格式
106
+ npm run proto:js # 只生成 CommonJS
107
+ npm run proto:json # 只生成 JSON
108
+
109
+ # 压缩版本
110
+ npm run proto:compress # 压缩 JavaScript 文件
111
+ ```
112
+
113
+ ## 💡 最佳实践
114
+
115
+ ### 1. 根据项目规模选择
116
+
117
+ - **小型项目 (<50 API)**: 使用 `protoJs/proto.js`
118
+ - **中大型项目 (>100 API)**: 使用 `protoSplit/` 多文件方案
119
+ - **需要灵活加载**: 使用 `protoJson/proto.json`
120
+
121
+ ### 2. Webpack/Vite 配置
122
+
123
+ ```javascript
124
+ // webpack.config.js
125
+ optimization: {
126
+ splitChunks: {
127
+ cacheGroups: {
128
+ proto: {
129
+ test: /[\\/]output[\\/]protoSplit[\\/]/,
130
+ name: 'proto-[name]',
131
+ chunks: 'all'
132
+ }
133
+ }
134
+ }
135
+ }
136
+ ```
137
+
138
+ ### 3. React/Vue 懒加载
139
+
140
+ ```javascript
141
+ // React
142
+ const UserModule = React.lazy(() =>
143
+ import('../output/protoSplit/user.js')
144
+ );
145
+
146
+ // Vue
147
+ async function loadProto() {
148
+ return await import('../output/protoSplit/user.js');
149
+ }
150
+ ```
151
+
152
+ ## 📚 相关文档
153
+
154
+ - [多文件方案详细指南](../PROTO-MULTI-FILE-GUIDE.md)
155
+ - [前端开发指南](../前端引导.md)
156
+ - [使用示例](../examples/multi-file-usage.js)
157
+ - [性能优化对比](../proto-optimization.md)
158
+
159
+ ## ⚠️ 注意事项
160
+
161
+ 1. **不要修改生成的文件**: 这些文件是通过 proto 文件自动生成的,修改后会被覆盖
162
+
163
+ 2. **更新 proto 文件后重新生成**: 修改 proto 定义后,需要运行 `npm run proto:split` 重新生成
164
+
165
+ 3. **保持目录结构**: 所有输出文件都在 output 目录下,便于管理和清理
166
+
167
+ 4. **使用正确的路径**: 在项目中引用时,路径应为 `../output/protoSplit/xxx.js`
168
+
169
+ ## 🎉 快速开始
170
+
171
+ ```bash
172
+ # 1. 生成所有格式
173
+ npm run proto:split
174
+
175
+ # 2. 在项目中使用
176
+ import userProto from './output/protoSplit/user.js';
177
+
178
+ # 3. 享受按需加载的性能优势
179
+ ```
180
+
181
+ **文件已统一管理在 output 目录,方便使用和维护!**
@@ -0,0 +1,115 @@
1
+ # 模块API列表文件索引
2
+
3
+ 已创建的核心模块API列表文件:
4
+
5
+ ---
6
+
7
+ ## 一、用户端模块(C端)
8
+
9
+ ### 01. [用户中心 (User Center)](01-user-center.md)
10
+ - **API数量**: 20个
11
+ - **核心功能**: 注册、登录、信息管理、认证、积分、余额、收藏
12
+ - **已创建proto**: user.proto ✓
13
+
14
+ ### 02. [商品浏览 (Product Browse)](02-product-browse.md)
15
+ - **API数量**: 18个
16
+ - **核心功能**: 商品列表、搜索、详情、分类、推荐、收藏、评价
17
+ - **已创建proto**: product.proto ✓
18
+
19
+ ### 03. [购物车 (Shopping Cart)](03-shopping-cart.md)
20
+ - **API数量**: 15个
21
+ - **核心功能**: 购物车管理、选择、合并、优惠计算、结算预览
22
+ - **已创建proto**: cart.proto ✓
23
+
24
+ ### 04. [订单管理 (Order Management)](04-order-management.md)
25
+ - **API数量**: 25个
26
+ - **核心功能**: 订单创建、查询、取消、退款、投诉、物流追踪、评价
27
+ - **已创建proto**: order.proto ✓
28
+
29
+ ---
30
+
31
+ ## 二、商户端模块(B端)
32
+
33
+ ### 16. [商户入驻 (Merchant Onboarding)](16-merchant-onboarding.md)
34
+ - **API数量**: 20个
35
+ - **核心功能**: 商户申请、审核、认证、签约、培训、保证金
36
+ - **已创建proto**: merchant.proto ✓
37
+
38
+ ---
39
+
40
+ ## 总计已规划
41
+
42
+ - **已创建API列表**: 5个模块
43
+ - **已规划API数量**: 98个API
44
+ - **已创建Proto文件**: 18个
45
+ - **已定义API数量**: 158对Request/Response
46
+
47
+ ---
48
+
49
+ ## 下一步计划
50
+
51
+ 继续创建以下核心模块API列表:
52
+
53
+ ### 待创建(优先级P0)
54
+
55
+ #### 用户端
56
+ - 05. 支付管理 (Payment) - 15 API
57
+ - 06. 地址管理 (Address) - 12 API
58
+ - 07. 优惠券 (Coupon) - 13 API
59
+ - 08. 评价系统 (Review) - 12 API
60
+ - 09. 售后服务 (After-sale) - 15 API
61
+ - 10. 收藏/关注 (Favorite/Follow) - 10 API
62
+ - 11. 消息通知 (Notification) - 12 API
63
+ - 12. 会员积分 (Member Points) - 15 API
64
+ - 13. 搜索 (Search) - 10 API
65
+ - 14. 推荐 (Recommendation) - 8 API
66
+ - 15. 客服 (Customer Service) - 10 API
67
+
68
+ #### 商户端
69
+ - 17. 商品管理 (Merchant Product) - 25 API
70
+ - 18. 店铺管理 (Shop Management) - 20 API
71
+ - 19. 订单处理 (Merchant Order) - 20 API
72
+ - 20. 库存管理 (Merchant Inventory) - 18 API
73
+ - 21. 营销推广 (Merchant Marketing) - 20 API
74
+ - 22. 财务结算 (Merchant Finance) - 15 API
75
+ - 23. 客户服务 (Merchant Service) - 15 API
76
+ - 24. 数据分析 (Merchant Analytics) - 15 API
77
+ - 25. 物流管理 (Merchant Logistics) - 12 API
78
+
79
+ #### 平台管理端
80
+ - 34. 平台运营 (Platform Operation) - 15 API
81
+ - 35. 商户管理 (Admin Merchant) - 25 API
82
+ - 36. 商品审核 (Admin Product Audit) - 15 API
83
+ - 37. 订单监管 (Admin Order) - 20 API
84
+ - 38. 营销管理 (Admin Marketing) - 20 API
85
+ - 39. 财务管理 (Admin Finance) - 20 API
86
+ - 40. 用户管理 (Admin User) - 15 API
87
+
88
+ ---
89
+
90
+ ## 目标
91
+
92
+ **第一阶段目标**: 完成P0级别37个模块的API列表规划,预计API数量达到1200+
93
+
94
+ **第二阶段目标**: 完成剩余42个模块的API列表规划,总API数量超过1500+
95
+
96
+ ---
97
+
98
+ ## API列表文档格式
99
+
100
+ 每个模块API列表文档包含:
101
+
102
+ 1. **模块概述**: 模块功能简介
103
+ 2. **API列表**: 每个API包含
104
+ - API路径
105
+ - Request Message名称
106
+ - Response Message名称
107
+ - 功能说明
108
+ - 关键字段说明
109
+ 3. **Message统计**: Request/Response数量
110
+ 4. **关联模块**: 相关依赖模块
111
+
112
+ ---
113
+
114
+ 创建时间: 2026-04-15
115
+ 文档版本: v1.0