@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/dist/index.es.js CHANGED
@@ -1,43 +1,43 @@
1
- import { HttpMethod as u, getFetcher as w, combineURLs as O, JsonResultExtractor as D, mergeRecordToMap as l, mergeRequest as H } from "@ahoo-wang/fetcher";
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 x = Symbol("endpoint:metadata");
4
- function h(t, e, r = {}) {
5
- return function(n, s) {
6
- const a = {
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
- x,
13
- a,
12
+ T,
13
+ s,
14
14
  n,
15
- s
15
+ a
16
16
  );
17
17
  };
18
18
  }
19
19
  function z(t = "", e = {}) {
20
- return h(u.GET, t, e);
20
+ return p(h.GET, t, e);
21
21
  }
22
22
  function Z(t = "", e = {}) {
23
- return h(u.POST, t, e);
23
+ return p(h.POST, t, e);
24
24
  }
25
25
  function F(t = "", e = {}) {
26
- return h(u.PUT, t, e);
26
+ return p(h.PUT, t, e);
27
27
  }
28
28
  function tt(t = "", e = {}) {
29
- return h(u.DELETE, t, e);
29
+ return p(h.DELETE, t, e);
30
30
  }
31
31
  function et(t = "", e = {}) {
32
- return h(u.PATCH, t, e);
32
+ return p(h.PATCH, t, e);
33
33
  }
34
34
  function rt(t = "", e = {}) {
35
- return h(u.HEAD, t, e);
35
+ return p(h.HEAD, t, e);
36
36
  }
37
37
  function nt(t = "", e = {}) {
38
- return h(u.OPTIONS, t, e);
38
+ return p(h.OPTIONS, t, e);
39
39
  }
40
- var b = /* @__PURE__ */ ((t) => (t.EXCHANGE = "Exchange", t.RESULT = "Result", t))(b || {});
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
- * Executes the HTTP request.
54
- *
55
- * This method resolves the path and request configuration from the metadata
56
- * and arguments, then executes the request using the configured fetcher.
57
- * It handles the complete request lifecycle from parameter processing to
58
- * response extraction.
59
- *
60
-
61
- * @param args - The runtime arguments passed to the decorated method.
62
- * These are mapped to request components based on parameter decorators.
63
- * @returns A Promise that resolves to the extracted result based on the configured result extractor.
64
- * By default, this is the Response object, but can be customized to return
65
- * parsed JSON, the raw exchange object, or any other transformed result.
66
- *
67
- * @example
68
- * ```typescript
69
- * // Given a decorated service method:
70
- * class UserService {
71
- * @get('/users/{id}')
72
- * getUser(@path('id') id: number): Promise<Response> {
73
- * // This method body is replaced by the executor at runtime
74
- * }
75
- * }
76
- *
77
- * // When calling:
78
- * const userService = new UserService();
79
- * const response = await userService.getUser(123);
80
- *
81
- * // The execute method will:
82
- * // 1. Resolve the path to '/users/123'
83
- * // 2. Create a request with method 'GET'
84
- * // 3. Execute the request using the configured fetcher
85
- * // 4. Return the Response object
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?.set(I, this.target), n.attributes?.set(
95
+ n.attributes.set(I, this.target), n.attributes.set(
91
96
  U,
92
97
  this.metadata
93
98
  );
94
- const s = this.metadata.resolveResultExtractor(), a = this.metadata.resolveEndpointReturnType(), o = await r.exchange(n.request, {
95
- resultExtractor: s,
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 a === b.EXCHANGE ? o : await o.extractResult();
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 s = [];
111
- return m.set(t, s), s;
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 s = t[e];
125
- if (s && typeof s == "function") {
126
- const a = N(s);
127
- if (r < a.length)
128
- return a[r];
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 = T(t, 0);
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 = T(t, e);
148
+ const r = A(t, e);
144
149
  return r === -1 ? "" : t.substring(e + 1, r);
145
150
  }
146
- function T(t, e) {
151
+ function A(t, e) {
147
152
  let r = 1;
148
153
  for (let n = e + 1; n < t.length; n++) {
149
- const s = t[n];
150
- if (s === "(")
154
+ const a = t[n];
155
+ if (a === "(")
151
156
  r++;
152
- else if (s === ")" && (r--, r === 0))
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(B).filter(Q).map(v);
163
+ return t.split(",").map(v).filter(B).map(Q);
159
164
  }
160
- function B(t) {
165
+ function v(t) {
161
166
  return t.trim();
162
167
  }
163
- function Q(t) {
168
+ function B(t) {
164
169
  return t.length > 0;
165
170
  }
166
- function v(t) {
167
- let e = j(t);
168
- return e = L(e), e.trim();
171
+ function Q(t) {
172
+ let e = L(t);
173
+ return e = j(e), e.trim();
169
174
  }
170
- function j(t) {
175
+ function L(t) {
171
176
  const e = t.indexOf("=");
172
177
  return e !== -1 ? t.substring(0, e) : t;
173
178
  }
174
- function L(t) {
179
+ function j(t) {
175
180
  const e = t.indexOf(":");
176
181
  return e !== -1 ? t.substring(0, e) : t;
177
182
  }
178
- var c = /* @__PURE__ */ ((t) => (t.PATH = "path", t.QUERY = "query", t.HEADER = "header", t.BODY = "body", t.REQUEST = "request", t.ATTRIBUTE = "attribute", t))(c || {});
179
- const P = Symbol("parameter:metadata");
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, s) {
182
- const a = Y(
186
+ return function(r, n, a) {
187
+ const s = Y(
183
188
  r,
184
189
  n,
185
- s,
190
+ a,
186
191
  e
187
- ), o = Reflect.getMetadata(P, r, n) || /* @__PURE__ */ new Map(), p = {
192
+ ), o = Reflect.getMetadata(b, r, n) || /* @__PURE__ */ new Map(), i = {
188
193
  type: t,
189
- name: a,
190
- index: s
194
+ name: s,
195
+ index: a
191
196
  };
192
- o.set(s, p), Reflect.defineMetadata(
193
- P,
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 st(t = "") {
205
+ function at(t = "") {
201
206
  return f("path", t);
202
207
  }
203
- function at(t = "") {
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, s) {
234
- this.name = e, this.api = r, this.endpoint = n, this.parameters = s;
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 || b.RESULT;
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 = {}, s = {
330
+ const r = {}, n = {}, a = {
326
331
  ...this.api.headers,
327
332
  ...this.endpoint.headers
328
333
  };
329
- let a, o, p, E = {};
334
+ let s, o, i, E = {};
330
335
  const R = this.resolveAttributes();
331
- e.forEach((i, _) => {
332
- if (i instanceof AbortSignal) {
333
- o = i;
336
+ e.forEach((c, _) => {
337
+ if (c instanceof AbortSignal) {
338
+ o = c;
334
339
  return;
335
340
  }
336
- if (i instanceof AbortController) {
337
- p = i;
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 c.PATH:
344
- this.processPathParam(d, i, r);
348
+ case u.PATH:
349
+ this.processPathParam(d, c, r);
345
350
  break;
346
- case c.QUERY:
347
- this.processQueryParam(d, i, n);
351
+ case u.QUERY:
352
+ this.processQueryParam(d, c, n);
348
353
  break;
349
- case c.HEADER:
350
- this.processHeaderParam(d, i, s);
354
+ case u.HEADER:
355
+ this.processHeaderParam(d, c, a);
351
356
  break;
352
- case c.BODY:
353
- a = i;
357
+ case u.BODY:
358
+ s = c;
354
359
  break;
355
- case c.REQUEST:
356
- E = this.processRequestParam(i);
360
+ case u.REQUEST:
361
+ E = this.processRequestParam(c);
357
362
  break;
358
- case c.ATTRIBUTE:
359
- this.processAttributeParam(d, i, R);
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: s,
370
- body: a,
374
+ headers: a,
375
+ body: s,
371
376
  timeout: this.resolveTimeout(),
372
377
  signal: o,
373
- abortController: p
374
- }, A = H(
378
+ abortController: i
379
+ }, x = H(
375
380
  q,
376
381
  E
377
382
  ), M = E.path;
378
- return A.url = this.resolvePath(M), {
379
- request: A,
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(([a, o]) => {
388
- n[a] = o;
392
+ Object.entries(r).forEach(([s, o]) => {
393
+ n[s] = o;
389
394
  });
390
395
  return;
391
396
  }
392
- const s = e.name || `param${e.index}`;
393
- n[s] = r;
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 s = Reflect.getMetadata(
452
- x,
456
+ const a = Reflect.getMetadata(
457
+ T,
453
458
  t.prototype,
454
459
  e
455
460
  );
456
- if (!s)
461
+ if (!a)
457
462
  return;
458
- const a = Reflect.getMetadata(
459
- P,
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
- s,
466
- a
470
+ a,
471
+ s
467
472
  );
468
- t.prototype[e] = async function(...p) {
473
+ t.prototype[e] = async function(...i) {
469
474
  return await X(
470
475
  this,
471
476
  o
472
- ).execute(p);
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 s = t.apiMetadata, a = {
486
+ const a = t.apiMetadata, s = {
482
487
  ...e.api,
483
- ...s
488
+ ...a
484
489
  };
485
490
  return n = new C(
486
491
  t,
487
492
  new g(
488
493
  e.name,
489
- a,
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((s) => {
502
- W(r, s, n);
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
- x as ENDPOINT_METADATA_KEY,
518
- b as EndpointReturnType,
522
+ T as ENDPOINT_METADATA_KEY,
523
+ P as EndpointReturnType,
519
524
  g as FunctionMetadata,
520
- P as PARAMETER_METADATA_KEY,
521
- c as ParameterType,
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
- h as endpoint,
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
- st as path,
543
+ at as path,
539
544
  Z as post,
540
545
  F as put,
541
- at as query,
546
+ st as query,
542
547
  ct as request
543
548
  };
544
549
  //# sourceMappingURL=index.es.js.map