utils-lib-js 1.7.23 → 2.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.en.md +12 -6
- package/README.md +8 -6
- package/dist/bundle/index.d.ts +0 -2
- package/dist/bundle/index.js +1 -1
- package/dist/bundle/types.d.ts +0 -54
- package/dist/cjs/index.d.ts +0 -2
- package/dist/cjs/index.js +1 -230
- package/dist/cjs/types.d.ts +0 -54
- package/dist/esm/index.d.ts +0 -2
- package/dist/esm/index.js +2 -230
- package/dist/esm/types.d.ts +0 -54
- package/dist/umd/index.d.ts +0 -2
- package/dist/umd/index.js +1 -230
- package/dist/umd/types.d.ts +0 -54
- package/package.json +2 -3
- package/dist/bundle/request.d.ts +0 -60
- package/dist/cjs/request.d.ts +0 -60
- package/dist/esm/request.d.ts +0 -60
- package/dist/umd/request.d.ts +0 -60
package/dist/esm/index.js
CHANGED
|
@@ -12,22 +12,6 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
12
12
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
13
|
PERFORMANCE OF THIS SOFTWARE.
|
|
14
14
|
***************************************************************************** */
|
|
15
|
-
/* global Reflect, Promise */
|
|
16
|
-
|
|
17
|
-
var extendStatics = function(d, b) {
|
|
18
|
-
extendStatics = Object.setPrototypeOf ||
|
|
19
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
20
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
21
|
-
return extendStatics(d, b);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
function __extends(d, b) {
|
|
25
|
-
if (typeof b !== "function" && b !== null)
|
|
26
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
27
|
-
extendStatics(d, b);
|
|
28
|
-
function __() { this.constructor = d; }
|
|
29
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
30
|
-
}
|
|
31
15
|
|
|
32
16
|
var __assign$1 = function() {
|
|
33
17
|
__assign$1 = Object.assign || function __assign(t) {
|
|
@@ -40,18 +24,6 @@ var __assign$1 = function() {
|
|
|
40
24
|
return __assign$1.apply(this, arguments);
|
|
41
25
|
};
|
|
42
26
|
|
|
43
|
-
function __rest(s, e) {
|
|
44
|
-
var t = {};
|
|
45
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
46
|
-
t[p] = s[p];
|
|
47
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
48
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
49
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
50
|
-
t[p[i]] = s[p[i]];
|
|
51
|
-
}
|
|
52
|
-
return t;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
27
|
function __spreadArray(to, from, pack) {
|
|
56
28
|
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
57
29
|
if (ar || !(i in from)) {
|
|
@@ -374,206 +346,6 @@ var element = {
|
|
|
374
346
|
createElement: createElement
|
|
375
347
|
};
|
|
376
348
|
|
|
377
|
-
var httpRequest, httpsRequest, parse, CustomAbortController;
|
|
378
|
-
if (typeof require !== "undefined") {
|
|
379
|
-
CustomAbortController = require("abort-controller");
|
|
380
|
-
httpRequest = require("http").request;
|
|
381
|
-
httpsRequest = require("https").request;
|
|
382
|
-
parse = require("url").parse;
|
|
383
|
-
}
|
|
384
|
-
else if (typeof AbortController !== "undefined") {
|
|
385
|
-
CustomAbortController = AbortController;
|
|
386
|
-
}
|
|
387
|
-
else {
|
|
388
|
-
CustomAbortController = function () {
|
|
389
|
-
throw new Error('AbortController is not defined');
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
var Interceptors = (function () {
|
|
393
|
-
function Interceptors() {
|
|
394
|
-
}
|
|
395
|
-
Interceptors.prototype.use = function (type, fn) {
|
|
396
|
-
switch (type) {
|
|
397
|
-
case "request":
|
|
398
|
-
this.requestSuccess = fn;
|
|
399
|
-
break;
|
|
400
|
-
case "response":
|
|
401
|
-
this.responseSuccess = fn;
|
|
402
|
-
break;
|
|
403
|
-
case "error":
|
|
404
|
-
this.error = fn;
|
|
405
|
-
break;
|
|
406
|
-
}
|
|
407
|
-
return this;
|
|
408
|
-
};
|
|
409
|
-
Object.defineProperty(Interceptors.prototype, "reqFn", {
|
|
410
|
-
get: function () {
|
|
411
|
-
return this.requestSuccess;
|
|
412
|
-
},
|
|
413
|
-
enumerable: false,
|
|
414
|
-
configurable: true
|
|
415
|
-
});
|
|
416
|
-
Object.defineProperty(Interceptors.prototype, "resFn", {
|
|
417
|
-
get: function () {
|
|
418
|
-
return this.responseSuccess;
|
|
419
|
-
},
|
|
420
|
-
enumerable: false,
|
|
421
|
-
configurable: true
|
|
422
|
-
});
|
|
423
|
-
Object.defineProperty(Interceptors.prototype, "errFn", {
|
|
424
|
-
get: function () {
|
|
425
|
-
return this.error;
|
|
426
|
-
},
|
|
427
|
-
enumerable: false,
|
|
428
|
-
configurable: true
|
|
429
|
-
});
|
|
430
|
-
return Interceptors;
|
|
431
|
-
}());
|
|
432
|
-
var RequestBase = (function (_super) {
|
|
433
|
-
__extends(RequestBase, _super);
|
|
434
|
-
function RequestBase(origin) {
|
|
435
|
-
var _this = _super.call(this) || this;
|
|
436
|
-
_this.chackUrl = function (url) {
|
|
437
|
-
return url.startsWith('/');
|
|
438
|
-
};
|
|
439
|
-
_this.checkIsHttps = function (url) {
|
|
440
|
-
return url.startsWith('https');
|
|
441
|
-
};
|
|
442
|
-
_this.fixOrigin = function (fixStr) {
|
|
443
|
-
if (_this.chackUrl(fixStr))
|
|
444
|
-
return _this.origin + fixStr;
|
|
445
|
-
return fixStr;
|
|
446
|
-
};
|
|
447
|
-
_this.envDesc = function () {
|
|
448
|
-
if (typeof Window !== "undefined") {
|
|
449
|
-
return "Window";
|
|
450
|
-
}
|
|
451
|
-
return "Node";
|
|
452
|
-
};
|
|
453
|
-
_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); }; };
|
|
454
|
-
_this.clearTimer = function (opts) { return !!opts.timer && (clearTimeout(opts.timer), opts.timer = null); };
|
|
455
|
-
_this.initAbort = function (params) {
|
|
456
|
-
var controller = params.controller, timer = params.timer, timeout = params.timeout;
|
|
457
|
-
!!!timer && (params.timer = setTimeout(function () { return controller.abort(); }, timeout));
|
|
458
|
-
return params;
|
|
459
|
-
};
|
|
460
|
-
_this.requestType = function () {
|
|
461
|
-
switch (_this.envDesc()) {
|
|
462
|
-
case "Window":
|
|
463
|
-
return _this.fetch;
|
|
464
|
-
case "Node":
|
|
465
|
-
return _this.http;
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
_this.getDataByType = function (type, response) {
|
|
469
|
-
switch (type) {
|
|
470
|
-
case "text":
|
|
471
|
-
case "json":
|
|
472
|
-
case "blob":
|
|
473
|
-
case "formData":
|
|
474
|
-
case "arrayBuffer":
|
|
475
|
-
return response[type]();
|
|
476
|
-
default:
|
|
477
|
-
return response['json']();
|
|
478
|
-
}
|
|
479
|
-
};
|
|
480
|
-
_this.formatBodyString = function (bodyString) {
|
|
481
|
-
return {
|
|
482
|
-
text: function () { return bodyString; },
|
|
483
|
-
json: function () { var _a; return (_a = stringToJson(bodyString)) !== null && _a !== void 0 ? _a : bodyString; },
|
|
484
|
-
blob: function () { return stringToJson(bodyString); },
|
|
485
|
-
formData: function () { return stringToJson(bodyString); },
|
|
486
|
-
arrayBuffer: function () { return stringToJson(bodyString); },
|
|
487
|
-
};
|
|
488
|
-
};
|
|
489
|
-
_this.origin = origin !== null && origin !== void 0 ? origin : '';
|
|
490
|
-
return _this;
|
|
491
|
-
}
|
|
492
|
-
return RequestBase;
|
|
493
|
-
}(Interceptors));
|
|
494
|
-
var RequestInit = (function (_super) {
|
|
495
|
-
__extends(RequestInit, _super);
|
|
496
|
-
function RequestInit(origin) {
|
|
497
|
-
var _this = _super.call(this, origin) || this;
|
|
498
|
-
_this.initDefaultParams = function (url, _a) {
|
|
499
|
-
var _b, _c;
|
|
500
|
-
var _d = _a.method, method = _d === void 0 ? "GET" : _d, _e = _a.query, query = _e === void 0 ? {} : _e, _f = _a.headers, headers = _f === void 0 ? {} : _f, _g = _a.body, body = _g === void 0 ? null : _g, _h = _a.timeout, timeout = _h === void 0 ? 30 * 1000 : _h, _j = _a.controller, controller = _j === void 0 ? new CustomAbortController() : _j, _k = _a.type, type = _k === void 0 ? "json" : _k, others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
|
|
501
|
-
var __params = __assign$1({ url: url, method: method, query: query, headers: headers, body: method === "GET" ? null : jsonToString(body), timeout: timeout, signal: controller === null || controller === void 0 ? void 0 : controller.signal, controller: controller, type: type, timer: null }, others);
|
|
502
|
-
var params = (_c = (_b = _this.reqFn) === null || _b === void 0 ? void 0 : _b.call(_this, __params)) !== null && _c !== void 0 ? _c : __params;
|
|
503
|
-
params.url = urlJoin(_this.fixOrigin(url), __params.query);
|
|
504
|
-
return params;
|
|
505
|
-
};
|
|
506
|
-
_this.initFetchParams = function (url, opts) {
|
|
507
|
-
var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
|
|
508
|
-
return _temp;
|
|
509
|
-
};
|
|
510
|
-
_this.initHttpParams = function (url, opts) {
|
|
511
|
-
var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
|
|
512
|
-
var options = parse(_temp.url, true);
|
|
513
|
-
return __assign$1(__assign$1({}, _temp), options);
|
|
514
|
-
};
|
|
515
|
-
return _this;
|
|
516
|
-
}
|
|
517
|
-
return RequestInit;
|
|
518
|
-
}(RequestBase));
|
|
519
|
-
var Request = (function (_super) {
|
|
520
|
-
__extends(Request, _super);
|
|
521
|
-
function Request(origin) {
|
|
522
|
-
var _this = _super.call(this, origin) || this;
|
|
523
|
-
_this.fetch = function (_url, _opts) {
|
|
524
|
-
var _a = defer(), promise = _a.promise, resolve = _a.resolve, reject = _a.reject;
|
|
525
|
-
var _b = _this.initFetchParams(_url, _opts), url = _b.url, opts = __rest(_b, ["url"]);
|
|
526
|
-
var signal = opts.signal;
|
|
527
|
-
promise.finally(function () { return _this.clearTimer(opts); });
|
|
528
|
-
signal.addEventListener('abort', function () { return _this.errorFn(reject); });
|
|
529
|
-
fetch(url, opts).then(function (response) {
|
|
530
|
-
if ((response === null || response === void 0 ? void 0 : response.status) >= 200 && (response === null || response === void 0 ? void 0 : response.status) < 300) {
|
|
531
|
-
return _this.getDataByType(opts.type, response);
|
|
532
|
-
}
|
|
533
|
-
return _this.errorFn(reject);
|
|
534
|
-
}).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));
|
|
535
|
-
return promise;
|
|
536
|
-
};
|
|
537
|
-
_this.http = function (_url, _opts) {
|
|
538
|
-
var _a = defer(), promise = _a.promise, resolve = _a.resolve, reject = _a.reject;
|
|
539
|
-
var params = _this.initHttpParams(_url, _opts);
|
|
540
|
-
var signal = params.signal, url = params.url, body = params.body;
|
|
541
|
-
promise.finally(function () { return _this.clearTimer(params); });
|
|
542
|
-
var request = _this.checkIsHttps(url) ? httpsRequest : httpRequest;
|
|
543
|
-
var req = request(params, function (response) {
|
|
544
|
-
var statusCode = response.statusCode, statusMessage = response.statusMessage;
|
|
545
|
-
var data = "";
|
|
546
|
-
response.setEncoding('utf8');
|
|
547
|
-
response.on('data', function (chunk) { return data += chunk; });
|
|
548
|
-
return response.on("end", function () {
|
|
549
|
-
var _a, _b;
|
|
550
|
-
var result = _this.getDataByType(params.type, _this.formatBodyString(data));
|
|
551
|
-
if (statusCode >= 200 && statusCode < 300) {
|
|
552
|
-
return resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, result)) !== null && _b !== void 0 ? _b : result);
|
|
553
|
-
}
|
|
554
|
-
return _this.errorFn(reject)({ statusCode: statusCode, statusMessage: statusMessage, result: result, data: data });
|
|
555
|
-
});
|
|
556
|
-
});
|
|
557
|
-
signal.addEventListener('abort', function () { return _this.errorFn(reject)(req.destroy(new Error('request timeout'))); });
|
|
558
|
-
body && req.write(body);
|
|
559
|
-
req.on('error', _this.errorFn(reject));
|
|
560
|
-
req.end();
|
|
561
|
-
return promise;
|
|
562
|
-
};
|
|
563
|
-
_this.GET = function (url, query, _, opts) { return _this.request(url, __assign$1({ query: query, method: "GET" }, opts)); };
|
|
564
|
-
_this.POST = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "POST", body: body }, opts)); };
|
|
565
|
-
_this.PUT = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "PUT", body: body }, opts)); };
|
|
566
|
-
_this.DELETE = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "DELETE", body: body }, opts)); };
|
|
567
|
-
_this.OPTIONS = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "OPTIONS", body: body }, opts)); };
|
|
568
|
-
_this.HEAD = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "HEAD", body: body }, opts)); };
|
|
569
|
-
_this.PATCH = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "PATCH", body: body }, opts)); };
|
|
570
|
-
_this.request = _this.requestType();
|
|
571
|
-
return _this;
|
|
572
|
-
}
|
|
573
|
-
return Request;
|
|
574
|
-
}(RequestInit));
|
|
575
|
-
var request = { Request: Request };
|
|
576
|
-
|
|
577
349
|
var addHandler = function (ele, type, handler) {
|
|
578
350
|
if (ele.addEventListener) {
|
|
579
351
|
ele.addEventListener(type, handler, false);
|
|
@@ -1088,6 +860,6 @@ var decoratorTaskQueue = function (opts) {
|
|
|
1088
860
|
};
|
|
1089
861
|
};
|
|
1090
862
|
|
|
1091
|
-
var index = __assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(
|
|
863
|
+
var index = __assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1({}, object), base), array), __function), element), __static), event), storage), log), animate), { eventMessageCenter: MessageCenter, taskQueueLib: TaskQueue });
|
|
1092
864
|
|
|
1093
|
-
export { AnimateFrame, MessageCenter, NBezier,
|
|
865
|
+
export { AnimateFrame, MessageCenter, NBezier, TaskQueue, addHandler, arrayDemote, arrayRandom, arrayUniq, catchAwait, classDecorator, clearStorage, cloneDeep, combination, createElement, createObject, createObjectVariable, cubicBezier, debounce, decoratorMessageCenter, decoratorTaskQueue, index as default, defer, dispatchEvent, enumInversion, factorial, getInstance, getStorage, getType, getTypeByList, getValue, inherit, isNotObject, isWindow, jsonToString, logLoop, logOneLine, messageCenter, mixIn, quadraticBezier, randomNum, removeHandler, setStorage, setValue, stopBubble, stopDefault, stringToJson, throttle, types, urlJoin, urlSplit };
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { AbortController } from "abort-controller";
|
|
2
1
|
export type IKey = string | symbol | number;
|
|
3
2
|
export interface IObject<T> {
|
|
4
3
|
[key: IKey]: T | IObject<any>;
|
|
@@ -57,59 +56,6 @@ export type ILogLoopParams = {
|
|
|
57
56
|
timer?: number;
|
|
58
57
|
};
|
|
59
58
|
export type ILogLoop = (opts?: ILogLoopParams) => ILogLoopParams | void;
|
|
60
|
-
export type IRequestParams<T> = T | IObject<any> | null;
|
|
61
|
-
export type IUrl = string;
|
|
62
|
-
export type IEnv = 'Window' | 'Node';
|
|
63
|
-
export type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
|
|
64
|
-
export type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION" | "HEAD" | "PATCH";
|
|
65
|
-
export type IRequestBody = IRequestParams<BodyInit>;
|
|
66
|
-
export type IRequestHeaders = IRequestParams<HeadersInit>;
|
|
67
|
-
export type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>;
|
|
68
|
-
export type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>;
|
|
69
|
-
export type IRequestOptions = {
|
|
70
|
-
method?: IRequestMethods;
|
|
71
|
-
query?: IRequestParams<IObject<any>>;
|
|
72
|
-
body?: IRequestBody;
|
|
73
|
-
headers?: IRequestHeaders;
|
|
74
|
-
controller?: AbortController;
|
|
75
|
-
timeout?: number;
|
|
76
|
-
timer?: number | unknown | null;
|
|
77
|
-
[key: string]: any;
|
|
78
|
-
};
|
|
79
|
-
export type IInterceptors = {
|
|
80
|
-
use(type: "request" | "response" | "error", fn: Function): IInterceptors;
|
|
81
|
-
get reqFn(): Function;
|
|
82
|
-
get resFn(): Function;
|
|
83
|
-
get errFn(): Function;
|
|
84
|
-
};
|
|
85
|
-
export type IRequestBase = {
|
|
86
|
-
readonly origin: string;
|
|
87
|
-
chackUrl: (url: IUrl) => boolean;
|
|
88
|
-
envDesc: () => IEnv;
|
|
89
|
-
errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R;
|
|
90
|
-
clearTimer: (opts: IRequestOptions) => void;
|
|
91
|
-
initAbort: (opts: IRequestOptions) => IRequestOptions;
|
|
92
|
-
requestType: () => IRequestBaseFn;
|
|
93
|
-
fixOrigin: (fixStr: string) => string;
|
|
94
|
-
fetch: IRequestBaseFn;
|
|
95
|
-
http: IRequestBaseFn;
|
|
96
|
-
getDataByType: (type: IDataType, response: Response) => Promise<any>;
|
|
97
|
-
formatBodyString: (bodyString: string) => IObject<Function>;
|
|
98
|
-
};
|
|
99
|
-
export type IRequestInit = {
|
|
100
|
-
initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
101
|
-
initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
102
|
-
initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
103
|
-
};
|
|
104
|
-
export type IRequest = {
|
|
105
|
-
GET: IRequestFn;
|
|
106
|
-
POST: IRequestFn;
|
|
107
|
-
DELETE: IRequestFn;
|
|
108
|
-
PUT: IRequestFn;
|
|
109
|
-
OPTIONS: IRequestFn;
|
|
110
|
-
HEAD: IRequestFn;
|
|
111
|
-
PATCH: IRequestFn;
|
|
112
|
-
} & IRequestBase;
|
|
113
59
|
export type IAnimateFrame = {
|
|
114
60
|
id: number | null;
|
|
115
61
|
duration: number;
|
package/dist/umd/index.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ export * from "./function";
|
|
|
5
5
|
export * from "./element";
|
|
6
6
|
export * from "./static";
|
|
7
7
|
export * from "./types";
|
|
8
|
-
export * from "./request";
|
|
9
8
|
export * from "./event";
|
|
10
9
|
export * from "./storage";
|
|
11
10
|
export * from "./log";
|
|
@@ -33,7 +32,6 @@ declare const _default: {
|
|
|
33
32
|
stopDefault: import("./types").IStopDefault;
|
|
34
33
|
removeHandler: import("./types").IRemoveHandler;
|
|
35
34
|
dispatchEvent: import("./types").IDispatchEvent;
|
|
36
|
-
Request: typeof import("./request").Request;
|
|
37
35
|
types: typeof import("./static").types;
|
|
38
36
|
createElement: import("./types").ICreateElement<HTMLElement>;
|
|
39
37
|
throttle: import("./types").IThrottle;
|
package/dist/umd/index.js
CHANGED
|
@@ -18,22 +18,6 @@
|
|
|
18
18
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
19
|
PERFORMANCE OF THIS SOFTWARE.
|
|
20
20
|
***************************************************************************** */
|
|
21
|
-
/* global Reflect, Promise */
|
|
22
|
-
|
|
23
|
-
var extendStatics = function(d, b) {
|
|
24
|
-
extendStatics = Object.setPrototypeOf ||
|
|
25
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
26
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
27
|
-
return extendStatics(d, b);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
function __extends(d, b) {
|
|
31
|
-
if (typeof b !== "function" && b !== null)
|
|
32
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
33
|
-
extendStatics(d, b);
|
|
34
|
-
function __() { this.constructor = d; }
|
|
35
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
36
|
-
}
|
|
37
21
|
|
|
38
22
|
var __assign$1 = function() {
|
|
39
23
|
__assign$1 = Object.assign || function __assign(t) {
|
|
@@ -46,18 +30,6 @@
|
|
|
46
30
|
return __assign$1.apply(this, arguments);
|
|
47
31
|
};
|
|
48
32
|
|
|
49
|
-
function __rest(s, e) {
|
|
50
|
-
var t = {};
|
|
51
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
52
|
-
t[p] = s[p];
|
|
53
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
54
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
55
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
56
|
-
t[p[i]] = s[p[i]];
|
|
57
|
-
}
|
|
58
|
-
return t;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
33
|
function __spreadArray(to, from, pack) {
|
|
62
34
|
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
63
35
|
if (ar || !(i in from)) {
|
|
@@ -380,206 +352,6 @@
|
|
|
380
352
|
createElement: createElement
|
|
381
353
|
};
|
|
382
354
|
|
|
383
|
-
var httpRequest, httpsRequest, parse, CustomAbortController;
|
|
384
|
-
if (typeof require !== "undefined") {
|
|
385
|
-
CustomAbortController = require("abort-controller");
|
|
386
|
-
httpRequest = require("http").request;
|
|
387
|
-
httpsRequest = require("https").request;
|
|
388
|
-
parse = require("url").parse;
|
|
389
|
-
}
|
|
390
|
-
else if (typeof AbortController !== "undefined") {
|
|
391
|
-
CustomAbortController = AbortController;
|
|
392
|
-
}
|
|
393
|
-
else {
|
|
394
|
-
CustomAbortController = function () {
|
|
395
|
-
throw new Error('AbortController is not defined');
|
|
396
|
-
};
|
|
397
|
-
}
|
|
398
|
-
var Interceptors = (function () {
|
|
399
|
-
function Interceptors() {
|
|
400
|
-
}
|
|
401
|
-
Interceptors.prototype.use = function (type, fn) {
|
|
402
|
-
switch (type) {
|
|
403
|
-
case "request":
|
|
404
|
-
this.requestSuccess = fn;
|
|
405
|
-
break;
|
|
406
|
-
case "response":
|
|
407
|
-
this.responseSuccess = fn;
|
|
408
|
-
break;
|
|
409
|
-
case "error":
|
|
410
|
-
this.error = fn;
|
|
411
|
-
break;
|
|
412
|
-
}
|
|
413
|
-
return this;
|
|
414
|
-
};
|
|
415
|
-
Object.defineProperty(Interceptors.prototype, "reqFn", {
|
|
416
|
-
get: function () {
|
|
417
|
-
return this.requestSuccess;
|
|
418
|
-
},
|
|
419
|
-
enumerable: false,
|
|
420
|
-
configurable: true
|
|
421
|
-
});
|
|
422
|
-
Object.defineProperty(Interceptors.prototype, "resFn", {
|
|
423
|
-
get: function () {
|
|
424
|
-
return this.responseSuccess;
|
|
425
|
-
},
|
|
426
|
-
enumerable: false,
|
|
427
|
-
configurable: true
|
|
428
|
-
});
|
|
429
|
-
Object.defineProperty(Interceptors.prototype, "errFn", {
|
|
430
|
-
get: function () {
|
|
431
|
-
return this.error;
|
|
432
|
-
},
|
|
433
|
-
enumerable: false,
|
|
434
|
-
configurable: true
|
|
435
|
-
});
|
|
436
|
-
return Interceptors;
|
|
437
|
-
}());
|
|
438
|
-
var RequestBase = (function (_super) {
|
|
439
|
-
__extends(RequestBase, _super);
|
|
440
|
-
function RequestBase(origin) {
|
|
441
|
-
var _this = _super.call(this) || this;
|
|
442
|
-
_this.chackUrl = function (url) {
|
|
443
|
-
return url.startsWith('/');
|
|
444
|
-
};
|
|
445
|
-
_this.checkIsHttps = function (url) {
|
|
446
|
-
return url.startsWith('https');
|
|
447
|
-
};
|
|
448
|
-
_this.fixOrigin = function (fixStr) {
|
|
449
|
-
if (_this.chackUrl(fixStr))
|
|
450
|
-
return _this.origin + fixStr;
|
|
451
|
-
return fixStr;
|
|
452
|
-
};
|
|
453
|
-
_this.envDesc = function () {
|
|
454
|
-
if (typeof Window !== "undefined") {
|
|
455
|
-
return "Window";
|
|
456
|
-
}
|
|
457
|
-
return "Node";
|
|
458
|
-
};
|
|
459
|
-
_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); }; };
|
|
460
|
-
_this.clearTimer = function (opts) { return !!opts.timer && (clearTimeout(opts.timer), opts.timer = null); };
|
|
461
|
-
_this.initAbort = function (params) {
|
|
462
|
-
var controller = params.controller, timer = params.timer, timeout = params.timeout;
|
|
463
|
-
!!!timer && (params.timer = setTimeout(function () { return controller.abort(); }, timeout));
|
|
464
|
-
return params;
|
|
465
|
-
};
|
|
466
|
-
_this.requestType = function () {
|
|
467
|
-
switch (_this.envDesc()) {
|
|
468
|
-
case "Window":
|
|
469
|
-
return _this.fetch;
|
|
470
|
-
case "Node":
|
|
471
|
-
return _this.http;
|
|
472
|
-
}
|
|
473
|
-
};
|
|
474
|
-
_this.getDataByType = function (type, response) {
|
|
475
|
-
switch (type) {
|
|
476
|
-
case "text":
|
|
477
|
-
case "json":
|
|
478
|
-
case "blob":
|
|
479
|
-
case "formData":
|
|
480
|
-
case "arrayBuffer":
|
|
481
|
-
return response[type]();
|
|
482
|
-
default:
|
|
483
|
-
return response['json']();
|
|
484
|
-
}
|
|
485
|
-
};
|
|
486
|
-
_this.formatBodyString = function (bodyString) {
|
|
487
|
-
return {
|
|
488
|
-
text: function () { return bodyString; },
|
|
489
|
-
json: function () { var _a; return (_a = stringToJson(bodyString)) !== null && _a !== void 0 ? _a : bodyString; },
|
|
490
|
-
blob: function () { return stringToJson(bodyString); },
|
|
491
|
-
formData: function () { return stringToJson(bodyString); },
|
|
492
|
-
arrayBuffer: function () { return stringToJson(bodyString); },
|
|
493
|
-
};
|
|
494
|
-
};
|
|
495
|
-
_this.origin = origin !== null && origin !== void 0 ? origin : '';
|
|
496
|
-
return _this;
|
|
497
|
-
}
|
|
498
|
-
return RequestBase;
|
|
499
|
-
}(Interceptors));
|
|
500
|
-
var RequestInit = (function (_super) {
|
|
501
|
-
__extends(RequestInit, _super);
|
|
502
|
-
function RequestInit(origin) {
|
|
503
|
-
var _this = _super.call(this, origin) || this;
|
|
504
|
-
_this.initDefaultParams = function (url, _a) {
|
|
505
|
-
var _b, _c;
|
|
506
|
-
var _d = _a.method, method = _d === void 0 ? "GET" : _d, _e = _a.query, query = _e === void 0 ? {} : _e, _f = _a.headers, headers = _f === void 0 ? {} : _f, _g = _a.body, body = _g === void 0 ? null : _g, _h = _a.timeout, timeout = _h === void 0 ? 30 * 1000 : _h, _j = _a.controller, controller = _j === void 0 ? new CustomAbortController() : _j, _k = _a.type, type = _k === void 0 ? "json" : _k, others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
|
|
507
|
-
var __params = __assign$1({ url: url, method: method, query: query, headers: headers, body: method === "GET" ? null : jsonToString(body), timeout: timeout, signal: controller === null || controller === void 0 ? void 0 : controller.signal, controller: controller, type: type, timer: null }, others);
|
|
508
|
-
var params = (_c = (_b = _this.reqFn) === null || _b === void 0 ? void 0 : _b.call(_this, __params)) !== null && _c !== void 0 ? _c : __params;
|
|
509
|
-
params.url = urlJoin(_this.fixOrigin(url), __params.query);
|
|
510
|
-
return params;
|
|
511
|
-
};
|
|
512
|
-
_this.initFetchParams = function (url, opts) {
|
|
513
|
-
var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
|
|
514
|
-
return _temp;
|
|
515
|
-
};
|
|
516
|
-
_this.initHttpParams = function (url, opts) {
|
|
517
|
-
var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
|
|
518
|
-
var options = parse(_temp.url, true);
|
|
519
|
-
return __assign$1(__assign$1({}, _temp), options);
|
|
520
|
-
};
|
|
521
|
-
return _this;
|
|
522
|
-
}
|
|
523
|
-
return RequestInit;
|
|
524
|
-
}(RequestBase));
|
|
525
|
-
var Request = (function (_super) {
|
|
526
|
-
__extends(Request, _super);
|
|
527
|
-
function Request(origin) {
|
|
528
|
-
var _this = _super.call(this, origin) || this;
|
|
529
|
-
_this.fetch = function (_url, _opts) {
|
|
530
|
-
var _a = defer(), promise = _a.promise, resolve = _a.resolve, reject = _a.reject;
|
|
531
|
-
var _b = _this.initFetchParams(_url, _opts), url = _b.url, opts = __rest(_b, ["url"]);
|
|
532
|
-
var signal = opts.signal;
|
|
533
|
-
promise.finally(function () { return _this.clearTimer(opts); });
|
|
534
|
-
signal.addEventListener('abort', function () { return _this.errorFn(reject); });
|
|
535
|
-
fetch(url, opts).then(function (response) {
|
|
536
|
-
if ((response === null || response === void 0 ? void 0 : response.status) >= 200 && (response === null || response === void 0 ? void 0 : response.status) < 300) {
|
|
537
|
-
return _this.getDataByType(opts.type, response);
|
|
538
|
-
}
|
|
539
|
-
return _this.errorFn(reject);
|
|
540
|
-
}).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));
|
|
541
|
-
return promise;
|
|
542
|
-
};
|
|
543
|
-
_this.http = function (_url, _opts) {
|
|
544
|
-
var _a = defer(), promise = _a.promise, resolve = _a.resolve, reject = _a.reject;
|
|
545
|
-
var params = _this.initHttpParams(_url, _opts);
|
|
546
|
-
var signal = params.signal, url = params.url, body = params.body;
|
|
547
|
-
promise.finally(function () { return _this.clearTimer(params); });
|
|
548
|
-
var request = _this.checkIsHttps(url) ? httpsRequest : httpRequest;
|
|
549
|
-
var req = request(params, function (response) {
|
|
550
|
-
var statusCode = response.statusCode, statusMessage = response.statusMessage;
|
|
551
|
-
var data = "";
|
|
552
|
-
response.setEncoding('utf8');
|
|
553
|
-
response.on('data', function (chunk) { return data += chunk; });
|
|
554
|
-
return response.on("end", function () {
|
|
555
|
-
var _a, _b;
|
|
556
|
-
var result = _this.getDataByType(params.type, _this.formatBodyString(data));
|
|
557
|
-
if (statusCode >= 200 && statusCode < 300) {
|
|
558
|
-
return resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, result)) !== null && _b !== void 0 ? _b : result);
|
|
559
|
-
}
|
|
560
|
-
return _this.errorFn(reject)({ statusCode: statusCode, statusMessage: statusMessage, result: result, data: data });
|
|
561
|
-
});
|
|
562
|
-
});
|
|
563
|
-
signal.addEventListener('abort', function () { return _this.errorFn(reject)(req.destroy(new Error('request timeout'))); });
|
|
564
|
-
body && req.write(body);
|
|
565
|
-
req.on('error', _this.errorFn(reject));
|
|
566
|
-
req.end();
|
|
567
|
-
return promise;
|
|
568
|
-
};
|
|
569
|
-
_this.GET = function (url, query, _, opts) { return _this.request(url, __assign$1({ query: query, method: "GET" }, opts)); };
|
|
570
|
-
_this.POST = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "POST", body: body }, opts)); };
|
|
571
|
-
_this.PUT = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "PUT", body: body }, opts)); };
|
|
572
|
-
_this.DELETE = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "DELETE", body: body }, opts)); };
|
|
573
|
-
_this.OPTIONS = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "OPTIONS", body: body }, opts)); };
|
|
574
|
-
_this.HEAD = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "HEAD", body: body }, opts)); };
|
|
575
|
-
_this.PATCH = function (url, query, body, opts) { return _this.request(url, __assign$1({ query: query, method: "PATCH", body: body }, opts)); };
|
|
576
|
-
_this.request = _this.requestType();
|
|
577
|
-
return _this;
|
|
578
|
-
}
|
|
579
|
-
return Request;
|
|
580
|
-
}(RequestInit));
|
|
581
|
-
var request = { Request: Request };
|
|
582
|
-
|
|
583
355
|
var addHandler = function (ele, type, handler) {
|
|
584
356
|
if (ele.addEventListener) {
|
|
585
357
|
ele.addEventListener(type, handler, false);
|
|
@@ -1094,12 +866,11 @@
|
|
|
1094
866
|
};
|
|
1095
867
|
};
|
|
1096
868
|
|
|
1097
|
-
var index = __assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(
|
|
869
|
+
var index = __assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1(__assign$1({}, object), base), array), __function), element), __static), event), storage), log), animate), { eventMessageCenter: MessageCenter, taskQueueLib: TaskQueue });
|
|
1098
870
|
|
|
1099
871
|
exports.AnimateFrame = AnimateFrame;
|
|
1100
872
|
exports.MessageCenter = MessageCenter;
|
|
1101
873
|
exports.NBezier = NBezier;
|
|
1102
|
-
exports.Request = Request;
|
|
1103
874
|
exports.TaskQueue = TaskQueue;
|
|
1104
875
|
exports.addHandler = addHandler;
|
|
1105
876
|
exports.arrayDemote = arrayDemote;
|
package/dist/umd/types.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { AbortController } from "abort-controller";
|
|
2
1
|
export type IKey = string | symbol | number;
|
|
3
2
|
export interface IObject<T> {
|
|
4
3
|
[key: IKey]: T | IObject<any>;
|
|
@@ -57,59 +56,6 @@ export type ILogLoopParams = {
|
|
|
57
56
|
timer?: number;
|
|
58
57
|
};
|
|
59
58
|
export type ILogLoop = (opts?: ILogLoopParams) => ILogLoopParams | void;
|
|
60
|
-
export type IRequestParams<T> = T | IObject<any> | null;
|
|
61
|
-
export type IUrl = string;
|
|
62
|
-
export type IEnv = 'Window' | 'Node';
|
|
63
|
-
export type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
|
|
64
|
-
export type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION" | "HEAD" | "PATCH";
|
|
65
|
-
export type IRequestBody = IRequestParams<BodyInit>;
|
|
66
|
-
export type IRequestHeaders = IRequestParams<HeadersInit>;
|
|
67
|
-
export type IRequestBaseFn = (url: IUrl, opts: IRequestOptions) => Promise<any>;
|
|
68
|
-
export type IRequestFn = (url?: IUrl, query?: IObject<any>, body?: IRequestBody, opts?: IRequestOptions) => Promise<any>;
|
|
69
|
-
export type IRequestOptions = {
|
|
70
|
-
method?: IRequestMethods;
|
|
71
|
-
query?: IRequestParams<IObject<any>>;
|
|
72
|
-
body?: IRequestBody;
|
|
73
|
-
headers?: IRequestHeaders;
|
|
74
|
-
controller?: AbortController;
|
|
75
|
-
timeout?: number;
|
|
76
|
-
timer?: number | unknown | null;
|
|
77
|
-
[key: string]: any;
|
|
78
|
-
};
|
|
79
|
-
export type IInterceptors = {
|
|
80
|
-
use(type: "request" | "response" | "error", fn: Function): IInterceptors;
|
|
81
|
-
get reqFn(): Function;
|
|
82
|
-
get resFn(): Function;
|
|
83
|
-
get errFn(): Function;
|
|
84
|
-
};
|
|
85
|
-
export type IRequestBase = {
|
|
86
|
-
readonly origin: string;
|
|
87
|
-
chackUrl: (url: IUrl) => boolean;
|
|
88
|
-
envDesc: () => IEnv;
|
|
89
|
-
errorFn: <Err = any, R = Function>(reject: R) => (err: Err) => R;
|
|
90
|
-
clearTimer: (opts: IRequestOptions) => void;
|
|
91
|
-
initAbort: (opts: IRequestOptions) => IRequestOptions;
|
|
92
|
-
requestType: () => IRequestBaseFn;
|
|
93
|
-
fixOrigin: (fixStr: string) => string;
|
|
94
|
-
fetch: IRequestBaseFn;
|
|
95
|
-
http: IRequestBaseFn;
|
|
96
|
-
getDataByType: (type: IDataType, response: Response) => Promise<any>;
|
|
97
|
-
formatBodyString: (bodyString: string) => IObject<Function>;
|
|
98
|
-
};
|
|
99
|
-
export type IRequestInit = {
|
|
100
|
-
initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
101
|
-
initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
102
|
-
initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
|
|
103
|
-
};
|
|
104
|
-
export type IRequest = {
|
|
105
|
-
GET: IRequestFn;
|
|
106
|
-
POST: IRequestFn;
|
|
107
|
-
DELETE: IRequestFn;
|
|
108
|
-
PUT: IRequestFn;
|
|
109
|
-
OPTIONS: IRequestFn;
|
|
110
|
-
HEAD: IRequestFn;
|
|
111
|
-
PATCH: IRequestFn;
|
|
112
|
-
} & IRequestBase;
|
|
113
59
|
export type IAnimateFrame = {
|
|
114
60
|
id: number | null;
|
|
115
61
|
duration: number;
|