@uni-helper/axios-adapter 1.5.2 → 1.16.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/README.md +22 -24
- package/dist/chunk-CKQMccvm.cjs +28 -0
- package/dist/index.cjs +321 -18
- package/dist/index.d.cts +17 -16
- package/dist/index.d.ts +17 -16
- package/dist/index.js +315 -9
- package/dist/types-BSLo1MIA.d.cts +7 -0
- package/dist/types-C-Kh3_5J.d.ts +7 -0
- package/dist/unplugin-B0XXJPh7.cjs +27 -0
- package/dist/unplugin-GFuC6tIl.js +21 -0
- package/dist/vite.cjs +4 -7
- package/dist/vite.d.cts +5 -5
- package/dist/vite.d.ts +6 -5
- package/dist/vite.js +5 -5
- package/dist/webpack.cjs +4 -7
- package/dist/webpack.d.cts +3 -3
- package/dist/webpack.d.ts +4 -3
- package/dist/webpack.js +5 -5
- package/package.json +27 -25
- package/dist/chunk-IWRE2NJT.js +0 -8
- package/dist/chunk-Y4IH4XRU.cjs +0 -10
- package/dist/types-76de936f.d.ts +0 -10
package/README.md
CHANGED
|
@@ -36,50 +36,50 @@ pnpm i @uni-helper/axios-adapter axios
|
|
|
36
36
|
### 基本用法
|
|
37
37
|
|
|
38
38
|
```ts
|
|
39
|
-
import
|
|
40
|
-
import
|
|
39
|
+
import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'
|
|
40
|
+
import axios from 'axios'
|
|
41
41
|
|
|
42
|
-
axios.defaults.adapter = createUniAppAxiosAdapter()
|
|
42
|
+
axios.defaults.adapter = createUniAppAxiosAdapter()
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
或者创建自定义实例
|
|
46
46
|
|
|
47
47
|
```ts
|
|
48
|
-
import
|
|
49
|
-
import
|
|
48
|
+
import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'
|
|
49
|
+
import axios from 'axios'
|
|
50
50
|
|
|
51
|
-
const instance = axios.create({ adapter: createUniAppAxiosAdapter() })
|
|
51
|
+
const instance = axios.create({ adapter: createUniAppAxiosAdapter() })
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
### 与 [useAxios](https://vueuse.org/integrations/useAxios/) 一起使用
|
|
55
55
|
|
|
56
56
|
```ts
|
|
57
|
-
import
|
|
58
|
-
import
|
|
57
|
+
import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'
|
|
58
|
+
import axios from 'axios'
|
|
59
59
|
|
|
60
|
-
axios.defaults.adapter = createUniAppAxiosAdapter()
|
|
61
|
-
const { data, isFinished } = useAxios(
|
|
60
|
+
axios.defaults.adapter = createUniAppAxiosAdapter()
|
|
61
|
+
const { data, isFinished } = useAxios('/posts')
|
|
62
62
|
```
|
|
63
63
|
|
|
64
64
|
### 上传和下载
|
|
65
65
|
|
|
66
66
|
```ts
|
|
67
67
|
// 下载
|
|
68
|
-
axios.download(
|
|
68
|
+
axios.download('/')
|
|
69
69
|
// or
|
|
70
70
|
axios.request({
|
|
71
|
-
url:
|
|
72
|
-
method:
|
|
73
|
-
})
|
|
71
|
+
url: '/',
|
|
72
|
+
method: 'download',
|
|
73
|
+
})
|
|
74
74
|
|
|
75
75
|
// 上传
|
|
76
|
-
axios.upload(
|
|
76
|
+
axios.upload('/', new File([new Blob()], 'fake file'))
|
|
77
77
|
// or
|
|
78
78
|
axios.request({
|
|
79
|
-
url:
|
|
80
|
-
method:
|
|
81
|
-
data: new File([new Blob()],
|
|
82
|
-
})
|
|
79
|
+
url: '/',
|
|
80
|
+
method: 'upload',
|
|
81
|
+
data: new File([new Blob()], 'fake file'),
|
|
82
|
+
})
|
|
83
83
|
```
|
|
84
84
|
|
|
85
85
|
### 小程序
|
|
@@ -93,14 +93,12 @@ axios.request({
|
|
|
93
93
|
使用 `pnpm add miniprogram-formdata miniprogram-blob` 来安装对应的 polyfill,然后使用插件
|
|
94
94
|
```ts
|
|
95
95
|
// vite.config.js
|
|
96
|
-
import uniAxiosAdapter from
|
|
96
|
+
import uniAxiosAdapter from '@uni-helper/axios-adapter/vite'
|
|
97
97
|
|
|
98
98
|
export default {
|
|
99
99
|
plugins: [
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
...
|
|
103
|
-
],
|
|
100
|
+
uniAxiosAdapter()
|
|
101
|
+
]
|
|
104
102
|
}
|
|
105
103
|
```
|
|
106
104
|
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
//#endregion
|
|
23
|
+
Object.defineProperty(exports, "__toESM", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function() {
|
|
26
|
+
return __toESM;
|
|
27
|
+
}
|
|
28
|
+
});
|
package/dist/index.cjs
CHANGED
|
@@ -1,18 +1,321 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_chunk = require("./chunk-CKQMccvm.cjs");
|
|
3
|
+
let axios = require("axios");
|
|
4
|
+
let axios_unsafe_core_buildFullPath = require("axios/unsafe/core/buildFullPath");
|
|
5
|
+
axios_unsafe_core_buildFullPath = require_chunk.__toESM(axios_unsafe_core_buildFullPath, 1);
|
|
6
|
+
let axios_unsafe_helpers_buildURL = require("axios/unsafe/helpers/buildURL");
|
|
7
|
+
axios_unsafe_helpers_buildURL = require_chunk.__toESM(axios_unsafe_helpers_buildURL, 1);
|
|
8
|
+
let axios_unsafe_helpers_speedometer = require("axios/unsafe/helpers/speedometer");
|
|
9
|
+
axios_unsafe_helpers_speedometer = require_chunk.__toESM(axios_unsafe_helpers_speedometer, 1);
|
|
10
|
+
let axios_unsafe_core_settle = require("axios/unsafe/core/settle");
|
|
11
|
+
axios_unsafe_core_settle = require_chunk.__toESM(axios_unsafe_core_settle, 1);
|
|
12
|
+
//#region src/utils.ts
|
|
13
|
+
function getMethodType(config) {
|
|
14
|
+
const { method: rawMethod = "GET" } = config;
|
|
15
|
+
switch (rawMethod.toLocaleLowerCase()) {
|
|
16
|
+
case "download": return "download";
|
|
17
|
+
case "upload": return "upload";
|
|
18
|
+
default: return "request";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function resolveOptions(userOptions) {
|
|
22
|
+
return { ...userOptions };
|
|
23
|
+
}
|
|
24
|
+
function resolveUniAppRequestOptions(config, _options) {
|
|
25
|
+
const data = config.data;
|
|
26
|
+
const responseType = config.responseType === "arraybuffer" ? "arraybuffer" : "text";
|
|
27
|
+
const dataType = responseType === "text" ? "json" : void 0;
|
|
28
|
+
const { headers, baseURL, ...requestConfig } = config;
|
|
29
|
+
const requestHeaders = axios.AxiosHeaders.from(serializeObject(headers)).normalize(false);
|
|
30
|
+
if (config.auth) {
|
|
31
|
+
const username = config.auth.username || "";
|
|
32
|
+
const password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : "";
|
|
33
|
+
requestHeaders.set("Authorization", `Basic ${btoa(`${username}:${password}`)}`);
|
|
34
|
+
}
|
|
35
|
+
const fullPath = (0, axios_unsafe_core_buildFullPath.default)(baseURL, config.url);
|
|
36
|
+
const method = config?.method?.toUpperCase() ?? "GET";
|
|
37
|
+
const url = (0, axios_unsafe_helpers_buildURL.default)(fullPath, config.params, config.paramsSerializer);
|
|
38
|
+
const timeout = config.timeout || 6e4;
|
|
39
|
+
let formData = {};
|
|
40
|
+
if (data && typeof data === "string") try {
|
|
41
|
+
formData = JSON.parse(data);
|
|
42
|
+
} catch {}
|
|
43
|
+
const header = requestHeaders.toJSON();
|
|
44
|
+
return {
|
|
45
|
+
...requestConfig,
|
|
46
|
+
url,
|
|
47
|
+
data,
|
|
48
|
+
header,
|
|
49
|
+
method,
|
|
50
|
+
responseType,
|
|
51
|
+
dataType,
|
|
52
|
+
timeout,
|
|
53
|
+
formData
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function progressEventReducer(listener, isDownloadStream) {
|
|
57
|
+
let bytesNotified = 0;
|
|
58
|
+
const _speedometer = (0, axios_unsafe_helpers_speedometer.default)(50, 250);
|
|
59
|
+
return (result) => {
|
|
60
|
+
const loaded = result.totalBytesWritten;
|
|
61
|
+
const total = result.totalBytesExpectedToWrite;
|
|
62
|
+
const progressBytes = loaded - bytesNotified;
|
|
63
|
+
const rate = _speedometer(progressBytes);
|
|
64
|
+
const inRange = loaded <= total;
|
|
65
|
+
bytesNotified = loaded;
|
|
66
|
+
const data = {
|
|
67
|
+
loaded,
|
|
68
|
+
total,
|
|
69
|
+
progress: total ? loaded / total : void 0,
|
|
70
|
+
bytes: progressBytes,
|
|
71
|
+
rate: rate || void 0,
|
|
72
|
+
estimated: rate && total && inRange ? (total - loaded) / rate : void 0,
|
|
73
|
+
event: result
|
|
74
|
+
};
|
|
75
|
+
data[isDownloadStream ? "download" : "upload"] = true;
|
|
76
|
+
listener(data);
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* ### 对象序列化
|
|
81
|
+
* - 将一个对象序列化为一个纯净的、类似JSON的新对象。
|
|
82
|
+
* - 此过程会过滤掉值为 null、undefined 或 false 的属性。
|
|
83
|
+
*
|
|
84
|
+
* @link https://github.com/axios/axios/blob/ef36347fb559383b04c755b07f1a8d11897fab7f/lib/core/AxiosHeaders.js#L238-L246
|
|
85
|
+
* @param {Record<string, any> | null | undefined} sourceObj 要进行序列化的源对象。
|
|
86
|
+
* @param {SerializeOptions} [options] 序列化选项。
|
|
87
|
+
* @returns {Record<string, any>} 返回一个新的、纯净的 JavaScript 对象。
|
|
88
|
+
*/
|
|
89
|
+
function serializeObject(sourceObj, options) {
|
|
90
|
+
const resultObj = Object.create(null);
|
|
91
|
+
if (!sourceObj) return resultObj;
|
|
92
|
+
const { asStrings = false } = options || {};
|
|
93
|
+
forEach(sourceObj, (value, key) => {
|
|
94
|
+
if (value != null && value !== false) resultObj[key] = asStrings && Array.isArray(value) ? value.join(", ") : value;
|
|
95
|
+
});
|
|
96
|
+
return resultObj;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Iterate over an Array or an Object invoking a function for each item.
|
|
100
|
+
*
|
|
101
|
+
* If `obj` is an Array callback will be called passing
|
|
102
|
+
* the value, index, and complete array for each item.
|
|
103
|
+
*
|
|
104
|
+
* If 'obj' is an Object callback will be called passing
|
|
105
|
+
* the value, key, and complete object for each property.
|
|
106
|
+
*
|
|
107
|
+
* @link https://github.com/axios/axios/blob/v1.x/lib/utils.js#L240
|
|
108
|
+
*
|
|
109
|
+
* @param {object | Array} obj The object to iterate
|
|
110
|
+
* @param {Function} fn The callback to invoke for each item
|
|
111
|
+
* @param {object} [options]
|
|
112
|
+
* @param {boolean} [options.allOwnKeys]
|
|
113
|
+
*/
|
|
114
|
+
function forEach(obj, fn, { allOwnKeys = false } = {}) {
|
|
115
|
+
if (obj === null || typeof obj === "undefined") return;
|
|
116
|
+
if (typeof obj !== "object") obj = [obj];
|
|
117
|
+
if (Array.isArray(obj)) for (let i = 0, l = obj.length; i < l; i++) fn(obj[i], i, obj);
|
|
118
|
+
else {
|
|
119
|
+
const keys = allOwnKeys ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
|
|
120
|
+
const len = keys.length;
|
|
121
|
+
let key;
|
|
122
|
+
for (let i = 0; i < len; i++) {
|
|
123
|
+
key = keys[i];
|
|
124
|
+
fn(obj[key], key, obj);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/methods/onCanceled.ts
|
|
130
|
+
var OnCanceled = class {
|
|
131
|
+
config;
|
|
132
|
+
onCanceled;
|
|
133
|
+
constructor(config) {
|
|
134
|
+
this.config = config;
|
|
135
|
+
}
|
|
136
|
+
subscribe(task, reject) {
|
|
137
|
+
if (this.config.cancelToken || this.config.signal) {
|
|
138
|
+
this.onCanceled = (cancel) => {
|
|
139
|
+
if (!task) return;
|
|
140
|
+
reject(!cancel || cancel.type ? new axios.CanceledError(void 0, void 0, this.config, task) : cancel);
|
|
141
|
+
task.abort();
|
|
142
|
+
task = null;
|
|
143
|
+
};
|
|
144
|
+
if (this.config.cancelToken) this.config.cancelToken.subscribe(this.onCanceled);
|
|
145
|
+
if (this.config.signal && this.config.signal.addEventListener) this.config.signal.aborted ? this.onCanceled() : this.config.signal.addEventListener("abort", this.onCanceled);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
unsubscribe() {
|
|
149
|
+
if (this.config.cancelToken) this.config.cancelToken.unsubscribe(this.onCanceled);
|
|
150
|
+
if (this.config.signal && this.config.signal.removeEventListener) this.config.signal.removeEventListener("abort", this.onCanceled);
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/methods/download.ts
|
|
155
|
+
const download = (config, options) => {
|
|
156
|
+
return new Promise((resolve, reject) => {
|
|
157
|
+
const requestOptions = resolveUniAppRequestOptions(config, options);
|
|
158
|
+
const responseConfig = config;
|
|
159
|
+
responseConfig.headers = new axios.AxiosHeaders(requestOptions.header);
|
|
160
|
+
const onCanceled = new OnCanceled(config);
|
|
161
|
+
let task = uni.downloadFile({
|
|
162
|
+
...requestOptions,
|
|
163
|
+
success(result) {
|
|
164
|
+
if (!task) return;
|
|
165
|
+
(0, axios_unsafe_core_settle.default)(resolve, reject, {
|
|
166
|
+
config: responseConfig,
|
|
167
|
+
data: result.tempFilePath,
|
|
168
|
+
headers: {},
|
|
169
|
+
status: result.statusCode,
|
|
170
|
+
statusText: result.errMsg ?? "OK",
|
|
171
|
+
request: task
|
|
172
|
+
});
|
|
173
|
+
task = null;
|
|
174
|
+
},
|
|
175
|
+
fail(error) {
|
|
176
|
+
const { errMsg = "" } = error ?? {};
|
|
177
|
+
if (errMsg) {
|
|
178
|
+
if (errMsg === "downloadFile:fail timeout") reject(new axios.AxiosError(errMsg, axios.AxiosError.ETIMEDOUT, responseConfig, task));
|
|
179
|
+
if (errMsg === "downloadFile:fail") reject(new axios.AxiosError(errMsg, axios.AxiosError.ERR_NETWORK, responseConfig, task));
|
|
180
|
+
}
|
|
181
|
+
reject(new axios.AxiosError(error.errMsg, void 0, responseConfig, task));
|
|
182
|
+
task = null;
|
|
183
|
+
},
|
|
184
|
+
complete() {
|
|
185
|
+
onCanceled.unsubscribe();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
if (typeof config.onDownloadProgress === "function") task.onProgressUpdate(progressEventReducer(config.onDownloadProgress, true));
|
|
189
|
+
if (typeof config.onHeadersReceived === "function") task.onHeadersReceived(config.onHeadersReceived);
|
|
190
|
+
onCanceled.subscribe(task, reject);
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/methods/request.ts
|
|
195
|
+
const request = (config, options) => {
|
|
196
|
+
return new Promise((resolve, reject) => {
|
|
197
|
+
const requestOptions = resolveUniAppRequestOptions(config, options);
|
|
198
|
+
const responseConfig = config;
|
|
199
|
+
responseConfig.headers = new axios.AxiosHeaders(requestOptions.header);
|
|
200
|
+
const onCanceled = new OnCanceled(config);
|
|
201
|
+
let task = uni.request({
|
|
202
|
+
...requestOptions,
|
|
203
|
+
success(result) {
|
|
204
|
+
if (!task) return;
|
|
205
|
+
if (Array.isArray(result.header)) {
|
|
206
|
+
const dingHeader = {};
|
|
207
|
+
result.header.forEach((h) => Object.assign(dingHeader, h));
|
|
208
|
+
result.header = dingHeader;
|
|
209
|
+
}
|
|
210
|
+
const headers = new axios.AxiosHeaders(result.header);
|
|
211
|
+
(0, axios_unsafe_core_settle.default)(resolve, reject, {
|
|
212
|
+
config: responseConfig,
|
|
213
|
+
data: result.data,
|
|
214
|
+
headers,
|
|
215
|
+
status: result.statusCode,
|
|
216
|
+
statusText: result.errMsg ?? "OK",
|
|
217
|
+
request: task,
|
|
218
|
+
cookies: result.cookies
|
|
219
|
+
});
|
|
220
|
+
task = null;
|
|
221
|
+
},
|
|
222
|
+
fail(error) {
|
|
223
|
+
const { errMsg = "" } = error ?? {};
|
|
224
|
+
if (errMsg) {
|
|
225
|
+
const isStatusError = errMsg === "request:fail http status error";
|
|
226
|
+
const isTimeoutError = errMsg === "request:fail timeout";
|
|
227
|
+
const isNetworkError = errMsg === "request:fail";
|
|
228
|
+
if (isTimeoutError) reject(new axios.AxiosError(errMsg, axios.AxiosError.ETIMEDOUT, responseConfig, task));
|
|
229
|
+
if (isNetworkError) reject(new axios.AxiosError(errMsg, axios.AxiosError.ERR_NETWORK, responseConfig, task));
|
|
230
|
+
if (isStatusError) {
|
|
231
|
+
const response = error ?? {};
|
|
232
|
+
reject(new axios.AxiosError(errMsg, response.statusCode, responseConfig, task, response));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
reject(new axios.AxiosError(error.errMsg, void 0, responseConfig, task));
|
|
236
|
+
task = null;
|
|
237
|
+
},
|
|
238
|
+
complete() {
|
|
239
|
+
onCanceled.unsubscribe();
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
if (typeof config.onHeadersReceived === "function") task.onHeadersReceived(config.onHeadersReceived);
|
|
243
|
+
onCanceled.subscribe(task, reject);
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
//#endregion
|
|
247
|
+
//#region src/methods/upload.ts
|
|
248
|
+
const upload = (config, options) => {
|
|
249
|
+
return new Promise((resolve, reject) => {
|
|
250
|
+
const requestOptions = resolveUniAppRequestOptions(config, options);
|
|
251
|
+
const responseConfig = config;
|
|
252
|
+
responseConfig.headers = new axios.AxiosHeaders(requestOptions.header);
|
|
253
|
+
const onCanceled = new OnCanceled(config);
|
|
254
|
+
let task = uni.uploadFile({
|
|
255
|
+
...requestOptions,
|
|
256
|
+
success(result) {
|
|
257
|
+
if (!task) return;
|
|
258
|
+
(0, axios_unsafe_core_settle.default)(resolve, reject, {
|
|
259
|
+
config: responseConfig,
|
|
260
|
+
data: result.data,
|
|
261
|
+
headers: {},
|
|
262
|
+
status: result.statusCode,
|
|
263
|
+
statusText: result.errMsg ?? "OK",
|
|
264
|
+
request: task
|
|
265
|
+
});
|
|
266
|
+
task = null;
|
|
267
|
+
},
|
|
268
|
+
fail(error) {
|
|
269
|
+
const { errMsg = "" } = error ?? {};
|
|
270
|
+
if (errMsg) {
|
|
271
|
+
const isTimeoutError = errMsg === "uploadFile:fail timeout";
|
|
272
|
+
const isNetworkError = errMsg === "uploadFile:fail file error";
|
|
273
|
+
if (isTimeoutError) reject(new axios.AxiosError(errMsg, axios.AxiosError.ETIMEDOUT, responseConfig, task));
|
|
274
|
+
if (isNetworkError) reject(new axios.AxiosError(errMsg, axios.AxiosError.ERR_NETWORK, responseConfig, task));
|
|
275
|
+
}
|
|
276
|
+
reject(new axios.AxiosError(error.errMsg, void 0, responseConfig, task));
|
|
277
|
+
task = null;
|
|
278
|
+
},
|
|
279
|
+
complete() {
|
|
280
|
+
onCanceled.unsubscribe();
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
if (typeof config.onHeadersReceived === "function") task.onHeadersReceived(config.onHeadersReceived);
|
|
284
|
+
onCanceled.subscribe(task, reject);
|
|
285
|
+
});
|
|
286
|
+
};
|
|
287
|
+
//#endregion
|
|
288
|
+
//#region src/methods/index.ts
|
|
289
|
+
function getMethod(config) {
|
|
290
|
+
switch (getMethodType(config)) {
|
|
291
|
+
case "download": return download;
|
|
292
|
+
case "upload": return upload;
|
|
293
|
+
default: return request;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/index.ts
|
|
298
|
+
function createUniAppAxiosAdapter(userOptions = {}) {
|
|
299
|
+
const options = resolveOptions(userOptions);
|
|
300
|
+
axios.Axios.prototype.download = function(url, config) {
|
|
301
|
+
return this.request({
|
|
302
|
+
url,
|
|
303
|
+
method: "download",
|
|
304
|
+
...config
|
|
305
|
+
});
|
|
306
|
+
};
|
|
307
|
+
axios.Axios.prototype.upload = function(url, data, config) {
|
|
308
|
+
return this.request({
|
|
309
|
+
url,
|
|
310
|
+
method: "upload",
|
|
311
|
+
data,
|
|
312
|
+
...config
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
const uniappAdapter = (config) => {
|
|
316
|
+
return getMethod(config)(config, options);
|
|
317
|
+
};
|
|
318
|
+
return uniappAdapter;
|
|
319
|
+
}
|
|
320
|
+
//#endregion
|
|
321
|
+
exports.createUniAppAxiosAdapter = createUniAppAxiosAdapter;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { t as UserOptions } from "./types-BSLo1MIA.cjs";
|
|
2
|
+
import { AxiosAdapter } from "axios";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
//#region src/globalExtensions.d.ts
|
|
5
|
+
declare module 'axios' {
|
|
6
|
+
interface AxiosRequestConfig extends Omit<UniApp.RequestOptions, 'success' | 'fail' | 'complete' | 'header'>, Omit<UniApp.UploadFileOption, 'success' | 'fail' | 'complete' | 'header' | 'formData'>, Omit<UniApp.DownloadFileOption, 'success' | 'fail' | 'complete' | 'header'>, Partial<Pick<UniApp.RequestTask, 'onHeadersReceived'>> {}
|
|
7
|
+
interface AxiosResponse {
|
|
8
|
+
cookies?: string[];
|
|
9
|
+
}
|
|
10
|
+
interface Axios {
|
|
11
|
+
upload: <T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<R>;
|
|
12
|
+
download: <T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>) => Promise<R>;
|
|
13
|
+
}
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/index.d.ts
|
|
17
|
+
declare function createUniAppAxiosAdapter(userOptions?: UserOptions): AxiosAdapter;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { createUniAppAxiosAdapter };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { t as UserOptions } from "./types-C-Kh3_5J.js";
|
|
2
|
+
import { AxiosAdapter } from "axios";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
//#region src/globalExtensions.d.ts
|
|
5
|
+
declare module 'axios' {
|
|
6
|
+
interface AxiosRequestConfig extends Omit<UniApp.RequestOptions, 'success' | 'fail' | 'complete' | 'header'>, Omit<UniApp.UploadFileOption, 'success' | 'fail' | 'complete' | 'header' | 'formData'>, Omit<UniApp.DownloadFileOption, 'success' | 'fail' | 'complete' | 'header'>, Partial<Pick<UniApp.RequestTask, 'onHeadersReceived'>> {}
|
|
7
|
+
interface AxiosResponse {
|
|
8
|
+
cookies?: string[];
|
|
9
|
+
}
|
|
10
|
+
interface Axios {
|
|
11
|
+
upload: <T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>) => Promise<R>;
|
|
12
|
+
download: <T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>) => Promise<R>;
|
|
13
|
+
}
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/index.d.ts
|
|
17
|
+
declare function createUniAppAxiosAdapter(userOptions?: UserOptions): AxiosAdapter;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { createUniAppAxiosAdapter };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,315 @@
|
|
|
1
|
-
import { Axios,
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { Axios, AxiosError, AxiosHeaders, CanceledError } from "axios";
|
|
2
|
+
import buildFullPath from "axios/unsafe/core/buildFullPath";
|
|
3
|
+
import buildURL from "axios/unsafe/helpers/buildURL";
|
|
4
|
+
import speedometer from "axios/unsafe/helpers/speedometer";
|
|
5
|
+
import settle from "axios/unsafe/core/settle";
|
|
6
|
+
//#region src/utils.ts
|
|
7
|
+
function getMethodType(config) {
|
|
8
|
+
const { method: rawMethod = "GET" } = config;
|
|
9
|
+
switch (rawMethod.toLocaleLowerCase()) {
|
|
10
|
+
case "download": return "download";
|
|
11
|
+
case "upload": return "upload";
|
|
12
|
+
default: return "request";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function resolveOptions(userOptions) {
|
|
16
|
+
return { ...userOptions };
|
|
17
|
+
}
|
|
18
|
+
function resolveUniAppRequestOptions(config, _options) {
|
|
19
|
+
const data = config.data;
|
|
20
|
+
const responseType = config.responseType === "arraybuffer" ? "arraybuffer" : "text";
|
|
21
|
+
const dataType = responseType === "text" ? "json" : void 0;
|
|
22
|
+
const { headers, baseURL, ...requestConfig } = config;
|
|
23
|
+
const requestHeaders = AxiosHeaders.from(serializeObject(headers)).normalize(false);
|
|
24
|
+
if (config.auth) {
|
|
25
|
+
const username = config.auth.username || "";
|
|
26
|
+
const password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : "";
|
|
27
|
+
requestHeaders.set("Authorization", `Basic ${btoa(`${username}:${password}`)}`);
|
|
28
|
+
}
|
|
29
|
+
const fullPath = buildFullPath(baseURL, config.url);
|
|
30
|
+
const method = config?.method?.toUpperCase() ?? "GET";
|
|
31
|
+
const url = buildURL(fullPath, config.params, config.paramsSerializer);
|
|
32
|
+
const timeout = config.timeout || 6e4;
|
|
33
|
+
let formData = {};
|
|
34
|
+
if (data && typeof data === "string") try {
|
|
35
|
+
formData = JSON.parse(data);
|
|
36
|
+
} catch {}
|
|
37
|
+
const header = requestHeaders.toJSON();
|
|
38
|
+
return {
|
|
39
|
+
...requestConfig,
|
|
40
|
+
url,
|
|
41
|
+
data,
|
|
42
|
+
header,
|
|
43
|
+
method,
|
|
44
|
+
responseType,
|
|
45
|
+
dataType,
|
|
46
|
+
timeout,
|
|
47
|
+
formData
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function progressEventReducer(listener, isDownloadStream) {
|
|
51
|
+
let bytesNotified = 0;
|
|
52
|
+
const _speedometer = speedometer(50, 250);
|
|
53
|
+
return (result) => {
|
|
54
|
+
const loaded = result.totalBytesWritten;
|
|
55
|
+
const total = result.totalBytesExpectedToWrite;
|
|
56
|
+
const progressBytes = loaded - bytesNotified;
|
|
57
|
+
const rate = _speedometer(progressBytes);
|
|
58
|
+
const inRange = loaded <= total;
|
|
59
|
+
bytesNotified = loaded;
|
|
60
|
+
const data = {
|
|
61
|
+
loaded,
|
|
62
|
+
total,
|
|
63
|
+
progress: total ? loaded / total : void 0,
|
|
64
|
+
bytes: progressBytes,
|
|
65
|
+
rate: rate || void 0,
|
|
66
|
+
estimated: rate && total && inRange ? (total - loaded) / rate : void 0,
|
|
67
|
+
event: result
|
|
68
|
+
};
|
|
69
|
+
data[isDownloadStream ? "download" : "upload"] = true;
|
|
70
|
+
listener(data);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* ### 对象序列化
|
|
75
|
+
* - 将一个对象序列化为一个纯净的、类似JSON的新对象。
|
|
76
|
+
* - 此过程会过滤掉值为 null、undefined 或 false 的属性。
|
|
77
|
+
*
|
|
78
|
+
* @link https://github.com/axios/axios/blob/ef36347fb559383b04c755b07f1a8d11897fab7f/lib/core/AxiosHeaders.js#L238-L246
|
|
79
|
+
* @param {Record<string, any> | null | undefined} sourceObj 要进行序列化的源对象。
|
|
80
|
+
* @param {SerializeOptions} [options] 序列化选项。
|
|
81
|
+
* @returns {Record<string, any>} 返回一个新的、纯净的 JavaScript 对象。
|
|
82
|
+
*/
|
|
83
|
+
function serializeObject(sourceObj, options) {
|
|
84
|
+
const resultObj = Object.create(null);
|
|
85
|
+
if (!sourceObj) return resultObj;
|
|
86
|
+
const { asStrings = false } = options || {};
|
|
87
|
+
forEach(sourceObj, (value, key) => {
|
|
88
|
+
if (value != null && value !== false) resultObj[key] = asStrings && Array.isArray(value) ? value.join(", ") : value;
|
|
89
|
+
});
|
|
90
|
+
return resultObj;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Iterate over an Array or an Object invoking a function for each item.
|
|
94
|
+
*
|
|
95
|
+
* If `obj` is an Array callback will be called passing
|
|
96
|
+
* the value, index, and complete array for each item.
|
|
97
|
+
*
|
|
98
|
+
* If 'obj' is an Object callback will be called passing
|
|
99
|
+
* the value, key, and complete object for each property.
|
|
100
|
+
*
|
|
101
|
+
* @link https://github.com/axios/axios/blob/v1.x/lib/utils.js#L240
|
|
102
|
+
*
|
|
103
|
+
* @param {object | Array} obj The object to iterate
|
|
104
|
+
* @param {Function} fn The callback to invoke for each item
|
|
105
|
+
* @param {object} [options]
|
|
106
|
+
* @param {boolean} [options.allOwnKeys]
|
|
107
|
+
*/
|
|
108
|
+
function forEach(obj, fn, { allOwnKeys = false } = {}) {
|
|
109
|
+
if (obj === null || typeof obj === "undefined") return;
|
|
110
|
+
if (typeof obj !== "object") obj = [obj];
|
|
111
|
+
if (Array.isArray(obj)) for (let i = 0, l = obj.length; i < l; i++) fn(obj[i], i, obj);
|
|
112
|
+
else {
|
|
113
|
+
const keys = allOwnKeys ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
|
|
114
|
+
const len = keys.length;
|
|
115
|
+
let key;
|
|
116
|
+
for (let i = 0; i < len; i++) {
|
|
117
|
+
key = keys[i];
|
|
118
|
+
fn(obj[key], key, obj);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/methods/onCanceled.ts
|
|
124
|
+
var OnCanceled = class {
|
|
125
|
+
config;
|
|
126
|
+
onCanceled;
|
|
127
|
+
constructor(config) {
|
|
128
|
+
this.config = config;
|
|
129
|
+
}
|
|
130
|
+
subscribe(task, reject) {
|
|
131
|
+
if (this.config.cancelToken || this.config.signal) {
|
|
132
|
+
this.onCanceled = (cancel) => {
|
|
133
|
+
if (!task) return;
|
|
134
|
+
reject(!cancel || cancel.type ? new CanceledError(void 0, void 0, this.config, task) : cancel);
|
|
135
|
+
task.abort();
|
|
136
|
+
task = null;
|
|
137
|
+
};
|
|
138
|
+
if (this.config.cancelToken) this.config.cancelToken.subscribe(this.onCanceled);
|
|
139
|
+
if (this.config.signal && this.config.signal.addEventListener) this.config.signal.aborted ? this.onCanceled() : this.config.signal.addEventListener("abort", this.onCanceled);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
unsubscribe() {
|
|
143
|
+
if (this.config.cancelToken) this.config.cancelToken.unsubscribe(this.onCanceled);
|
|
144
|
+
if (this.config.signal && this.config.signal.removeEventListener) this.config.signal.removeEventListener("abort", this.onCanceled);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/methods/download.ts
|
|
149
|
+
const download = (config, options) => {
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
const requestOptions = resolveUniAppRequestOptions(config, options);
|
|
152
|
+
const responseConfig = config;
|
|
153
|
+
responseConfig.headers = new AxiosHeaders(requestOptions.header);
|
|
154
|
+
const onCanceled = new OnCanceled(config);
|
|
155
|
+
let task = uni.downloadFile({
|
|
156
|
+
...requestOptions,
|
|
157
|
+
success(result) {
|
|
158
|
+
if (!task) return;
|
|
159
|
+
settle(resolve, reject, {
|
|
160
|
+
config: responseConfig,
|
|
161
|
+
data: result.tempFilePath,
|
|
162
|
+
headers: {},
|
|
163
|
+
status: result.statusCode,
|
|
164
|
+
statusText: result.errMsg ?? "OK",
|
|
165
|
+
request: task
|
|
166
|
+
});
|
|
167
|
+
task = null;
|
|
168
|
+
},
|
|
169
|
+
fail(error) {
|
|
170
|
+
const { errMsg = "" } = error ?? {};
|
|
171
|
+
if (errMsg) {
|
|
172
|
+
if (errMsg === "downloadFile:fail timeout") reject(new AxiosError(errMsg, AxiosError.ETIMEDOUT, responseConfig, task));
|
|
173
|
+
if (errMsg === "downloadFile:fail") reject(new AxiosError(errMsg, AxiosError.ERR_NETWORK, responseConfig, task));
|
|
174
|
+
}
|
|
175
|
+
reject(new AxiosError(error.errMsg, void 0, responseConfig, task));
|
|
176
|
+
task = null;
|
|
177
|
+
},
|
|
178
|
+
complete() {
|
|
179
|
+
onCanceled.unsubscribe();
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
if (typeof config.onDownloadProgress === "function") task.onProgressUpdate(progressEventReducer(config.onDownloadProgress, true));
|
|
183
|
+
if (typeof config.onHeadersReceived === "function") task.onHeadersReceived(config.onHeadersReceived);
|
|
184
|
+
onCanceled.subscribe(task, reject);
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/methods/request.ts
|
|
189
|
+
const request = (config, options) => {
|
|
190
|
+
return new Promise((resolve, reject) => {
|
|
191
|
+
const requestOptions = resolveUniAppRequestOptions(config, options);
|
|
192
|
+
const responseConfig = config;
|
|
193
|
+
responseConfig.headers = new AxiosHeaders(requestOptions.header);
|
|
194
|
+
const onCanceled = new OnCanceled(config);
|
|
195
|
+
let task = uni.request({
|
|
196
|
+
...requestOptions,
|
|
197
|
+
success(result) {
|
|
198
|
+
if (!task) return;
|
|
199
|
+
if (Array.isArray(result.header)) {
|
|
200
|
+
const dingHeader = {};
|
|
201
|
+
result.header.forEach((h) => Object.assign(dingHeader, h));
|
|
202
|
+
result.header = dingHeader;
|
|
203
|
+
}
|
|
204
|
+
const headers = new AxiosHeaders(result.header);
|
|
205
|
+
settle(resolve, reject, {
|
|
206
|
+
config: responseConfig,
|
|
207
|
+
data: result.data,
|
|
208
|
+
headers,
|
|
209
|
+
status: result.statusCode,
|
|
210
|
+
statusText: result.errMsg ?? "OK",
|
|
211
|
+
request: task,
|
|
212
|
+
cookies: result.cookies
|
|
213
|
+
});
|
|
214
|
+
task = null;
|
|
215
|
+
},
|
|
216
|
+
fail(error) {
|
|
217
|
+
const { errMsg = "" } = error ?? {};
|
|
218
|
+
if (errMsg) {
|
|
219
|
+
const isStatusError = errMsg === "request:fail http status error";
|
|
220
|
+
const isTimeoutError = errMsg === "request:fail timeout";
|
|
221
|
+
const isNetworkError = errMsg === "request:fail";
|
|
222
|
+
if (isTimeoutError) reject(new AxiosError(errMsg, AxiosError.ETIMEDOUT, responseConfig, task));
|
|
223
|
+
if (isNetworkError) reject(new AxiosError(errMsg, AxiosError.ERR_NETWORK, responseConfig, task));
|
|
224
|
+
if (isStatusError) {
|
|
225
|
+
const response = error ?? {};
|
|
226
|
+
reject(new AxiosError(errMsg, response.statusCode, responseConfig, task, response));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
reject(new AxiosError(error.errMsg, void 0, responseConfig, task));
|
|
230
|
+
task = null;
|
|
231
|
+
},
|
|
232
|
+
complete() {
|
|
233
|
+
onCanceled.unsubscribe();
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
if (typeof config.onHeadersReceived === "function") task.onHeadersReceived(config.onHeadersReceived);
|
|
237
|
+
onCanceled.subscribe(task, reject);
|
|
238
|
+
});
|
|
239
|
+
};
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/methods/upload.ts
|
|
242
|
+
const upload = (config, options) => {
|
|
243
|
+
return new Promise((resolve, reject) => {
|
|
244
|
+
const requestOptions = resolveUniAppRequestOptions(config, options);
|
|
245
|
+
const responseConfig = config;
|
|
246
|
+
responseConfig.headers = new AxiosHeaders(requestOptions.header);
|
|
247
|
+
const onCanceled = new OnCanceled(config);
|
|
248
|
+
let task = uni.uploadFile({
|
|
249
|
+
...requestOptions,
|
|
250
|
+
success(result) {
|
|
251
|
+
if (!task) return;
|
|
252
|
+
settle(resolve, reject, {
|
|
253
|
+
config: responseConfig,
|
|
254
|
+
data: result.data,
|
|
255
|
+
headers: {},
|
|
256
|
+
status: result.statusCode,
|
|
257
|
+
statusText: result.errMsg ?? "OK",
|
|
258
|
+
request: task
|
|
259
|
+
});
|
|
260
|
+
task = null;
|
|
261
|
+
},
|
|
262
|
+
fail(error) {
|
|
263
|
+
const { errMsg = "" } = error ?? {};
|
|
264
|
+
if (errMsg) {
|
|
265
|
+
const isTimeoutError = errMsg === "uploadFile:fail timeout";
|
|
266
|
+
const isNetworkError = errMsg === "uploadFile:fail file error";
|
|
267
|
+
if (isTimeoutError) reject(new AxiosError(errMsg, AxiosError.ETIMEDOUT, responseConfig, task));
|
|
268
|
+
if (isNetworkError) reject(new AxiosError(errMsg, AxiosError.ERR_NETWORK, responseConfig, task));
|
|
269
|
+
}
|
|
270
|
+
reject(new AxiosError(error.errMsg, void 0, responseConfig, task));
|
|
271
|
+
task = null;
|
|
272
|
+
},
|
|
273
|
+
complete() {
|
|
274
|
+
onCanceled.unsubscribe();
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
if (typeof config.onHeadersReceived === "function") task.onHeadersReceived(config.onHeadersReceived);
|
|
278
|
+
onCanceled.subscribe(task, reject);
|
|
279
|
+
});
|
|
280
|
+
};
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/methods/index.ts
|
|
283
|
+
function getMethod(config) {
|
|
284
|
+
switch (getMethodType(config)) {
|
|
285
|
+
case "download": return download;
|
|
286
|
+
case "upload": return upload;
|
|
287
|
+
default: return request;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
//#endregion
|
|
291
|
+
//#region src/index.ts
|
|
292
|
+
function createUniAppAxiosAdapter(userOptions = {}) {
|
|
293
|
+
const options = resolveOptions(userOptions);
|
|
294
|
+
Axios.prototype.download = function(url, config) {
|
|
295
|
+
return this.request({
|
|
296
|
+
url,
|
|
297
|
+
method: "download",
|
|
298
|
+
...config
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
Axios.prototype.upload = function(url, data, config) {
|
|
302
|
+
return this.request({
|
|
303
|
+
url,
|
|
304
|
+
method: "upload",
|
|
305
|
+
data,
|
|
306
|
+
...config
|
|
307
|
+
});
|
|
308
|
+
};
|
|
309
|
+
const uniappAdapter = (config) => {
|
|
310
|
+
return getMethod(config)(config, options);
|
|
311
|
+
};
|
|
312
|
+
return uniappAdapter;
|
|
313
|
+
}
|
|
314
|
+
//#endregion
|
|
315
|
+
export { createUniAppAxiosAdapter };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface Options {}
|
|
3
|
+
interface UserOptions extends Partial<Options> {}
|
|
4
|
+
interface UnpluginOptions {}
|
|
5
|
+
interface UserUnpluginOptions extends Partial<UnpluginOptions> {}
|
|
6
|
+
//#endregion
|
|
7
|
+
export { UserUnpluginOptions as n, UserOptions as t };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface Options {}
|
|
3
|
+
interface UserOptions extends Partial<Options> {}
|
|
4
|
+
interface UnpluginOptions {}
|
|
5
|
+
interface UserUnpluginOptions extends Partial<UnpluginOptions> {}
|
|
6
|
+
//#endregion
|
|
7
|
+
export { UserUnpluginOptions as n, UserOptions as t };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const require_chunk = require("./chunk-CKQMccvm.cjs");
|
|
2
|
+
let node_process = require("node:process");
|
|
3
|
+
node_process = require_chunk.__toESM(node_process, 1);
|
|
4
|
+
let local_pkg = require("local-pkg");
|
|
5
|
+
//#region src/unplugin.ts
|
|
6
|
+
const unplugin$1 = (0, require("unplugin").createUnplugin)((_options = {}) => {
|
|
7
|
+
const hasFormDataPolyfill = (0, local_pkg.isPackageExists)("miniprogram-formdata");
|
|
8
|
+
const hasBlobPolyfill = (0, local_pkg.isPackageExists)("miniprogram-blob");
|
|
9
|
+
return {
|
|
10
|
+
name: "unplugin-uni-axios-adapter",
|
|
11
|
+
enforce: "pre",
|
|
12
|
+
transform(code, id) {
|
|
13
|
+
if (node_process.default.env.UNI_PLATFORM?.includes("mp")) {
|
|
14
|
+
if (id.includes("/form-data/lib/browser.js")) return { code: code.replace("window", "globalThis") };
|
|
15
|
+
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) return { code: `${hasFormDataPolyfill ? "import FormData from 'miniprogram-formdata';" : "class FormData {};"}\nexport default FormData;` };
|
|
16
|
+
if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) return { code: `${hasBlobPolyfill ? "import Blob from 'miniprogram-blob';" : "class Blob {};"}\nexport default Blob;` };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
//#endregion
|
|
22
|
+
Object.defineProperty(exports, "unplugin", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: function() {
|
|
25
|
+
return unplugin$1;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import process from "node:process";
|
|
2
|
+
import { isPackageExists } from "local-pkg";
|
|
3
|
+
import { createUnplugin } from "unplugin";
|
|
4
|
+
//#region src/unplugin.ts
|
|
5
|
+
const unplugin = createUnplugin((_options = {}) => {
|
|
6
|
+
const hasFormDataPolyfill = isPackageExists("miniprogram-formdata");
|
|
7
|
+
const hasBlobPolyfill = isPackageExists("miniprogram-blob");
|
|
8
|
+
return {
|
|
9
|
+
name: "unplugin-uni-axios-adapter",
|
|
10
|
+
enforce: "pre",
|
|
11
|
+
transform(code, id) {
|
|
12
|
+
if (process.env.UNI_PLATFORM?.includes("mp")) {
|
|
13
|
+
if (id.includes("/form-data/lib/browser.js")) return { code: code.replace("window", "globalThis") };
|
|
14
|
+
if (id.includes("/axios/lib/platform/browser/classes/FormData.js")) return { code: `${hasFormDataPolyfill ? "import FormData from 'miniprogram-formdata';" : "class FormData {};"}\nexport default FormData;` };
|
|
15
|
+
if (id.includes("/axios/lib/platform/browser/classes/Blob.js")) return { code: `${hasBlobPolyfill ? "import Blob from 'miniprogram-blob';" : "class Blob {};"}\nexport default Blob;` };
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
//#endregion
|
|
21
|
+
export { unplugin as t };
|
package/dist/vite.cjs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var i=chunkY4IH4XRU_cjs.a.vite;
|
|
6
|
-
|
|
7
|
-
module.exports = i;
|
|
1
|
+
//#region src/vite.ts
|
|
2
|
+
var vite_default = require("./unplugin-B0XXJPh7.cjs").unplugin.vite;
|
|
3
|
+
//#endregion
|
|
4
|
+
module.exports = vite_default;
|
package/dist/vite.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { n as UserUnpluginOptions } from "./types-BSLo1MIA.cjs";
|
|
2
|
+
import * as _$vite from "vite";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
4
|
+
//#region src/vite.d.ts
|
|
5
|
+
declare const _default: (options: UserUnpluginOptions) => _$vite.Plugin<any> | _$vite.Plugin<any>[];
|
|
6
|
+
export = _default;
|
package/dist/vite.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { n as UserUnpluginOptions } from "./types-C-Kh3_5J.js";
|
|
2
|
+
import * as _$vite from "vite";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
//#region src/vite.d.ts
|
|
5
|
+
declare const _default: (options: UserUnpluginOptions) => _$vite.Plugin<any> | _$vite.Plugin<any>[];
|
|
6
|
+
//#endregion
|
|
7
|
+
export { _default as default };
|
package/dist/vite.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
export {
|
|
1
|
+
import { t as unplugin } from "./unplugin-GFuC6tIl.js";
|
|
2
|
+
//#region src/vite.ts
|
|
3
|
+
var vite_default = unplugin.vite;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { vite_default as default };
|
package/dist/webpack.cjs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var o=chunkY4IH4XRU_cjs.a.webpack;
|
|
6
|
-
|
|
7
|
-
module.exports = o;
|
|
1
|
+
//#region src/webpack.ts
|
|
2
|
+
var webpack_default = require("./unplugin-B0XXJPh7.cjs").unplugin.webpack;
|
|
3
|
+
//#endregion
|
|
4
|
+
module.exports = webpack_default;
|
package/dist/webpack.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as UserUnpluginOptions } from "./types-BSLo1MIA.cjs";
|
|
2
2
|
|
|
3
|
+
//#region src/webpack.d.ts
|
|
3
4
|
declare const _default: (options: UserUnpluginOptions) => WebpackPluginInstance;
|
|
4
|
-
|
|
5
|
-
export { _default as default };
|
|
5
|
+
export = _default;
|
package/dist/webpack.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as UserUnpluginOptions } from "./types-C-Kh3_5J.js";
|
|
2
2
|
|
|
3
|
+
//#region src/webpack.d.ts
|
|
3
4
|
declare const _default: (options: UserUnpluginOptions) => WebpackPluginInstance;
|
|
4
|
-
|
|
5
|
-
export { _default as default };
|
|
5
|
+
//#endregion
|
|
6
|
+
export { _default as default };
|
package/dist/webpack.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
export {
|
|
1
|
+
import { t as unplugin } from "./unplugin-GFuC6tIl.js";
|
|
2
|
+
//#region src/webpack.ts
|
|
3
|
+
var webpack_default = unplugin.webpack;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { webpack_default as default };
|
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uni-helper/axios-adapter",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
5
|
-
"
|
|
4
|
+
"version": "1.16.0",
|
|
5
|
+
"description": "The Axios adapter for uniapp",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "kejunmao",
|
|
8
8
|
"email": "kejun1997@gmail.com"
|
|
9
9
|
},
|
|
10
|
-
"
|
|
11
|
-
"description": "The Axios adapter for uniapp",
|
|
10
|
+
"license": "MIT",
|
|
12
11
|
"homepage": "https://github.com/uni-helper/axios-adapter",
|
|
13
12
|
"repository": {
|
|
14
13
|
"type": "git",
|
|
@@ -57,35 +56,38 @@
|
|
|
57
56
|
"module": "./dist/index.js",
|
|
58
57
|
"types": "./dist/index.d.ts",
|
|
59
58
|
"files": [
|
|
60
|
-
"
|
|
61
|
-
"
|
|
59
|
+
"./client.d.ts",
|
|
60
|
+
"dist"
|
|
62
61
|
],
|
|
63
|
-
"devDependencies": {
|
|
64
|
-
"@dcloudio/types": "^3.3.3",
|
|
65
|
-
"@types/node": "^20.6.0",
|
|
66
|
-
"axios": "^1.5.0",
|
|
67
|
-
"bumpp": "^9.2.0",
|
|
68
|
-
"eslint": "^8.49.0",
|
|
69
|
-
"pnpm": "^8.7.5",
|
|
70
|
-
"rimraf": "^5.0.1",
|
|
71
|
-
"tsup": "^7.2.0",
|
|
72
|
-
"typescript": "^5.2.2",
|
|
73
|
-
"vite": "^4.4.9",
|
|
74
|
-
"vitest": "^0.34.4"
|
|
75
|
-
},
|
|
76
62
|
"peerDependencies": {
|
|
77
|
-
"axios": "^1.
|
|
63
|
+
"axios": "^1.16.0",
|
|
64
|
+
"vite": "^5.0.0"
|
|
78
65
|
},
|
|
79
66
|
"dependencies": {
|
|
80
|
-
"local-pkg": "^
|
|
81
|
-
"unplugin": "^
|
|
67
|
+
"local-pkg": "^1.1.2",
|
|
68
|
+
"unplugin": "^2.3.11"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@antfu/eslint-config": "^8.2.0",
|
|
72
|
+
"@dcloudio/types": "^3.4.31",
|
|
73
|
+
"@types/node": "^24.12.2",
|
|
74
|
+
"axios": "^1.16.0",
|
|
75
|
+
"bumpp": "^11.0.1",
|
|
76
|
+
"eslint": "^10.3.0",
|
|
77
|
+
"msw": "^2.14.3",
|
|
78
|
+
"tsdown": "^0.22.0",
|
|
79
|
+
"typescript": "^5.9.3",
|
|
80
|
+
"vite": "^5.4.21",
|
|
81
|
+
"vitest": "^3.2.4"
|
|
82
82
|
},
|
|
83
83
|
"scripts": {
|
|
84
|
-
"build": "
|
|
85
|
-
"dev": "
|
|
84
|
+
"build": "tsdown",
|
|
85
|
+
"dev": "tsdown --watch",
|
|
86
86
|
"play": "npm -C playground run dev:h5",
|
|
87
87
|
"release": "bumpp",
|
|
88
88
|
"test": "vitest",
|
|
89
|
-
"typecheck": "tsc --noEmit"
|
|
89
|
+
"typecheck": "tsc --noEmit",
|
|
90
|
+
"lint": "eslint .",
|
|
91
|
+
"lint:fix": "eslint . --fix"
|
|
90
92
|
}
|
|
91
93
|
}
|
package/dist/chunk-IWRE2NJT.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { createUnplugin } from 'unplugin';
|
|
2
|
-
import { isPackageExists } from 'local-pkg';
|
|
3
|
-
|
|
4
|
-
var p=createUnplugin((e={})=>{let i=isPackageExists("miniprogram-formdata"),s=isPackageExists("miniprogram-blob");return {name:"unplugin-uni-axios-adapter",enforce:"pre",transform(l,o){var r;if((r=process.env.UNI_PLATFORM)!=null&&r.includes("mp")){if(o.includes("/form-data/lib/browser.js"))return {code:l.replace("window","globalThis")};if(o.includes("/axios/lib/platform/browser/classes/FormData.js"))return {code:`${i?"import FormData from 'miniprogram-formdata';":"class FormData {};"}
|
|
5
|
-
export default FormData;`};if(o.includes("/axios/lib/platform/browser/classes/Blob.js"))return {code:`${s?"import Blob from 'miniprogram-blob';":"class Blob {};"}
|
|
6
|
-
export default Blob;`}}}}});
|
|
7
|
-
|
|
8
|
-
export { p as a };
|
package/dist/chunk-Y4IH4XRU.cjs
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var unplugin = require('unplugin');
|
|
4
|
-
var localPkg = require('local-pkg');
|
|
5
|
-
|
|
6
|
-
var p=unplugin.createUnplugin((e={})=>{let i=localPkg.isPackageExists("miniprogram-formdata"),s=localPkg.isPackageExists("miniprogram-blob");return {name:"unplugin-uni-axios-adapter",enforce:"pre",transform(l,o){var r;if((r=process.env.UNI_PLATFORM)!=null&&r.includes("mp")){if(o.includes("/form-data/lib/browser.js"))return {code:l.replace("window","globalThis")};if(o.includes("/axios/lib/platform/browser/classes/FormData.js"))return {code:`${i?"import FormData from 'miniprogram-formdata';":"class FormData {};"}
|
|
7
|
-
export default FormData;`};if(o.includes("/axios/lib/platform/browser/classes/Blob.js"))return {code:`${s?"import Blob from 'miniprogram-blob';":"class Blob {};"}
|
|
8
|
-
export default Blob;`}}}}});
|
|
9
|
-
|
|
10
|
-
exports.a = p;
|
package/dist/types-76de936f.d.ts
DELETED