utils-lib-js 1.0.2 → 1.0.5
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 +2 -2
- package/dist/common/base.d.ts +2 -2
- package/dist/common/base.js +18 -5
- package/dist/common/element.d.ts +1 -1
- package/dist/common/event.d.ts +1 -1
- package/dist/common/function.d.ts +1 -1
- package/dist/common/index.d.ts +10 -8
- package/dist/common/index.js +10 -8
- package/dist/common/object.d.ts +5 -1
- package/dist/common/object.js +55 -4
- package/dist/common/request.d.ts +42 -0
- package/dist/common/request.js +221 -0
- package/dist/common/types.d.ts +49 -2
- package/dist/esm/array.d.ts +1 -1
- package/dist/esm/array.js +1 -1
- package/dist/esm/base.d.ts +2 -2
- package/dist/esm/base.js +14 -2
- package/dist/esm/element.d.ts +1 -1
- package/dist/esm/event.d.ts +1 -1
- package/dist/esm/function.d.ts +1 -1
- package/dist/esm/index.d.ts +10 -8
- package/dist/esm/index.js +10 -8
- package/dist/esm/object.d.ts +5 -1
- package/dist/esm/object.js +48 -1
- package/dist/esm/request.d.ts +42 -0
- package/dist/esm/request.js +217 -0
- package/dist/esm/types.d.ts +49 -2
- package/index.html +37 -0
- package/package.json +2 -1
- package/tsconfig.es.json +3 -7
- package/dist/common/fetch.d.ts +0 -0
- package/dist/common/fetch.js +0 -0
- package/dist/esm/fetch.d.ts +0 -0
- package/dist/esm/fetch.js +0 -0
package/dist/common/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/common/array.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.arrayDemote = exports.arrayUniq = exports.arrayRandom = void 0;
|
|
5
|
-
var
|
|
5
|
+
var index_js_1 = require("./index.js");
|
|
6
6
|
var arrayRandom = function (arr) {
|
|
7
7
|
return arr.sort(function () {
|
|
8
8
|
return Math.random() - 0.5;
|
|
@@ -18,7 +18,7 @@ var arrayDemote = function (arr, result) {
|
|
|
18
18
|
result = [];
|
|
19
19
|
}
|
|
20
20
|
arr.forEach(function (i) {
|
|
21
|
-
return (0,
|
|
21
|
+
return (0, index_js_1.getType)(i) === "array" ? (0, exports.arrayDemote)(i, result) : result.push(i);
|
|
22
22
|
});
|
|
23
23
|
return result;
|
|
24
24
|
};
|
package/dist/common/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/common/base.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.getType = exports.urlJoin = exports.urlSplit = exports.randomNum = void 0;
|
|
5
|
-
var
|
|
4
|
+
exports.getTypeByList = exports.getType = exports.urlJoin = exports.urlSplit = exports.randomNum = void 0;
|
|
5
|
+
var index_js_1 = require("./index.js");
|
|
6
6
|
var randomNum = function (min, max, bool) {
|
|
7
7
|
if (bool === void 0) {
|
|
8
8
|
bool = false;
|
|
@@ -24,7 +24,12 @@ var urlSplit = function (url) {
|
|
|
24
24
|
};
|
|
25
25
|
exports.urlSplit = urlSplit;
|
|
26
26
|
var urlJoin = function (url, query) {
|
|
27
|
-
|
|
27
|
+
if (query === void 0) {
|
|
28
|
+
query = {};
|
|
29
|
+
}
|
|
30
|
+
var queryObject = Object.keys(query);
|
|
31
|
+
if (queryObject.length === 0) return url;
|
|
32
|
+
var params = queryObject.map(function (i) {
|
|
28
33
|
return "".concat(i, "=").concat(query[i]);
|
|
29
34
|
});
|
|
30
35
|
return "".concat(url).concat(url.includes("?") ? "&" : '?').concat(params.join("&"));
|
|
@@ -36,8 +41,16 @@ var getType = function (data) {
|
|
|
36
41
|
return "null";
|
|
37
42
|
} else if (type === "object") {
|
|
38
43
|
var key = Object.prototype.toString.call(data);
|
|
39
|
-
return
|
|
44
|
+
return index_js_1.types[key];
|
|
40
45
|
}
|
|
41
46
|
return type;
|
|
42
47
|
};
|
|
43
|
-
exports.getType = getType;
|
|
48
|
+
exports.getType = getType;
|
|
49
|
+
var getTypeByList = function (data, whiteList) {
|
|
50
|
+
if (whiteList === void 0) {
|
|
51
|
+
whiteList = [];
|
|
52
|
+
}
|
|
53
|
+
var __type = (0, exports.getType)(data);
|
|
54
|
+
return whiteList.indexOf(__type) > 0;
|
|
55
|
+
};
|
|
56
|
+
exports.getTypeByList = getTypeByList;
|
package/dist/common/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/common/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;
|
package/dist/common/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export * from "./object";
|
|
2
|
-
export * from "./base";
|
|
3
|
-
export * from "./array";
|
|
4
|
-
export * from "./function";
|
|
5
|
-
export * from "./element";
|
|
6
|
-
export * from "./static";
|
|
7
|
-
export * from "./types";
|
|
8
|
-
export * from "
|
|
1
|
+
export * from "./object.js";
|
|
2
|
+
export * from "./base.js";
|
|
3
|
+
export * from "./array.js";
|
|
4
|
+
export * from "./function.js";
|
|
5
|
+
export * from "./element.js";
|
|
6
|
+
export * from "./static.js";
|
|
7
|
+
export * from "./types.js";
|
|
8
|
+
export * from "./request.js";
|
|
9
|
+
export * from "./event.js";
|
|
10
|
+
export * from "./storage.js";
|
package/dist/common/index.js
CHANGED
|
@@ -17,11 +17,13 @@ var __exportStar = this && this.__exportStar || function (m, exports) {
|
|
|
17
17
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
__exportStar(require("./object"), exports);
|
|
21
|
-
__exportStar(require("./base"), exports);
|
|
22
|
-
__exportStar(require("./array"), exports);
|
|
23
|
-
__exportStar(require("./function"), exports);
|
|
24
|
-
__exportStar(require("./element"), exports);
|
|
25
|
-
__exportStar(require("./static"), exports);
|
|
26
|
-
__exportStar(require("./types"), exports);
|
|
27
|
-
__exportStar(require("
|
|
20
|
+
__exportStar(require("./object.js"), exports);
|
|
21
|
+
__exportStar(require("./base.js"), exports);
|
|
22
|
+
__exportStar(require("./array.js"), exports);
|
|
23
|
+
__exportStar(require("./function.js"), exports);
|
|
24
|
+
__exportStar(require("./element.js"), exports);
|
|
25
|
+
__exportStar(require("./static.js"), exports);
|
|
26
|
+
__exportStar(require("./types.js"), exports);
|
|
27
|
+
__exportStar(require("./request.js"), exports);
|
|
28
|
+
__exportStar(require("./event.js"), exports);
|
|
29
|
+
__exportStar(require("./storage.js"), exports);
|
package/dist/common/object.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IGetValue, ISetValue, IMixIn, ICloneDeep, ICreateObjectVariable, IEnumInversion, IInherit, ICreateObject } from "./
|
|
1
|
+
import { IGetValue, ISetValue, IMixIn, ICloneDeep, ICreateObjectVariable, IEnumInversion, IInherit, ICreateObject, IGetInstance, IClassDecorator, IStringToJson, IJsonToString } from "./index.js";
|
|
2
2
|
export declare const getValue: IGetValue;
|
|
3
3
|
export declare const setValue: ISetValue;
|
|
4
4
|
export declare const mixIn: IMixIn;
|
|
@@ -8,3 +8,7 @@ export declare const cloneDeep: ICloneDeep;
|
|
|
8
8
|
export declare const createObjectVariable: ICreateObjectVariable;
|
|
9
9
|
export declare const createObject: ICreateObject;
|
|
10
10
|
export declare const inherit: IInherit;
|
|
11
|
+
export declare const getInstance: IGetInstance;
|
|
12
|
+
export declare const classDecorator: IClassDecorator;
|
|
13
|
+
export declare const stringToJson: IStringToJson;
|
|
14
|
+
export declare const jsonToString: IJsonToString;
|
package/dist/common/object.js
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
|
|
4
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
5
|
+
if (ar || !(i in from)) {
|
|
6
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
7
|
+
ar[i] = from[i];
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
11
|
+
};
|
|
3
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.inherit = exports.createObject = exports.createObjectVariable = exports.cloneDeep = exports.isNotObject = exports.enumInversion = exports.mixIn = exports.setValue = exports.getValue = void 0;
|
|
5
|
-
var
|
|
13
|
+
exports.jsonToString = exports.stringToJson = exports.classDecorator = exports.getInstance = exports.inherit = exports.createObject = exports.createObjectVariable = exports.cloneDeep = exports.isNotObject = exports.enumInversion = exports.mixIn = exports.setValue = exports.getValue = void 0;
|
|
14
|
+
var index_js_1 = require("./index.js");
|
|
6
15
|
var getValue = function (object, key, defaultValue) {
|
|
7
16
|
if (defaultValue === void 0) {
|
|
8
17
|
defaultValue = '';
|
|
@@ -72,7 +81,7 @@ var isNotObject = function (source, type) {
|
|
|
72
81
|
};
|
|
73
82
|
exports.isNotObject = isNotObject;
|
|
74
83
|
var cloneDeep = function (target) {
|
|
75
|
-
var __type = (0,
|
|
84
|
+
var __type = (0, index_js_1.getType)(target);
|
|
76
85
|
if ((0, exports.isNotObject)(target, __type)) return target;
|
|
77
86
|
var __init = (0, exports.createObjectVariable)(__type, target);
|
|
78
87
|
if (__type === "map") {
|
|
@@ -131,4 +140,46 @@ var inherit = function (source, target) {
|
|
|
131
140
|
target.prototype.constructor = target;
|
|
132
141
|
return target;
|
|
133
142
|
};
|
|
134
|
-
exports.inherit = inherit;
|
|
143
|
+
exports.inherit = inherit;
|
|
144
|
+
var getInstance = function (classProto, overwrite) {
|
|
145
|
+
if (overwrite === void 0) {
|
|
146
|
+
overwrite = false;
|
|
147
|
+
}
|
|
148
|
+
var params = [];
|
|
149
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
150
|
+
params[_i - 2] = arguments[_i];
|
|
151
|
+
}
|
|
152
|
+
if (!classProto._instance || overwrite) {
|
|
153
|
+
classProto._instance = new (classProto.bind.apply(classProto, __spreadArray([void 0], params, false)))();
|
|
154
|
+
}
|
|
155
|
+
return classProto._instance;
|
|
156
|
+
};
|
|
157
|
+
exports.getInstance = getInstance;
|
|
158
|
+
var classDecorator = function (params) {
|
|
159
|
+
return function (target) {
|
|
160
|
+
for (var key in params) {
|
|
161
|
+
if (!!!Reflect.has(target.prototype, key)) {
|
|
162
|
+
target.prototype[key] = params[key];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
exports.classDecorator = classDecorator;
|
|
168
|
+
var stringToJson = function (target) {
|
|
169
|
+
if ((0, index_js_1.getType)(target) !== "string") return {};
|
|
170
|
+
try {
|
|
171
|
+
return JSON.parse(target);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
return {};
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
exports.stringToJson = stringToJson;
|
|
177
|
+
var jsonToString = function (target) {
|
|
178
|
+
if (!(0, index_js_1.getTypeByList)(target, ["array", "object", "set", "map"])) return "";
|
|
179
|
+
try {
|
|
180
|
+
return JSON.stringify(target);
|
|
181
|
+
} catch (error) {
|
|
182
|
+
return "";
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
exports.jsonToString = jsonToString;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { IRequest, IRequestBase, IInterceptors } from "./index.js";
|
|
2
|
+
export declare class Interceptors implements IInterceptors {
|
|
3
|
+
private requestSuccess;
|
|
4
|
+
private responseSuccess;
|
|
5
|
+
private error;
|
|
6
|
+
use(type: any, fn: any): this;
|
|
7
|
+
get reqFn(): Function;
|
|
8
|
+
get resFn(): Function;
|
|
9
|
+
get errFn(): Function;
|
|
10
|
+
}
|
|
11
|
+
export declare abstract class RequestBase extends Interceptors implements IRequestBase {
|
|
12
|
+
origin: string;
|
|
13
|
+
constructor(origin: any);
|
|
14
|
+
abstract fetch(url: any, opts: any): Promise<void>;
|
|
15
|
+
abstract http(url: any, opts: any): Promise<void>;
|
|
16
|
+
chackUrl: (url: any) => any;
|
|
17
|
+
fixOrigin: (fixStr: string) => string;
|
|
18
|
+
envDesc: () => "Window" | "Node";
|
|
19
|
+
requestType: () => (url: any, opts: any) => Promise<void>;
|
|
20
|
+
initFetchParams: (url: any, { method, query, headers, body, timeout, abort, type, ...others }: {
|
|
21
|
+
[x: string]: any;
|
|
22
|
+
method?: string;
|
|
23
|
+
query?: {};
|
|
24
|
+
headers?: {};
|
|
25
|
+
body?: any;
|
|
26
|
+
timeout?: number;
|
|
27
|
+
abort?: AbortController;
|
|
28
|
+
type?: string;
|
|
29
|
+
}) => any;
|
|
30
|
+
getDataByType: (type: any, response: any) => any;
|
|
31
|
+
}
|
|
32
|
+
export declare class Request extends RequestBase implements IRequest {
|
|
33
|
+
request: Function;
|
|
34
|
+
constructor(origin: any);
|
|
35
|
+
fetch: (_url: any, _opts: any) => Promise<void>;
|
|
36
|
+
http: (url: any, opts: any) => Promise<void>;
|
|
37
|
+
GET: (url: any, query: {}, _: any, opts: any) => any;
|
|
38
|
+
POST: (url: any, query: {}, body: any, opts: any) => any;
|
|
39
|
+
PUT: (url: any, query: {}, body: any, opts: any) => any;
|
|
40
|
+
DELETE: (url: any, query: {}, body: any, opts: any) => any;
|
|
41
|
+
OPTION: (url: any, query: {}, body: any, opts: any) => any;
|
|
42
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __extends = this && this.__extends || function () {
|
|
4
|
+
var extendStatics = function (d, b) {
|
|
5
|
+
extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) {
|
|
6
|
+
d.__proto__ = b;
|
|
7
|
+
} || function (d, b) {
|
|
8
|
+
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
|
|
9
|
+
};
|
|
10
|
+
return extendStatics(d, b);
|
|
11
|
+
};
|
|
12
|
+
return function (d, b) {
|
|
13
|
+
if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
14
|
+
extendStatics(d, b);
|
|
15
|
+
function __() {
|
|
16
|
+
this.constructor = d;
|
|
17
|
+
}
|
|
18
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
19
|
+
};
|
|
20
|
+
}();
|
|
21
|
+
var __assign = this && this.__assign || function () {
|
|
22
|
+
__assign = Object.assign || function (t) {
|
|
23
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
24
|
+
s = arguments[i];
|
|
25
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
26
|
+
}
|
|
27
|
+
return t;
|
|
28
|
+
};
|
|
29
|
+
return __assign.apply(this, arguments);
|
|
30
|
+
};
|
|
31
|
+
var __rest = this && this.__rest || function (s, e) {
|
|
32
|
+
var t = {};
|
|
33
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
|
|
34
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
35
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
|
|
36
|
+
}
|
|
37
|
+
return t;
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
exports.Request = exports.RequestBase = exports.Interceptors = void 0;
|
|
41
|
+
var index_js_1 = require("./index.js");
|
|
42
|
+
var Interceptors = function () {
|
|
43
|
+
function Interceptors() {}
|
|
44
|
+
Interceptors.prototype.use = function (type, fn) {
|
|
45
|
+
switch (type) {
|
|
46
|
+
case "request":
|
|
47
|
+
this.requestSuccess = fn;
|
|
48
|
+
break;
|
|
49
|
+
case "response":
|
|
50
|
+
this.responseSuccess = fn;
|
|
51
|
+
break;
|
|
52
|
+
case "error":
|
|
53
|
+
this.error = fn;
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
return this;
|
|
57
|
+
};
|
|
58
|
+
Object.defineProperty(Interceptors.prototype, "reqFn", {
|
|
59
|
+
get: function () {
|
|
60
|
+
return this.requestSuccess;
|
|
61
|
+
},
|
|
62
|
+
enumerable: false,
|
|
63
|
+
configurable: true
|
|
64
|
+
});
|
|
65
|
+
Object.defineProperty(Interceptors.prototype, "resFn", {
|
|
66
|
+
get: function () {
|
|
67
|
+
return this.responseSuccess;
|
|
68
|
+
},
|
|
69
|
+
enumerable: false,
|
|
70
|
+
configurable: true
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(Interceptors.prototype, "errFn", {
|
|
73
|
+
get: function () {
|
|
74
|
+
return this.error;
|
|
75
|
+
},
|
|
76
|
+
enumerable: false,
|
|
77
|
+
configurable: true
|
|
78
|
+
});
|
|
79
|
+
return Interceptors;
|
|
80
|
+
}();
|
|
81
|
+
exports.Interceptors = Interceptors;
|
|
82
|
+
var RequestBase = function (_super) {
|
|
83
|
+
__extends(RequestBase, _super);
|
|
84
|
+
function RequestBase(origin) {
|
|
85
|
+
var _this = _super.call(this) || this;
|
|
86
|
+
_this.chackUrl = function (url) {
|
|
87
|
+
return url.startsWith('/');
|
|
88
|
+
};
|
|
89
|
+
_this.fixOrigin = function (fixStr) {
|
|
90
|
+
if (_this.chackUrl(fixStr)) return _this.origin + fixStr;
|
|
91
|
+
return fixStr;
|
|
92
|
+
};
|
|
93
|
+
_this.envDesc = function () {
|
|
94
|
+
if (Window) {
|
|
95
|
+
return "Window";
|
|
96
|
+
}
|
|
97
|
+
return "Node";
|
|
98
|
+
};
|
|
99
|
+
_this.requestType = function () {
|
|
100
|
+
switch (_this.envDesc()) {
|
|
101
|
+
case "Window":
|
|
102
|
+
return _this.fetch;
|
|
103
|
+
case "Node":
|
|
104
|
+
return _this.http;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
_this.initFetchParams = function (url, _a) {
|
|
108
|
+
var _b, _c;
|
|
109
|
+
var _d = _a.method,
|
|
110
|
+
method = _d === void 0 ? "GET" : _d,
|
|
111
|
+
_e = _a.query,
|
|
112
|
+
query = _e === void 0 ? {} : _e,
|
|
113
|
+
_f = _a.headers,
|
|
114
|
+
headers = _f === void 0 ? {} : _f,
|
|
115
|
+
_g = _a.body,
|
|
116
|
+
body = _g === void 0 ? null : _g,
|
|
117
|
+
_h = _a.timeout,
|
|
118
|
+
timeout = _h === void 0 ? 30 * 1000 : _h,
|
|
119
|
+
_j = _a.abort,
|
|
120
|
+
abort = _j === void 0 ? new AbortController() : _j,
|
|
121
|
+
_k = _a.type,
|
|
122
|
+
type = _k === void 0 ? "json" : _k,
|
|
123
|
+
others = __rest(_a, ["method", "query", "headers", "body", "timeout", "abort", "type"]);
|
|
124
|
+
url = (0, index_js_1.urlJoin)(_this.fixOrigin(url), query);
|
|
125
|
+
var timer = setTimeout(function () {
|
|
126
|
+
return abort.abort();
|
|
127
|
+
}, timeout);
|
|
128
|
+
var params = __assign({ url: url, method: method, headers: headers, body: method === "GET" ? null : (0, index_js_1.jsonToString)(body), timeout: timeout, timer: timer, signal: abort.signal, type: type }, others);
|
|
129
|
+
return (_c = (_b = _this.reqFn) === null || _b === void 0 ? void 0 : _b.call(_this, params)) !== null && _c !== void 0 ? _c : params;
|
|
130
|
+
};
|
|
131
|
+
_this.getDataByType = function (type, response) {
|
|
132
|
+
switch (type) {
|
|
133
|
+
case "text":
|
|
134
|
+
case "json":
|
|
135
|
+
case "blob":
|
|
136
|
+
case "formData":
|
|
137
|
+
case "arrayBuffer":
|
|
138
|
+
return response[type]();
|
|
139
|
+
default:
|
|
140
|
+
return response['json']();
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
_this.origin = origin !== null && origin !== void 0 ? origin : '';
|
|
144
|
+
return _this;
|
|
145
|
+
}
|
|
146
|
+
return RequestBase;
|
|
147
|
+
}(Interceptors);
|
|
148
|
+
exports.RequestBase = RequestBase;
|
|
149
|
+
var Request = function (_super) {
|
|
150
|
+
__extends(Request, _super);
|
|
151
|
+
function Request(origin) {
|
|
152
|
+
var _this = _super.call(this, origin) || this;
|
|
153
|
+
_this.fetch = function (_url, _opts) {
|
|
154
|
+
var _a = (0, index_js_1.defer)(),
|
|
155
|
+
promise = _a.promise,
|
|
156
|
+
resolve = _a.resolve,
|
|
157
|
+
reject = _a.reject;
|
|
158
|
+
var _b = _this.initFetchParams(_url, _opts),
|
|
159
|
+
url = _b.url,
|
|
160
|
+
opts = __rest(_b, ["url"]);
|
|
161
|
+
var signal = opts.signal,
|
|
162
|
+
timer = opts.timer;
|
|
163
|
+
var errorFn = function (err) {
|
|
164
|
+
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);
|
|
165
|
+
};
|
|
166
|
+
signal.addEventListener('abort', errorFn);
|
|
167
|
+
fetch(url, opts).then(function (response) {
|
|
168
|
+
if (response.status >= 200 && response.status < 300) {
|
|
169
|
+
return _this.getDataByType(opts.type, response);
|
|
170
|
+
}
|
|
171
|
+
return errorFn(response.statusText);
|
|
172
|
+
}).then(function (res) {
|
|
173
|
+
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);
|
|
174
|
+
}).catch(errorFn).finally(function () {
|
|
175
|
+
return clearTimeout(timer);
|
|
176
|
+
});
|
|
177
|
+
return promise;
|
|
178
|
+
};
|
|
179
|
+
_this.http = function (url, opts) {
|
|
180
|
+
var _a = (0, index_js_1.defer)(),
|
|
181
|
+
promise = _a.promise,
|
|
182
|
+
resolve = _a.resolve,
|
|
183
|
+
reject = _a.reject;
|
|
184
|
+
return promise;
|
|
185
|
+
};
|
|
186
|
+
_this.GET = function (url, query, _, opts) {
|
|
187
|
+
if (query === void 0) {
|
|
188
|
+
query = {};
|
|
189
|
+
}
|
|
190
|
+
return _this.request(url, __assign({ query: query, method: "GET" }, opts));
|
|
191
|
+
};
|
|
192
|
+
_this.POST = function (url, query, body, opts) {
|
|
193
|
+
if (query === void 0) {
|
|
194
|
+
query = {};
|
|
195
|
+
}
|
|
196
|
+
return _this.request(url, __assign({ query: query, method: "POST", body: body }, opts));
|
|
197
|
+
};
|
|
198
|
+
_this.PUT = function (url, query, body, opts) {
|
|
199
|
+
if (query === void 0) {
|
|
200
|
+
query = {};
|
|
201
|
+
}
|
|
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) {
|
|
206
|
+
query = {};
|
|
207
|
+
}
|
|
208
|
+
return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
|
|
209
|
+
};
|
|
210
|
+
_this.OPTION = function (url, query, body, opts) {
|
|
211
|
+
if (query === void 0) {
|
|
212
|
+
query = {};
|
|
213
|
+
}
|
|
214
|
+
return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
|
|
215
|
+
};
|
|
216
|
+
_this.request = _this.requestType();
|
|
217
|
+
return _this;
|
|
218
|
+
}
|
|
219
|
+
return Request;
|
|
220
|
+
}(RequestBase);
|
|
221
|
+
exports.Request = Request;
|
package/dist/common/types.d.ts
CHANGED
|
@@ -4,14 +4,18 @@ 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
|
+
export declare type IInstance<T> = {
|
|
11
|
+
_instance: Function;
|
|
12
|
+
} & T;
|
|
10
13
|
export declare type IDemoteArray<T> = Array<IDemoteArray<T> | T>;
|
|
11
14
|
export declare type IRandomNum = (min: number, max: number, bool?: boolean) => number;
|
|
12
15
|
export declare type IUrlSplit = (url: string) => IObject<any>;
|
|
13
16
|
export declare type IUrlJoin = (url: string, query: object) => string;
|
|
14
17
|
export declare type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
|
|
18
|
+
export declare type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
|
|
15
19
|
export declare type IGetValue = <T>(object: IObject<T> | IObject<T>[IKey], key: string, defaultValue?: any) => IObject<T>[IKey];
|
|
16
20
|
export declare type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>;
|
|
17
21
|
export declare type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U;
|
|
@@ -20,6 +24,10 @@ export declare type ICloneDeep = (target?: any) => any;
|
|
|
20
24
|
export declare type ICreateObjectVariable = (type: string, source?: any) => any;
|
|
21
25
|
export declare type ICreateObject = <T, U extends T>(source: T) => U;
|
|
22
26
|
export declare type IInherit = <T extends Function>(source: T, target?: Function) => Function;
|
|
27
|
+
export declare type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
|
|
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;
|
|
23
31
|
export declare type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
|
|
24
32
|
export declare type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
|
|
25
33
|
export declare type IDefer = () => IPromise;
|
|
@@ -39,4 +47,43 @@ export declare type IStopBubble = (e: Event) => void;
|
|
|
39
47
|
export declare type IStopDefault = (e: Event) => void;
|
|
40
48
|
export declare type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
|
|
41
49
|
export declare type IDispatchEvent = <T extends Document>(ele: T, data: any) => void;
|
|
50
|
+
export declare type IRequestParams<T> = T | IObject<any> | null;
|
|
51
|
+
export declare type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
|
|
52
|
+
export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION";
|
|
53
|
+
export declare type IRequestBody = IRequestParams<BodyInit>;
|
|
54
|
+
export declare type IRequestHeaders = IRequestParams<HeadersInit>;
|
|
55
|
+
export declare type IRequestFn = (url: string, params: IObject<any>, body: IRequestBody, opts: IRequestOptions) => Promise<any>;
|
|
56
|
+
export declare type IRequestOptions = {
|
|
57
|
+
method?: IRequestMethods;
|
|
58
|
+
query?: IRequestParams<IObject<any>>;
|
|
59
|
+
body?: IRequestBody;
|
|
60
|
+
headers?: IRequestHeaders;
|
|
61
|
+
abort?: AbortController;
|
|
62
|
+
timeout?: number;
|
|
63
|
+
[key: string]: any;
|
|
64
|
+
};
|
|
65
|
+
export declare type IInterceptors = {
|
|
66
|
+
use(type: "request" | "response" | "error", fn: Function): void;
|
|
67
|
+
get reqFn(): Function;
|
|
68
|
+
get resFn(): Function;
|
|
69
|
+
get errFn(): Function;
|
|
70
|
+
};
|
|
71
|
+
export declare type IRequestBase = {
|
|
72
|
+
origin: string;
|
|
73
|
+
fixOrigin: (fixStr: string) => string;
|
|
74
|
+
envDesc: () => "Window" | "Node";
|
|
75
|
+
chackUrl: (url: string) => boolean;
|
|
76
|
+
fetch: (url: string, opts: IRequestOptions) => Promise<any>;
|
|
77
|
+
http: (url: string, opts: IRequestOptions) => Promise<any>;
|
|
78
|
+
initFetchParams: (url: string, opts: IRequestOptions) => any;
|
|
79
|
+
getDataByType: (type: IDataType, response: Response) => Promise<any>;
|
|
80
|
+
};
|
|
81
|
+
export declare type IRequest = {
|
|
82
|
+
request: Function;
|
|
83
|
+
GET: IRequestFn;
|
|
84
|
+
POST: IRequestFn;
|
|
85
|
+
DELETE: IRequestFn;
|
|
86
|
+
PUT: IRequestFn;
|
|
87
|
+
OPTION: IRequestFn;
|
|
88
|
+
} & IRequestBase;
|
|
42
89
|
export {};
|
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
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,4 +1,4 @@
|
|
|
1
|
-
import { types } from "./
|
|
1
|
+
import { types } from "./index.js";
|
|
2
2
|
export var randomNum = function (min, max, bool) {
|
|
3
3
|
if (bool === void 0) {
|
|
4
4
|
bool = false;
|
|
@@ -18,7 +18,12 @@ export var urlSplit = function (url) {
|
|
|
18
18
|
return result;
|
|
19
19
|
};
|
|
20
20
|
export var urlJoin = function (url, query) {
|
|
21
|
-
|
|
21
|
+
if (query === void 0) {
|
|
22
|
+
query = {};
|
|
23
|
+
}
|
|
24
|
+
var queryObject = Object.keys(query);
|
|
25
|
+
if (queryObject.length === 0) return url;
|
|
26
|
+
var params = queryObject.map(function (i) {
|
|
22
27
|
return "".concat(i, "=").concat(query[i]);
|
|
23
28
|
});
|
|
24
29
|
return "".concat(url).concat(url.includes("?") ? "&" : '?').concat(params.join("&"));
|
|
@@ -32,4 +37,11 @@ export var getType = function (data) {
|
|
|
32
37
|
return types[key];
|
|
33
38
|
}
|
|
34
39
|
return type;
|
|
40
|
+
};
|
|
41
|
+
export var getTypeByList = function (data, whiteList) {
|
|
42
|
+
if (whiteList === void 0) {
|
|
43
|
+
whiteList = [];
|
|
44
|
+
}
|
|
45
|
+
var __type = getType(data);
|
|
46
|
+
return whiteList.indexOf(__type) > 0;
|
|
35
47
|
};
|
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/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;
|
package/dist/esm/function.d.ts
CHANGED
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export * from "./object";
|
|
2
|
-
export * from "./base";
|
|
3
|
-
export * from "./array";
|
|
4
|
-
export * from "./function";
|
|
5
|
-
export * from "./element";
|
|
6
|
-
export * from "./static";
|
|
7
|
-
export * from "./types";
|
|
8
|
-
export * from "
|
|
1
|
+
export * from "./object.js";
|
|
2
|
+
export * from "./base.js";
|
|
3
|
+
export * from "./array.js";
|
|
4
|
+
export * from "./function.js";
|
|
5
|
+
export * from "./element.js";
|
|
6
|
+
export * from "./static.js";
|
|
7
|
+
export * from "./types.js";
|
|
8
|
+
export * from "./request.js";
|
|
9
|
+
export * from "./event.js";
|
|
10
|
+
export * from "./storage.js";
|
package/dist/esm/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export * from "./object";
|
|
2
|
-
export * from "./base";
|
|
3
|
-
export * from "./array";
|
|
4
|
-
export * from "./function";
|
|
5
|
-
export * from "./element";
|
|
6
|
-
export * from "./static";
|
|
7
|
-
export * from "./types";
|
|
8
|
-
export * from "
|
|
1
|
+
export * from "./object.js";
|
|
2
|
+
export * from "./base.js";
|
|
3
|
+
export * from "./array.js";
|
|
4
|
+
export * from "./function.js";
|
|
5
|
+
export * from "./element.js";
|
|
6
|
+
export * from "./static.js";
|
|
7
|
+
export * from "./types.js";
|
|
8
|
+
export * from "./request.js";
|
|
9
|
+
export * from "./event.js";
|
|
10
|
+
export * from "./storage.js";
|
package/dist/esm/object.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IGetValue, ISetValue, IMixIn, ICloneDeep, ICreateObjectVariable, IEnumInversion, IInherit, ICreateObject } from "./
|
|
1
|
+
import { IGetValue, ISetValue, IMixIn, ICloneDeep, ICreateObjectVariable, IEnumInversion, IInherit, ICreateObject, IGetInstance, IClassDecorator, IStringToJson, IJsonToString } from "./index.js";
|
|
2
2
|
export declare const getValue: IGetValue;
|
|
3
3
|
export declare const setValue: ISetValue;
|
|
4
4
|
export declare const mixIn: IMixIn;
|
|
@@ -8,3 +8,7 @@ export declare const cloneDeep: ICloneDeep;
|
|
|
8
8
|
export declare const createObjectVariable: ICreateObjectVariable;
|
|
9
9
|
export declare const createObject: ICreateObject;
|
|
10
10
|
export declare const inherit: IInherit;
|
|
11
|
+
export declare const getInstance: IGetInstance;
|
|
12
|
+
export declare const classDecorator: IClassDecorator;
|
|
13
|
+
export declare const stringToJson: IStringToJson;
|
|
14
|
+
export declare const jsonToString: IJsonToString;
|
package/dist/esm/object.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
|
|
2
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
3
|
+
if (ar || !(i in from)) {
|
|
4
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
5
|
+
ar[i] = from[i];
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
9
|
+
};
|
|
10
|
+
import { getType, getTypeByList } from "./index.js";
|
|
2
11
|
export var getValue = function (object, key, defaultValue) {
|
|
3
12
|
if (defaultValue === void 0) {
|
|
4
13
|
defaultValue = '';
|
|
@@ -118,4 +127,42 @@ export var inherit = function (source, target) {
|
|
|
118
127
|
target.prototype.super = source;
|
|
119
128
|
target.prototype.constructor = target;
|
|
120
129
|
return target;
|
|
130
|
+
};
|
|
131
|
+
export var getInstance = function (classProto, overwrite) {
|
|
132
|
+
if (overwrite === void 0) {
|
|
133
|
+
overwrite = false;
|
|
134
|
+
}
|
|
135
|
+
var params = [];
|
|
136
|
+
for (var _i = 2; _i < arguments.length; _i++) {
|
|
137
|
+
params[_i - 2] = arguments[_i];
|
|
138
|
+
}
|
|
139
|
+
if (!classProto._instance || overwrite) {
|
|
140
|
+
classProto._instance = new (classProto.bind.apply(classProto, __spreadArray([void 0], params, false)))();
|
|
141
|
+
}
|
|
142
|
+
return classProto._instance;
|
|
143
|
+
};
|
|
144
|
+
export var classDecorator = function (params) {
|
|
145
|
+
return function (target) {
|
|
146
|
+
for (var key in params) {
|
|
147
|
+
if (!!!Reflect.has(target.prototype, key)) {
|
|
148
|
+
target.prototype[key] = params[key];
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
export var stringToJson = function (target) {
|
|
154
|
+
if (getType(target) !== "string") return {};
|
|
155
|
+
try {
|
|
156
|
+
return JSON.parse(target);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
return {};
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
export var jsonToString = function (target) {
|
|
162
|
+
if (!getTypeByList(target, ["array", "object", "set", "map"])) return "";
|
|
163
|
+
try {
|
|
164
|
+
return JSON.stringify(target);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
return "";
|
|
167
|
+
}
|
|
121
168
|
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { IRequest, IRequestBase, IInterceptors } from "./index.js";
|
|
2
|
+
export declare class Interceptors implements IInterceptors {
|
|
3
|
+
private requestSuccess;
|
|
4
|
+
private responseSuccess;
|
|
5
|
+
private error;
|
|
6
|
+
use(type: any, fn: any): this;
|
|
7
|
+
get reqFn(): Function;
|
|
8
|
+
get resFn(): Function;
|
|
9
|
+
get errFn(): Function;
|
|
10
|
+
}
|
|
11
|
+
export declare abstract class RequestBase extends Interceptors implements IRequestBase {
|
|
12
|
+
origin: string;
|
|
13
|
+
constructor(origin: any);
|
|
14
|
+
abstract fetch(url: any, opts: any): Promise<void>;
|
|
15
|
+
abstract http(url: any, opts: any): Promise<void>;
|
|
16
|
+
chackUrl: (url: any) => any;
|
|
17
|
+
fixOrigin: (fixStr: string) => string;
|
|
18
|
+
envDesc: () => "Window" | "Node";
|
|
19
|
+
requestType: () => (url: any, opts: any) => Promise<void>;
|
|
20
|
+
initFetchParams: (url: any, { method, query, headers, body, timeout, abort, type, ...others }: {
|
|
21
|
+
[x: string]: any;
|
|
22
|
+
method?: string;
|
|
23
|
+
query?: {};
|
|
24
|
+
headers?: {};
|
|
25
|
+
body?: any;
|
|
26
|
+
timeout?: number;
|
|
27
|
+
abort?: AbortController;
|
|
28
|
+
type?: string;
|
|
29
|
+
}) => any;
|
|
30
|
+
getDataByType: (type: any, response: any) => any;
|
|
31
|
+
}
|
|
32
|
+
export declare class Request extends RequestBase implements IRequest {
|
|
33
|
+
request: Function;
|
|
34
|
+
constructor(origin: any);
|
|
35
|
+
fetch: (_url: any, _opts: any) => Promise<void>;
|
|
36
|
+
http: (url: any, opts: any) => Promise<void>;
|
|
37
|
+
GET: (url: any, query: {}, _: any, opts: any) => any;
|
|
38
|
+
POST: (url: any, query: {}, body: any, opts: any) => any;
|
|
39
|
+
PUT: (url: any, query: {}, body: any, opts: any) => any;
|
|
40
|
+
DELETE: (url: any, query: {}, body: any, opts: any) => any;
|
|
41
|
+
OPTION: (url: any, query: {}, body: any, opts: any) => any;
|
|
42
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
var __extends = this && this.__extends || function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function (d, b) {
|
|
4
|
+
d.__proto__ = b;
|
|
5
|
+
} || function (d, b) {
|
|
6
|
+
for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
|
|
7
|
+
};
|
|
8
|
+
return extendStatics(d, b);
|
|
9
|
+
};
|
|
10
|
+
return function (d, b) {
|
|
11
|
+
if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() {
|
|
14
|
+
this.constructor = d;
|
|
15
|
+
}
|
|
16
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
17
|
+
};
|
|
18
|
+
}();
|
|
19
|
+
var __assign = this && this.__assign || function () {
|
|
20
|
+
__assign = Object.assign || function (t) {
|
|
21
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
22
|
+
s = arguments[i];
|
|
23
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
24
|
+
}
|
|
25
|
+
return t;
|
|
26
|
+
};
|
|
27
|
+
return __assign.apply(this, arguments);
|
|
28
|
+
};
|
|
29
|
+
var __rest = this && this.__rest || function (s, e) {
|
|
30
|
+
var t = {};
|
|
31
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
|
|
32
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
33
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
|
|
34
|
+
}
|
|
35
|
+
return t;
|
|
36
|
+
};
|
|
37
|
+
import { urlJoin, defer, jsonToString } from "./index.js";
|
|
38
|
+
var Interceptors = function () {
|
|
39
|
+
function Interceptors() {}
|
|
40
|
+
Interceptors.prototype.use = function (type, fn) {
|
|
41
|
+
switch (type) {
|
|
42
|
+
case "request":
|
|
43
|
+
this.requestSuccess = fn;
|
|
44
|
+
break;
|
|
45
|
+
case "response":
|
|
46
|
+
this.responseSuccess = fn;
|
|
47
|
+
break;
|
|
48
|
+
case "error":
|
|
49
|
+
this.error = fn;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
};
|
|
54
|
+
Object.defineProperty(Interceptors.prototype, "reqFn", {
|
|
55
|
+
get: function () {
|
|
56
|
+
return this.requestSuccess;
|
|
57
|
+
},
|
|
58
|
+
enumerable: false,
|
|
59
|
+
configurable: true
|
|
60
|
+
});
|
|
61
|
+
Object.defineProperty(Interceptors.prototype, "resFn", {
|
|
62
|
+
get: function () {
|
|
63
|
+
return this.responseSuccess;
|
|
64
|
+
},
|
|
65
|
+
enumerable: false,
|
|
66
|
+
configurable: true
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(Interceptors.prototype, "errFn", {
|
|
69
|
+
get: function () {
|
|
70
|
+
return this.error;
|
|
71
|
+
},
|
|
72
|
+
enumerable: false,
|
|
73
|
+
configurable: true
|
|
74
|
+
});
|
|
75
|
+
return Interceptors;
|
|
76
|
+
}();
|
|
77
|
+
export { Interceptors };
|
|
78
|
+
var RequestBase = function (_super) {
|
|
79
|
+
__extends(RequestBase, _super);
|
|
80
|
+
function RequestBase(origin) {
|
|
81
|
+
var _this = _super.call(this) || this;
|
|
82
|
+
_this.chackUrl = function (url) {
|
|
83
|
+
return url.startsWith('/');
|
|
84
|
+
};
|
|
85
|
+
_this.fixOrigin = function (fixStr) {
|
|
86
|
+
if (_this.chackUrl(fixStr)) return _this.origin + fixStr;
|
|
87
|
+
return fixStr;
|
|
88
|
+
};
|
|
89
|
+
_this.envDesc = function () {
|
|
90
|
+
if (Window) {
|
|
91
|
+
return "Window";
|
|
92
|
+
}
|
|
93
|
+
return "Node";
|
|
94
|
+
};
|
|
95
|
+
_this.requestType = function () {
|
|
96
|
+
switch (_this.envDesc()) {
|
|
97
|
+
case "Window":
|
|
98
|
+
return _this.fetch;
|
|
99
|
+
case "Node":
|
|
100
|
+
return _this.http;
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
_this.initFetchParams = function (url, _a) {
|
|
104
|
+
var _b, _c;
|
|
105
|
+
var _d = _a.method,
|
|
106
|
+
method = _d === void 0 ? "GET" : _d,
|
|
107
|
+
_e = _a.query,
|
|
108
|
+
query = _e === void 0 ? {} : _e,
|
|
109
|
+
_f = _a.headers,
|
|
110
|
+
headers = _f === void 0 ? {} : _f,
|
|
111
|
+
_g = _a.body,
|
|
112
|
+
body = _g === void 0 ? null : _g,
|
|
113
|
+
_h = _a.timeout,
|
|
114
|
+
timeout = _h === void 0 ? 30 * 1000 : _h,
|
|
115
|
+
_j = _a.abort,
|
|
116
|
+
abort = _j === void 0 ? new AbortController() : _j,
|
|
117
|
+
_k = _a.type,
|
|
118
|
+
type = _k === void 0 ? "json" : _k,
|
|
119
|
+
others = __rest(_a, ["method", "query", "headers", "body", "timeout", "abort", "type"]);
|
|
120
|
+
url = urlJoin(_this.fixOrigin(url), query);
|
|
121
|
+
var timer = setTimeout(function () {
|
|
122
|
+
return abort.abort();
|
|
123
|
+
}, timeout);
|
|
124
|
+
var params = __assign({ url: url, method: method, headers: headers, body: method === "GET" ? null : jsonToString(body), timeout: timeout, timer: timer, signal: abort.signal, type: type }, others);
|
|
125
|
+
return (_c = (_b = _this.reqFn) === null || _b === void 0 ? void 0 : _b.call(_this, params)) !== null && _c !== void 0 ? _c : params;
|
|
126
|
+
};
|
|
127
|
+
_this.getDataByType = function (type, response) {
|
|
128
|
+
switch (type) {
|
|
129
|
+
case "text":
|
|
130
|
+
case "json":
|
|
131
|
+
case "blob":
|
|
132
|
+
case "formData":
|
|
133
|
+
case "arrayBuffer":
|
|
134
|
+
return response[type]();
|
|
135
|
+
default:
|
|
136
|
+
return response['json']();
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
_this.origin = origin !== null && origin !== void 0 ? origin : '';
|
|
140
|
+
return _this;
|
|
141
|
+
}
|
|
142
|
+
return RequestBase;
|
|
143
|
+
}(Interceptors);
|
|
144
|
+
export { RequestBase };
|
|
145
|
+
var Request = function (_super) {
|
|
146
|
+
__extends(Request, _super);
|
|
147
|
+
function Request(origin) {
|
|
148
|
+
var _this = _super.call(this, origin) || this;
|
|
149
|
+
_this.fetch = function (_url, _opts) {
|
|
150
|
+
var _a = defer(),
|
|
151
|
+
promise = _a.promise,
|
|
152
|
+
resolve = _a.resolve,
|
|
153
|
+
reject = _a.reject;
|
|
154
|
+
var _b = _this.initFetchParams(_url, _opts),
|
|
155
|
+
url = _b.url,
|
|
156
|
+
opts = __rest(_b, ["url"]);
|
|
157
|
+
var signal = opts.signal,
|
|
158
|
+
timer = opts.timer;
|
|
159
|
+
var errorFn = function (err) {
|
|
160
|
+
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);
|
|
161
|
+
};
|
|
162
|
+
signal.addEventListener('abort', errorFn);
|
|
163
|
+
fetch(url, opts).then(function (response) {
|
|
164
|
+
if (response.status >= 200 && response.status < 300) {
|
|
165
|
+
return _this.getDataByType(opts.type, response);
|
|
166
|
+
}
|
|
167
|
+
return errorFn(response.statusText);
|
|
168
|
+
}).then(function (res) {
|
|
169
|
+
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);
|
|
170
|
+
}).catch(errorFn).finally(function () {
|
|
171
|
+
return clearTimeout(timer);
|
|
172
|
+
});
|
|
173
|
+
return promise;
|
|
174
|
+
};
|
|
175
|
+
_this.http = function (url, opts) {
|
|
176
|
+
var _a = defer(),
|
|
177
|
+
promise = _a.promise,
|
|
178
|
+
resolve = _a.resolve,
|
|
179
|
+
reject = _a.reject;
|
|
180
|
+
return promise;
|
|
181
|
+
};
|
|
182
|
+
_this.GET = function (url, query, _, opts) {
|
|
183
|
+
if (query === void 0) {
|
|
184
|
+
query = {};
|
|
185
|
+
}
|
|
186
|
+
return _this.request(url, __assign({ query: query, method: "GET" }, opts));
|
|
187
|
+
};
|
|
188
|
+
_this.POST = function (url, query, body, opts) {
|
|
189
|
+
if (query === void 0) {
|
|
190
|
+
query = {};
|
|
191
|
+
}
|
|
192
|
+
return _this.request(url, __assign({ query: query, method: "POST", body: body }, opts));
|
|
193
|
+
};
|
|
194
|
+
_this.PUT = function (url, query, body, opts) {
|
|
195
|
+
if (query === void 0) {
|
|
196
|
+
query = {};
|
|
197
|
+
}
|
|
198
|
+
return _this.request(url, __assign({ query: query, method: "PUT", body: body }, opts));
|
|
199
|
+
};
|
|
200
|
+
_this.DELETE = function (url, query, body, opts) {
|
|
201
|
+
if (query === void 0) {
|
|
202
|
+
query = {};
|
|
203
|
+
}
|
|
204
|
+
return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
|
|
205
|
+
};
|
|
206
|
+
_this.OPTION = function (url, query, body, opts) {
|
|
207
|
+
if (query === void 0) {
|
|
208
|
+
query = {};
|
|
209
|
+
}
|
|
210
|
+
return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
|
|
211
|
+
};
|
|
212
|
+
_this.request = _this.requestType();
|
|
213
|
+
return _this;
|
|
214
|
+
}
|
|
215
|
+
return Request;
|
|
216
|
+
}(RequestBase);
|
|
217
|
+
export { Request };
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -4,14 +4,18 @@ 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
|
+
export declare type IInstance<T> = {
|
|
11
|
+
_instance: Function;
|
|
12
|
+
} & T;
|
|
10
13
|
export declare type IDemoteArray<T> = Array<IDemoteArray<T> | T>;
|
|
11
14
|
export declare type IRandomNum = (min: number, max: number, bool?: boolean) => number;
|
|
12
15
|
export declare type IUrlSplit = (url: string) => IObject<any>;
|
|
13
16
|
export declare type IUrlJoin = (url: string, query: object) => string;
|
|
14
17
|
export declare type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
|
|
18
|
+
export declare type IGetTypeByList = (data: any, whiteList: string[]) => boolean;
|
|
15
19
|
export declare type IGetValue = <T>(object: IObject<T> | IObject<T>[IKey], key: string, defaultValue?: any) => IObject<T>[IKey];
|
|
16
20
|
export declare type ISetValue = <T>(object: IObject<T>, key: string, value?: any) => IObject<T>;
|
|
17
21
|
export declare type IMixIn = <U extends IObject<any>>(target?: U, source?: IObject<any>, overwrite?: boolean) => U;
|
|
@@ -20,6 +24,10 @@ export declare type ICloneDeep = (target?: any) => any;
|
|
|
20
24
|
export declare type ICreateObjectVariable = (type: string, source?: any) => any;
|
|
21
25
|
export declare type ICreateObject = <T, U extends T>(source: T) => U;
|
|
22
26
|
export declare type IInherit = <T extends Function>(source: T, target?: Function) => Function;
|
|
27
|
+
export declare type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
|
|
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;
|
|
23
31
|
export declare type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
|
|
24
32
|
export declare type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
|
|
25
33
|
export declare type IDefer = () => IPromise;
|
|
@@ -39,4 +47,43 @@ export declare type IStopBubble = (e: Event) => void;
|
|
|
39
47
|
export declare type IStopDefault = (e: Event) => void;
|
|
40
48
|
export declare type IRemoveHandler = <T extends Document>(ele: T, type: string, handler: (e: Event) => void) => void;
|
|
41
49
|
export declare type IDispatchEvent = <T extends Document>(ele: T, data: any) => void;
|
|
50
|
+
export declare type IRequestParams<T> = T | IObject<any> | null;
|
|
51
|
+
export declare type IDataType = "text" | "json" | "blob" | "formData" | "arrayBuffer";
|
|
52
|
+
export declare type IRequestMethods = "GET" | "POST" | "DELETE" | "PUT" | "OPTION";
|
|
53
|
+
export declare type IRequestBody = IRequestParams<BodyInit>;
|
|
54
|
+
export declare type IRequestHeaders = IRequestParams<HeadersInit>;
|
|
55
|
+
export declare type IRequestFn = (url: string, params: IObject<any>, body: IRequestBody, opts: IRequestOptions) => Promise<any>;
|
|
56
|
+
export declare type IRequestOptions = {
|
|
57
|
+
method?: IRequestMethods;
|
|
58
|
+
query?: IRequestParams<IObject<any>>;
|
|
59
|
+
body?: IRequestBody;
|
|
60
|
+
headers?: IRequestHeaders;
|
|
61
|
+
abort?: AbortController;
|
|
62
|
+
timeout?: number;
|
|
63
|
+
[key: string]: any;
|
|
64
|
+
};
|
|
65
|
+
export declare type IInterceptors = {
|
|
66
|
+
use(type: "request" | "response" | "error", fn: Function): void;
|
|
67
|
+
get reqFn(): Function;
|
|
68
|
+
get resFn(): Function;
|
|
69
|
+
get errFn(): Function;
|
|
70
|
+
};
|
|
71
|
+
export declare type IRequestBase = {
|
|
72
|
+
origin: string;
|
|
73
|
+
fixOrigin: (fixStr: string) => string;
|
|
74
|
+
envDesc: () => "Window" | "Node";
|
|
75
|
+
chackUrl: (url: string) => boolean;
|
|
76
|
+
fetch: (url: string, opts: IRequestOptions) => Promise<any>;
|
|
77
|
+
http: (url: string, opts: IRequestOptions) => Promise<any>;
|
|
78
|
+
initFetchParams: (url: string, opts: IRequestOptions) => any;
|
|
79
|
+
getDataByType: (type: IDataType, response: Response) => Promise<any>;
|
|
80
|
+
};
|
|
81
|
+
export declare type IRequest = {
|
|
82
|
+
request: Function;
|
|
83
|
+
GET: IRequestFn;
|
|
84
|
+
POST: IRequestFn;
|
|
85
|
+
DELETE: IRequestFn;
|
|
86
|
+
PUT: IRequestFn;
|
|
87
|
+
OPTION: IRequestFn;
|
|
88
|
+
} & IRequestBase;
|
|
42
89
|
export {};
|
package/index.html
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
7
|
+
<title>Document</title>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<script type="module">
|
|
11
|
+
import { Request } from "./dist/esm/request.js";
|
|
12
|
+
const resource = new Request("http://127.0.0.1:1024");
|
|
13
|
+
resource
|
|
14
|
+
.use("request", (params) => {
|
|
15
|
+
return params;
|
|
16
|
+
})
|
|
17
|
+
.use("response", (params) => {
|
|
18
|
+
return params;
|
|
19
|
+
})
|
|
20
|
+
.use("error", (error) => {
|
|
21
|
+
console.log(error)
|
|
22
|
+
return error;
|
|
23
|
+
});
|
|
24
|
+
resource
|
|
25
|
+
.GET(
|
|
26
|
+
"/getList",
|
|
27
|
+
{
|
|
28
|
+
name: "hello",
|
|
29
|
+
age: 22,
|
|
30
|
+
},
|
|
31
|
+
{},
|
|
32
|
+
{}
|
|
33
|
+
)
|
|
34
|
+
.then((res) => console.log(res));
|
|
35
|
+
</script>
|
|
36
|
+
</body>
|
|
37
|
+
</html>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "utils-lib-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "JavaScript工具函数,封装的一些常用的js函数",
|
|
5
5
|
"main": "./dist/common/index.js",
|
|
6
6
|
"types": "./dist/common/index.d.ts",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"scripts": {
|
|
13
13
|
"debug": "start cmd /k pnpm run build:hot & pnpm run node:hot",
|
|
14
14
|
"node:hot": "nodemon ./dist/common/index.js",
|
|
15
|
+
"build:hot:esm": "tsc -p tsconfig.es.json -w",
|
|
15
16
|
"build:hot": "tsc -p tsconfig.json -w",
|
|
16
17
|
"build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
|
|
17
18
|
"tsc:build": "rm -fr dist && tsc -p tsconfig.json && tsc -p tsconfig.es.json",
|
package/tsconfig.es.json
CHANGED
package/dist/common/fetch.d.ts
DELETED
|
File without changes
|
package/dist/common/fetch.js
DELETED
|
File without changes
|
package/dist/esm/fetch.d.ts
DELETED
|
File without changes
|
package/dist/esm/fetch.js
DELETED
|
File without changes
|