@ahoo-wang/fetcher-decorator 0.6.1 → 0.6.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 +39 -4
- package/README.zh-CN.md +36 -4
- package/dist/index.es.js +532 -450
- package/dist/index.umd.js +2 -2
- package/dist/parameterDecorator.d.ts +21 -1
- package/dist/parameterDecorator.d.ts.map +1 -1
- package/dist/reflection.d.ts.map +1 -1
- package/dist/requestExecutor.d.ts +60 -1
- package/dist/requestExecutor.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -63,10 +63,7 @@ class UserService {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
@get('/{id}')
|
|
66
|
-
getUser(
|
|
67
|
-
@path() id: number,
|
|
68
|
-
@query() include: string,
|
|
69
|
-
): Promise<Response> {
|
|
66
|
+
getUser(@path() id: number, @query() include: string): Promise<Response> {
|
|
70
67
|
throw new Error('Implementation will be generated automatically.');
|
|
71
68
|
}
|
|
72
69
|
}
|
|
@@ -192,6 +189,12 @@ parameter name.
|
|
|
192
189
|
|
|
193
190
|
- `name`: Name of the header (optional - auto-extracted if not provided)
|
|
194
191
|
|
|
192
|
+
#### `@request()`
|
|
193
|
+
|
|
194
|
+
Defines a request parameter that will be used as the base request object. This allows you to pass a complete
|
|
195
|
+
FetcherRequest
|
|
196
|
+
object to customize the request configuration.
|
|
197
|
+
|
|
195
198
|
**Example:**
|
|
196
199
|
|
|
197
200
|
```typescript
|
|
@@ -205,6 +208,11 @@ class UserService {
|
|
|
205
208
|
throw new Error('Implementation will be generated automatically.');
|
|
206
209
|
}
|
|
207
210
|
|
|
211
|
+
@post('/users')
|
|
212
|
+
createUsers(@request() request: FetcherRequest): Promise<Response> {
|
|
213
|
+
throw new Error('Implementation will be generated automatically.');
|
|
214
|
+
}
|
|
215
|
+
|
|
208
216
|
@put('/{id}')
|
|
209
217
|
updateUser(@path() id: number, @body() user: User): Promise<Response> {
|
|
210
218
|
throw new Error('Implementation will be generated automatically.');
|
|
@@ -250,6 +258,33 @@ class ComplexService {
|
|
|
250
258
|
}
|
|
251
259
|
```
|
|
252
260
|
|
|
261
|
+
### Request Merging
|
|
262
|
+
|
|
263
|
+
When using the `@request()` decorator, the provided `FetcherRequest` object is merged with endpoint-specific
|
|
264
|
+
configuration using a sophisticated merging strategy:
|
|
265
|
+
|
|
266
|
+
- **Nested objects** (path, query, headers) are recursively merged, with parameter values taking precedence
|
|
267
|
+
- **Primitive values** (method, body, timeout, signal) from the parameter request override endpoint values
|
|
268
|
+
- **Empty objects** are handled gracefully, falling back to endpoint configuration
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
|
|
272
|
+
@api('/users')
|
|
273
|
+
class UserService {
|
|
274
|
+
@post('/')
|
|
275
|
+
createUsers(
|
|
276
|
+
@body() user: User,
|
|
277
|
+
@request() request: FetcherRequest,
|
|
278
|
+
): Promise<Response> {
|
|
279
|
+
// The final request will merge:
|
|
280
|
+
// - Endpoint method (POST)
|
|
281
|
+
// - Body parameter (user object)
|
|
282
|
+
// - Any configuration from the request parameter
|
|
283
|
+
throw new Error('Implementation will be generated automatically.');
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
253
288
|
## 🧪 Testing
|
|
254
289
|
|
|
255
290
|
```bash
|
package/README.zh-CN.md
CHANGED
|
@@ -62,10 +62,7 @@ class UserService {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
@get('/{id}')
|
|
65
|
-
getUser(
|
|
66
|
-
@path() id: number,
|
|
67
|
-
@query() include: string,
|
|
68
|
-
): Promise<Response> {
|
|
65
|
+
getUser(@path() id: number, @query() include: string): Promise<Response> {
|
|
69
66
|
throw new Error('实现将自动生成');
|
|
70
67
|
}
|
|
71
68
|
}
|
|
@@ -188,6 +185,10 @@ class UserService {
|
|
|
188
185
|
|
|
189
186
|
- `name`: 头部名称(可选 - 如果未提供则自动提取)
|
|
190
187
|
|
|
188
|
+
#### `@request()`
|
|
189
|
+
|
|
190
|
+
定义请求参数,将用作基础请求对象。这允许您传递一个完整的 FetcherRequest 对象来自定义请求配置。
|
|
191
|
+
|
|
191
192
|
**示例:**
|
|
192
193
|
|
|
193
194
|
```typescript
|
|
@@ -201,6 +202,11 @@ class UserService {
|
|
|
201
202
|
throw new Error('实现将自动生成');
|
|
202
203
|
}
|
|
203
204
|
|
|
205
|
+
@post('/users')
|
|
206
|
+
createUsers(@request() request: FetcherRequest): Promise<Response> {
|
|
207
|
+
throw new Error('实现将自动生成');
|
|
208
|
+
}
|
|
209
|
+
|
|
204
210
|
@put('/{id}')
|
|
205
211
|
updateUser(@path('id') id: number, @body() user: User): Promise<Response> {
|
|
206
212
|
throw new Error('实现将自动生成');
|
|
@@ -246,6 +252,32 @@ class ComplexService {
|
|
|
246
252
|
}
|
|
247
253
|
```
|
|
248
254
|
|
|
255
|
+
### 请求合并
|
|
256
|
+
|
|
257
|
+
当使用 `@request()` 装饰器时,提供的 `FetcherRequest` 对象会使用复杂的合并策略与端点特定配置进行合并:
|
|
258
|
+
|
|
259
|
+
- **嵌套对象**(路径、查询、头部)会被递归合并,参数值优先
|
|
260
|
+
- **基本值**(方法、请求体、超时、信号)来自参数请求的值会覆盖端点值
|
|
261
|
+
- **空对象**会被优雅地处理,回退到端点配置
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
|
|
265
|
+
@api('/users')
|
|
266
|
+
class UserService {
|
|
267
|
+
@post('/')
|
|
268
|
+
createUsers(
|
|
269
|
+
@body() user: User,
|
|
270
|
+
@request() request: FetcherRequest,
|
|
271
|
+
): Promise<Response> {
|
|
272
|
+
// 最终请求将合并:
|
|
273
|
+
// - 端点方法(POST)
|
|
274
|
+
// - 请求体参数(user 对象)
|
|
275
|
+
// - 来自 request 参数的任何配置
|
|
276
|
+
throw new Error('实现将自动生成');
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
249
281
|
## 🧪 测试
|
|
250
282
|
|
|
251
283
|
```bash
|