@ahoo-wang/fetcher-decorator 2.9.3 → 2.9.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 +87 -6
- package/README.zh-CN.md +81 -0
- package/dist/executeLifeCycle.d.ts +28 -0
- package/dist/executeLifeCycle.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +147 -142
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/requestExecutor.d.ts +57 -25
- package/dist/requestExecutor.d.ts.map +1 -1
- package/package.json +4 -4
package/dist/index.es.js
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
import { HttpMethod as
|
|
1
|
+
import { HttpMethod as h, getFetcher as w, combineURLs as O, JsonResultExtractor as D, mergeRecordToMap as l, mergeRequest as H } from "@ahoo-wang/fetcher";
|
|
2
2
|
import "reflect-metadata";
|
|
3
|
-
const
|
|
4
|
-
function
|
|
5
|
-
return function(n,
|
|
6
|
-
const
|
|
3
|
+
const T = Symbol("endpoint:metadata");
|
|
4
|
+
function p(t, e, r = {}) {
|
|
5
|
+
return function(n, a) {
|
|
6
|
+
const s = {
|
|
7
7
|
method: t,
|
|
8
8
|
path: e,
|
|
9
9
|
...r
|
|
10
10
|
};
|
|
11
11
|
Reflect.defineMetadata(
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
T,
|
|
13
|
+
s,
|
|
14
14
|
n,
|
|
15
|
-
|
|
15
|
+
a
|
|
16
16
|
);
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
19
|
function z(t = "", e = {}) {
|
|
20
|
-
return h
|
|
20
|
+
return p(h.GET, t, e);
|
|
21
21
|
}
|
|
22
22
|
function Z(t = "", e = {}) {
|
|
23
|
-
return h
|
|
23
|
+
return p(h.POST, t, e);
|
|
24
24
|
}
|
|
25
25
|
function F(t = "", e = {}) {
|
|
26
|
-
return h
|
|
26
|
+
return p(h.PUT, t, e);
|
|
27
27
|
}
|
|
28
28
|
function tt(t = "", e = {}) {
|
|
29
|
-
return h
|
|
29
|
+
return p(h.DELETE, t, e);
|
|
30
30
|
}
|
|
31
31
|
function et(t = "", e = {}) {
|
|
32
|
-
return h
|
|
32
|
+
return p(h.PATCH, t, e);
|
|
33
33
|
}
|
|
34
34
|
function rt(t = "", e = {}) {
|
|
35
|
-
return h
|
|
35
|
+
return p(h.HEAD, t, e);
|
|
36
36
|
}
|
|
37
37
|
function nt(t = "", e = {}) {
|
|
38
|
-
return h
|
|
38
|
+
return p(h.OPTIONS, t, e);
|
|
39
39
|
}
|
|
40
|
-
var
|
|
40
|
+
var P = /* @__PURE__ */ ((t) => (t.EXCHANGE = "Exchange", t.RESULT = "Result", t))(P || {});
|
|
41
41
|
const I = "__decorator_target__", U = "__decorator_metadata__";
|
|
42
42
|
class C {
|
|
43
43
|
/**
|
|
@@ -50,52 +50,57 @@ class C {
|
|
|
50
50
|
this.target = e, this.metadata = r;
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
53
|
+
* Executes the HTTP request based on the decorated method metadata.
|
|
54
|
+
*
|
|
55
|
+
* This method orchestrates the complete HTTP request lifecycle:
|
|
56
|
+
* 1. Resolves request configuration from metadata and runtime arguments
|
|
57
|
+
* 2. Sets up request attributes for interceptor access
|
|
58
|
+
* 3. Creates the FetchExchange object
|
|
59
|
+
* 4. Executes lifecycle hooks (beforeExecute)
|
|
60
|
+
* 5. Processes the request through the interceptor chain
|
|
61
|
+
* 6. Executes lifecycle hooks (afterExecute)
|
|
62
|
+
* 7. Extracts and returns the result based on endpoint return type
|
|
63
|
+
*
|
|
64
|
+
* @param args - The runtime arguments passed to the decorated method.
|
|
65
|
+
* These are mapped to request components (path variables, query params, body, etc.)
|
|
66
|
+
* based on parameter decorators applied to the method.
|
|
67
|
+
* @returns A Promise that resolves to the extracted result. The return type depends on:
|
|
68
|
+
* - EndpointReturnType.EXCHANGE: Returns the FetchExchange object directly
|
|
69
|
+
* - Otherwise: Returns the result of exchange.extractResult() (e.g., Response, parsed JSON)
|
|
70
|
+
* @throws Error if the request fails during execution or if lifecycle hooks/interceptors throw
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // Given a decorated service method:
|
|
75
|
+
* class UserService {
|
|
76
|
+
* @get('/users/{id}')
|
|
77
|
+
* getUser(@path('id') id: number): Promise<User> {
|
|
78
|
+
* // This method body is replaced by the executor at runtime
|
|
79
|
+
* }
|
|
80
|
+
* }
|
|
81
|
+
*
|
|
82
|
+
* // When calling:
|
|
83
|
+
* const userService = new UserService();
|
|
84
|
+
* const user = await userService.getUser(123);
|
|
85
|
+
*
|
|
86
|
+
* // The execute method will:
|
|
87
|
+
* // 1. Resolve the path to '/users/123'
|
|
88
|
+
* // 2. Create a GET request
|
|
89
|
+
* // 3. Execute the request using the configured fetcher
|
|
90
|
+
* // 4. Extract and return the parsed User object
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
88
93
|
async execute(e) {
|
|
89
94
|
const r = this.metadata.fetcher, n = this.metadata.resolveExchangeInit(e);
|
|
90
|
-
n.attributes
|
|
95
|
+
n.attributes.set(I, this.target), n.attributes.set(
|
|
91
96
|
U,
|
|
92
97
|
this.metadata
|
|
93
98
|
);
|
|
94
|
-
const
|
|
95
|
-
resultExtractor:
|
|
99
|
+
const a = this.metadata.resolveResultExtractor(), s = this.metadata.resolveEndpointReturnType(), o = r.resolveExchange(n.request, {
|
|
100
|
+
resultExtractor: a,
|
|
96
101
|
attributes: n.attributes
|
|
97
|
-
});
|
|
98
|
-
return
|
|
102
|
+
}), i = this.target;
|
|
103
|
+
return i.beforeExecute && await i.beforeExecute(o), await r.interceptors.exchange(o), i.afterExecute && await i.afterExecute(o), s === P.EXCHANGE ? o : await o.extractResult();
|
|
99
104
|
}
|
|
100
105
|
}
|
|
101
106
|
const m = /* @__PURE__ */ new WeakMap();
|
|
@@ -107,8 +112,8 @@ function N(t) {
|
|
|
107
112
|
try {
|
|
108
113
|
const e = t.toString().trim(), r = G(e);
|
|
109
114
|
if (!k(r)) {
|
|
110
|
-
const
|
|
111
|
-
return m.set(t,
|
|
115
|
+
const a = [];
|
|
116
|
+
return m.set(t, a), a;
|
|
112
117
|
}
|
|
113
118
|
const n = S(r);
|
|
114
119
|
return m.set(t, n), n;
|
|
@@ -121,11 +126,11 @@ function Y(t, e, r, n) {
|
|
|
121
126
|
if (n)
|
|
122
127
|
return n;
|
|
123
128
|
try {
|
|
124
|
-
const
|
|
125
|
-
if (
|
|
126
|
-
const
|
|
127
|
-
if (r <
|
|
128
|
-
return
|
|
129
|
+
const a = t[e];
|
|
130
|
+
if (a && typeof a == "function") {
|
|
131
|
+
const s = N(a);
|
|
132
|
+
if (r < s.length)
|
|
133
|
+
return s[r];
|
|
129
134
|
}
|
|
130
135
|
} catch {
|
|
131
136
|
}
|
|
@@ -135,72 +140,72 @@ function k(t) {
|
|
|
135
140
|
}
|
|
136
141
|
function G(t) {
|
|
137
142
|
if (t.startsWith("(")) {
|
|
138
|
-
const n =
|
|
143
|
+
const n = A(t, 0);
|
|
139
144
|
return n === -1 ? "" : t.substring(1, n);
|
|
140
145
|
}
|
|
141
146
|
const e = t.indexOf("(");
|
|
142
147
|
if (e === -1) return "";
|
|
143
|
-
const r =
|
|
148
|
+
const r = A(t, e);
|
|
144
149
|
return r === -1 ? "" : t.substring(e + 1, r);
|
|
145
150
|
}
|
|
146
|
-
function
|
|
151
|
+
function A(t, e) {
|
|
147
152
|
let r = 1;
|
|
148
153
|
for (let n = e + 1; n < t.length; n++) {
|
|
149
|
-
const
|
|
150
|
-
if (
|
|
154
|
+
const a = t[n];
|
|
155
|
+
if (a === "(")
|
|
151
156
|
r++;
|
|
152
|
-
else if (
|
|
157
|
+
else if (a === ")" && (r--, r === 0))
|
|
153
158
|
return n;
|
|
154
159
|
}
|
|
155
160
|
return -1;
|
|
156
161
|
}
|
|
157
162
|
function S(t) {
|
|
158
|
-
return t.split(",").map(
|
|
163
|
+
return t.split(",").map(v).filter(B).map(Q);
|
|
159
164
|
}
|
|
160
|
-
function
|
|
165
|
+
function v(t) {
|
|
161
166
|
return t.trim();
|
|
162
167
|
}
|
|
163
|
-
function
|
|
168
|
+
function B(t) {
|
|
164
169
|
return t.length > 0;
|
|
165
170
|
}
|
|
166
|
-
function
|
|
167
|
-
let e =
|
|
168
|
-
return e =
|
|
171
|
+
function Q(t) {
|
|
172
|
+
let e = L(t);
|
|
173
|
+
return e = j(e), e.trim();
|
|
169
174
|
}
|
|
170
|
-
function
|
|
175
|
+
function L(t) {
|
|
171
176
|
const e = t.indexOf("=");
|
|
172
177
|
return e !== -1 ? t.substring(0, e) : t;
|
|
173
178
|
}
|
|
174
|
-
function
|
|
179
|
+
function j(t) {
|
|
175
180
|
const e = t.indexOf(":");
|
|
176
181
|
return e !== -1 ? t.substring(0, e) : t;
|
|
177
182
|
}
|
|
178
|
-
var
|
|
179
|
-
const
|
|
183
|
+
var u = /* @__PURE__ */ ((t) => (t.PATH = "path", t.QUERY = "query", t.HEADER = "header", t.BODY = "body", t.REQUEST = "request", t.ATTRIBUTE = "attribute", t))(u || {});
|
|
184
|
+
const b = Symbol("parameter:metadata");
|
|
180
185
|
function f(t, e = "") {
|
|
181
|
-
return function(r, n,
|
|
182
|
-
const
|
|
186
|
+
return function(r, n, a) {
|
|
187
|
+
const s = Y(
|
|
183
188
|
r,
|
|
184
189
|
n,
|
|
185
|
-
|
|
190
|
+
a,
|
|
186
191
|
e
|
|
187
|
-
), o = Reflect.getMetadata(
|
|
192
|
+
), o = Reflect.getMetadata(b, r, n) || /* @__PURE__ */ new Map(), i = {
|
|
188
193
|
type: t,
|
|
189
|
-
name:
|
|
190
|
-
index:
|
|
194
|
+
name: s,
|
|
195
|
+
index: a
|
|
191
196
|
};
|
|
192
|
-
o.set(
|
|
193
|
-
|
|
197
|
+
o.set(a, i), Reflect.defineMetadata(
|
|
198
|
+
b,
|
|
194
199
|
o,
|
|
195
200
|
r,
|
|
196
201
|
n
|
|
197
202
|
);
|
|
198
203
|
};
|
|
199
204
|
}
|
|
200
|
-
function
|
|
205
|
+
function at(t = "") {
|
|
201
206
|
return f("path", t);
|
|
202
207
|
}
|
|
203
|
-
function
|
|
208
|
+
function st(t = "") {
|
|
204
209
|
return f("query", t);
|
|
205
210
|
}
|
|
206
211
|
function ot(t = "") {
|
|
@@ -230,8 +235,8 @@ class g {
|
|
|
230
235
|
* @param endpoint - Endpoint-level metadata
|
|
231
236
|
* @param parameters - Parameter metadata array
|
|
232
237
|
*/
|
|
233
|
-
constructor(e, r, n,
|
|
234
|
-
this.name = e, this.api = r, this.endpoint = n, this.parameters =
|
|
238
|
+
constructor(e, r, n, a) {
|
|
239
|
+
this.name = e, this.api = r, this.endpoint = n, this.parameters = a;
|
|
235
240
|
}
|
|
236
241
|
/**
|
|
237
242
|
* Gets the fetcher instance to use for this function.
|
|
@@ -273,7 +278,7 @@ class g {
|
|
|
273
278
|
return l(this.endpoint.attributes, e);
|
|
274
279
|
}
|
|
275
280
|
resolveEndpointReturnType() {
|
|
276
|
-
return this.endpoint.returnType || this.api.returnType ||
|
|
281
|
+
return this.endpoint.returnType || this.api.returnType || P.RESULT;
|
|
277
282
|
}
|
|
278
283
|
/**
|
|
279
284
|
* Resolves the request configuration from the method arguments.
|
|
@@ -322,41 +327,41 @@ class g {
|
|
|
322
327
|
* ```
|
|
323
328
|
*/
|
|
324
329
|
resolveExchangeInit(e) {
|
|
325
|
-
const r = {}, n = {},
|
|
330
|
+
const r = {}, n = {}, a = {
|
|
326
331
|
...this.api.headers,
|
|
327
332
|
...this.endpoint.headers
|
|
328
333
|
};
|
|
329
|
-
let
|
|
334
|
+
let s, o, i, E = {};
|
|
330
335
|
const R = this.resolveAttributes();
|
|
331
|
-
e.forEach((
|
|
332
|
-
if (
|
|
333
|
-
o =
|
|
336
|
+
e.forEach((c, _) => {
|
|
337
|
+
if (c instanceof AbortSignal) {
|
|
338
|
+
o = c;
|
|
334
339
|
return;
|
|
335
340
|
}
|
|
336
|
-
if (
|
|
337
|
-
|
|
341
|
+
if (c instanceof AbortController) {
|
|
342
|
+
i = c;
|
|
338
343
|
return;
|
|
339
344
|
}
|
|
340
345
|
const d = this.parameters.get(_);
|
|
341
346
|
if (d)
|
|
342
347
|
switch (d.type) {
|
|
343
|
-
case
|
|
344
|
-
this.processPathParam(d,
|
|
348
|
+
case u.PATH:
|
|
349
|
+
this.processPathParam(d, c, r);
|
|
345
350
|
break;
|
|
346
|
-
case
|
|
347
|
-
this.processQueryParam(d,
|
|
351
|
+
case u.QUERY:
|
|
352
|
+
this.processQueryParam(d, c, n);
|
|
348
353
|
break;
|
|
349
|
-
case
|
|
350
|
-
this.processHeaderParam(d,
|
|
354
|
+
case u.HEADER:
|
|
355
|
+
this.processHeaderParam(d, c, a);
|
|
351
356
|
break;
|
|
352
|
-
case
|
|
353
|
-
|
|
357
|
+
case u.BODY:
|
|
358
|
+
s = c;
|
|
354
359
|
break;
|
|
355
|
-
case
|
|
356
|
-
E = this.processRequestParam(
|
|
360
|
+
case u.REQUEST:
|
|
361
|
+
E = this.processRequestParam(c);
|
|
357
362
|
break;
|
|
358
|
-
case
|
|
359
|
-
this.processAttributeParam(d,
|
|
363
|
+
case u.ATTRIBUTE:
|
|
364
|
+
this.processAttributeParam(d, c, R);
|
|
360
365
|
break;
|
|
361
366
|
}
|
|
362
367
|
});
|
|
@@ -366,17 +371,17 @@ class g {
|
|
|
366
371
|
}, q = {
|
|
367
372
|
method: this.endpoint.method,
|
|
368
373
|
urlParams: y,
|
|
369
|
-
headers:
|
|
370
|
-
body:
|
|
374
|
+
headers: a,
|
|
375
|
+
body: s,
|
|
371
376
|
timeout: this.resolveTimeout(),
|
|
372
377
|
signal: o,
|
|
373
|
-
abortController:
|
|
374
|
-
},
|
|
378
|
+
abortController: i
|
|
379
|
+
}, x = H(
|
|
375
380
|
q,
|
|
376
381
|
E
|
|
377
382
|
), M = E.path;
|
|
378
|
-
return
|
|
379
|
-
request:
|
|
383
|
+
return x.url = this.resolvePath(M), {
|
|
384
|
+
request: x,
|
|
380
385
|
attributes: R
|
|
381
386
|
};
|
|
382
387
|
}
|
|
@@ -384,13 +389,13 @@ class g {
|
|
|
384
389
|
if (r == null)
|
|
385
390
|
return;
|
|
386
391
|
if (typeof r == "object") {
|
|
387
|
-
Object.entries(r).forEach(([
|
|
388
|
-
n[
|
|
392
|
+
Object.entries(r).forEach(([s, o]) => {
|
|
393
|
+
n[s] = o;
|
|
389
394
|
});
|
|
390
395
|
return;
|
|
391
396
|
}
|
|
392
|
-
const
|
|
393
|
-
n[
|
|
397
|
+
const a = e.name || `param${e.index}`;
|
|
398
|
+
n[a] = r;
|
|
394
399
|
}
|
|
395
400
|
processPathParam(e, r, n) {
|
|
396
401
|
this.processHttpParam(e, r, n);
|
|
@@ -448,28 +453,28 @@ function W(t, e, r) {
|
|
|
448
453
|
const n = t.prototype[e];
|
|
449
454
|
if (e === "constructor" || typeof n != "function")
|
|
450
455
|
return;
|
|
451
|
-
const
|
|
452
|
-
|
|
456
|
+
const a = Reflect.getMetadata(
|
|
457
|
+
T,
|
|
453
458
|
t.prototype,
|
|
454
459
|
e
|
|
455
460
|
);
|
|
456
|
-
if (!
|
|
461
|
+
if (!a)
|
|
457
462
|
return;
|
|
458
|
-
const
|
|
459
|
-
|
|
463
|
+
const s = Reflect.getMetadata(
|
|
464
|
+
b,
|
|
460
465
|
t.prototype,
|
|
461
466
|
e
|
|
462
467
|
) || /* @__PURE__ */ new Map(), o = new g(
|
|
463
468
|
e,
|
|
464
469
|
r,
|
|
465
|
-
|
|
466
|
-
|
|
470
|
+
a,
|
|
471
|
+
s
|
|
467
472
|
);
|
|
468
|
-
t.prototype[e] = async function(...
|
|
473
|
+
t.prototype[e] = async function(...i) {
|
|
469
474
|
return await X(
|
|
470
475
|
this,
|
|
471
476
|
o
|
|
472
|
-
).execute(
|
|
477
|
+
).execute(i);
|
|
473
478
|
};
|
|
474
479
|
}
|
|
475
480
|
function X(t, e) {
|
|
@@ -478,15 +483,15 @@ function X(t, e) {
|
|
|
478
483
|
let n = r.get(e.name);
|
|
479
484
|
if (n)
|
|
480
485
|
return n;
|
|
481
|
-
const
|
|
486
|
+
const a = t.apiMetadata, s = {
|
|
482
487
|
...e.api,
|
|
483
|
-
...
|
|
488
|
+
...a
|
|
484
489
|
};
|
|
485
490
|
return n = new C(
|
|
486
491
|
t,
|
|
487
492
|
new g(
|
|
488
493
|
e.name,
|
|
489
|
-
|
|
494
|
+
s,
|
|
490
495
|
e.endpoint,
|
|
491
496
|
e.parameters
|
|
492
497
|
)
|
|
@@ -498,8 +503,8 @@ function ht(t = "", e = {}) {
|
|
|
498
503
|
basePath: t,
|
|
499
504
|
...e
|
|
500
505
|
};
|
|
501
|
-
return Reflect.defineMetadata(K, n, r), Object.getOwnPropertyNames(r.prototype).forEach((
|
|
502
|
-
W(r,
|
|
506
|
+
return Reflect.defineMetadata(K, n, r), Object.getOwnPropertyNames(r.prototype).forEach((a) => {
|
|
507
|
+
W(r, a, n);
|
|
503
508
|
}), r;
|
|
504
509
|
};
|
|
505
510
|
}
|
|
@@ -514,11 +519,11 @@ export {
|
|
|
514
519
|
J as AutoGenerated,
|
|
515
520
|
U as DECORATOR_METADATA_ATTRIBUTE_KEY,
|
|
516
521
|
I as DECORATOR_TARGET_ATTRIBUTE_KEY,
|
|
517
|
-
|
|
518
|
-
|
|
522
|
+
T as ENDPOINT_METADATA_KEY,
|
|
523
|
+
P as EndpointReturnType,
|
|
519
524
|
g as FunctionMetadata,
|
|
520
|
-
|
|
521
|
-
|
|
525
|
+
b as PARAMETER_METADATA_KEY,
|
|
526
|
+
u as ParameterType,
|
|
522
527
|
C as RequestExecutor,
|
|
523
528
|
ht as api,
|
|
524
529
|
ut as attribute,
|
|
@@ -526,7 +531,7 @@ export {
|
|
|
526
531
|
it as body,
|
|
527
532
|
X as buildRequestExecutor,
|
|
528
533
|
tt as del,
|
|
529
|
-
|
|
534
|
+
p as endpoint,
|
|
530
535
|
z as get,
|
|
531
536
|
Y as getParameterName,
|
|
532
537
|
N as getParameterNames,
|
|
@@ -535,10 +540,10 @@ export {
|
|
|
535
540
|
nt as options,
|
|
536
541
|
f as parameter,
|
|
537
542
|
et as patch,
|
|
538
|
-
|
|
543
|
+
at as path,
|
|
539
544
|
Z as post,
|
|
540
545
|
F as put,
|
|
541
|
-
|
|
546
|
+
st as query,
|
|
542
547
|
ct as request
|
|
543
548
|
};
|
|
544
549
|
//# sourceMappingURL=index.es.js.map
|