utils-lib-js 1.0.3 → 1.0.6
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/dist/common/array.d.ts +1 -1
- package/dist/common/array.js +14 -25
- package/dist/common/base.d.ts +2 -2
- package/dist/common/base.js +49 -43
- package/dist/common/element.d.ts +1 -1
- package/dist/common/element.js +13 -21
- package/dist/common/event.d.ts +1 -1
- package/dist/common/event.js +46 -43
- package/dist/common/function.d.ts +1 -1
- package/dist/common/function.js +60 -66
- package/dist/common/index.d.ts +10 -11
- package/dist/common/index.js +26 -30
- package/dist/common/object.d.ts +3 -1
- package/dist/common/object.js +175 -167
- package/dist/common/request.d.ts +65 -11
- package/dist/common/request.js +225 -26
- package/dist/common/static.js +17 -18
- package/dist/common/storage.js +36 -34
- package/dist/common/types.d.ts +57 -8
- package/dist/common/types.js +2 -3
- package/dist/esm/array.d.ts +1 -1
- package/dist/esm/array.js +8 -18
- package/dist/esm/base.d.ts +2 -2
- package/dist/esm/base.js +41 -35
- package/dist/esm/element.d.ts +1 -1
- package/dist/esm/element.js +9 -16
- package/dist/esm/event.d.ts +1 -1
- package/dist/esm/event.js +38 -34
- package/dist/esm/function.d.ts +1 -1
- package/dist/esm/function.js +53 -58
- package/dist/esm/index.d.ts +10 -11
- package/dist/esm/index.js +10 -11
- package/dist/esm/object.d.ts +3 -1
- package/dist/esm/object.js +159 -152
- package/dist/esm/request.d.ts +56 -9
- package/dist/esm/request.js +199 -18
- package/dist/esm/static.js +14 -14
- package/dist/esm/storage.js +30 -27
- package/dist/esm/types.d.ts +47 -6
- package/dist/esm/types.js +1 -1
- package/package.json +12 -5
- package/tsconfig.es.json +3 -7
package/dist/common/request.js
CHANGED
|
@@ -1,26 +1,225 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
var
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
29
|
+
var t = {};
|
|
30
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
31
|
+
t[p] = s[p];
|
|
32
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
33
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
34
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
35
|
+
t[p[i]] = s[p[i]];
|
|
36
|
+
}
|
|
37
|
+
return t;
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
exports.Request = void 0;
|
|
41
|
+
var index_js_1 = require("./index.js");
|
|
42
|
+
var node_http_1 = require("node:http");
|
|
43
|
+
var node_url_1 = require("node:url");
|
|
44
|
+
var Interceptors = (function () {
|
|
45
|
+
function Interceptors() {
|
|
46
|
+
}
|
|
47
|
+
Interceptors.prototype.use = function (type, fn) {
|
|
48
|
+
switch (type) {
|
|
49
|
+
case "request":
|
|
50
|
+
this.requestSuccess = fn;
|
|
51
|
+
break;
|
|
52
|
+
case "response":
|
|
53
|
+
this.responseSuccess = fn;
|
|
54
|
+
break;
|
|
55
|
+
case "error":
|
|
56
|
+
this.error = fn;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
return this;
|
|
60
|
+
};
|
|
61
|
+
Object.defineProperty(Interceptors.prototype, "reqFn", {
|
|
62
|
+
get: function () {
|
|
63
|
+
return this.requestSuccess;
|
|
64
|
+
},
|
|
65
|
+
enumerable: false,
|
|
66
|
+
configurable: true
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(Interceptors.prototype, "resFn", {
|
|
69
|
+
get: function () {
|
|
70
|
+
return this.responseSuccess;
|
|
71
|
+
},
|
|
72
|
+
enumerable: false,
|
|
73
|
+
configurable: true
|
|
74
|
+
});
|
|
75
|
+
Object.defineProperty(Interceptors.prototype, "errFn", {
|
|
76
|
+
get: function () {
|
|
77
|
+
return this.error;
|
|
78
|
+
},
|
|
79
|
+
enumerable: false,
|
|
80
|
+
configurable: true
|
|
81
|
+
});
|
|
82
|
+
return Interceptors;
|
|
83
|
+
}());
|
|
84
|
+
var RequestBase = (function (_super) {
|
|
85
|
+
__extends(RequestBase, _super);
|
|
86
|
+
function RequestBase(origin) {
|
|
87
|
+
var _this = _super.call(this) || this;
|
|
88
|
+
_this.chackUrl = function (url) {
|
|
89
|
+
return url.startsWith('/');
|
|
90
|
+
};
|
|
91
|
+
_this.fixOrigin = function (fixStr) {
|
|
92
|
+
if (_this.chackUrl(fixStr))
|
|
93
|
+
return _this.origin + fixStr;
|
|
94
|
+
return fixStr;
|
|
95
|
+
};
|
|
96
|
+
_this.envDesc = function () {
|
|
97
|
+
if (typeof Window !== "undefined") {
|
|
98
|
+
return "Window";
|
|
99
|
+
}
|
|
100
|
+
return "Node";
|
|
101
|
+
};
|
|
102
|
+
_this.errorFn = function (reject) { return function (err) { var _a, _b; return reject((_b = (_a = _this.errFn) === null || _a === void 0 ? void 0 : _a.call(_this, err)) !== null && _b !== void 0 ? _b : err); }; };
|
|
103
|
+
_this.clearTimer = function (opts) { return !!opts.timer && (clearTimeout(opts.timer), opts.timer = null); };
|
|
104
|
+
_this.initAbort = function (params) {
|
|
105
|
+
var controller = params.controller, timer = params.timer, timeout = params.timeout;
|
|
106
|
+
!!!timer && (params.timer = setTimeout(function () { return controller.abort(); }, timeout));
|
|
107
|
+
return params;
|
|
108
|
+
};
|
|
109
|
+
_this.requestType = function () {
|
|
110
|
+
switch (_this.envDesc()) {
|
|
111
|
+
case "Window":
|
|
112
|
+
return _this.fetch;
|
|
113
|
+
case "Node":
|
|
114
|
+
return _this.http;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
_this.getDataByType = function (type, response) {
|
|
118
|
+
switch (type) {
|
|
119
|
+
case "text":
|
|
120
|
+
case "json":
|
|
121
|
+
case "blob":
|
|
122
|
+
case "formData":
|
|
123
|
+
case "arrayBuffer":
|
|
124
|
+
return response[type]();
|
|
125
|
+
default:
|
|
126
|
+
return response['json']();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
_this.origin = origin !== null && origin !== void 0 ? origin : '';
|
|
130
|
+
return _this;
|
|
131
|
+
}
|
|
132
|
+
return RequestBase;
|
|
133
|
+
}(Interceptors));
|
|
134
|
+
var RequestInit = (function (_super) {
|
|
135
|
+
__extends(RequestInit, _super);
|
|
136
|
+
function RequestInit(origin) {
|
|
137
|
+
var _this = _super.call(this, origin) || this;
|
|
138
|
+
_this.initDefaultParams = function (url, _a) {
|
|
139
|
+
var _b = _a.method, method = _b === void 0 ? "GET" : _b, _c = _a.query, query = _c === void 0 ? {} : _c, _d = _a.headers, headers = _d === void 0 ? {} : _d, _e = _a.body, body = _e === void 0 ? null : _e, _f = _a.timeout, timeout = _f === void 0 ? 30 * 1000 : _f, _g = _a.controller, controller = _g === void 0 ? new AbortController() : _g, _h = _a.type, type = _h === void 0 ? "json" : _h, others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
|
|
140
|
+
return (__assign({ url: (0, index_js_1.urlJoin)(_this.fixOrigin(url), query), method: method, headers: headers, body: method === "GET" ? null : (0, index_js_1.jsonToString)(body), timeout: timeout, signal: controller === null || controller === void 0 ? void 0 : controller.signal, controller: controller, type: type, timer: null }, others));
|
|
141
|
+
};
|
|
142
|
+
_this.initFetchParams = function (url, opts) {
|
|
143
|
+
var _a, _b;
|
|
144
|
+
var params = _this.initAbort(_this.initDefaultParams(url, opts));
|
|
145
|
+
return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, params)) !== null && _b !== void 0 ? _b : params;
|
|
146
|
+
};
|
|
147
|
+
_this.initHttpParams = function (url, opts) {
|
|
148
|
+
var _a, _b;
|
|
149
|
+
var params = _this.initAbort(_this.initDefaultParams(url, opts));
|
|
150
|
+
var options = (0, node_url_1.parse)(params.url, true);
|
|
151
|
+
return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : params;
|
|
152
|
+
};
|
|
153
|
+
return _this;
|
|
154
|
+
}
|
|
155
|
+
return RequestInit;
|
|
156
|
+
}(RequestBase));
|
|
157
|
+
var Request = (function (_super) {
|
|
158
|
+
__extends(Request, _super);
|
|
159
|
+
function Request(origin) {
|
|
160
|
+
var _this = _super.call(this, origin) || this;
|
|
161
|
+
_this.fetch = function (_url, _opts) {
|
|
162
|
+
var _a = (0, index_js_1.defer)(), promise = _a.promise, resolve = _a.resolve, reject = _a.reject;
|
|
163
|
+
var _b = _this.initFetchParams(_url, _opts), url = _b.url, opts = __rest(_b, ["url"]);
|
|
164
|
+
var signal = opts.signal;
|
|
165
|
+
signal.addEventListener('abort', function () { return _this.errorFn(reject); });
|
|
166
|
+
fetch(url, opts).then(function (response) {
|
|
167
|
+
if (response.status >= 200 && response.status < 300) {
|
|
168
|
+
return _this.getDataByType(opts.type, response);
|
|
169
|
+
}
|
|
170
|
+
return _this.errorFn(reject);
|
|
171
|
+
}).then(function (res) { var _a, _b; return resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, res)) !== null && _b !== void 0 ? _b : res); }).catch(_this.errorFn(reject)).finally(function () { return _this.clearTimer(opts); });
|
|
172
|
+
return promise;
|
|
173
|
+
};
|
|
174
|
+
_this.http = function (_url, _opts) {
|
|
175
|
+
var _a = (0, index_js_1.defer)(), promise = _a.promise, resolve = _a.resolve, reject = _a.reject;
|
|
176
|
+
var params = _this.initHttpParams(_url, _opts);
|
|
177
|
+
var signal = params.signal;
|
|
178
|
+
var req = (0, node_http_1.request)(params, function (response) {
|
|
179
|
+
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
180
|
+
var data_1 = "";
|
|
181
|
+
response.setEncoding('utf8');
|
|
182
|
+
response.on('data', function (chunk) { return data_1 += chunk; });
|
|
183
|
+
return response.on("end", function () { var _a, _b; return resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, data_1)) !== null && _b !== void 0 ? _b : data_1); });
|
|
184
|
+
}
|
|
185
|
+
return _this.errorFn(reject)(response === null || response === void 0 ? void 0 : response.statusMessage);
|
|
186
|
+
});
|
|
187
|
+
signal.addEventListener('abort', function () { return _this.errorFn(reject)(req.destroy(new Error('request timeout'))); });
|
|
188
|
+
req.on('error', _this.errorFn(reject));
|
|
189
|
+
req.end();
|
|
190
|
+
return promise;
|
|
191
|
+
};
|
|
192
|
+
_this.GET = function (url, query, _, opts) {
|
|
193
|
+
if (query === void 0) { query = {}; }
|
|
194
|
+
return _this.request(url, __assign({ query: query, method: "GET" }, opts));
|
|
195
|
+
};
|
|
196
|
+
_this.POST = function (url, query, body, opts) {
|
|
197
|
+
if (query === void 0) { query = {}; }
|
|
198
|
+
return _this.request(url, __assign({ query: query, method: "POST", body: body }, opts));
|
|
199
|
+
};
|
|
200
|
+
_this.PUT = function (url, query, body, opts) {
|
|
201
|
+
if (query === void 0) { query = {}; }
|
|
202
|
+
return _this.request(url, __assign({ query: query, method: "PUT", body: body }, opts));
|
|
203
|
+
};
|
|
204
|
+
_this.DELETE = function (url, query, body, opts) {
|
|
205
|
+
if (query === void 0) { query = {}; }
|
|
206
|
+
return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
|
|
207
|
+
};
|
|
208
|
+
_this.OPTIONS = function (url, query, body, opts) {
|
|
209
|
+
if (query === void 0) { query = {}; }
|
|
210
|
+
return _this.request(url, __assign({ query: query, method: "OPTIONS", body: body }, opts));
|
|
211
|
+
};
|
|
212
|
+
_this.HEAD = function (url, query, body, opts) {
|
|
213
|
+
if (query === void 0) { query = {}; }
|
|
214
|
+
return _this.request(url, __assign({ query: query, method: "HEAD", body: body }, opts));
|
|
215
|
+
};
|
|
216
|
+
_this.PATCH = function (url, query, body, opts) {
|
|
217
|
+
if (query === void 0) { query = {}; }
|
|
218
|
+
return _this.request(url, __assign({ query: query, method: "PATCH", body: body }, opts));
|
|
219
|
+
};
|
|
220
|
+
_this.request = _this.requestType();
|
|
221
|
+
return _this;
|
|
222
|
+
}
|
|
223
|
+
return Request;
|
|
224
|
+
}(RequestInit));
|
|
225
|
+
exports.Request = Request;
|
package/dist/common/static.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
types["[object
|
|
8
|
-
types["[object
|
|
9
|
-
types["[object
|
|
10
|
-
types["[object
|
|
11
|
-
types["[object
|
|
12
|
-
types["[object
|
|
13
|
-
types["[object
|
|
14
|
-
types["[object
|
|
15
|
-
types["[object
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.types = void 0;
|
|
4
|
+
var types;
|
|
5
|
+
(function (types) {
|
|
6
|
+
types["[object Array]"] = "array";
|
|
7
|
+
types["[object Object]"] = "object";
|
|
8
|
+
types["[object Function]"] = "function";
|
|
9
|
+
types["[object Set]"] = "set";
|
|
10
|
+
types["[object Map]"] = "map";
|
|
11
|
+
types["[object WeakMap]"] = "weakMap";
|
|
12
|
+
types["[object WeakSet]"] = "weakSet";
|
|
13
|
+
types["[object Date]"] = "date";
|
|
14
|
+
types["[object RegExp]"] = "regExp";
|
|
15
|
+
types["[object Math]"] = "math";
|
|
16
|
+
})(types = exports.types || (exports.types = {}));
|
|
17
|
+
;
|
package/dist/common/storage.js
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
console.error(error);
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
exports.setStorage = setStorage;
|
|
13
|
-
var getStorage = function (key) {
|
|
14
|
-
try {
|
|
15
|
-
var str = localStorage.getItem(key);
|
|
16
|
-
if (str === null || str === undefined) {
|
|
17
|
-
return null;
|
|
18
|
-
}
|
|
19
|
-
return JSON.parse(str);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clearStorage = exports.getStorage = exports.setStorage = void 0;
|
|
4
|
+
var setStorage = function (key, val) {
|
|
5
|
+
try {
|
|
6
|
+
localStorage.setItem(key, JSON.stringify(val));
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
console.error(error);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
exports.setStorage = setStorage;
|
|
13
|
+
var getStorage = function (key) {
|
|
14
|
+
try {
|
|
15
|
+
var str = localStorage.getItem(key);
|
|
16
|
+
if (str === null || str === undefined) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
return JSON.parse(str);
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
console.error(error);
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
exports.getStorage = getStorage;
|
|
27
|
+
var clearStorage = function (key) {
|
|
28
|
+
try {
|
|
29
|
+
key && localStorage.removeItem(key);
|
|
30
|
+
!key && localStorage.clear();
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
console.error(error);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
exports.clearStorage = clearStorage;
|
package/dist/common/types.d.ts
CHANGED
|
@@ -4,8 +4,8 @@ export interface IObject<T> {
|
|
|
4
4
|
}
|
|
5
5
|
export interface IPromise extends IObject<any> {
|
|
6
6
|
promise: Promise<void>;
|
|
7
|
-
resolve: () => unknown;
|
|
8
|
-
reject: () => unknown;
|
|
7
|
+
resolve: (res: any) => unknown;
|
|
8
|
+
reject: (err: any) => unknown;
|
|
9
9
|
}
|
|
10
10
|
export declare type IInstance<T> = {
|
|
11
11
|
_instance: Function;
|
|
@@ -15,7 +15,8 @@ export declare type IRandomNum = (min: number, max: number, bool?: boolean) => n
|
|
|
15
15
|
export declare type IUrlSplit = (url: string) => IObject<any>;
|
|
16
16
|
export declare type IUrlJoin = (url: string, query: object) => string;
|
|
17
17
|
export declare type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
|
|
18
|
-
export declare type
|
|
18
|
+
export declare type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
|
|
19
|
+
export declare type IGetValue = <T, U = IObject<T> | IObject<T>[IKey]>(object: U, key: string, defaultValue?: any) => U;
|
|
19
20
|
export declare type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>;
|
|
20
21
|
export declare type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U;
|
|
21
22
|
export declare type IEnumInversion = (target: IObject<string>) => IObject<string>;
|
|
@@ -25,6 +26,8 @@ export declare type ICreateObject = <T, U extends T>(source: T) => U;
|
|
|
25
26
|
export declare type IInherit = <T extends Function>(source: T, target?: Function) => Function;
|
|
26
27
|
export declare type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
|
|
27
28
|
export declare type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void;
|
|
29
|
+
export declare type IStringToJson = (target: string) => IObject<any>;
|
|
30
|
+
export declare type IJsonToString = (target: IObject<any>) => string;
|
|
28
31
|
export declare type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
|
|
29
32
|
export declare type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
|
|
30
33
|
export declare type IDefer = () => IPromise;
|
|
@@ -44,10 +47,56 @@ export declare type IStopBubble = (e: Event) => void;
|
|
|
44
47
|
export declare type IStopDefault = (e: Event) => void;
|
|
45
48
|
export declare type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
|
|
46
49
|
export declare type IDispatchEvent = <T extends Document>(ele: T, data: any) => void;
|
|
47
|
-
export declare type
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
export declare type IRequestParams<T> = T | IObject<any> | null;
|
|
51
|
+
export declare type IUrl = string;
|
|
52
|
+
export declare type IEnv = 'Window' | 'Node';
|
|
53
|
+
export declare type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
|
|
54
|
+
export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION";
|
|
55
|
+
export declare type IRequestBody = IRequestParams<BodyInit>;
|
|
56
|
+
export declare type IRequestHeaders = IRequestParams<HeadersInit>;
|
|
57
|
+
export declare type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>;
|
|
58
|
+
export declare type IRequestFn = (url: IUrl, params: IObject<any>, body: IRequestBody, opts: IRequestOptions) => Promise<any>;
|
|
59
|
+
export declare type IRequestOptions = {
|
|
60
|
+
method?: IRequestMethods;
|
|
61
|
+
query?: IRequestParams<IObject<any>>;
|
|
62
|
+
body?: IRequestBody;
|
|
63
|
+
headers?: IRequestHeaders;
|
|
64
|
+
controller?: AbortController;
|
|
65
|
+
timeout?: number;
|
|
66
|
+
timer?: number | unknown | null;
|
|
67
|
+
[key: string]: any;
|
|
68
|
+
};
|
|
69
|
+
export declare type IInterceptors = {
|
|
70
|
+
use(type: "request" | "response" | "error", fn: Function): void;
|
|
71
|
+
get reqFn(): Function;
|
|
72
|
+
get resFn(): Function;
|
|
73
|
+
get errFn(): Function;
|
|
74
|
+
};
|
|
75
|
+
export declare type IRequestBase = {
|
|
76
|
+
readonly origin: string;
|
|
77
|
+
chackUrl: (url: IUrl) => boolean;
|
|
78
|
+
envDesc: () => IEnv;
|
|
79
|
+
errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R;
|
|
80
|
+
clearTimer: (opts: IRequestOptions) => void;
|
|
81
|
+
initAbort: <T = IRequestOptions>(opts: T) => T;
|
|
82
|
+
requestType: () => IRequestBaseFn;
|
|
83
|
+
fixOrigin: (fixStr: string) => string;
|
|
84
|
+
fetch: IRequestBaseFn;
|
|
85
|
+
http: IRequestBaseFn;
|
|
86
|
+
getDataByType: (type: IDataType, response: Response) => Promise<any>;
|
|
51
87
|
};
|
|
52
|
-
export declare type
|
|
88
|
+
export declare type IRequestInit = {
|
|
89
|
+
initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
90
|
+
initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
91
|
+
initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
92
|
+
};
|
|
93
|
+
export declare type IRequest = {
|
|
94
|
+
GET: IRequestFn;
|
|
95
|
+
POST: IRequestFn;
|
|
96
|
+
DELETE: IRequestFn;
|
|
97
|
+
PUT: IRequestFn;
|
|
98
|
+
OPTIONS: IRequestFn;
|
|
99
|
+
HEAD: IRequestFn;
|
|
100
|
+
PATCH: IRequestFn;
|
|
101
|
+
} & IRequestBase;
|
|
53
102
|
export {};
|
package/dist/common/types.js
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
package/dist/esm/array.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IArrayRandom, IArrayUniq, IArrayDemote, IDemoteArray } from "./
|
|
1
|
+
import { IArrayRandom, IArrayUniq, IArrayDemote, IDemoteArray } from "./index.js";
|
|
2
2
|
export declare const arrayRandom: IArrayRandom<any[]>;
|
|
3
3
|
export declare const arrayUniq: IArrayUniq<any[]>;
|
|
4
4
|
export declare const arrayDemote: IArrayDemote<IDemoteArray<any>>;
|
package/dist/esm/array.js
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
import { getType } from "./
|
|
2
|
-
export var arrayRandom = function (arr) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
export var arrayDemote = function (arr, result) {
|
|
11
|
-
if (result === void 0) {
|
|
12
|
-
result = [];
|
|
13
|
-
}
|
|
14
|
-
arr.forEach(function (i) {
|
|
15
|
-
return getType(i) === "array" ? arrayDemote(i, result) : result.push(i);
|
|
16
|
-
});
|
|
17
|
-
return result;
|
|
18
|
-
};
|
|
1
|
+
import { getType } from "./index.js";
|
|
2
|
+
export var arrayRandom = function (arr) { return arr.sort(function () { return Math.random() - 0.5; }); };
|
|
3
|
+
export var arrayUniq = function (arr) { return Array.from(new Set(arr)); };
|
|
4
|
+
export var arrayDemote = function (arr, result) {
|
|
5
|
+
if (result === void 0) { result = []; }
|
|
6
|
+
arr.forEach(function (i) { return getType(i) === "array" ? arrayDemote(i, result) : result.push(i); });
|
|
7
|
+
return result;
|
|
8
|
+
};
|
package/dist/esm/base.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { IRandomNum, IUrlSplit, IUrlJoin, IGetType } from "./
|
|
2
|
-
import { types } from "./static";
|
|
1
|
+
import { IRandomNum, IUrlSplit, IUrlJoin, IGetType, IGetTypeByList, types } from "./index.js";
|
|
3
2
|
export declare const randomNum: IRandomNum;
|
|
4
3
|
export declare const urlSplit: IUrlSplit;
|
|
5
4
|
export declare const urlJoin: IUrlJoin;
|
|
6
5
|
export declare const getType: IGetType<types>;
|
|
6
|
+
export declare const getTypeByList: IGetTypeByList;
|
package/dist/esm/base.js
CHANGED
|
@@ -1,35 +1,41 @@
|
|
|
1
|
-
import { types } from "./
|
|
2
|
-
export var randomNum = function (min, max, bool) {
|
|
3
|
-
if (bool === void 0) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return
|
|
23
|
-
});
|
|
24
|
-
return "".concat(url).concat(url.includes("?") ? "&" : '?').concat(params.join("&"));
|
|
25
|
-
};
|
|
26
|
-
export var getType = function (data) {
|
|
27
|
-
var type = typeof data;
|
|
28
|
-
if (data === null) {
|
|
29
|
-
return "null";
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
1
|
+
import { types } from "./index.js";
|
|
2
|
+
export var randomNum = function (min, max, bool) {
|
|
3
|
+
if (bool === void 0) { bool = false; }
|
|
4
|
+
return Math.floor(Math.random() * (max - min + (bool ? 1 : 0)) + min);
|
|
5
|
+
};
|
|
6
|
+
export var urlSplit = function (url) {
|
|
7
|
+
var result = {};
|
|
8
|
+
if (!url.includes("?")) {
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
var params = url.split("?")[1].split("&");
|
|
12
|
+
params.forEach(function (i) {
|
|
13
|
+
var key = i.split("=")[0];
|
|
14
|
+
result[key] = i.split("=")[1];
|
|
15
|
+
});
|
|
16
|
+
return result;
|
|
17
|
+
};
|
|
18
|
+
export var urlJoin = function (url, query) {
|
|
19
|
+
if (query === void 0) { query = {}; }
|
|
20
|
+
var queryObject = Object.keys(query);
|
|
21
|
+
if (queryObject.length === 0)
|
|
22
|
+
return url;
|
|
23
|
+
var params = queryObject.map(function (i) { return "".concat(i, "=").concat(query[i]); });
|
|
24
|
+
return "".concat(url).concat(url.includes("?") ? "&" : '?').concat(params.join("&"));
|
|
25
|
+
};
|
|
26
|
+
export var getType = function (data) {
|
|
27
|
+
var type = typeof data;
|
|
28
|
+
if (data === null) {
|
|
29
|
+
return "null";
|
|
30
|
+
}
|
|
31
|
+
else if (type === "object") {
|
|
32
|
+
var key = Object.prototype.toString.call(data);
|
|
33
|
+
return types[key];
|
|
34
|
+
}
|
|
35
|
+
return type;
|
|
36
|
+
};
|
|
37
|
+
export var getTypeByList = function (data, whiteList) {
|
|
38
|
+
if (whiteList === void 0) { whiteList = []; }
|
|
39
|
+
var __type = getType(data);
|
|
40
|
+
return whiteList.indexOf(__type) > 0;
|
|
41
|
+
};
|
package/dist/esm/element.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ICreateElement } from "./
|
|
1
|
+
import { ICreateElement } from "./index.js";
|
|
2
2
|
export declare const createElement: ICreateElement;
|
package/dist/esm/element.js
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
export var createElement = function (_a) {
|
|
2
|
-
var _b, _c;
|
|
3
|
-
var ele = _a.ele,
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}));
|
|
11
|
-
attr && ((_c = Object.keys(style)) === null || _c === void 0 ? void 0 : _c.forEach(function (key) {
|
|
12
|
-
return element[key] = attr[key];
|
|
13
|
-
}));
|
|
14
|
-
parent && parent.appendChild(element);
|
|
15
|
-
return element;
|
|
16
|
-
};
|
|
1
|
+
export var createElement = function (_a) {
|
|
2
|
+
var _b, _c;
|
|
3
|
+
var ele = _a.ele, style = _a.style, attr = _a.attr, parent = _a.parent;
|
|
4
|
+
var element = ele instanceof HTMLElement ? ele : document.createElement(ele !== null && ele !== void 0 ? ele : 'div');
|
|
5
|
+
style && ((_b = Object.keys(style)) === null || _b === void 0 ? void 0 : _b.forEach(function (key) { return element.style[key] = style[key]; }));
|
|
6
|
+
attr && ((_c = Object.keys(style)) === null || _c === void 0 ? void 0 : _c.forEach(function (key) { return element[key] = attr[key]; }));
|
|
7
|
+
parent && parent.appendChild(element);
|
|
8
|
+
return element;
|
|
9
|
+
};
|
package/dist/esm/event.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IAddHandler, IStopBubble, IStopDefault, IRemoveHandler, IDispatchEvent } from "./
|
|
1
|
+
import { IAddHandler, IStopBubble, IStopDefault, IRemoveHandler, IDispatchEvent } from "./index.js";
|
|
2
2
|
export declare const addHandler: IAddHandler;
|
|
3
3
|
export declare const stopBubble: IStopBubble;
|
|
4
4
|
export declare const stopDefault: IStopDefault;
|