@kevisual/router 0.2.4 → 0.2.6

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/README.md CHANGED
@@ -24,6 +24,11 @@ app
24
24
  })
25
25
  .addTo(app);
26
26
  ```
27
+ ## 浏览器模块使用 router
28
+
29
+ ```ts
30
+ import { App } from '@kevisual/router/browser';
31
+ ```
27
32
 
28
33
  ## 核心概念
29
34
 
@@ -112,7 +117,7 @@ app
112
117
  .define(async (ctx) => {
113
118
  const { id } = ctx.query;
114
119
  if (!id) {
115
- ctx.throw(400, '缺少参数', 'id is required');
120
+ ctx.throw(400, '缺少参数:id is required');
116
121
  }
117
122
  ctx.body = { success: true };
118
123
  })
@@ -129,7 +134,7 @@ const app = new App();
129
134
  // 定义中间件
130
135
  app
131
136
  .route({
132
- rid: 'auth-example',
137
+ rid: 'auth',
133
138
  description: '权限校验中间件',
134
139
  })
135
140
  .define(async (ctx) => {
@@ -144,7 +149,7 @@ app
144
149
 
145
150
  // 使用中间件(通过 rid 引用)
146
151
  app
147
- .route({ path: 'admin', key: 'panel', middleware: ['auth-example'] })
152
+ .route({ path: 'admin', key: 'panel', middleware: ['auth'] })
148
153
  .define(async (ctx) => {
149
154
  // 可以访问中间件设置的 state
150
155
  const { tokenUser } = ctx.state;
@@ -163,18 +168,18 @@ app
163
168
  .router({
164
169
  path: 'dog',
165
170
  key: 'info',
166
- description: '获取狗的信息',
171
+ description: '获取小狗的信息',
167
172
  metadata: {
168
173
  args: {
169
- name: z.string().describe('狗的姓名'),
170
- age: z.number().describe('狗的年龄'),
174
+ name: z.string().describe('小狗的姓名'),
175
+ age: z.number().describe('小狗的年龄'),
171
176
  },
172
177
  },
173
178
  })
174
179
  .define(async (ctx) => {
175
180
  const { name, age } = ctx.query;
176
181
  ctx.body = {
177
- content: `这是一只${age}岁的狗,名字是${name}`,
182
+ content: `这是一只${age}岁的小狗,名字是${name}`,
178
183
  };
179
184
  })
180
185
  .addTo(app);
@@ -184,9 +189,7 @@ app
184
189
 
185
190
  1. **path 和 key 的组合是路由的唯一标识**,同一个 path+key 只能添加一个路由,后添加的会覆盖之前的。
186
191
 
187
- 2. **ctx.call vs ctx.run**:
188
- - `call` 返回完整 context,包含所有属性
189
- - `run` 返回 `{ code, data, message }` 格式,data 即 body
192
+ 2. `ctx.run` 返回 `{ code, data, message }` 格式,data 即 body
190
193
 
191
194
  3. **ctx.throw 会自动结束执行**,抛出自定义错误。
192
195
 
@@ -199,5 +202,3 @@ app
199
202
  7. **needSerialize 默认为 true**,会自动对 body 进行 JSON 序列化和反序列化。
200
203
 
201
204
  8. **progress 记录执行路径**,可用于调试和追踪路由调用链。
202
-
203
- 9. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。