@lx-frontend/multi-ajax 0.2.5 → 0.2.6-beta.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/build/scripts/patch-axios-post-install.js +8 -2
- package/build/utils.js +5 -3
- package/dist/index.js +816 -0
- package/dist/index.js.map +1 -0
- package/output/src/adapters/index.js +2 -0
- package/output/src/adapters/wx-adapter.js +142 -0
- package/output/src/cancelLastSameRequest.js +81 -0
- package/output/src/canceller.js +94 -0
- package/output/src/compatibleMethods.js +303 -0
- package/output/src/duplicateRequestPrevent.js +17 -0
- package/output/src/helpers.js +130 -0
- package/output/src/index.js +36 -0
- package/output/src/methods.js +318 -0
- package/output/src/types.js +1 -0
- package/output/src/utils.js +108 -0
- package/package.json +1 -1
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// 兼容以前的老项目写法
|
|
2
|
+
function _array_like_to_array(arr, len) {
|
|
3
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
4
|
+
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
5
|
+
return arr2;
|
|
6
|
+
}
|
|
7
|
+
function _array_with_holes(arr) {
|
|
8
|
+
if (Array.isArray(arr)) return arr;
|
|
9
|
+
}
|
|
10
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
11
|
+
try {
|
|
12
|
+
var info = gen[key](arg);
|
|
13
|
+
var value = info.value;
|
|
14
|
+
} catch (error) {
|
|
15
|
+
reject(error);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (info.done) {
|
|
19
|
+
resolve(value);
|
|
20
|
+
} else {
|
|
21
|
+
Promise.resolve(value).then(_next, _throw);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function _async_to_generator(fn) {
|
|
25
|
+
return function() {
|
|
26
|
+
var self = this, args = arguments;
|
|
27
|
+
return new Promise(function(resolve, reject) {
|
|
28
|
+
var gen = fn.apply(self, args);
|
|
29
|
+
function _next(value) {
|
|
30
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
31
|
+
}
|
|
32
|
+
function _throw(err) {
|
|
33
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
34
|
+
}
|
|
35
|
+
_next(undefined);
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function _define_property(obj, key, value) {
|
|
40
|
+
if (key in obj) {
|
|
41
|
+
Object.defineProperty(obj, key, {
|
|
42
|
+
value: value,
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true
|
|
46
|
+
});
|
|
47
|
+
} else {
|
|
48
|
+
obj[key] = value;
|
|
49
|
+
}
|
|
50
|
+
return obj;
|
|
51
|
+
}
|
|
52
|
+
function _iterable_to_array_limit(arr, i) {
|
|
53
|
+
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
54
|
+
if (_i == null) return;
|
|
55
|
+
var _arr = [];
|
|
56
|
+
var _n = true;
|
|
57
|
+
var _d = false;
|
|
58
|
+
var _s, _e;
|
|
59
|
+
try {
|
|
60
|
+
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
61
|
+
_arr.push(_s.value);
|
|
62
|
+
if (i && _arr.length === i) break;
|
|
63
|
+
}
|
|
64
|
+
} catch (err) {
|
|
65
|
+
_d = true;
|
|
66
|
+
_e = err;
|
|
67
|
+
} finally{
|
|
68
|
+
try {
|
|
69
|
+
if (!_n && _i["return"] != null) _i["return"]();
|
|
70
|
+
} finally{
|
|
71
|
+
if (_d) throw _e;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return _arr;
|
|
75
|
+
}
|
|
76
|
+
function _non_iterable_rest() {
|
|
77
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
78
|
+
}
|
|
79
|
+
function _object_spread(target) {
|
|
80
|
+
for(var i = 1; i < arguments.length; i++){
|
|
81
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
82
|
+
var ownKeys = Object.keys(source);
|
|
83
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
84
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
85
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
86
|
+
}));
|
|
87
|
+
}
|
|
88
|
+
ownKeys.forEach(function(key) {
|
|
89
|
+
_define_property(target, key, source[key]);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return target;
|
|
93
|
+
}
|
|
94
|
+
function ownKeys(object, enumerableOnly) {
|
|
95
|
+
var keys = Object.keys(object);
|
|
96
|
+
if (Object.getOwnPropertySymbols) {
|
|
97
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
|
98
|
+
if (enumerableOnly) {
|
|
99
|
+
symbols = symbols.filter(function(sym) {
|
|
100
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
keys.push.apply(keys, symbols);
|
|
104
|
+
}
|
|
105
|
+
return keys;
|
|
106
|
+
}
|
|
107
|
+
function _object_spread_props(target, source) {
|
|
108
|
+
source = source != null ? source : {};
|
|
109
|
+
if (Object.getOwnPropertyDescriptors) {
|
|
110
|
+
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
111
|
+
} else {
|
|
112
|
+
ownKeys(Object(source)).forEach(function(key) {
|
|
113
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return target;
|
|
117
|
+
}
|
|
118
|
+
function _sliced_to_array(arr, i) {
|
|
119
|
+
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
120
|
+
}
|
|
121
|
+
function _unsupported_iterable_to_array(o, minLen) {
|
|
122
|
+
if (!o) return;
|
|
123
|
+
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
124
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
125
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
126
|
+
if (n === "Map" || n === "Set") return Array.from(n);
|
|
127
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
128
|
+
}
|
|
129
|
+
function _ts_generator(thisArg, body) {
|
|
130
|
+
var f, y, t, g, _ = {
|
|
131
|
+
label: 0,
|
|
132
|
+
sent: function() {
|
|
133
|
+
if (t[0] & 1) throw t[1];
|
|
134
|
+
return t[1];
|
|
135
|
+
},
|
|
136
|
+
trys: [],
|
|
137
|
+
ops: []
|
|
138
|
+
};
|
|
139
|
+
return g = {
|
|
140
|
+
next: verb(0),
|
|
141
|
+
"throw": verb(1),
|
|
142
|
+
"return": verb(2)
|
|
143
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
144
|
+
return this;
|
|
145
|
+
}), g;
|
|
146
|
+
function verb(n) {
|
|
147
|
+
return function(v) {
|
|
148
|
+
return step([
|
|
149
|
+
n,
|
|
150
|
+
v
|
|
151
|
+
]);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function step(op) {
|
|
155
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
156
|
+
while(_)try {
|
|
157
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
158
|
+
if (y = 0, t) op = [
|
|
159
|
+
op[0] & 2,
|
|
160
|
+
t.value
|
|
161
|
+
];
|
|
162
|
+
switch(op[0]){
|
|
163
|
+
case 0:
|
|
164
|
+
case 1:
|
|
165
|
+
t = op;
|
|
166
|
+
break;
|
|
167
|
+
case 4:
|
|
168
|
+
_.label++;
|
|
169
|
+
return {
|
|
170
|
+
value: op[1],
|
|
171
|
+
done: false
|
|
172
|
+
};
|
|
173
|
+
case 5:
|
|
174
|
+
_.label++;
|
|
175
|
+
y = op[1];
|
|
176
|
+
op = [
|
|
177
|
+
0
|
|
178
|
+
];
|
|
179
|
+
continue;
|
|
180
|
+
case 7:
|
|
181
|
+
op = _.ops.pop();
|
|
182
|
+
_.trys.pop();
|
|
183
|
+
continue;
|
|
184
|
+
default:
|
|
185
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
186
|
+
_ = 0;
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
190
|
+
_.label = op[1];
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
194
|
+
_.label = t[1];
|
|
195
|
+
t = op;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
if (t && _.label < t[2]) {
|
|
199
|
+
_.label = t[2];
|
|
200
|
+
_.ops.push(op);
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
if (t[2]) _.ops.pop();
|
|
204
|
+
_.trys.pop();
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
op = body.call(thisArg, _);
|
|
208
|
+
} catch (e) {
|
|
209
|
+
op = [
|
|
210
|
+
6,
|
|
211
|
+
e
|
|
212
|
+
];
|
|
213
|
+
y = 0;
|
|
214
|
+
} finally{
|
|
215
|
+
f = t = 0;
|
|
216
|
+
}
|
|
217
|
+
if (op[0] & 5) throw op[1];
|
|
218
|
+
return {
|
|
219
|
+
value: op[0] ? op[1] : void 0,
|
|
220
|
+
done: true
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
import { addPendingRequest, checkIfHasPendingRequest, clearPendingRequest, getCachedRequest } from './duplicateRequestPrevent';
|
|
225
|
+
import { normalizeConfig } from './helpers';
|
|
226
|
+
import { generateRequestHash } from './utils';
|
|
227
|
+
var methodMap = {
|
|
228
|
+
query: 'GET',
|
|
229
|
+
create: 'POST',
|
|
230
|
+
putWay: 'PUT',
|
|
231
|
+
deletes: 'DELETE',
|
|
232
|
+
patchWay: 'PATCH'
|
|
233
|
+
};
|
|
234
|
+
export var generateCustomMethod = function(ctx, name) {
|
|
235
|
+
return function(path) {
|
|
236
|
+
var config = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
237
|
+
return function() {
|
|
238
|
+
var _ref = _async_to_generator(function(data, expand) {
|
|
239
|
+
var url, _ref, newParams, newData, config1, options, hashKey, request, res;
|
|
240
|
+
return _ts_generator(this, function(_state) {
|
|
241
|
+
switch(_state.label){
|
|
242
|
+
case 0:
|
|
243
|
+
url = expand ? "".concat(path, "/").concat(expand) : path;
|
|
244
|
+
if (expand) {
|
|
245
|
+
// eslint-disable-next-line no-console
|
|
246
|
+
console.warn("业务代码中拼接url发送请求的方式即将废弃,请使用ajax.".concat(methodMap[name].toLocaleLowerCase(), "接口替换。相关url:").concat(url));
|
|
247
|
+
}
|
|
248
|
+
_ref = _sliced_to_array(methodMap[name] === 'GET' ? [
|
|
249
|
+
data,
|
|
250
|
+
undefined
|
|
251
|
+
] : [
|
|
252
|
+
undefined,
|
|
253
|
+
data
|
|
254
|
+
], 2), newParams = _ref[0], newData = _ref[1];
|
|
255
|
+
config1 = normalizeConfig(ctx, _object_spread_props(_object_spread({}, config), {
|
|
256
|
+
url: url,
|
|
257
|
+
params: newParams,
|
|
258
|
+
data: newData,
|
|
259
|
+
method: methodMap[name]
|
|
260
|
+
}));
|
|
261
|
+
options = _object_spread_props(_object_spread({}, config1), {
|
|
262
|
+
custom: _object_spread({}, ctx.$options, config1)
|
|
263
|
+
});
|
|
264
|
+
hashKey = generateRequestHash(config1);
|
|
265
|
+
if (checkIfHasPendingRequest(hashKey)) {
|
|
266
|
+
// 重复,且处于pending状态的请求,复用
|
|
267
|
+
return [
|
|
268
|
+
2,
|
|
269
|
+
getCachedRequest(hashKey)
|
|
270
|
+
];
|
|
271
|
+
}
|
|
272
|
+
request = ctx.request(options);
|
|
273
|
+
// 缓存请求
|
|
274
|
+
addPendingRequest(hashKey, request, options.reUseDuration);
|
|
275
|
+
return [
|
|
276
|
+
4,
|
|
277
|
+
request
|
|
278
|
+
];
|
|
279
|
+
case 1:
|
|
280
|
+
res = _state.sent();
|
|
281
|
+
// 清理缓存的请求
|
|
282
|
+
clearPendingRequest(hashKey);
|
|
283
|
+
return [
|
|
284
|
+
2,
|
|
285
|
+
res
|
|
286
|
+
];
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
return function(data, expand) {
|
|
291
|
+
return _ref.apply(this, arguments);
|
|
292
|
+
};
|
|
293
|
+
}();
|
|
294
|
+
};
|
|
295
|
+
};
|
|
296
|
+
function addCompatibleMethods(ctx) {
|
|
297
|
+
ctx.query = generateCustomMethod(ctx, 'query');
|
|
298
|
+
ctx.create = generateCustomMethod(ctx, 'create');
|
|
299
|
+
ctx.deletes = generateCustomMethod(ctx, 'deletes');
|
|
300
|
+
ctx.putWay = generateCustomMethod(ctx, 'putWay');
|
|
301
|
+
ctx.patchWay = generateCustomMethod(ctx, 'patchWay');
|
|
302
|
+
}
|
|
303
|
+
export default addCompatibleMethods;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { generateRequestHash } from "./utils";
|
|
2
|
+
var pendingRequest = {};
|
|
3
|
+
export function addPendingRequest(hash, request, timeout) {
|
|
4
|
+
pendingRequest[hash] = request;
|
|
5
|
+
setTimeout(function() {
|
|
6
|
+
clearPendingRequest(hash);
|
|
7
|
+
}, timeout);
|
|
8
|
+
}
|
|
9
|
+
export function clearPendingRequest(hash) {
|
|
10
|
+
Reflect.deleteProperty(pendingRequest, hash);
|
|
11
|
+
}
|
|
12
|
+
export function checkIfHasPendingRequest(hashKey) {
|
|
13
|
+
return Reflect.has(pendingRequest, hashKey);
|
|
14
|
+
}
|
|
15
|
+
export function getCachedRequest(hashKey) {
|
|
16
|
+
return pendingRequest[hashKey];
|
|
17
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
function _object_spread(target) {
|
|
15
|
+
for(var i = 1; i < arguments.length; i++){
|
|
16
|
+
var source = arguments[i] != null ? arguments[i] : {};
|
|
17
|
+
var ownKeys = Object.keys(source);
|
|
18
|
+
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
19
|
+
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
20
|
+
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
21
|
+
}));
|
|
22
|
+
}
|
|
23
|
+
ownKeys.forEach(function(key) {
|
|
24
|
+
_define_property(target, key, source[key]);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
}
|
|
29
|
+
function _object_without_properties(source, excluded) {
|
|
30
|
+
if (source == null) return {};
|
|
31
|
+
var target = _object_without_properties_loose(source, excluded);
|
|
32
|
+
var key, i;
|
|
33
|
+
if (Object.getOwnPropertySymbols) {
|
|
34
|
+
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
35
|
+
for(i = 0; i < sourceSymbolKeys.length; i++){
|
|
36
|
+
key = sourceSymbolKeys[i];
|
|
37
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
38
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
39
|
+
target[key] = source[key];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return target;
|
|
43
|
+
}
|
|
44
|
+
function _object_without_properties_loose(source, excluded) {
|
|
45
|
+
if (source == null) return {};
|
|
46
|
+
var target = {};
|
|
47
|
+
var sourceKeys = Object.keys(source);
|
|
48
|
+
var key, i;
|
|
49
|
+
for(i = 0; i < sourceKeys.length; i++){
|
|
50
|
+
key = sourceKeys[i];
|
|
51
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
52
|
+
target[key] = source[key];
|
|
53
|
+
}
|
|
54
|
+
return target;
|
|
55
|
+
}
|
|
56
|
+
function _type_of(obj) {
|
|
57
|
+
"@swc/helpers - typeof";
|
|
58
|
+
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
|
|
59
|
+
}
|
|
60
|
+
import qs from 'qs';
|
|
61
|
+
import { callCancellerByCancelKey, hasCancelers } from './canceller';
|
|
62
|
+
/**
|
|
63
|
+
* 标准化config配置,或根据配置项设置相关的参数
|
|
64
|
+
* 该函数返回的配置对象,应该完全符合axios的配置规范
|
|
65
|
+
* @param {*} config
|
|
66
|
+
*/ export function normalizeConfig(ctx, config) {
|
|
67
|
+
var _ref = _object_spread({}, ctx.$options, config), _ref_headers = _ref.headers, headers = _ref_headers === void 0 ? {} : _ref_headers, _ref_sendFormData = _ref.sendFormData, sendFormData = _ref_sendFormData === void 0 ? false : _ref_sendFormData, _ref_data = _ref.data, data = _ref_data === void 0 ? undefined : _ref_data, _ref_params = _ref.params, params = _ref_params === void 0 ? {} : _ref_params, _ref_baseUrl = _ref.baseUrl, baseUrl = _ref_baseUrl === void 0 ? '' : _ref_baseUrl, _ref_baseURL = _ref.baseURL, baseURL = _ref_baseURL === void 0 ? '' : _ref_baseURL, _ref_reUseDuration = _ref.reUseDuration, reUseDuration = _ref_reUseDuration === void 0 ? 300 : _ref_reUseDuration, rest = _object_without_properties(_ref, [
|
|
68
|
+
"headers",
|
|
69
|
+
"sendFormData",
|
|
70
|
+
"data",
|
|
71
|
+
"params",
|
|
72
|
+
"baseUrl",
|
|
73
|
+
"baseURL",
|
|
74
|
+
"reUseDuration"
|
|
75
|
+
]);
|
|
76
|
+
// 数据格式为FormData,修改content-type头,以及修改data格式
|
|
77
|
+
var contentType = headers['content-type'] || headers['Content-Type'] || 'application/json';
|
|
78
|
+
headers['content-type'] = sendFormData ? 'application/x-www-form-urlencoded' : contentType;
|
|
79
|
+
var sendData = sendFormData && (typeof data === "undefined" ? "undefined" : _type_of(data)) === 'object' && data !== null ? qs.stringify(data) : data;
|
|
80
|
+
// baseUrl,支持两种写法,且用户配置的baseUrl优先级最高
|
|
81
|
+
var bUrl = baseUrl || baseURL || ctx.$baseUrl;
|
|
82
|
+
if (typeof reUseDuration !== 'number' || Number.isNaN(reUseDuration)) {
|
|
83
|
+
throw new Error('ajax配置reUseDuration必须是数字');
|
|
84
|
+
}
|
|
85
|
+
return _object_spread({
|
|
86
|
+
headers: headers,
|
|
87
|
+
params: params,
|
|
88
|
+
data: sendData,
|
|
89
|
+
baseURL: bUrl,
|
|
90
|
+
reUseDuration: reUseDuration
|
|
91
|
+
}, rest);
|
|
92
|
+
}
|
|
93
|
+
export function addInstanceExtendMethod(ctx) {
|
|
94
|
+
var extend = function(source) {
|
|
95
|
+
Object.keys(source).forEach(function(key) {
|
|
96
|
+
ctx[key] = source[key];
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
ctx.extend = extend;
|
|
100
|
+
}
|
|
101
|
+
export function proxyAxiosInstanceAttrs(target, source) {
|
|
102
|
+
var attrs = [
|
|
103
|
+
'request',
|
|
104
|
+
'interceptors',
|
|
105
|
+
'defaults'
|
|
106
|
+
];
|
|
107
|
+
// const methods: ProxyedAttrs[] = ['request', 'get', 'post', 'delete', 'head', 'options', 'put', 'patch', 'interceptors'];
|
|
108
|
+
attrs.forEach(function(method) {
|
|
109
|
+
Object.defineProperty(target, method, {
|
|
110
|
+
value: source[method],
|
|
111
|
+
writable: false
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
export function proxyAjaxCanceler(ctx) {
|
|
116
|
+
ctx.canceller = new Proxy({}, {
|
|
117
|
+
get: function get(target, prop) {
|
|
118
|
+
if (!hasCancelers(prop)) {
|
|
119
|
+
return function() {
|
|
120
|
+
var str = "ajax.canceller对象上不存在名为".concat(prop, "的函数!");
|
|
121
|
+
console.warn(str);
|
|
122
|
+
return str;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return function() {
|
|
126
|
+
callCancellerByCancelKey(prop);
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { wxAdatper } from './adapters/index';
|
|
3
|
+
import addCompatibleMethods from './compatibleMethods';
|
|
4
|
+
import { addInstanceExtendMethod, proxyAjaxCanceler, proxyAxiosInstanceAttrs } from './helpers';
|
|
5
|
+
import { generateMethod } from './methods';
|
|
6
|
+
function isWxapp() {
|
|
7
|
+
return typeof wx !== 'undefined' && typeof wx.request === 'function';
|
|
8
|
+
}
|
|
9
|
+
export default function initAjax(options) {
|
|
10
|
+
var ajax = {};
|
|
11
|
+
ajax.$options = options;
|
|
12
|
+
// 检测wx如果没有自定义的适配器,则使用默认适配器
|
|
13
|
+
var adapter = ajax.$options.adapter || (isWxapp() ? wxAdatper : undefined);
|
|
14
|
+
ajax.$options.adapter = adapter;
|
|
15
|
+
ajax.$engine = axios.create();
|
|
16
|
+
var requestMethods = [
|
|
17
|
+
'POST',
|
|
18
|
+
'PUT',
|
|
19
|
+
'PATCH',
|
|
20
|
+
'DELETE',
|
|
21
|
+
'GET',
|
|
22
|
+
'HEAD'
|
|
23
|
+
];
|
|
24
|
+
requestMethods.forEach(function(name) {
|
|
25
|
+
var queryMethods = name.toLocaleLowerCase();
|
|
26
|
+
ajax[queryMethods] = generateMethod(ajax, name);
|
|
27
|
+
});
|
|
28
|
+
// 挂载请求的手动取消函数
|
|
29
|
+
proxyAjaxCanceler(ajax);
|
|
30
|
+
// 把ajax实例上的'request', 'interceptors', defaults属性代理到axios实例上去
|
|
31
|
+
proxyAxiosInstanceAttrs(ajax, ajax.$engine);
|
|
32
|
+
addCompatibleMethods(ajax);
|
|
33
|
+
// 添加extend方法,用于扩展ajax实例
|
|
34
|
+
addInstanceExtendMethod(ajax);
|
|
35
|
+
return ajax;
|
|
36
|
+
}
|