yuand 1.0.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 +23 -0
- package/es/components/Table/index.d.ts +12 -0
- package/es/components/Table/index.js +326 -0
- package/es/components/Table/style.css +3 -0
- package/es/components/Table/types.d.ts +101 -0
- package/es/components/Table/types.js +1 -0
- package/es/components/Table/useTable.d.ts +3 -0
- package/es/components/Table/useTable.js +76 -0
- package/es/components/TableConfig/index.d.ts +11 -0
- package/es/components/TableConfig/index.js +19 -0
- package/es/components/TableUseFetch/index.d.ts +12 -0
- package/es/components/TableUseFetch/index.js +283 -0
- package/es/components/TableUseFetch/style.css +3 -0
- package/es/components/TableUseFetch/types.d.ts +100 -0
- package/es/components/TableUseFetch/types.js +1 -0
- package/es/components/TableUseFetch/useTable.d.ts +3 -0
- package/es/components/TableUseFetch/useTable.js +78 -0
- package/es/components/index.d.ts +3 -0
- package/es/components/index.js +3 -0
- package/es/fetch.d.ts +137 -0
- package/es/fetch.js +534 -0
- package/es/hooks/index.d.ts +4 -0
- package/es/hooks/index.js +4 -0
- package/es/hooks/useFetch.d.ts +17 -0
- package/es/hooks/useFetch.js +41 -0
- package/es/hooks/useMutation.d.ts +14 -0
- package/es/hooks/useMutation.js +26 -0
- package/es/hooks/useQuery.d.ts +14 -0
- package/es/hooks/useQuery.js +40 -0
- package/es/hooks/useX.d.ts +1 -0
- package/es/hooks/useX.js +13 -0
- package/es/index.d.ts +4 -0
- package/es/index.js +4 -0
- package/es/utils/index.d.ts +6 -0
- package/es/utils/index.js +23 -0
- package/es/utils/table.d.ts +15 -0
- package/es/utils/table.js +106 -0
- package/es/utils/util.d.ts +2 -0
- package/es/utils/util.js +6 -0
- package/lang/en_US.json +6 -0
- package/lang/zh_CN.json +6 -0
- package/lib/components/Table/index.d.ts +12 -0
- package/lib/components/Table/index.js +330 -0
- package/lib/components/Table/style.css +3 -0
- package/lib/components/Table/types.d.ts +101 -0
- package/lib/components/Table/types.js +17 -0
- package/lib/components/Table/useTable.d.ts +3 -0
- package/lib/components/Table/useTable.js +85 -0
- package/lib/components/TableConfig/index.d.ts +11 -0
- package/lib/components/TableConfig/index.js +55 -0
- package/lib/components/TableUseFetch/index.d.ts +12 -0
- package/lib/components/TableUseFetch/index.js +303 -0
- package/lib/components/TableUseFetch/style.css +3 -0
- package/lib/components/TableUseFetch/types.d.ts +100 -0
- package/lib/components/TableUseFetch/types.js +17 -0
- package/lib/components/TableUseFetch/useTable.d.ts +3 -0
- package/lib/components/TableUseFetch/useTable.js +86 -0
- package/lib/components/index.d.ts +3 -0
- package/lib/components/index.js +45 -0
- package/lib/fetch.d.ts +137 -0
- package/lib/fetch.js +366 -0
- package/lib/hooks/index.d.ts +4 -0
- package/lib/hooks/index.js +48 -0
- package/lib/hooks/useFetch.d.ts +17 -0
- package/lib/hooks/useFetch.js +62 -0
- package/lib/hooks/useMutation.d.ts +14 -0
- package/lib/hooks/useMutation.js +43 -0
- package/lib/hooks/useQuery.d.ts +14 -0
- package/lib/hooks/useQuery.js +57 -0
- package/lib/hooks/useX.d.ts +1 -0
- package/lib/hooks/useX.js +37 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +29 -0
- package/lib/utils/index.d.ts +6 -0
- package/lib/utils/index.js +60 -0
- package/lib/utils/table.d.ts +15 -0
- package/lib/utils/table.js +124 -0
- package/lib/utils/util.d.ts +2 -0
- package/lib/utils/util.js +32 -0
- package/package.json +46 -0
package/es/fetch.js
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
3
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
4
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
5
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
6
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
7
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
|
8
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
|
9
|
+
import _inherits from "@babel/runtime/helpers/esm/inherits";
|
|
10
|
+
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
|
|
11
|
+
import _wrapNativeSuper from "@babel/runtime/helpers/esm/wrapNativeSuper";
|
|
12
|
+
var _excluded = ["json", "params", "ignoreError", "returnData", "timeout", "onError", "onLogout", "onSuccess"];
|
|
13
|
+
export var isObject = function isObject(oj) {
|
|
14
|
+
return Object.prototype.toString.call(oj) === "[object Object]";
|
|
15
|
+
};
|
|
16
|
+
export var isFunction = function isFunction(oj) {
|
|
17
|
+
return Object.prototype.toString.call(oj) === "[object Function]";
|
|
18
|
+
};
|
|
19
|
+
var DEFAULT_SUCCESS_CODES = [200];
|
|
20
|
+
var DEFAULT_LOGOUT_CODES = [401, 403];
|
|
21
|
+
var DEFAULT_METHOD = "GET";
|
|
22
|
+
var DEFAULT_CODE_KEY = "code";
|
|
23
|
+
var DEFAULT_DATA_KEY = "data";
|
|
24
|
+
var DEFAULT_MESSAGE_KEY = "message";
|
|
25
|
+
var DEFAULT_CONTENT_TYPE = "application/json;charset=UTF-8";
|
|
26
|
+
var BLOB_CONTENT_TYPES = ["stream", "excel", "download", "blob", "octet-stream"];
|
|
27
|
+
var METHODS_WITHOUT_BODY = ["GET", "HEAD"];
|
|
28
|
+
export var HttpError = /*#__PURE__*/function (_Error) {
|
|
29
|
+
_inherits(HttpError, _Error);
|
|
30
|
+
var _super = _createSuper(HttpError);
|
|
31
|
+
function HttpError(message, code, response, statusCode) {
|
|
32
|
+
var _this;
|
|
33
|
+
_classCallCheck(this, HttpError);
|
|
34
|
+
_this = _super.call(this, message);
|
|
35
|
+
_this.code = code;
|
|
36
|
+
_this.response = response;
|
|
37
|
+
_this.statusCode = statusCode;
|
|
38
|
+
_this.name = "HttpError";
|
|
39
|
+
return _this;
|
|
40
|
+
}
|
|
41
|
+
return _createClass(HttpError);
|
|
42
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
43
|
+
export var HttpClient = /*#__PURE__*/function () {
|
|
44
|
+
function HttpClient() {
|
|
45
|
+
var _configure$baseUrl, _configure$blobConten, _configure$codeKey, _configure$dataKey, _configure$messageKey, _configure$returnData, _configure$defaultMet, _configure$timeout, _configure$codes$succ, _configure$codes, _configure$codes$logo, _configure$codes2, _configure$codes$igno, _configure$codes3;
|
|
46
|
+
var configure = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
47
|
+
_classCallCheck(this, HttpClient);
|
|
48
|
+
_defineProperty(this, "configure", void 0);
|
|
49
|
+
this.configure = {
|
|
50
|
+
baseUrl: (_configure$baseUrl = configure.baseUrl) !== null && _configure$baseUrl !== void 0 ? _configure$baseUrl : "",
|
|
51
|
+
blobContentTypes: (_configure$blobConten = configure.blobContentTypes) !== null && _configure$blobConten !== void 0 ? _configure$blobConten : [].concat(BLOB_CONTENT_TYPES),
|
|
52
|
+
headers: configure.headers,
|
|
53
|
+
codeKey: (_configure$codeKey = configure.codeKey) !== null && _configure$codeKey !== void 0 ? _configure$codeKey : DEFAULT_CODE_KEY,
|
|
54
|
+
dataKey: (_configure$dataKey = configure.dataKey) !== null && _configure$dataKey !== void 0 ? _configure$dataKey : DEFAULT_DATA_KEY,
|
|
55
|
+
messageKey: (_configure$messageKey = configure.messageKey) !== null && _configure$messageKey !== void 0 ? _configure$messageKey : DEFAULT_MESSAGE_KEY,
|
|
56
|
+
returnData: (_configure$returnData = configure.returnData) !== null && _configure$returnData !== void 0 ? _configure$returnData : false,
|
|
57
|
+
defaultMethod: (_configure$defaultMet = configure.defaultMethod) !== null && _configure$defaultMet !== void 0 ? _configure$defaultMet : DEFAULT_METHOD,
|
|
58
|
+
timeout: (_configure$timeout = configure.timeout) !== null && _configure$timeout !== void 0 ? _configure$timeout : 30000,
|
|
59
|
+
codes: {
|
|
60
|
+
success: (_configure$codes$succ = (_configure$codes = configure.codes) === null || _configure$codes === void 0 ? void 0 : _configure$codes.success) !== null && _configure$codes$succ !== void 0 ? _configure$codes$succ : [].concat(DEFAULT_SUCCESS_CODES),
|
|
61
|
+
logout: (_configure$codes$logo = (_configure$codes2 = configure.codes) === null || _configure$codes2 === void 0 ? void 0 : _configure$codes2.logout) !== null && _configure$codes$logo !== void 0 ? _configure$codes$logo : [].concat(DEFAULT_LOGOUT_CODES),
|
|
62
|
+
ignoreError: (_configure$codes$igno = (_configure$codes3 = configure.codes) === null || _configure$codes3 === void 0 ? void 0 : _configure$codes3.ignoreError) !== null && _configure$codes$igno !== void 0 ? _configure$codes$igno : []
|
|
63
|
+
},
|
|
64
|
+
onError: configure.onError,
|
|
65
|
+
onLogout: configure.onLogout,
|
|
66
|
+
onSuccess: configure.onSuccess,
|
|
67
|
+
requestInterceptor: configure.requestInterceptor,
|
|
68
|
+
responseInterceptor: configure.responseInterceptor
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 更新客户端配置
|
|
74
|
+
*/
|
|
75
|
+
_createClass(HttpClient, [{
|
|
76
|
+
key: "config",
|
|
77
|
+
value: function config(configure) {
|
|
78
|
+
if (!isObject(configure)) {
|
|
79
|
+
throw new TypeError("Config must be an object");
|
|
80
|
+
}
|
|
81
|
+
this.configure = _objectSpread(_objectSpread({}, this.configure), configure);
|
|
82
|
+
}
|
|
83
|
+
}, {
|
|
84
|
+
key: "get",
|
|
85
|
+
value: function get(url, options) {
|
|
86
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
87
|
+
method: "GET"
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
}, {
|
|
91
|
+
key: "post",
|
|
92
|
+
value: function post(url, options) {
|
|
93
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
94
|
+
method: "POST"
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
}, {
|
|
98
|
+
key: "put",
|
|
99
|
+
value: function put(url, options) {
|
|
100
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
101
|
+
method: "PUT"
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
}, {
|
|
105
|
+
key: "delete",
|
|
106
|
+
value: function _delete(url, options) {
|
|
107
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
108
|
+
method: "DELETE"
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
111
|
+
}, {
|
|
112
|
+
key: "patch",
|
|
113
|
+
value: function patch(url, options) {
|
|
114
|
+
return this.request(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
115
|
+
method: "PATCH"
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
}, {
|
|
119
|
+
key: "request",
|
|
120
|
+
value: function () {
|
|
121
|
+
var _request = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url) {
|
|
122
|
+
var options,
|
|
123
|
+
json,
|
|
124
|
+
params,
|
|
125
|
+
_options$ignoreError,
|
|
126
|
+
ignoreError,
|
|
127
|
+
_options$returnData,
|
|
128
|
+
returnData,
|
|
129
|
+
_options$timeout,
|
|
130
|
+
timeout,
|
|
131
|
+
onError,
|
|
132
|
+
onLogout,
|
|
133
|
+
onSuccess,
|
|
134
|
+
restOptions,
|
|
135
|
+
fetchOptions,
|
|
136
|
+
_options$method,
|
|
137
|
+
fullUrl,
|
|
138
|
+
method,
|
|
139
|
+
_response,
|
|
140
|
+
result,
|
|
141
|
+
_args = arguments;
|
|
142
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
143
|
+
while (1) switch (_context.prev = _context.next) {
|
|
144
|
+
case 0:
|
|
145
|
+
options = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
|
|
146
|
+
json = options.json, params = options.params, _options$ignoreError = options.ignoreError, ignoreError = _options$ignoreError === void 0 ? false : _options$ignoreError, _options$returnData = options.returnData, returnData = _options$returnData === void 0 ? this.configure.returnData : _options$returnData, _options$timeout = options.timeout, timeout = _options$timeout === void 0 ? this.configure.timeout : _options$timeout, onError = options.onError, onLogout = options.onLogout, onSuccess = options.onSuccess, restOptions = _objectWithoutProperties(options, _excluded);
|
|
147
|
+
fetchOptions = _objectSpread({}, restOptions);
|
|
148
|
+
_context.prev = 3;
|
|
149
|
+
// 构建完整 URL
|
|
150
|
+
fullUrl = this.buildUrl(url); // 处理查询参数
|
|
151
|
+
if (params) {
|
|
152
|
+
fullUrl = this.appendParams(fullUrl, params);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 设置请求方法
|
|
156
|
+
method = ((_options$method = options.method) !== null && _options$method !== void 0 ? _options$method : this.configure.defaultMethod).toUpperCase();
|
|
157
|
+
fetchOptions.method = method;
|
|
158
|
+
|
|
159
|
+
// 设置请求头
|
|
160
|
+
_context.next = 10;
|
|
161
|
+
return this.buildHeaders(options.headers);
|
|
162
|
+
case 10:
|
|
163
|
+
fetchOptions.headers = _context.sent;
|
|
164
|
+
// 处理请求体
|
|
165
|
+
if (json) {
|
|
166
|
+
if (METHODS_WITHOUT_BODY.includes(method)) {
|
|
167
|
+
// GET/HEAD 请求将 json 转为查询参数
|
|
168
|
+
if (isObject(json) && !(json instanceof FormData)) {
|
|
169
|
+
fullUrl = this.appendParams(fullUrl, json);
|
|
170
|
+
}
|
|
171
|
+
} else {
|
|
172
|
+
// 其他方法处理 body
|
|
173
|
+
if (json instanceof FormData) {
|
|
174
|
+
fetchOptions.headers.delete("Content-Type");
|
|
175
|
+
fetchOptions.body = json;
|
|
176
|
+
} else {
|
|
177
|
+
fetchOptions.body = JSON.stringify(json);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// 执行请求拦截器
|
|
183
|
+
if (!this.configure.requestInterceptor) {
|
|
184
|
+
_context.next = 15;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
_context.next = 15;
|
|
188
|
+
return this.configure.requestInterceptor(fullUrl, fetchOptions);
|
|
189
|
+
case 15:
|
|
190
|
+
_context.next = 17;
|
|
191
|
+
return this.fetchWithTimeout(fullUrl, fetchOptions, timeout);
|
|
192
|
+
case 17:
|
|
193
|
+
_response = _context.sent;
|
|
194
|
+
if (_response.ok) {
|
|
195
|
+
_context.next = 20;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
throw new HttpError(_response.statusText || "Request failed", undefined, _response, _response.status);
|
|
199
|
+
case 20:
|
|
200
|
+
_context.next = 22;
|
|
201
|
+
return this.handleResponse(_response, returnData, ignoreError, onSuccess);
|
|
202
|
+
case 22:
|
|
203
|
+
result = _context.sent;
|
|
204
|
+
if (!(this.configure.responseInterceptor && isObject(result))) {
|
|
205
|
+
_context.next = 27;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
_context.next = 26;
|
|
209
|
+
return this.configure.responseInterceptor(result);
|
|
210
|
+
case 26:
|
|
211
|
+
return _context.abrupt("return", _context.sent);
|
|
212
|
+
case 27:
|
|
213
|
+
return _context.abrupt("return", result);
|
|
214
|
+
case 30:
|
|
215
|
+
_context.prev = 30;
|
|
216
|
+
_context.t0 = _context["catch"](3);
|
|
217
|
+
return _context.abrupt("return", this.handleError(_context.t0, ignoreError, onError, onLogout));
|
|
218
|
+
case 33:
|
|
219
|
+
case "end":
|
|
220
|
+
return _context.stop();
|
|
221
|
+
}
|
|
222
|
+
}, _callee, this, [[3, 30]]);
|
|
223
|
+
}));
|
|
224
|
+
function request(_x) {
|
|
225
|
+
return _request.apply(this, arguments);
|
|
226
|
+
}
|
|
227
|
+
return request;
|
|
228
|
+
}()
|
|
229
|
+
/**
|
|
230
|
+
* 构建完整 URL
|
|
231
|
+
*/
|
|
232
|
+
}, {
|
|
233
|
+
key: "buildUrl",
|
|
234
|
+
value: function buildUrl(url) {
|
|
235
|
+
if (typeof url !== "string") {
|
|
236
|
+
throw new TypeError("URL must be a string");
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// 如果是完整 URL,直接返回
|
|
240
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
241
|
+
return url;
|
|
242
|
+
}
|
|
243
|
+
var baseUrl = this.configure.baseUrl;
|
|
244
|
+
if (!baseUrl) return url;
|
|
245
|
+
var normalizedBase = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
246
|
+
var normalizedUrl = url.startsWith("/") ? url : "/".concat(url);
|
|
247
|
+
return normalizedBase + normalizedUrl;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* 添加查询参数
|
|
252
|
+
*/
|
|
253
|
+
}, {
|
|
254
|
+
key: "appendParams",
|
|
255
|
+
value: function appendParams(url, params) {
|
|
256
|
+
if (!isObject(params)) {
|
|
257
|
+
throw new TypeError("Params must be an object");
|
|
258
|
+
}
|
|
259
|
+
var searchParams = new URLSearchParams();
|
|
260
|
+
Object.entries(params).forEach(function (_ref) {
|
|
261
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
262
|
+
key = _ref2[0],
|
|
263
|
+
value = _ref2[1];
|
|
264
|
+
if (value !== undefined && value !== null) {
|
|
265
|
+
searchParams.append(key, String(value));
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
var queryString = searchParams.toString();
|
|
269
|
+
if (!queryString) return url;
|
|
270
|
+
var separator = url.includes("?") ? "&" : "?";
|
|
271
|
+
return "".concat(url).concat(separator).concat(queryString);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 构建请求头
|
|
276
|
+
*/
|
|
277
|
+
}, {
|
|
278
|
+
key: "buildHeaders",
|
|
279
|
+
value: (function () {
|
|
280
|
+
var _buildHeaders = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(customHeaders) {
|
|
281
|
+
var baseHeaders, configureHeaders, headers, custom;
|
|
282
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
283
|
+
while (1) switch (_context2.prev = _context2.next) {
|
|
284
|
+
case 0:
|
|
285
|
+
configureHeaders = this.configure.headers; // 如果是函数,执行获取
|
|
286
|
+
if (!(configureHeaders && isFunction(configureHeaders))) {
|
|
287
|
+
_context2.next = 7;
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
_context2.next = 4;
|
|
291
|
+
return configureHeaders();
|
|
292
|
+
case 4:
|
|
293
|
+
baseHeaders = _context2.sent;
|
|
294
|
+
_context2.next = 8;
|
|
295
|
+
break;
|
|
296
|
+
case 7:
|
|
297
|
+
if (configureHeaders) {
|
|
298
|
+
baseHeaders = configureHeaders;
|
|
299
|
+
}
|
|
300
|
+
case 8:
|
|
301
|
+
headers = new Headers(baseHeaders); // 合并自定义请求头
|
|
302
|
+
if (customHeaders) {
|
|
303
|
+
custom = new Headers(customHeaders);
|
|
304
|
+
custom.forEach(function (value, key) {
|
|
305
|
+
headers.set(key, value);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// 设置默认 Content-Type
|
|
310
|
+
if (!headers.has("Content-Type")) {
|
|
311
|
+
headers.set("Content-Type", DEFAULT_CONTENT_TYPE);
|
|
312
|
+
}
|
|
313
|
+
return _context2.abrupt("return", headers);
|
|
314
|
+
case 12:
|
|
315
|
+
case "end":
|
|
316
|
+
return _context2.stop();
|
|
317
|
+
}
|
|
318
|
+
}, _callee2, this);
|
|
319
|
+
}));
|
|
320
|
+
function buildHeaders(_x2) {
|
|
321
|
+
return _buildHeaders.apply(this, arguments);
|
|
322
|
+
}
|
|
323
|
+
return buildHeaders;
|
|
324
|
+
}()
|
|
325
|
+
/**
|
|
326
|
+
* 带超时的 fetch 请求
|
|
327
|
+
*/
|
|
328
|
+
)
|
|
329
|
+
}, {
|
|
330
|
+
key: "fetchWithTimeout",
|
|
331
|
+
value: (function () {
|
|
332
|
+
var _fetchWithTimeout = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(url, options, timeout) {
|
|
333
|
+
var controller, timeoutId, _response2;
|
|
334
|
+
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
|
335
|
+
while (1) switch (_context3.prev = _context3.next) {
|
|
336
|
+
case 0:
|
|
337
|
+
controller = new AbortController();
|
|
338
|
+
timeoutId = setTimeout(function () {
|
|
339
|
+
return controller.abort();
|
|
340
|
+
}, timeout);
|
|
341
|
+
_context3.prev = 2;
|
|
342
|
+
_context3.next = 5;
|
|
343
|
+
return fetch(url, _objectSpread(_objectSpread({}, options), {}, {
|
|
344
|
+
signal: controller.signal
|
|
345
|
+
}));
|
|
346
|
+
case 5:
|
|
347
|
+
_response2 = _context3.sent;
|
|
348
|
+
return _context3.abrupt("return", _response2);
|
|
349
|
+
case 9:
|
|
350
|
+
_context3.prev = 9;
|
|
351
|
+
_context3.t0 = _context3["catch"](2);
|
|
352
|
+
if (!(_context3.t0.name === "AbortError")) {
|
|
353
|
+
_context3.next = 13;
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
throw new HttpError("Request timeout after ".concat(timeout, "ms"), undefined, undefined, 408);
|
|
357
|
+
case 13:
|
|
358
|
+
throw _context3.t0;
|
|
359
|
+
case 14:
|
|
360
|
+
_context3.prev = 14;
|
|
361
|
+
clearTimeout(timeoutId);
|
|
362
|
+
return _context3.finish(14);
|
|
363
|
+
case 17:
|
|
364
|
+
case "end":
|
|
365
|
+
return _context3.stop();
|
|
366
|
+
}
|
|
367
|
+
}, _callee3, null, [[2, 9, 14, 17]]);
|
|
368
|
+
}));
|
|
369
|
+
function fetchWithTimeout(_x3, _x4, _x5) {
|
|
370
|
+
return _fetchWithTimeout.apply(this, arguments);
|
|
371
|
+
}
|
|
372
|
+
return fetchWithTimeout;
|
|
373
|
+
}()
|
|
374
|
+
/**
|
|
375
|
+
* 处理响应数据
|
|
376
|
+
*/
|
|
377
|
+
)
|
|
378
|
+
}, {
|
|
379
|
+
key: "handleResponse",
|
|
380
|
+
value: (function () {
|
|
381
|
+
var _handleResponse = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(response, returnData, ignoreError, onSuccess) {
|
|
382
|
+
var _response$headers$get, _response$headers$get2, _codes$success, _codes$logout, _codes$ignoreError;
|
|
383
|
+
var contentType, _this$configure, codeKey, dataKey, messageKey, codes, successCodes, logoutCodes, ignoreErrorCodes, blob, filename, data, code, message;
|
|
384
|
+
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
|
385
|
+
while (1) switch (_context4.prev = _context4.next) {
|
|
386
|
+
case 0:
|
|
387
|
+
contentType = (_response$headers$get = (_response$headers$get2 = response.headers.get("content-type")) === null || _response$headers$get2 === void 0 ? void 0 : _response$headers$get2.toLowerCase()) !== null && _response$headers$get !== void 0 ? _response$headers$get : "";
|
|
388
|
+
_this$configure = this.configure, codeKey = _this$configure.codeKey, dataKey = _this$configure.dataKey, messageKey = _this$configure.messageKey, codes = _this$configure.codes;
|
|
389
|
+
successCodes = (_codes$success = codes.success) !== null && _codes$success !== void 0 ? _codes$success : DEFAULT_SUCCESS_CODES;
|
|
390
|
+
logoutCodes = (_codes$logout = codes.logout) !== null && _codes$logout !== void 0 ? _codes$logout : DEFAULT_LOGOUT_CODES;
|
|
391
|
+
ignoreErrorCodes = (_codes$ignoreError = codes.ignoreError) !== null && _codes$ignoreError !== void 0 ? _codes$ignoreError : []; // 处理文件下载(Blob)
|
|
392
|
+
if (!this.configure.blobContentTypes.some(function (type) {
|
|
393
|
+
return contentType.includes(type);
|
|
394
|
+
})) {
|
|
395
|
+
_context4.next = 11;
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
_context4.next = 8;
|
|
399
|
+
return response.blob();
|
|
400
|
+
case 8:
|
|
401
|
+
blob = _context4.sent;
|
|
402
|
+
filename = this.extractFilename(response.headers);
|
|
403
|
+
return _context4.abrupt("return", _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, codeKey, successCodes[0]), "data", blob), "filename", filename), "response", response));
|
|
404
|
+
case 11:
|
|
405
|
+
_context4.next = 13;
|
|
406
|
+
return response.json();
|
|
407
|
+
case 13:
|
|
408
|
+
data = _context4.sent;
|
|
409
|
+
if (data.hasOwnProperty(codeKey)) {
|
|
410
|
+
_context4.next = 16;
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
return _context4.abrupt("return", data);
|
|
414
|
+
case 16:
|
|
415
|
+
code = data[codeKey];
|
|
416
|
+
message = data[messageKey]; // 成功响应
|
|
417
|
+
if (!successCodes.includes(code)) {
|
|
418
|
+
_context4.next = 23;
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
if (!(returnData && data.hasOwnProperty(dataKey))) {
|
|
422
|
+
_context4.next = 21;
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
return _context4.abrupt("return", data[dataKey]);
|
|
426
|
+
case 21:
|
|
427
|
+
onSuccess === null || onSuccess === void 0 || onSuccess(data);
|
|
428
|
+
return _context4.abrupt("return", data);
|
|
429
|
+
case 23:
|
|
430
|
+
if (!(ignoreError || ignoreErrorCodes.includes(code))) {
|
|
431
|
+
_context4.next = 25;
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
return _context4.abrupt("return", data);
|
|
435
|
+
case 25:
|
|
436
|
+
if (!logoutCodes.includes(code)) {
|
|
437
|
+
_context4.next = 27;
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
throw new HttpError(message || "Unauthorized", code, data);
|
|
441
|
+
case 27:
|
|
442
|
+
throw new HttpError(message || "Request failed", code, data);
|
|
443
|
+
case 28:
|
|
444
|
+
case "end":
|
|
445
|
+
return _context4.stop();
|
|
446
|
+
}
|
|
447
|
+
}, _callee4, this);
|
|
448
|
+
}));
|
|
449
|
+
function handleResponse(_x6, _x7, _x8, _x9) {
|
|
450
|
+
return _handleResponse.apply(this, arguments);
|
|
451
|
+
}
|
|
452
|
+
return handleResponse;
|
|
453
|
+
}()
|
|
454
|
+
/**
|
|
455
|
+
* 从响应头提取文件名
|
|
456
|
+
*/
|
|
457
|
+
)
|
|
458
|
+
}, {
|
|
459
|
+
key: "extractFilename",
|
|
460
|
+
value: function extractFilename(headers) {
|
|
461
|
+
var disposition = headers.get("content-disposition");
|
|
462
|
+
if (!disposition) return undefined;
|
|
463
|
+
var matches = disposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
|
|
464
|
+
if (!(matches !== null && matches !== void 0 && matches[1])) return undefined;
|
|
465
|
+
var filename = matches[1].replace(/['"]/g, "");
|
|
466
|
+
try {
|
|
467
|
+
filename = decodeURIComponent(filename);
|
|
468
|
+
} catch (_unused) {
|
|
469
|
+
// 解码失败时使用原始文件名
|
|
470
|
+
}
|
|
471
|
+
return filename;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* 统一错误处理
|
|
476
|
+
*/
|
|
477
|
+
}, {
|
|
478
|
+
key: "handleError",
|
|
479
|
+
value: function handleError(error, ignoreError, customOnError, customOnLogout) {
|
|
480
|
+
var httpError = error instanceof HttpError ? error : new HttpError(error.message || "Unknown error", undefined, error);
|
|
481
|
+
|
|
482
|
+
// 不忽略错误时执行回调
|
|
483
|
+
if (!ignoreError) {
|
|
484
|
+
var _this$configure$codes, _this$configure$codes2;
|
|
485
|
+
// 登出错误
|
|
486
|
+
var logoutCodes = (_this$configure$codes = (_this$configure$codes2 = this.configure.codes) === null || _this$configure$codes2 === void 0 ? void 0 : _this$configure$codes2.logout) !== null && _this$configure$codes !== void 0 ? _this$configure$codes : DEFAULT_LOGOUT_CODES;
|
|
487
|
+
if (httpError.code && logoutCodes.includes(httpError.code)) {
|
|
488
|
+
var logoutHandler = customOnLogout !== null && customOnLogout !== void 0 ? customOnLogout : this.configure.onLogout;
|
|
489
|
+
logoutHandler === null || logoutHandler === void 0 || logoutHandler(httpError);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// 通用错误处理
|
|
493
|
+
var errorHandler = customOnError !== null && customOnError !== void 0 ? customOnError : this.configure.onError;
|
|
494
|
+
errorHandler === null || errorHandler === void 0 || errorHandler(httpError);
|
|
495
|
+
}
|
|
496
|
+
throw httpError;
|
|
497
|
+
}
|
|
498
|
+
}]);
|
|
499
|
+
return HttpClient;
|
|
500
|
+
}();
|
|
501
|
+
|
|
502
|
+
// ==================== 工具函数 ====================
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* 下载文件(用于 Blob 响应)
|
|
506
|
+
* @example
|
|
507
|
+
* const response = await http.get('/api/download');
|
|
508
|
+
* downloadfile(response);
|
|
509
|
+
*/
|
|
510
|
+
export function downloadfile(response) {
|
|
511
|
+
var data = response.data,
|
|
512
|
+
filename = response.filename;
|
|
513
|
+
if (!(data instanceof Blob)) {
|
|
514
|
+
throw new TypeError("Data must be a Blob");
|
|
515
|
+
}
|
|
516
|
+
var url = URL.createObjectURL(data);
|
|
517
|
+
var link = document.createElement("a");
|
|
518
|
+
link.href = url;
|
|
519
|
+
link.download = filename || "download-".concat(Date.now());
|
|
520
|
+
link.style.display = "none";
|
|
521
|
+
document.body.appendChild(link);
|
|
522
|
+
link.click();
|
|
523
|
+
|
|
524
|
+
// 清理
|
|
525
|
+
setTimeout(function () {
|
|
526
|
+
document.body.removeChild(link);
|
|
527
|
+
URL.revokeObjectURL(url);
|
|
528
|
+
}, 100);
|
|
529
|
+
}
|
|
530
|
+
export function createHttpClient(config) {
|
|
531
|
+
return new HttpClient(config);
|
|
532
|
+
}
|
|
533
|
+
export var rq = new HttpClient();
|
|
534
|
+
export default HttpClient;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { HttpClientConfig, HttpMethod } from "../fetch";
|
|
2
|
+
import type { Result } from "ahooks/lib/useRequest/src/types";
|
|
3
|
+
import type { Options } from "ahooks/lib/useRequest/src/types";
|
|
4
|
+
export interface UseFetchOption<TData = any> extends Options<TData, any[]> {
|
|
5
|
+
ignoreError?: boolean;
|
|
6
|
+
returnData?: boolean;
|
|
7
|
+
json?: unknown;
|
|
8
|
+
data?: unknown;
|
|
9
|
+
method?: HttpMethod;
|
|
10
|
+
headers?: HeadersInit;
|
|
11
|
+
onLogout?: (error: any) => any;
|
|
12
|
+
}
|
|
13
|
+
declare const useFetch: {
|
|
14
|
+
<TData = any>(url: string, options?: UseFetchOption<TData>): Result<TData, any[]>;
|
|
15
|
+
config(options?: HttpClientConfig): void;
|
|
16
|
+
};
|
|
17
|
+
export default useFetch;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
3
|
+
var _excluded = ["ignoreError", "returnData", "method", "json", "data", "headers", "onLogout"];
|
|
4
|
+
import { useRequest } from "ahooks";
|
|
5
|
+
import { rq } from "../fetch";
|
|
6
|
+
import { isObject } from "../utils/util";
|
|
7
|
+
var useFetch = function useFetch(url, options) {
|
|
8
|
+
var _ref = options || {},
|
|
9
|
+
ignoreError = _ref.ignoreError,
|
|
10
|
+
returnData = _ref.returnData,
|
|
11
|
+
method = _ref.method,
|
|
12
|
+
json = _ref.json,
|
|
13
|
+
data = _ref.data,
|
|
14
|
+
headers = _ref.headers,
|
|
15
|
+
onLogout = _ref.onLogout,
|
|
16
|
+
others = _objectWithoutProperties(_ref, _excluded);
|
|
17
|
+
var body = json !== null && json !== void 0 ? json : data;
|
|
18
|
+
var fetcher = function fetcher(fetcherData, fetcherOptions) {
|
|
19
|
+
var _fetcherData, _fetcherOptions;
|
|
20
|
+
if (isObject(fetcherData) && Object.prototype.hasOwnProperty.call(fetcherData, "nativeEvent")) {
|
|
21
|
+
fetcherData = undefined;
|
|
22
|
+
}
|
|
23
|
+
body = (_fetcherData = fetcherData) !== null && _fetcherData !== void 0 ? _fetcherData : body;
|
|
24
|
+
fetcherOptions = (_fetcherOptions = fetcherOptions) !== null && _fetcherOptions !== void 0 ? _fetcherOptions : {};
|
|
25
|
+
fetcherOptions = _objectSpread({
|
|
26
|
+
json: body,
|
|
27
|
+
returnData: returnData,
|
|
28
|
+
method: method,
|
|
29
|
+
ignoreError: ignoreError,
|
|
30
|
+
headers: headers,
|
|
31
|
+
onLogout: onLogout
|
|
32
|
+
}, fetcherOptions);
|
|
33
|
+
return rq.request(url, fetcherOptions);
|
|
34
|
+
};
|
|
35
|
+
return useRequest(fetcher, others);
|
|
36
|
+
};
|
|
37
|
+
useFetch.config = function () {
|
|
38
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
39
|
+
rq.config(options);
|
|
40
|
+
};
|
|
41
|
+
export default useFetch;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { HttpMethod } from "../fetch";
|
|
2
|
+
import type { UseMutationResult, UseMutationOptions } from "@tanstack/react-query";
|
|
3
|
+
interface Options<TData, TError = unknown, TVariables = void, TOnMutateResult = unknown> extends UseMutationOptions<TData, TError, TVariables, TOnMutateResult> {
|
|
4
|
+
url: string;
|
|
5
|
+
method?: HttpMethod;
|
|
6
|
+
json?: unknown;
|
|
7
|
+
headers?: Record<string, string>;
|
|
8
|
+
}
|
|
9
|
+
type Result<TData, TError = unknown, TVariables = any, TOnMutateResult = unknown> = UseMutationResult<TData, TError, TVariables, TOnMutateResult> & {
|
|
10
|
+
mutate: (...args: any[]) => void;
|
|
11
|
+
mutateAsync: (...args: any[]) => void;
|
|
12
|
+
};
|
|
13
|
+
export default function useMutationHooks<TData = unknown, TError = unknown, TVariables = void, TOnMutateResult = unknown>(options: Options<TData, TError, TVariables, TOnMutateResult>): Result<TData, TError, TVariables, TOnMutateResult>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
3
|
+
var _excluded = ["url", "json", "method", "headers"];
|
|
4
|
+
import { useMutation } from "@tanstack/react-query";
|
|
5
|
+
import { rq } from "../fetch";
|
|
6
|
+
import { isObject } from "../utils/util";
|
|
7
|
+
export default function useMutationHooks(options) {
|
|
8
|
+
var url = options.url,
|
|
9
|
+
json = options.json,
|
|
10
|
+
method = options.method,
|
|
11
|
+
headers = options.headers,
|
|
12
|
+
others = _objectWithoutProperties(options, _excluded);
|
|
13
|
+
return useMutation(_objectSpread({
|
|
14
|
+
mutationFn: function mutationFn(data) {
|
|
15
|
+
var _data;
|
|
16
|
+
if (isObject(data) && Object.prototype.hasOwnProperty.call(data, "nativeEvent")) {
|
|
17
|
+
data = undefined;
|
|
18
|
+
}
|
|
19
|
+
return rq.request(url, {
|
|
20
|
+
method: method,
|
|
21
|
+
json: (_data = data) !== null && _data !== void 0 ? _data : json,
|
|
22
|
+
headers: headers
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}, others));
|
|
26
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { HttpMethod } from "../fetch";
|
|
2
|
+
import type { UseQueryResult, UseQueryOptions } from "@tanstack/react-query";
|
|
3
|
+
type UseQueryOptionsPartial<TData> = Partial<Pick<UseQueryOptions<TData>, "queryFn">> & Omit<UseQueryOptions<TData>, "queryFn">;
|
|
4
|
+
interface Options<TData> extends UseQueryOptionsPartial<TData> {
|
|
5
|
+
method?: HttpMethod;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
ignoreError?: boolean;
|
|
8
|
+
returnData?: boolean;
|
|
9
|
+
useSignal?: boolean;
|
|
10
|
+
onBefore?: () => void;
|
|
11
|
+
onLogout?: () => void;
|
|
12
|
+
}
|
|
13
|
+
declare function useQueryHooks<TData = unknown>(options: Options<TData>): UseQueryResult<TData>;
|
|
14
|
+
export default useQueryHooks;
|