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