@unicom-cloud/axios 0.1.1
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 +18 -0
- package/chunk/CBBFLbi8.js +253 -0
- package/chunk/Turrizj0.js +62 -0
- package/index.d.ts +793 -0
- package/index.js +39 -0
- package/lib/axios.js +1643 -0
- package/lib/utils.js +5 -0
- package/mock-adapter/index.d.ts +59 -0
- package/mock-adapter/index.js +205 -0
- package/mock-adapter/request.js +50 -0
- package/mock-adapter/utils.js +123 -0
- package/package.json +1 -0
package/lib/utils.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AxiosAdapter,
|
|
3
|
+
AxiosInstance,
|
|
4
|
+
AxiosRequestConfig,
|
|
5
|
+
} from '../index.d.ts';
|
|
6
|
+
|
|
7
|
+
type CallbackResponseSpecFunc = (
|
|
8
|
+
config: AxiosRequestConfig,
|
|
9
|
+
) => any[] | Promise<any[]>;
|
|
10
|
+
|
|
11
|
+
type ResponseSpecFunc = (
|
|
12
|
+
statusOrCallback: number | CallbackResponseSpecFunc,
|
|
13
|
+
data?: any,
|
|
14
|
+
headers?: any,
|
|
15
|
+
) => MockAdapter;
|
|
16
|
+
|
|
17
|
+
interface RequestHandler {
|
|
18
|
+
reply: ResponseSpecFunc;
|
|
19
|
+
replyOnce: ResponseSpecFunc;
|
|
20
|
+
|
|
21
|
+
passThrough(): void;
|
|
22
|
+
networkError(): void;
|
|
23
|
+
timeout(): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface MockAdapterOptions {
|
|
27
|
+
delayResponse?: number;
|
|
28
|
+
onNoMatch?: 'passthrough' | 'throwException';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface RequestDataMatcher {
|
|
32
|
+
[index: string]: any;
|
|
33
|
+
params?: {
|
|
34
|
+
[index: string]: any;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type RequestMatcherFunc = (
|
|
39
|
+
matcher?: string | RegExp,
|
|
40
|
+
body?: string | RequestDataMatcher,
|
|
41
|
+
) => RequestHandler;
|
|
42
|
+
|
|
43
|
+
declare class MockAdapter {
|
|
44
|
+
constructor(axiosInstance: AxiosInstance, options?: MockAdapterOptions);
|
|
45
|
+
|
|
46
|
+
adapter(): AxiosAdapter;
|
|
47
|
+
reset(): void;
|
|
48
|
+
restore(): void;
|
|
49
|
+
|
|
50
|
+
onGet: RequestMatcherFunc;
|
|
51
|
+
onPost: RequestMatcherFunc;
|
|
52
|
+
onPut: RequestMatcherFunc;
|
|
53
|
+
onHead: RequestMatcherFunc;
|
|
54
|
+
onDelete: RequestMatcherFunc;
|
|
55
|
+
onPatch: RequestMatcherFunc;
|
|
56
|
+
onAny: RequestMatcherFunc;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default MockAdapter;
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/* 2025-11-14 09:03:18 */
|
|
2
|
+
import { A as E } from "../chunk/CBBFLbi8.js";
|
|
3
|
+
import { request as w } from "./request.js";
|
|
4
|
+
import i from "./utils.js";
|
|
5
|
+
const A = [
|
|
6
|
+
"get",
|
|
7
|
+
"post",
|
|
8
|
+
"head",
|
|
9
|
+
"delete",
|
|
10
|
+
"patch",
|
|
11
|
+
"put",
|
|
12
|
+
"options",
|
|
13
|
+
"list",
|
|
14
|
+
"link",
|
|
15
|
+
"unlink"
|
|
16
|
+
];
|
|
17
|
+
function m() {
|
|
18
|
+
const r = [];
|
|
19
|
+
return A.forEach(function(e) {
|
|
20
|
+
Object.defineProperty(r, e, {
|
|
21
|
+
get() {
|
|
22
|
+
return r.filter(function(n) {
|
|
23
|
+
return !n.method || n.method === e;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}), r;
|
|
28
|
+
}
|
|
29
|
+
class N {
|
|
30
|
+
constructor(e, n = {
|
|
31
|
+
delayResponse: 27,
|
|
32
|
+
onNoMatch: "passthrough"
|
|
33
|
+
}) {
|
|
34
|
+
if (this.reset(), e)
|
|
35
|
+
this.axiosInstance = e, this.axiosInstanceWithoutInterceptors = e.create ? e.create() : void 0, this.originalAdapter = e.defaults.adapter, this.delayResponse = n.delayResponse > 0 ? n.delayResponse : null, this.onNoMatch = n.onNoMatch || null, e.defaults.adapter = this.adapter();
|
|
36
|
+
else
|
|
37
|
+
throw new Error("Please provide an instance of axios to mock");
|
|
38
|
+
}
|
|
39
|
+
adapter() {
|
|
40
|
+
return (e) => w(this, e);
|
|
41
|
+
}
|
|
42
|
+
restore() {
|
|
43
|
+
this.axiosInstance && (this.axiosInstance.defaults.adapter = this.originalAdapter, this.axiosInstance = void 0);
|
|
44
|
+
}
|
|
45
|
+
reset() {
|
|
46
|
+
this.resetHandlers(), this.resetHistory();
|
|
47
|
+
}
|
|
48
|
+
resetHandlers() {
|
|
49
|
+
this.handlers ? this.handlers.length = 0 : this.handlers = m();
|
|
50
|
+
}
|
|
51
|
+
resetHistory() {
|
|
52
|
+
this.history ? this.history.length = 0 : this.history = m();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const R = [
|
|
56
|
+
"any",
|
|
57
|
+
"get",
|
|
58
|
+
"delete",
|
|
59
|
+
"head",
|
|
60
|
+
"options"
|
|
61
|
+
];
|
|
62
|
+
function T(r, e, n) {
|
|
63
|
+
return R.includes(r) ? O(r, e || {}) : O(r, Object.assign({}, n, { data: e }));
|
|
64
|
+
}
|
|
65
|
+
const C = ["headers", "params", "data"];
|
|
66
|
+
function O(r, e) {
|
|
67
|
+
for (const n in e)
|
|
68
|
+
if (!C.includes(n))
|
|
69
|
+
throw new Error(
|
|
70
|
+
`Invalid config property ${JSON.stringify(
|
|
71
|
+
n
|
|
72
|
+
)} provided to ${g(r)}. Config: ${JSON.stringify(
|
|
73
|
+
e
|
|
74
|
+
)}`
|
|
75
|
+
);
|
|
76
|
+
return e;
|
|
77
|
+
}
|
|
78
|
+
function g(r) {
|
|
79
|
+
return `on${r.charAt(0).toUpperCase()}${r.slice(1)}`;
|
|
80
|
+
}
|
|
81
|
+
A.concat("any").forEach(function(r) {
|
|
82
|
+
N.prototype[g(r)] = function(e, n, a) {
|
|
83
|
+
const s = this;
|
|
84
|
+
let d;
|
|
85
|
+
e = e === void 0 ? /.*/ : e;
|
|
86
|
+
const o = T(r, n, a);
|
|
87
|
+
function c(t, u, p) {
|
|
88
|
+
const h = {
|
|
89
|
+
url: e,
|
|
90
|
+
method: r === "any" ? void 0 : r,
|
|
91
|
+
params: o.params,
|
|
92
|
+
data: o.data,
|
|
93
|
+
headers: o.headers,
|
|
94
|
+
replyOnce: !1,
|
|
95
|
+
delay: d,
|
|
96
|
+
response: E.isFunction(t) ? t : [t, u, p]
|
|
97
|
+
};
|
|
98
|
+
return y(r, s.handlers, h), s;
|
|
99
|
+
}
|
|
100
|
+
function x(t) {
|
|
101
|
+
d = t;
|
|
102
|
+
const u = f.reply.bind(f);
|
|
103
|
+
return Object.assign(u, f), u;
|
|
104
|
+
}
|
|
105
|
+
function l(t, u, p) {
|
|
106
|
+
const h = {
|
|
107
|
+
url: e,
|
|
108
|
+
method: r === "any" ? void 0 : r,
|
|
109
|
+
params: o.params,
|
|
110
|
+
data: o.data,
|
|
111
|
+
headers: o.headers,
|
|
112
|
+
replyOnce: !0,
|
|
113
|
+
delay: d,
|
|
114
|
+
response: E.isFunction(t) ? t : [t, u, p]
|
|
115
|
+
};
|
|
116
|
+
return y(r, s.handlers, h), s;
|
|
117
|
+
}
|
|
118
|
+
const f = {
|
|
119
|
+
reply: c,
|
|
120
|
+
replyOnce: l,
|
|
121
|
+
withDelayInMs: x,
|
|
122
|
+
passThrough() {
|
|
123
|
+
const t = {
|
|
124
|
+
passThrough: !0,
|
|
125
|
+
method: r === "any" ? void 0 : r,
|
|
126
|
+
url: e,
|
|
127
|
+
params: o.params,
|
|
128
|
+
data: o.data,
|
|
129
|
+
headers: o.headers
|
|
130
|
+
};
|
|
131
|
+
return y(r, s.handlers, t), s;
|
|
132
|
+
},
|
|
133
|
+
abortRequest() {
|
|
134
|
+
return c(async function(t) {
|
|
135
|
+
throw i.createAxiosError(
|
|
136
|
+
"Request aborted",
|
|
137
|
+
t,
|
|
138
|
+
void 0,
|
|
139
|
+
"ECONNABORTED"
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
},
|
|
143
|
+
abortRequestOnce() {
|
|
144
|
+
return l(async function(t) {
|
|
145
|
+
throw i.createAxiosError(
|
|
146
|
+
"Request aborted",
|
|
147
|
+
t,
|
|
148
|
+
void 0,
|
|
149
|
+
"ECONNABORTED"
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
},
|
|
153
|
+
networkError() {
|
|
154
|
+
return c(async function(t) {
|
|
155
|
+
throw i.createAxiosError("Network Error", t);
|
|
156
|
+
});
|
|
157
|
+
},
|
|
158
|
+
networkErrorOnce() {
|
|
159
|
+
return l(async function(t) {
|
|
160
|
+
throw i.createAxiosError("Network Error", t);
|
|
161
|
+
});
|
|
162
|
+
},
|
|
163
|
+
timeout() {
|
|
164
|
+
return c(async function(t) {
|
|
165
|
+
throw i.createAxiosError(
|
|
166
|
+
t.timeoutErrorMessage || `timeout of ${t.timeout}ms exceeded`,
|
|
167
|
+
t,
|
|
168
|
+
void 0,
|
|
169
|
+
t.transitional && t.transitional.clarifyTimeoutError ? "ETIMEDOUT" : "ECONNABORTED"
|
|
170
|
+
);
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
timeoutOnce() {
|
|
174
|
+
return l(async function(t) {
|
|
175
|
+
throw i.createAxiosError(
|
|
176
|
+
t.timeoutErrorMessage || `timeout of ${t.timeout}ms exceeded`,
|
|
177
|
+
t,
|
|
178
|
+
void 0,
|
|
179
|
+
t.transitional && t.transitional.clarifyTimeoutError ? "ETIMEDOUT" : "ECONNABORTED"
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
return f;
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
function M(r, e) {
|
|
188
|
+
let n = -1;
|
|
189
|
+
for (let a = 0; a < r.length; a += 1) {
|
|
190
|
+
const s = r[a], d = s.url instanceof RegExp && e.url instanceof RegExp ? String(s.url) === String(e.url) : s.url === e.url;
|
|
191
|
+
(!s.method || s.method === e.method) && d && i.isEqual(s.params, e.params) && i.isEqual(s.data, e.data) && i.isEqual(s.headers, e.headers) && !s.replyOnce && (n = a);
|
|
192
|
+
}
|
|
193
|
+
return n;
|
|
194
|
+
}
|
|
195
|
+
function y(r, e, n) {
|
|
196
|
+
if (r === "any")
|
|
197
|
+
e.push(n);
|
|
198
|
+
else {
|
|
199
|
+
const a = M(e, n);
|
|
200
|
+
a > -1 && !n.replyOnce ? e.splice(a, 1, n) : e.push(n);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
export {
|
|
204
|
+
N as default
|
|
205
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/* 2025-11-14 09:03:18 */
|
|
2
|
+
import { A as l } from "../chunk/CBBFLbi8.js";
|
|
3
|
+
import a from "./utils.js";
|
|
4
|
+
function u(s, e) {
|
|
5
|
+
let t = e.baseURL;
|
|
6
|
+
return t && !/^https?:/.test(t) && (t = void 0), l.isFunction(s.originalAdapter) ? s.originalAdapter(e) : s.axiosInstanceWithoutInterceptors(
|
|
7
|
+
Object.assign({}, e, {
|
|
8
|
+
baseURL: t,
|
|
9
|
+
// Use the original adapter, not the mock adapter
|
|
10
|
+
adapter: s.originalAdapter,
|
|
11
|
+
// The request transformation runs on the original axios handler already
|
|
12
|
+
transformRequest: [],
|
|
13
|
+
transformResponse: []
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
function n(s, e) {
|
|
18
|
+
return l.isNumber(e.delay) ? e.delay : s.delayResponse;
|
|
19
|
+
}
|
|
20
|
+
async function i(s, e) {
|
|
21
|
+
let t = e.url || "";
|
|
22
|
+
e.baseURL && t.substr(0, e.baseURL.length) === e.baseURL && (t = t.slice(e.baseURL.length)), delete e.adapter, s.history.push(e);
|
|
23
|
+
const r = a.findHandler(
|
|
24
|
+
s.handlers,
|
|
25
|
+
e.method,
|
|
26
|
+
t,
|
|
27
|
+
e.data,
|
|
28
|
+
e.params,
|
|
29
|
+
e.headers && e.headers.constructor.name === "AxiosHeaders" ? Object.assign({}, e.headers.toJSON()) : e.headers,
|
|
30
|
+
e.baseURL
|
|
31
|
+
);
|
|
32
|
+
if (r)
|
|
33
|
+
return r.replyOnce && a.purgeIfReplyOnce(s, r), r.passThrough ? u(s, e) : a.settle(
|
|
34
|
+
e,
|
|
35
|
+
r.response,
|
|
36
|
+
n(s, r)
|
|
37
|
+
);
|
|
38
|
+
switch (s.onNoMatch) {
|
|
39
|
+
case "passthrough":
|
|
40
|
+
return u(s, e);
|
|
41
|
+
case "throwException":
|
|
42
|
+
throw a.createCouldNotFindMockError(e);
|
|
43
|
+
default:
|
|
44
|
+
return a.settle(e, { status: 404 }, s.delayResponse);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export {
|
|
48
|
+
i as default,
|
|
49
|
+
i as request
|
|
50
|
+
};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/* 2025-11-14 09:03:18 */
|
|
2
|
+
import S from "lodash/isArrayBuffer.js";
|
|
3
|
+
import O from "lodash/isBuffer.js";
|
|
4
|
+
import d from "lodash/isEqual.js";
|
|
5
|
+
import { A as m } from "../chunk/Turrizj0.js";
|
|
6
|
+
import { A as o } from "../chunk/CBBFLbi8.js";
|
|
7
|
+
function p(t, r) {
|
|
8
|
+
const n = t.length;
|
|
9
|
+
for (let e = 0; e < n; e++) {
|
|
10
|
+
const i = t[e];
|
|
11
|
+
if (r(i)) return i;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function l(t, r) {
|
|
15
|
+
return t ? `${t.replace(/\/+$/, "")}/${r.replace(/^\/+/, "")}` : r;
|
|
16
|
+
}
|
|
17
|
+
function w(t, r, n, e, i, c, a) {
|
|
18
|
+
return p(t[r.toLowerCase()], function(s) {
|
|
19
|
+
let u = !1;
|
|
20
|
+
return o.isString(s.url) ? u = h(n, s.url) || h(l(a, n), s.url) : s.url instanceof RegExp && (u = s.url.test(n) || s.url.test(l(a, n))), u && N(e, i, s) && f(c, s.headers);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function h(t, r) {
|
|
24
|
+
const n = t[0] === "/" ? t.substr(1) : t, e = r[0] === "/" ? r.substr(1) : r;
|
|
25
|
+
return n === e;
|
|
26
|
+
}
|
|
27
|
+
function N(t, r, n) {
|
|
28
|
+
return f(r, n.params) && E(t, n.data);
|
|
29
|
+
}
|
|
30
|
+
function f(t, r) {
|
|
31
|
+
return r === void 0 ? !0 : o.isFunction(r.asymmetricMatch) ? r.asymmetricMatch(t) : d(t, r);
|
|
32
|
+
}
|
|
33
|
+
function E(t, r) {
|
|
34
|
+
if (r === void 0)
|
|
35
|
+
return !0;
|
|
36
|
+
let n;
|
|
37
|
+
try {
|
|
38
|
+
n = JSON.parse(t);
|
|
39
|
+
} catch (e) {
|
|
40
|
+
console.error(e);
|
|
41
|
+
}
|
|
42
|
+
return f(n || t, r);
|
|
43
|
+
}
|
|
44
|
+
function b(t, r) {
|
|
45
|
+
const n = t.handlers.indexOf(r);
|
|
46
|
+
n > -1 && t.handlers.splice(n, 1);
|
|
47
|
+
}
|
|
48
|
+
function y(t) {
|
|
49
|
+
return S(t) || o.isBuffer(t) || o.isStream(t) || o.isBlob(t) ? t : o.isObject(t) ? JSON.parse(JSON.stringify(t)) : t;
|
|
50
|
+
}
|
|
51
|
+
async function M(t, r) {
|
|
52
|
+
o.isFunction(t) && (t = await t(r));
|
|
53
|
+
const n = t.status || t[0], e = y(t.data || t[1]), i = t.headers || t[2];
|
|
54
|
+
return t.config && (r = t.config), {
|
|
55
|
+
status: n,
|
|
56
|
+
data: e,
|
|
57
|
+
headers: i,
|
|
58
|
+
config: r,
|
|
59
|
+
request: { responseURL: r.url }
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
async function A(t, r, n) {
|
|
63
|
+
n > 0 && await new Promise((i) => setTimeout(i, n));
|
|
64
|
+
const e = await M(r, t);
|
|
65
|
+
if (!e.config.validateStatus || e.config.validateStatus(e.status))
|
|
66
|
+
return e;
|
|
67
|
+
throw g(
|
|
68
|
+
`Request failed with status code ${e.status}`,
|
|
69
|
+
e.config,
|
|
70
|
+
e
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
function g(t, r, n, e) {
|
|
74
|
+
if (o.isFunction(m))
|
|
75
|
+
return m.from(new Error(t), e, r, null, n);
|
|
76
|
+
const i = new Error(t);
|
|
77
|
+
return i.isAxiosError = !0, i.config = r, n !== void 0 && (i.response = n), e !== void 0 && (i.code = e), i.toJSON = function() {
|
|
78
|
+
return {
|
|
79
|
+
// Standard
|
|
80
|
+
message: this.message,
|
|
81
|
+
name: this.name,
|
|
82
|
+
// Microsoft
|
|
83
|
+
description: this.description,
|
|
84
|
+
number: this.number,
|
|
85
|
+
// Mozilla
|
|
86
|
+
fileName: this.fileName,
|
|
87
|
+
lineNumber: this.lineNumber,
|
|
88
|
+
columnNumber: this.columnNumber,
|
|
89
|
+
stack: this.stack,
|
|
90
|
+
// Axios
|
|
91
|
+
config: this.config,
|
|
92
|
+
code: this.code
|
|
93
|
+
};
|
|
94
|
+
}, i;
|
|
95
|
+
}
|
|
96
|
+
function B(t) {
|
|
97
|
+
const r = `Could not find mock for:
|
|
98
|
+
${JSON.stringify(
|
|
99
|
+
{
|
|
100
|
+
method: t.method,
|
|
101
|
+
url: t.url,
|
|
102
|
+
params: t.params,
|
|
103
|
+
headers: t.headers
|
|
104
|
+
},
|
|
105
|
+
null,
|
|
106
|
+
2
|
|
107
|
+
)}`, n = new Error(r);
|
|
108
|
+
return n.isCouldNotFindMockError = !0, n.url = t.url, n.method = t.method, n;
|
|
109
|
+
}
|
|
110
|
+
const $ = {
|
|
111
|
+
createAxiosError: g,
|
|
112
|
+
createCouldNotFindMockError: B,
|
|
113
|
+
find: p,
|
|
114
|
+
findHandler: w,
|
|
115
|
+
isBodyOrParametersMatching: N,
|
|
116
|
+
isBuffer: O,
|
|
117
|
+
isEqual: d,
|
|
118
|
+
purgeIfReplyOnce: b,
|
|
119
|
+
settle: A
|
|
120
|
+
};
|
|
121
|
+
export {
|
|
122
|
+
$ as default
|
|
123
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"@unicom-cloud/axios","version":"0.1.1","dependencies":{"@unicom-cloud/utils":"latest"},"peerDependencies":{},"type":"module","types":"index.d.ts","main":"./index.js","publishConfig":{"registry":"https://registry.npmjs.org/","access":"public"}}
|