@decoraxios/awe-axios-core 0.3.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/dist/index.cjs ADDED
@@ -0,0 +1,851 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // src/index.ts
32
+ var index_exports = {};
33
+ __export(index_exports, {
34
+ AxiosRef: () => AxiosRef,
35
+ BodyParam: () => BodyParam,
36
+ Debounce: () => Debounce,
37
+ Delete: () => Delete,
38
+ Get: () => Get,
39
+ Head: () => Head,
40
+ HttpApi: () => HttpApi,
41
+ Options: () => Options,
42
+ Patch: () => Patch,
43
+ PathParam: () => PathParam,
44
+ Post: () => Post,
45
+ Put: () => Put,
46
+ QueryParam: () => QueryParam,
47
+ RefAxios: () => RefAxios,
48
+ Retry: () => Retry,
49
+ SignalController: () => SignalController,
50
+ Throttle: () => Throttle,
51
+ TransformRequest: () => TransformRequest,
52
+ TransformResponse: () => TransformResponse,
53
+ axiosPlus: () => axiosPlus,
54
+ composeDecoratedUrl: () => composeDecoratedUrl,
55
+ createDebouncePlugin: () => createDebouncePlugin,
56
+ createRetryPlugin: () => createRetryPlugin,
57
+ createThrottlePlugin: () => createThrottlePlugin,
58
+ executeHttpCall: () => executeHttpCall,
59
+ isAbsoluteHttpUrl: () => isAbsoluteHttpUrl,
60
+ joinPathname: () => joinPathname,
61
+ resolveAbsoluteHttpTarget: () => resolveAbsoluteHttpTarget,
62
+ resolvePathParams: () => resolvePathParams,
63
+ useDebounce: () => useDebounce,
64
+ useRequest: () => useRequest,
65
+ useRetry: () => useRetry,
66
+ useThrottle: () => useThrottle,
67
+ withHttpClassConfig: () => withHttpClassConfig,
68
+ withHttpClassPlugins: () => withHttpClassPlugins,
69
+ withHttpMethodConfig: () => withHttpMethodConfig,
70
+ withHttpMethodPlugins: () => withHttpMethodPlugins
71
+ });
72
+ module.exports = __toCommonJS(index_exports);
73
+ var import_axios3 = __toESM(require("axios"), 1);
74
+
75
+ // src/utils/config.ts
76
+ var import_axios = require("axios");
77
+ function normalizeArray(value) {
78
+ if (value === void 0) {
79
+ return [];
80
+ }
81
+ return Array.isArray(value) ? value : [
82
+ value
83
+ ];
84
+ }
85
+ __name(normalizeArray, "normalizeArray");
86
+ function isPlainObject(value) {
87
+ return Object.prototype.toString.call(value) === "[object Object]";
88
+ }
89
+ __name(isPlainObject, "isPlainObject");
90
+ function toPlainHeaders(headers) {
91
+ if (!headers) {
92
+ return void 0;
93
+ }
94
+ if (headers instanceof import_axios.AxiosHeaders) {
95
+ return headers.toJSON();
96
+ }
97
+ const maybeSerializable = headers;
98
+ if (typeof maybeSerializable.toJSON === "function") {
99
+ return maybeSerializable.toJSON();
100
+ }
101
+ return {
102
+ ...headers
103
+ };
104
+ }
105
+ __name(toPlainHeaders, "toPlainHeaders");
106
+ function mergeHeaders(...headersList) {
107
+ const merged = headersList.reduce((accumulator, current) => {
108
+ const plainHeaders = toPlainHeaders(current);
109
+ if (plainHeaders) {
110
+ Object.assign(accumulator, plainHeaders);
111
+ }
112
+ return accumulator;
113
+ }, {});
114
+ return Object.keys(merged).length > 0 ? merged : void 0;
115
+ }
116
+ __name(mergeHeaders, "mergeHeaders");
117
+ function mergeHttpConfigPatch(base, patch) {
118
+ const next = {
119
+ ...base,
120
+ ...patch
121
+ };
122
+ const headers = mergeHeaders(base.headers, patch.headers);
123
+ if (headers) {
124
+ next.headers = headers;
125
+ }
126
+ const transformRequest = [
127
+ ...normalizeArray(base.transformRequest),
128
+ ...normalizeArray(patch.transformRequest)
129
+ ];
130
+ if (transformRequest.length > 0) {
131
+ next.transformRequest = transformRequest;
132
+ }
133
+ const transformResponse = [
134
+ ...normalizeArray(base.transformResponse),
135
+ ...normalizeArray(patch.transformResponse)
136
+ ];
137
+ if (transformResponse.length > 0) {
138
+ next.transformResponse = transformResponse;
139
+ }
140
+ const plugins = [
141
+ ...base.plugins ?? [],
142
+ ...patch.plugins ?? []
143
+ ];
144
+ if (plugins.length > 0) {
145
+ next.plugins = plugins;
146
+ }
147
+ return next;
148
+ }
149
+ __name(mergeHttpConfigPatch, "mergeHttpConfigPatch");
150
+ function mergeRecords(...values) {
151
+ const merged = values.reduce((accumulator, current) => {
152
+ if (current) {
153
+ Object.assign(accumulator, current);
154
+ }
155
+ return accumulator;
156
+ }, {});
157
+ return Object.keys(merged).length > 0 ? merged : void 0;
158
+ }
159
+ __name(mergeRecords, "mergeRecords");
160
+ function toAxiosRequestConfig(config) {
161
+ const { refAxios: _refAxios, methodId: _methodId, plugins: _plugins, ...axiosConfig } = config;
162
+ return axiosConfig;
163
+ }
164
+ __name(toAxiosRequestConfig, "toAxiosRequestConfig");
165
+
166
+ // src/metadata.ts
167
+ var classConfigStore = /* @__PURE__ */ new WeakMap();
168
+ var methodConfigStore = /* @__PURE__ */ new WeakMap();
169
+ var paramBindingStore = /* @__PURE__ */ new WeakMap();
170
+ var wrappedStore = /* @__PURE__ */ new WeakMap();
171
+ var methodCounter = 0;
172
+ function getOrCreateMethodMap(target) {
173
+ let map = methodConfigStore.get(target);
174
+ if (!map) {
175
+ map = /* @__PURE__ */ new Map();
176
+ methodConfigStore.set(target, map);
177
+ }
178
+ return map;
179
+ }
180
+ __name(getOrCreateMethodMap, "getOrCreateMethodMap");
181
+ function getOrCreateParamMap(target) {
182
+ let map = paramBindingStore.get(target);
183
+ if (!map) {
184
+ map = /* @__PURE__ */ new Map();
185
+ paramBindingStore.set(target, map);
186
+ }
187
+ return map;
188
+ }
189
+ __name(getOrCreateParamMap, "getOrCreateParamMap");
190
+ function createMethodId(propertyKey) {
191
+ methodCounter += 1;
192
+ return `${String(propertyKey)}_${methodCounter}`;
193
+ }
194
+ __name(createMethodId, "createMethodId");
195
+ function getClassConfig(target) {
196
+ return classConfigStore.get(target);
197
+ }
198
+ __name(getClassConfig, "getClassConfig");
199
+ function mergeClassConfig(target, patch) {
200
+ const current = classConfigStore.get(target) ?? {};
201
+ const next = mergeHttpConfigPatch(current, patch);
202
+ classConfigStore.set(target, next);
203
+ return next;
204
+ }
205
+ __name(mergeClassConfig, "mergeClassConfig");
206
+ function getMethodConfig(target, propertyKey) {
207
+ return methodConfigStore.get(target)?.get(propertyKey);
208
+ }
209
+ __name(getMethodConfig, "getMethodConfig");
210
+ function mergeMethodConfig(target, propertyKey, patch) {
211
+ const map = getOrCreateMethodMap(target);
212
+ const current = map.get(propertyKey) ?? {
213
+ methodId: createMethodId(propertyKey)
214
+ };
215
+ const next = mergeHttpConfigPatch(current, patch);
216
+ map.set(propertyKey, next);
217
+ return next;
218
+ }
219
+ __name(mergeMethodConfig, "mergeMethodConfig");
220
+ function addParamBinding(target, propertyKey, binding) {
221
+ const map = getOrCreateParamMap(target);
222
+ const bindings = map.get(propertyKey) ?? [];
223
+ bindings.push(binding);
224
+ bindings.sort((left, right) => left.index - right.index);
225
+ map.set(propertyKey, bindings);
226
+ }
227
+ __name(addParamBinding, "addParamBinding");
228
+ function getParamBindings(target, propertyKey) {
229
+ return [
230
+ ...paramBindingStore.get(target)?.get(propertyKey) ?? []
231
+ ];
232
+ }
233
+ __name(getParamBindings, "getParamBindings");
234
+ function isMethodWrapped(target, propertyKey) {
235
+ return wrappedStore.get(target)?.has(propertyKey) ?? false;
236
+ }
237
+ __name(isMethodWrapped, "isMethodWrapped");
238
+ function markMethodWrapped(target, propertyKey) {
239
+ let wrapped = wrappedStore.get(target);
240
+ if (!wrapped) {
241
+ wrapped = /* @__PURE__ */ new Set();
242
+ wrappedStore.set(target, wrapped);
243
+ }
244
+ wrapped.add(propertyKey);
245
+ }
246
+ __name(markMethodWrapped, "markMethodWrapped");
247
+
248
+ // src/runtime/request.ts
249
+ var import_axios2 = __toESM(require("axios"), 1);
250
+
251
+ // src/utils/path.ts
252
+ function isAbsoluteHttpUrl(value) {
253
+ return typeof value === "string" && /^[a-z][a-z\d+\-.]*:\/\//i.test(value);
254
+ }
255
+ __name(isAbsoluteHttpUrl, "isAbsoluteHttpUrl");
256
+ function trimSegment(value) {
257
+ return value.trim().replace(/^\/+|\/+$/g, "");
258
+ }
259
+ __name(trimSegment, "trimSegment");
260
+ function joinPathname(...parts) {
261
+ const filtered = parts.filter((part) => typeof part === "string" && part.trim().length > 0);
262
+ if (filtered.length === 0) {
263
+ return "";
264
+ }
265
+ const leadingSlash = filtered.some((part) => part.startsWith("/"));
266
+ const segments = filtered.map(trimSegment).filter(Boolean);
267
+ if (segments.length === 0) {
268
+ return leadingSlash ? "/" : "";
269
+ }
270
+ return `${leadingSlash ? "/" : ""}${segments.join("/")}`;
271
+ }
272
+ __name(joinPathname, "joinPathname");
273
+ function composeDecoratedUrl(classUrl, methodUrl) {
274
+ if (isAbsoluteHttpUrl(methodUrl)) {
275
+ return methodUrl;
276
+ }
277
+ if (isAbsoluteHttpUrl(classUrl)) {
278
+ const parsed = new URL(classUrl);
279
+ if (!methodUrl) {
280
+ return classUrl;
281
+ }
282
+ return `${parsed.origin}${joinPathname(parsed.pathname, methodUrl)}`;
283
+ }
284
+ return joinPathname(classUrl, methodUrl);
285
+ }
286
+ __name(composeDecoratedUrl, "composeDecoratedUrl");
287
+ function resolvePathParams(url, params) {
288
+ if (!url) {
289
+ return url;
290
+ }
291
+ return url.replace(/:([\w-]+)/g, (placeholder, key) => {
292
+ const value = params[key];
293
+ if (value === void 0 || value === null) {
294
+ return placeholder;
295
+ }
296
+ return encodeURIComponent(String(value));
297
+ });
298
+ }
299
+ __name(resolvePathParams, "resolvePathParams");
300
+ function resolveAbsoluteHttpTarget(baseURL, url) {
301
+ if (isAbsoluteHttpUrl(url)) {
302
+ const parsed2 = new URL(url);
303
+ return {
304
+ origin: parsed2.origin,
305
+ pathname: joinPathname(parsed2.pathname)
306
+ };
307
+ }
308
+ if (!baseURL || !isAbsoluteHttpUrl(baseURL)) {
309
+ throw new Error("Mock support requires an absolute baseURL or an absolute request URL.");
310
+ }
311
+ const parsed = new URL(baseURL);
312
+ return {
313
+ origin: parsed.origin,
314
+ pathname: joinPathname(parsed.pathname, url)
315
+ };
316
+ }
317
+ __name(resolveAbsoluteHttpTarget, "resolveAbsoluteHttpTarget");
318
+
319
+ // src/runtime/params.ts
320
+ function assignMultiValue(target, key, value) {
321
+ const current = target[key];
322
+ if (current === void 0) {
323
+ target[key] = value;
324
+ return;
325
+ }
326
+ if (Array.isArray(current)) {
327
+ current.push(value);
328
+ return;
329
+ }
330
+ target[key] = [
331
+ current,
332
+ value
333
+ ];
334
+ }
335
+ __name(assignMultiValue, "assignMultiValue");
336
+ function extractBoundParams(bindings, args) {
337
+ const pathParams = {};
338
+ const queryParams = {};
339
+ const namedBody = {};
340
+ const unnamedBody = [];
341
+ for (const binding of bindings) {
342
+ const value = args[binding.index];
343
+ if (binding.kind === "path" && binding.name) {
344
+ pathParams[binding.name] = value;
345
+ continue;
346
+ }
347
+ if (binding.kind === "query" && binding.name) {
348
+ assignMultiValue(queryParams, binding.name, value);
349
+ continue;
350
+ }
351
+ if (binding.kind === "body") {
352
+ if (binding.name) {
353
+ namedBody[binding.name] = value;
354
+ } else {
355
+ unnamedBody.push(value);
356
+ }
357
+ }
358
+ }
359
+ let body = void 0;
360
+ if (Object.keys(namedBody).length > 0) {
361
+ const mergedUnnamed = unnamedBody.filter(isPlainObject).reduce((accumulator, current) => {
362
+ Object.assign(accumulator, current);
363
+ return accumulator;
364
+ }, {});
365
+ body = {
366
+ ...mergedUnnamed,
367
+ ...namedBody
368
+ };
369
+ } else if (unnamedBody.length === 1) {
370
+ body = unnamedBody[0];
371
+ } else if (unnamedBody.length > 1) {
372
+ if (unnamedBody.every(isPlainObject)) {
373
+ body = unnamedBody.reduce((accumulator, current) => {
374
+ Object.assign(accumulator, current);
375
+ return accumulator;
376
+ }, {});
377
+ } else {
378
+ body = unnamedBody;
379
+ }
380
+ }
381
+ return {
382
+ pathParams,
383
+ queryParams,
384
+ body
385
+ };
386
+ }
387
+ __name(extractBoundParams, "extractBoundParams");
388
+
389
+ // src/runtime/request.ts
390
+ function resolveClassConfig(instance, target) {
391
+ if (instance && typeof instance === "object") {
392
+ const ctor = instance.constructor;
393
+ if (ctor) {
394
+ return getClassConfig(ctor);
395
+ }
396
+ }
397
+ return getClassConfig(target);
398
+ }
399
+ __name(resolveClassConfig, "resolveClassConfig");
400
+ function buildResolvedRequestConfig(instance, target, propertyKey, args) {
401
+ const methodConfig = getMethodConfig(target, propertyKey);
402
+ if (!methodConfig?.method) {
403
+ throw new Error(`Method "${String(propertyKey)}" is missing an HTTP decorator.`);
404
+ }
405
+ const classConfig = resolveClassConfig(instance, target) ?? {};
406
+ const { pathParams, queryParams, body } = extractBoundParams(getParamBindings(target, propertyKey), args);
407
+ const refAxios = methodConfig.refAxios ?? classConfig.refAxios ?? import_axios2.default;
408
+ const baseURL = methodConfig.baseURL ?? classConfig.baseURL ?? refAxios.defaults.baseURL;
409
+ const composedUrl = composeDecoratedUrl(classConfig.url, methodConfig.url);
410
+ const url = resolvePathParams(composedUrl, pathParams);
411
+ if (!baseURL && !isAbsoluteHttpUrl(url)) {
412
+ throw new Error(`Method "${String(propertyKey)}" requires a baseURL or an absolute URL.`);
413
+ }
414
+ const params = mergeRecords(classConfig.params, methodConfig.params, queryParams);
415
+ const plugins = [
416
+ ...classConfig.plugins ?? [],
417
+ ...methodConfig.plugins ?? []
418
+ ];
419
+ const transformRequest = [
420
+ ...normalizeArray(refAxios.defaults.transformRequest),
421
+ ...normalizeArray(classConfig.transformRequest),
422
+ ...normalizeArray(methodConfig.transformRequest)
423
+ ];
424
+ const transformResponse = [
425
+ ...normalizeArray(refAxios.defaults.transformResponse),
426
+ ...normalizeArray(classConfig.transformResponse),
427
+ ...normalizeArray(methodConfig.transformResponse)
428
+ ];
429
+ return {
430
+ ...classConfig,
431
+ ...methodConfig,
432
+ baseURL,
433
+ url,
434
+ params,
435
+ data: body !== void 0 ? body : methodConfig.data ?? classConfig.data,
436
+ headers: mergeHeaders(classConfig.headers, methodConfig.headers),
437
+ transformRequest: transformRequest.length > 0 ? transformRequest : void 0,
438
+ transformResponse: transformResponse.length > 0 ? transformResponse : void 0,
439
+ refAxios,
440
+ plugins,
441
+ methodId: methodConfig.methodId,
442
+ method: methodConfig.method
443
+ };
444
+ }
445
+ __name(buildResolvedRequestConfig, "buildResolvedRequestConfig");
446
+ var baseExecutor = /* @__PURE__ */ __name((config) => {
447
+ return config.refAxios.request(toAxiosRequestConfig(config));
448
+ }, "baseExecutor");
449
+ function executeHttpCall(instance, target, propertyKey, originalMethod, args) {
450
+ const resolvedConfig = buildResolvedRequestConfig(instance, target, propertyKey, args);
451
+ const context = {
452
+ instance,
453
+ target,
454
+ propertyKey,
455
+ methodId: resolvedConfig.methodId,
456
+ args,
457
+ originalMethod
458
+ };
459
+ const executor = [
460
+ ...resolvedConfig.plugins ?? []
461
+ ].reverse().reduce((next, plugin) => {
462
+ return plugin(next, context);
463
+ }, baseExecutor);
464
+ return executor(resolvedConfig);
465
+ }
466
+ __name(executeHttpCall, "executeHttpCall");
467
+
468
+ // src/decorators/http.ts
469
+ function isAxiosInstance(value) {
470
+ return !!value && typeof value === "object" && typeof value.request === "function";
471
+ }
472
+ __name(isAxiosInstance, "isAxiosInstance");
473
+ function normalizeMethodConfig(config, method) {
474
+ if (typeof config === "string") {
475
+ return {
476
+ method,
477
+ url: config
478
+ };
479
+ }
480
+ return {
481
+ ...config ?? {},
482
+ method
483
+ };
484
+ }
485
+ __name(normalizeMethodConfig, "normalizeMethodConfig");
486
+ function normalizeClassConfig(config) {
487
+ if (typeof config === "string") {
488
+ return isAbsoluteHttpUrl(config) ? {
489
+ baseURL: config
490
+ } : {
491
+ url: config
492
+ };
493
+ }
494
+ if (isAxiosInstance(config)) {
495
+ return {
496
+ refAxios: config
497
+ };
498
+ }
499
+ return config ?? {};
500
+ }
501
+ __name(normalizeClassConfig, "normalizeClassConfig");
502
+ function wrapHttpMethod(target, propertyKey, descriptor) {
503
+ if (isMethodWrapped(target, propertyKey)) {
504
+ return descriptor;
505
+ }
506
+ const originalMethod = descriptor.value;
507
+ if (typeof originalMethod !== "function") {
508
+ throw new Error(`"${String(propertyKey)}" must be a method to use HTTP decorators.`);
509
+ }
510
+ descriptor.value = /* @__PURE__ */ __name(function wrappedHttpMethod(...args) {
511
+ return executeHttpCall(this, target, propertyKey, originalMethod, args);
512
+ }, "wrappedHttpMethod");
513
+ markMethodWrapped(target, propertyKey);
514
+ return descriptor;
515
+ }
516
+ __name(wrapHttpMethod, "wrapHttpMethod");
517
+ function createHttpMethodDecorator(method) {
518
+ return (config) => {
519
+ return (target, propertyKey, descriptor) => {
520
+ if (propertyKey === void 0 || descriptor === void 0) {
521
+ throw new Error(`"${method}" can only be used on instance methods.`);
522
+ }
523
+ const existing = getMethodConfig(target, propertyKey);
524
+ if (existing?.method && existing.method !== method) {
525
+ throw new Error(`"${String(propertyKey)}" already has an HTTP decorator.`);
526
+ }
527
+ mergeMethodConfig(target, propertyKey, normalizeMethodConfig(config, method));
528
+ return wrapHttpMethod(target, propertyKey, descriptor);
529
+ };
530
+ };
531
+ }
532
+ __name(createHttpMethodDecorator, "createHttpMethodDecorator");
533
+ function HttpApi(config) {
534
+ return (target) => {
535
+ mergeClassConfig(target, normalizeClassConfig(config));
536
+ };
537
+ }
538
+ __name(HttpApi, "HttpApi");
539
+ var Get = createHttpMethodDecorator("get");
540
+ var Post = createHttpMethodDecorator("post");
541
+ var Put = createHttpMethodDecorator("put");
542
+ var Delete = createHttpMethodDecorator("delete");
543
+ var Patch = createHttpMethodDecorator("patch");
544
+ var Options = createHttpMethodDecorator("options");
545
+ var Head = createHttpMethodDecorator("head");
546
+
547
+ // src/decorators/params.ts
548
+ function createParamDecorator(kind, name) {
549
+ return (target, propertyKey, parameterIndex) => {
550
+ if (propertyKey === void 0) {
551
+ throw new Error(`"${kind}" parameter decorators are only supported on instance methods.`);
552
+ }
553
+ addParamBinding(target, propertyKey, {
554
+ index: parameterIndex,
555
+ kind,
556
+ name
557
+ });
558
+ };
559
+ }
560
+ __name(createParamDecorator, "createParamDecorator");
561
+ function BodyParam(name) {
562
+ return createParamDecorator("body", name);
563
+ }
564
+ __name(BodyParam, "BodyParam");
565
+ function PathParam(name) {
566
+ return createParamDecorator("path", name);
567
+ }
568
+ __name(PathParam, "PathParam");
569
+ function QueryParam(name) {
570
+ return createParamDecorator("query", name);
571
+ }
572
+ __name(QueryParam, "QueryParam");
573
+
574
+ // src/runtime/strategies.ts
575
+ function sleep(delay) {
576
+ return new Promise((resolve) => {
577
+ setTimeout(resolve, delay);
578
+ });
579
+ }
580
+ __name(sleep, "sleep");
581
+ function useRequest(requester) {
582
+ return requester;
583
+ }
584
+ __name(useRequest, "useRequest");
585
+ function useRetry(requester, options = {}) {
586
+ const { count = 3, delay = 100, signal, shouldRetry = /* @__PURE__ */ __name(() => true, "shouldRetry") } = options;
587
+ return async (...args) => {
588
+ let attempt = 0;
589
+ let lastError;
590
+ while (attempt < count) {
591
+ if (signal?.aborted) {
592
+ throw signal.reason ?? new Error("Retry execution was aborted.");
593
+ }
594
+ try {
595
+ return await requester(...args);
596
+ } catch (error) {
597
+ attempt += 1;
598
+ lastError = error;
599
+ if (attempt >= count || !shouldRetry(error, attempt)) {
600
+ throw error;
601
+ }
602
+ await sleep(delay);
603
+ }
604
+ }
605
+ throw lastError;
606
+ };
607
+ }
608
+ __name(useRetry, "useRetry");
609
+ function useDebounce(requester, options = {}) {
610
+ const { delay = 100, immediate = false } = options;
611
+ let timer;
612
+ let latestArgs;
613
+ let pending = [];
614
+ const flush = /* @__PURE__ */ __name(async () => {
615
+ const resolvers = pending;
616
+ pending = [];
617
+ timer = void 0;
618
+ if (!latestArgs) {
619
+ return;
620
+ }
621
+ try {
622
+ const result = await requester(...latestArgs);
623
+ resolvers.forEach((item) => item.resolve(result));
624
+ } catch (error) {
625
+ resolvers.forEach((item) => item.reject(error));
626
+ }
627
+ }, "flush");
628
+ return (...args) => {
629
+ latestArgs = args;
630
+ return new Promise((resolve, reject) => {
631
+ pending.push({
632
+ resolve,
633
+ reject
634
+ });
635
+ const shouldCallImmediately = immediate && !timer;
636
+ if (timer) {
637
+ clearTimeout(timer);
638
+ }
639
+ if (shouldCallImmediately) {
640
+ void flush();
641
+ timer = setTimeout(() => {
642
+ timer = void 0;
643
+ }, delay);
644
+ return;
645
+ }
646
+ timer = setTimeout(() => {
647
+ void flush();
648
+ }, delay);
649
+ });
650
+ };
651
+ }
652
+ __name(useDebounce, "useDebounce");
653
+ function createSingleConfigPlugin(factory) {
654
+ let wrapped;
655
+ return (next) => {
656
+ if (!wrapped) {
657
+ wrapped = factory(next);
658
+ }
659
+ return (config) => wrapped(config);
660
+ };
661
+ }
662
+ __name(createSingleConfigPlugin, "createSingleConfigPlugin");
663
+ function createRetryPlugin(options = {}) {
664
+ return createSingleConfigPlugin((next) => useRetry((config) => next(config), options));
665
+ }
666
+ __name(createRetryPlugin, "createRetryPlugin");
667
+ function createDebouncePlugin(options = {}) {
668
+ return createSingleConfigPlugin((next) => useDebounce((config) => next(config), options));
669
+ }
670
+ __name(createDebouncePlugin, "createDebouncePlugin");
671
+ function createThrottlePlugin(options = {}) {
672
+ return createSingleConfigPlugin((next) => useThrottle((config) => next(config), options));
673
+ }
674
+ __name(createThrottlePlugin, "createThrottlePlugin");
675
+ function useThrottle(requester, options = {}) {
676
+ const { interval = 100 } = options;
677
+ let active = false;
678
+ let trailingArgs;
679
+ let trailingResolvers = [];
680
+ const run = /* @__PURE__ */ __name(async (args, resolvers) => {
681
+ active = true;
682
+ try {
683
+ const result = await requester(...args);
684
+ resolvers.forEach((item) => item.resolve(result));
685
+ } catch (error) {
686
+ resolvers.forEach((item) => item.reject(error));
687
+ } finally {
688
+ setTimeout(() => {
689
+ active = false;
690
+ if (trailingArgs) {
691
+ const nextArgs = trailingArgs;
692
+ const nextResolvers = trailingResolvers;
693
+ trailingArgs = void 0;
694
+ trailingResolvers = [];
695
+ void run(nextArgs, nextResolvers);
696
+ }
697
+ }, interval);
698
+ }
699
+ }, "run");
700
+ return (...args) => {
701
+ return new Promise((resolve, reject) => {
702
+ if (!active) {
703
+ void run(args, [
704
+ {
705
+ resolve,
706
+ reject
707
+ }
708
+ ]);
709
+ return;
710
+ }
711
+ trailingArgs = args;
712
+ trailingResolvers.push({
713
+ resolve,
714
+ reject
715
+ });
716
+ });
717
+ };
718
+ }
719
+ __name(useThrottle, "useThrottle");
720
+
721
+ // src/decorators/extensions.ts
722
+ function withHttpMethodConfig(config) {
723
+ return (target, propertyKey) => {
724
+ if (propertyKey === void 0) {
725
+ throw new Error("HTTP method config decorators can only be used on instance methods.");
726
+ }
727
+ mergeMethodConfig(target, propertyKey, config);
728
+ };
729
+ }
730
+ __name(withHttpMethodConfig, "withHttpMethodConfig");
731
+ function withHttpClassConfig(config) {
732
+ return (target) => {
733
+ mergeClassConfig(target, config);
734
+ };
735
+ }
736
+ __name(withHttpClassConfig, "withHttpClassConfig");
737
+ function withHttpMethodPlugins(...plugins) {
738
+ return withHttpMethodConfig({
739
+ plugins
740
+ });
741
+ }
742
+ __name(withHttpMethodPlugins, "withHttpMethodPlugins");
743
+ function withHttpClassPlugins(...plugins) {
744
+ return withHttpClassConfig({
745
+ plugins
746
+ });
747
+ }
748
+ __name(withHttpClassPlugins, "withHttpClassPlugins");
749
+ function TransformRequest(transformRequest) {
750
+ return withHttpMethodConfig({
751
+ transformRequest: Array.isArray(transformRequest) ? transformRequest : [
752
+ transformRequest
753
+ ]
754
+ });
755
+ }
756
+ __name(TransformRequest, "TransformRequest");
757
+ function TransformResponse(transformResponse) {
758
+ return withHttpMethodConfig({
759
+ transformResponse: Array.isArray(transformResponse) ? transformResponse : [
760
+ transformResponse
761
+ ]
762
+ });
763
+ }
764
+ __name(TransformResponse, "TransformResponse");
765
+ function RefAxios(refAxios) {
766
+ return withHttpClassConfig({
767
+ refAxios
768
+ });
769
+ }
770
+ __name(RefAxios, "RefAxios");
771
+ function AxiosRef(refAxios) {
772
+ return withHttpMethodConfig({
773
+ refAxios
774
+ });
775
+ }
776
+ __name(AxiosRef, "AxiosRef");
777
+ function Retry(options = {}) {
778
+ return withHttpMethodPlugins(createRetryPlugin(options));
779
+ }
780
+ __name(Retry, "Retry");
781
+ function Debounce(options = {}) {
782
+ return withHttpMethodPlugins(createDebouncePlugin(options));
783
+ }
784
+ __name(Debounce, "Debounce");
785
+ function Throttle(options = {}) {
786
+ return withHttpMethodPlugins(createThrottlePlugin(options));
787
+ }
788
+ __name(Throttle, "Throttle");
789
+
790
+ // src/runtime/signal.ts
791
+ var SignalController = class {
792
+ static {
793
+ __name(this, "SignalController");
794
+ }
795
+ controller = new AbortController();
796
+ get signal() {
797
+ return this.controller.signal;
798
+ }
799
+ abort(reason) {
800
+ this.controller.abort(reason);
801
+ }
802
+ enable() {
803
+ if (this.controller.signal.aborted) {
804
+ this.controller = new AbortController();
805
+ }
806
+ }
807
+ };
808
+
809
+ // src/index.ts
810
+ var axiosPlus = import_axios3.default;
811
+ // Annotate the CommonJS export names for ESM import in node:
812
+ 0 && (module.exports = {
813
+ AxiosRef,
814
+ BodyParam,
815
+ Debounce,
816
+ Delete,
817
+ Get,
818
+ Head,
819
+ HttpApi,
820
+ Options,
821
+ Patch,
822
+ PathParam,
823
+ Post,
824
+ Put,
825
+ QueryParam,
826
+ RefAxios,
827
+ Retry,
828
+ SignalController,
829
+ Throttle,
830
+ TransformRequest,
831
+ TransformResponse,
832
+ axiosPlus,
833
+ composeDecoratedUrl,
834
+ createDebouncePlugin,
835
+ createRetryPlugin,
836
+ createThrottlePlugin,
837
+ executeHttpCall,
838
+ isAbsoluteHttpUrl,
839
+ joinPathname,
840
+ resolveAbsoluteHttpTarget,
841
+ resolvePathParams,
842
+ useDebounce,
843
+ useRequest,
844
+ useRetry,
845
+ useThrottle,
846
+ withHttpClassConfig,
847
+ withHttpClassPlugins,
848
+ withHttpMethodConfig,
849
+ withHttpMethodPlugins
850
+ });
851
+ //# sourceMappingURL=index.cjs.map