utils-lib-js 1.0.4 → 1.0.7

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.
@@ -1,5 +1,6 @@
1
- import { IRandomNum, IUrlSplit, IUrlJoin, IGetType, types } from "./index.js";
1
+ import { IRandomNum, IUrlSplit, IUrlJoin, IGetType, IGetTypeByList, types } from "./index.js";
2
2
  export declare const randomNum: IRandomNum;
3
3
  export declare const urlSplit: IUrlSplit;
4
4
  export declare const urlJoin: IUrlJoin;
5
5
  export declare const getType: IGetType<types>;
6
+ export declare const getTypeByList: IGetTypeByList;
@@ -1,7 +1,7 @@
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;
4
+ exports.getTypeByList = exports.getType = exports.urlJoin = exports.urlSplit = exports.randomNum = void 0;
5
5
  var index_js_1 = require("./index.js");
6
6
  var randomNum = function (min, max, bool) {
7
7
  if (bool === void 0) {
@@ -24,7 +24,12 @@ var urlSplit = function (url) {
24
24
  };
25
25
  exports.urlSplit = urlSplit;
26
26
  var urlJoin = function (url, query) {
27
- var params = Object.keys(query).map(function (i) {
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("&"));
@@ -40,4 +45,12 @@ var getType = function (data) {
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;
@@ -1,4 +1,4 @@
1
- import { IGetValue, ISetValue, IMixIn, ICloneDeep, ICreateObjectVariable, IEnumInversion, IInherit, ICreateObject, IGetInstance, IClassDecorator } from "./index.js";
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;
@@ -10,3 +10,5 @@ export declare const createObject: ICreateObject;
10
10
  export declare const inherit: IInherit;
11
11
  export declare const getInstance: IGetInstance;
12
12
  export declare const classDecorator: IClassDecorator;
13
+ export declare const stringToJson: IStringToJson;
14
+ export declare const jsonToString: IJsonToString;
@@ -10,7 +10,7 @@ var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
10
10
  return to.concat(ar || Array.prototype.slice.call(from));
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.classDecorator = exports.getInstance = exports.inherit = exports.createObject = exports.createObjectVariable = exports.cloneDeep = exports.isNotObject = exports.enumInversion = exports.mixIn = exports.setValue = exports.getValue = void 0;
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
14
  var index_js_1 = require("./index.js");
15
15
  var getValue = function (object, key, defaultValue) {
16
16
  if (defaultValue === void 0) {
@@ -164,4 +164,22 @@ var classDecorator = function (params) {
164
164
  }
165
165
  };
166
166
  };
167
- exports.classDecorator = classDecorator;
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;
@@ -1,15 +1,65 @@
1
- import { IRequest } from "./index.js";
2
- export declare class Request implements IRequest {
3
- origin: string;
4
- constructor();
5
- fixOrigin(origin: any): this;
6
- envDesc(): "Window" | "Node";
7
- ajax(url: any, opts: any): void;
8
- fetch(): Promise<Response>;
9
- http(): void;
10
- }
11
- export declare const GET: () => void;
12
- export declare const POST: () => void;
13
- export declare const PUT: () => void;
14
- export declare const DELETE: () => void;
15
- export declare const OPTION: () => void;
1
+ import { IRequest, IRequestBase, IRequestInit, IInterceptors } from "./index.js";
2
+ 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
+ declare abstract class RequestBase extends Interceptors implements IRequestBase {
12
+ readonly 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: string) => boolean;
17
+ fixOrigin: (fixStr: string) => string;
18
+ envDesc: () => "Window" | "Node";
19
+ errorFn: (reject: any) => (err: any) => any;
20
+ clearTimer: (opts: any) => any;
21
+ initAbort: (params: any) => any;
22
+ requestType: () => (url: any, opts: any) => Promise<void>;
23
+ getDataByType: (type: any, response: any) => any;
24
+ }
25
+ declare abstract class RequestInit extends RequestBase implements IRequestInit {
26
+ constructor(origin: any);
27
+ abstract fetch(url: any, opts: any): Promise<void>;
28
+ abstract http(url: any, opts: any): Promise<void>;
29
+ initDefaultParams: (url: any, { method, query, headers, body, timeout, controller, type, ...others }: {
30
+ [x: string]: any;
31
+ method?: string;
32
+ query?: {};
33
+ headers?: {};
34
+ body?: any;
35
+ timeout?: number;
36
+ controller?: AbortController;
37
+ type?: string;
38
+ }) => {
39
+ url: string;
40
+ method: string;
41
+ headers: {};
42
+ body: string;
43
+ timeout: number;
44
+ signal: AbortSignal;
45
+ controller: AbortController;
46
+ type: string;
47
+ timer: any;
48
+ };
49
+ initFetchParams: (url: any, opts: any) => any;
50
+ initHttpParams: (url: any, opts: any) => any;
51
+ }
52
+ export declare class Request extends RequestInit implements IRequest {
53
+ private request;
54
+ constructor(origin: any);
55
+ fetch: (_url: any, _opts: any) => Promise<void>;
56
+ http: (_url: any, _opts: any) => Promise<void>;
57
+ GET: (url: any, query: {}, _: any, opts: any) => any;
58
+ POST: (url: any, query: {}, body: any, opts: any) => any;
59
+ PUT: (url: any, query: {}, body: any, opts: any) => any;
60
+ DELETE: (url: any, query: {}, body: any, opts: any) => any;
61
+ OPTIONS: (url: any, query: {}, body: any, opts: any) => any;
62
+ HEAD: (url: any, query: {}, body: any, opts: any) => any;
63
+ PATCH: (url: any, query: {}, body: any, opts: any) => any;
64
+ }
65
+ export {};
@@ -1,65 +1,281 @@
1
1
  "use strict";
2
2
 
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.OPTION = exports.DELETE = exports.PUT = exports.POST = exports.GET = exports.Request = void 0;
5
- var index_js_1 = require("./index.js");
6
- var Request = function () {
7
- function Request() {}
8
- Request.prototype.fixOrigin = function (origin) {
9
- this.origin = origin;
10
- return this;
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
11
  };
12
- Request.prototype.envDesc = function () {
13
- if (Window) {
14
- return "Window";
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;
15
17
  }
16
- return "Node";
18
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
17
19
  };
18
- Request.prototype.ajax = function (url, opts) {
19
- var _a = (0, index_js_1.defer)(),
20
- promise = _a.promise,
21
- resolve = _a.resolve,
22
- reject = _a.reject;
23
- var _b = opts.method,
24
- method = _b === void 0 ? "GET" : _b,
25
- _c = opts.params,
26
- params = _c === void 0 ? {} : _c,
27
- _d = opts.body,
28
- body = _d === void 0 ? {} : _d,
29
- _e = opts.async,
30
- async = _e === void 0 ? true : _e,
31
- _f = opts.timeout,
32
- timeout = _f === void 0 ? 30 * 1000 : _f;
33
- var xhr = new XMLHttpRequest();
34
- switch (method) {
35
- case 'GET':
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 = 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
+ Interceptors.prototype.use = function (type, fn) {
47
+ switch (type) {
48
+ case "request":
49
+ this.requestSuccess = fn;
50
+ break;
51
+ case "response":
52
+ this.responseSuccess = fn;
36
53
  break;
37
- default:
54
+ case "error":
55
+ this.error = fn;
38
56
  break;
39
57
  }
40
- xhr.addEventListener('load', function () {
41
- if (xhr.readyState === 4 && xhr.status === 200) {
42
- try {} catch (error) {}
43
- } else {}
44
- });
45
- xhr.open(method, url, async);
46
- xhr.send(body);
47
- xhr.timeout = async && timeout;
48
- };
49
- Request.prototype.fetch = function () {
50
- return fetch("");
58
+ return this;
51
59
  };
52
- Request.prototype.http = function () {};
53
- return Request;
60
+ Object.defineProperty(Interceptors.prototype, "reqFn", {
61
+ get: function () {
62
+ return this.requestSuccess;
63
+ },
64
+ enumerable: false,
65
+ configurable: true
66
+ });
67
+ Object.defineProperty(Interceptors.prototype, "resFn", {
68
+ get: function () {
69
+ return this.responseSuccess;
70
+ },
71
+ enumerable: false,
72
+ configurable: true
73
+ });
74
+ Object.defineProperty(Interceptors.prototype, "errFn", {
75
+ get: function () {
76
+ return this.error;
77
+ },
78
+ enumerable: false,
79
+ configurable: true
80
+ });
81
+ return Interceptors;
54
82
  }();
55
- exports.Request = Request;
56
- var GET = function () {};
57
- exports.GET = GET;
58
- var POST = function () {};
59
- exports.POST = POST;
60
- var PUT = function () {};
61
- exports.PUT = PUT;
62
- var DELETE = function () {};
63
- exports.DELETE = DELETE;
64
- var OPTION = function () {};
65
- exports.OPTION = OPTION;
83
+ var RequestBase = function (_super) {
84
+ __extends(RequestBase, _super);
85
+ function RequestBase(origin) {
86
+ var _this = _super.call(this) || this;
87
+ _this.chackUrl = function (url) {
88
+ return url.startsWith('/');
89
+ };
90
+ _this.fixOrigin = function (fixStr) {
91
+ if (_this.chackUrl(fixStr)) return _this.origin + fixStr;
92
+ return fixStr;
93
+ };
94
+ _this.envDesc = function () {
95
+ if (typeof Window !== "undefined") {
96
+ return "Window";
97
+ }
98
+ return "Node";
99
+ };
100
+ _this.errorFn = function (reject) {
101
+ return function (err) {
102
+ 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
+ };
104
+ };
105
+ _this.clearTimer = function (opts) {
106
+ return !!opts.timer && (clearTimeout(opts.timer), opts.timer = null);
107
+ };
108
+ _this.initAbort = function (params) {
109
+ var controller = params.controller,
110
+ timer = params.timer,
111
+ timeout = params.timeout;
112
+ !!!timer && (params.timer = setTimeout(function () {
113
+ return controller.abort();
114
+ }, timeout));
115
+ return params;
116
+ };
117
+ _this.requestType = function () {
118
+ switch (_this.envDesc()) {
119
+ case "Window":
120
+ return _this.fetch;
121
+ case "Node":
122
+ return _this.http;
123
+ }
124
+ };
125
+ _this.getDataByType = function (type, response) {
126
+ switch (type) {
127
+ case "text":
128
+ case "json":
129
+ case "blob":
130
+ case "formData":
131
+ case "arrayBuffer":
132
+ return response[type]();
133
+ default:
134
+ return response['json']();
135
+ }
136
+ };
137
+ _this.origin = origin !== null && origin !== void 0 ? origin : '';
138
+ return _this;
139
+ }
140
+ return RequestBase;
141
+ }(Interceptors);
142
+ var RequestInit = function (_super) {
143
+ __extends(RequestInit, _super);
144
+ function RequestInit(origin) {
145
+ var _this = _super.call(this, origin) || this;
146
+ _this.initDefaultParams = function (url, _a) {
147
+ var _b = _a.method,
148
+ method = _b === void 0 ? "GET" : _b,
149
+ _c = _a.query,
150
+ query = _c === void 0 ? {} : _c,
151
+ _d = _a.headers,
152
+ headers = _d === void 0 ? {} : _d,
153
+ _e = _a.body,
154
+ body = _e === void 0 ? null : _e,
155
+ _f = _a.timeout,
156
+ timeout = _f === void 0 ? 30 * 1000 : _f,
157
+ _g = _a.controller,
158
+ controller = _g === void 0 ? new AbortController() : _g,
159
+ _h = _a.type,
160
+ type = _h === void 0 ? "json" : _h,
161
+ others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
162
+ 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);
163
+ };
164
+ _this.initFetchParams = function (url, opts) {
165
+ var _a, _b;
166
+ var params = _this.initAbort(_this.initDefaultParams(url, opts));
167
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, params)) !== null && _b !== void 0 ? _b : params;
168
+ };
169
+ _this.initHttpParams = function (url, opts) {
170
+ var _a, _b;
171
+ var params = _this.initAbort(_this.initDefaultParams(url, opts));
172
+ var options = (0, node_url_1.parse)(params.url, true);
173
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : params;
174
+ };
175
+ return _this;
176
+ }
177
+ return RequestInit;
178
+ }(RequestBase);
179
+ var Request = function (_super) {
180
+ __extends(Request, _super);
181
+ function Request(origin) {
182
+ var _this = _super.call(this, origin) || this;
183
+ _this.fetch = function (_url, _opts) {
184
+ var _a = (0, index_js_1.defer)(),
185
+ promise = _a.promise,
186
+ resolve = _a.resolve,
187
+ reject = _a.reject;
188
+ var _b = _this.initFetchParams(_url, _opts),
189
+ url = _b.url,
190
+ opts = __rest(_b, ["url"]);
191
+ var signal = opts.signal;
192
+ signal.addEventListener('abort', function () {
193
+ return _this.errorFn(reject);
194
+ });
195
+ fetch(url, opts).then(function (response) {
196
+ if (response.status >= 200 && response.status < 300) {
197
+ return _this.getDataByType(opts.type, response);
198
+ }
199
+ return _this.errorFn(reject);
200
+ }).then(function (res) {
201
+ 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);
202
+ }).catch(_this.errorFn(reject)).finally(function () {
203
+ return _this.clearTimer(opts);
204
+ });
205
+ return promise;
206
+ };
207
+ _this.http = function (_url, _opts) {
208
+ var _a = (0, index_js_1.defer)(),
209
+ promise = _a.promise,
210
+ resolve = _a.resolve,
211
+ reject = _a.reject;
212
+ var params = _this.initHttpParams(_url, _opts);
213
+ var signal = params.signal;
214
+ var req = (0, node_http_1.request)(params, function (response) {
215
+ if (response.statusCode >= 200 && response.statusCode < 300) {
216
+ var data_1 = "";
217
+ response.setEncoding('utf8');
218
+ response.on('data', function (chunk) {
219
+ return data_1 += chunk;
220
+ });
221
+ return response.on("end", function () {
222
+ 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);
223
+ });
224
+ }
225
+ return _this.errorFn(reject)(response === null || response === void 0 ? void 0 : response.statusMessage);
226
+ });
227
+ signal.addEventListener('abort', function () {
228
+ return _this.errorFn(reject)(req.destroy(new Error('request timeout')));
229
+ });
230
+ req.on('error', _this.errorFn(reject));
231
+ req.end();
232
+ return promise;
233
+ };
234
+ _this.GET = function (url, query, _, opts) {
235
+ if (query === void 0) {
236
+ query = {};
237
+ }
238
+ return _this.request(url, __assign({ query: query, method: "GET" }, opts));
239
+ };
240
+ _this.POST = function (url, query, body, opts) {
241
+ if (query === void 0) {
242
+ query = {};
243
+ }
244
+ return _this.request(url, __assign({ query: query, method: "POST", body: body }, opts));
245
+ };
246
+ _this.PUT = function (url, query, body, opts) {
247
+ if (query === void 0) {
248
+ query = {};
249
+ }
250
+ return _this.request(url, __assign({ query: query, method: "PUT", body: body }, opts));
251
+ };
252
+ _this.DELETE = function (url, query, body, opts) {
253
+ if (query === void 0) {
254
+ query = {};
255
+ }
256
+ return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
257
+ };
258
+ _this.OPTIONS = function (url, query, body, opts) {
259
+ if (query === void 0) {
260
+ query = {};
261
+ }
262
+ return _this.request(url, __assign({ query: query, method: "OPTIONS", body: body }, opts));
263
+ };
264
+ _this.HEAD = function (url, query, body, opts) {
265
+ if (query === void 0) {
266
+ query = {};
267
+ }
268
+ return _this.request(url, __assign({ query: query, method: "HEAD", body: body }, opts));
269
+ };
270
+ _this.PATCH = function (url, query, body, opts) {
271
+ if (query === void 0) {
272
+ query = {};
273
+ }
274
+ return _this.request(url, __assign({ query: query, method: "PATCH", body: body }, opts));
275
+ };
276
+ _this.request = _this.requestType();
277
+ return _this;
278
+ }
279
+ return Request;
280
+ }(RequestInit);
281
+ exports.Request = Request;
@@ -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 IGetValue = <T>(object: IObject<T> | IObject<T>[IKey], key: string, defaultValue?: any) => IObject<T>[IKey];
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,11 +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 IRequest = {
48
- origin: string;
49
- fixOrigin: <T = string>(origin: T) => IRequest;
50
- envDesc: () => "Window" | "Node";
51
- fetch: () => Promise<Response>;
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>;
52
87
  };
53
- export declare type IGet = (url: string, params: IObject<any>) => Promise<void>;
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;
54
102
  export {};
@@ -1,5 +1,6 @@
1
- import { IRandomNum, IUrlSplit, IUrlJoin, IGetType, types } from "./index.js";
1
+ import { IRandomNum, IUrlSplit, IUrlJoin, IGetType, IGetTypeByList, types } from "./index.js";
2
2
  export declare const randomNum: IRandomNum;
3
3
  export declare const urlSplit: IUrlSplit;
4
4
  export declare const urlJoin: IUrlJoin;
5
5
  export declare const getType: IGetType<types>;
6
+ export declare const getTypeByList: IGetTypeByList;
package/dist/esm/base.js CHANGED
@@ -18,7 +18,12 @@ export var urlSplit = function (url) {
18
18
  return result;
19
19
  };
20
20
  export var urlJoin = function (url, query) {
21
- var params = Object.keys(query).map(function (i) {
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
  };
@@ -1,4 +1,4 @@
1
- import { IGetValue, ISetValue, IMixIn, ICloneDeep, ICreateObjectVariable, IEnumInversion, IInherit, ICreateObject, IGetInstance, IClassDecorator } from "./index.js";
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;
@@ -10,3 +10,5 @@ export declare const createObject: ICreateObject;
10
10
  export declare const inherit: IInherit;
11
11
  export declare const getInstance: IGetInstance;
12
12
  export declare const classDecorator: IClassDecorator;
13
+ export declare const stringToJson: IStringToJson;
14
+ export declare const jsonToString: IJsonToString;
@@ -7,7 +7,7 @@ var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
7
7
  }
8
8
  return to.concat(ar || Array.prototype.slice.call(from));
9
9
  };
10
- import { getType } from "./index.js";
10
+ import { getType, getTypeByList } from "./index.js";
11
11
  export var getValue = function (object, key, defaultValue) {
12
12
  if (defaultValue === void 0) {
13
13
  defaultValue = '';
@@ -149,4 +149,20 @@ export var classDecorator = function (params) {
149
149
  }
150
150
  }
151
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
+ }
152
168
  };
@@ -1,15 +1,65 @@
1
- import { IRequest } from "./index.js";
2
- export declare class Request implements IRequest {
3
- origin: string;
4
- constructor();
5
- fixOrigin(origin: any): this;
6
- envDesc(): "Window" | "Node";
7
- ajax(url: any, opts: any): void;
8
- fetch(): Promise<Response>;
9
- http(): void;
10
- }
11
- export declare const GET: () => void;
12
- export declare const POST: () => void;
13
- export declare const PUT: () => void;
14
- export declare const DELETE: () => void;
15
- export declare const OPTION: () => void;
1
+ import { IRequest, IRequestBase, IRequestInit, IInterceptors } from "./index.js";
2
+ 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
+ declare abstract class RequestBase extends Interceptors implements IRequestBase {
12
+ readonly 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: string) => boolean;
17
+ fixOrigin: (fixStr: string) => string;
18
+ envDesc: () => "Window" | "Node";
19
+ errorFn: (reject: any) => (err: any) => any;
20
+ clearTimer: (opts: any) => any;
21
+ initAbort: (params: any) => any;
22
+ requestType: () => (url: any, opts: any) => Promise<void>;
23
+ getDataByType: (type: any, response: any) => any;
24
+ }
25
+ declare abstract class RequestInit extends RequestBase implements IRequestInit {
26
+ constructor(origin: any);
27
+ abstract fetch(url: any, opts: any): Promise<void>;
28
+ abstract http(url: any, opts: any): Promise<void>;
29
+ initDefaultParams: (url: any, { method, query, headers, body, timeout, controller, type, ...others }: {
30
+ [x: string]: any;
31
+ method?: string;
32
+ query?: {};
33
+ headers?: {};
34
+ body?: any;
35
+ timeout?: number;
36
+ controller?: AbortController;
37
+ type?: string;
38
+ }) => {
39
+ url: string;
40
+ method: string;
41
+ headers: {};
42
+ body: string;
43
+ timeout: number;
44
+ signal: AbortSignal;
45
+ controller: AbortController;
46
+ type: string;
47
+ timer: any;
48
+ };
49
+ initFetchParams: (url: any, opts: any) => any;
50
+ initHttpParams: (url: any, opts: any) => any;
51
+ }
52
+ export declare class Request extends RequestInit implements IRequest {
53
+ private request;
54
+ constructor(origin: any);
55
+ fetch: (_url: any, _opts: any) => Promise<void>;
56
+ http: (_url: any, _opts: any) => Promise<void>;
57
+ GET: (url: any, query: {}, _: any, opts: any) => any;
58
+ POST: (url: any, query: {}, body: any, opts: any) => any;
59
+ PUT: (url: any, query: {}, body: any, opts: any) => any;
60
+ DELETE: (url: any, query: {}, body: any, opts: any) => any;
61
+ OPTIONS: (url: any, query: {}, body: any, opts: any) => any;
62
+ HEAD: (url: any, query: {}, body: any, opts: any) => any;
63
+ PATCH: (url: any, query: {}, body: any, opts: any) => any;
64
+ }
65
+ export {};
@@ -1,56 +1,277 @@
1
- import { defer } from "./index.js";
2
- var Request = function () {
3
- function Request() {}
4
- Request.prototype.fixOrigin = function (origin) {
5
- this.origin = origin;
6
- return this;
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);
7
9
  };
8
- Request.prototype.envDesc = function () {
9
- if (Window) {
10
- return "Window";
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;
11
15
  }
12
- return "Node";
16
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
17
  };
14
- Request.prototype.ajax = function (url, opts) {
15
- var _a = defer(),
16
- promise = _a.promise,
17
- resolve = _a.resolve,
18
- reject = _a.reject;
19
- var _b = opts.method,
20
- method = _b === void 0 ? "GET" : _b,
21
- _c = opts.params,
22
- params = _c === void 0 ? {} : _c,
23
- _d = opts.body,
24
- body = _d === void 0 ? {} : _d,
25
- _e = opts.async,
26
- async = _e === void 0 ? true : _e,
27
- _f = opts.timeout,
28
- timeout = _f === void 0 ? 30 * 1000 : _f;
29
- var xhr = new XMLHttpRequest();
30
- switch (method) {
31
- case 'GET':
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
+ import { request } from "node:http";
39
+ import { parse } from "node:url";
40
+ var Interceptors = function () {
41
+ function Interceptors() {}
42
+ Interceptors.prototype.use = function (type, fn) {
43
+ switch (type) {
44
+ case "request":
45
+ this.requestSuccess = fn;
46
+ break;
47
+ case "response":
48
+ this.responseSuccess = fn;
32
49
  break;
33
- default:
50
+ case "error":
51
+ this.error = fn;
34
52
  break;
35
53
  }
36
- xhr.addEventListener('load', function () {
37
- if (xhr.readyState === 4 && xhr.status === 200) {
38
- try {} catch (error) {}
39
- } else {}
40
- });
41
- xhr.open(method, url, async);
42
- xhr.send(body);
43
- xhr.timeout = async && timeout;
44
- };
45
- Request.prototype.fetch = function () {
46
- return fetch("");
54
+ return this;
47
55
  };
48
- Request.prototype.http = function () {};
49
- return Request;
56
+ Object.defineProperty(Interceptors.prototype, "reqFn", {
57
+ get: function () {
58
+ return this.requestSuccess;
59
+ },
60
+ enumerable: false,
61
+ configurable: true
62
+ });
63
+ Object.defineProperty(Interceptors.prototype, "resFn", {
64
+ get: function () {
65
+ return this.responseSuccess;
66
+ },
67
+ enumerable: false,
68
+ configurable: true
69
+ });
70
+ Object.defineProperty(Interceptors.prototype, "errFn", {
71
+ get: function () {
72
+ return this.error;
73
+ },
74
+ enumerable: false,
75
+ configurable: true
76
+ });
77
+ return Interceptors;
50
78
  }();
51
- export { Request };
52
- export var GET = function () {};
53
- export var POST = function () {};
54
- export var PUT = function () {};
55
- export var DELETE = function () {};
56
- export var OPTION = function () {};
79
+ var RequestBase = function (_super) {
80
+ __extends(RequestBase, _super);
81
+ function RequestBase(origin) {
82
+ var _this = _super.call(this) || this;
83
+ _this.chackUrl = function (url) {
84
+ return url.startsWith('/');
85
+ };
86
+ _this.fixOrigin = function (fixStr) {
87
+ if (_this.chackUrl(fixStr)) return _this.origin + fixStr;
88
+ return fixStr;
89
+ };
90
+ _this.envDesc = function () {
91
+ if (typeof Window !== "undefined") {
92
+ return "Window";
93
+ }
94
+ return "Node";
95
+ };
96
+ _this.errorFn = function (reject) {
97
+ return function (err) {
98
+ 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);
99
+ };
100
+ };
101
+ _this.clearTimer = function (opts) {
102
+ return !!opts.timer && (clearTimeout(opts.timer), opts.timer = null);
103
+ };
104
+ _this.initAbort = function (params) {
105
+ var controller = params.controller,
106
+ timer = params.timer,
107
+ timeout = params.timeout;
108
+ !!!timer && (params.timer = setTimeout(function () {
109
+ return controller.abort();
110
+ }, timeout));
111
+ return params;
112
+ };
113
+ _this.requestType = function () {
114
+ switch (_this.envDesc()) {
115
+ case "Window":
116
+ return _this.fetch;
117
+ case "Node":
118
+ return _this.http;
119
+ }
120
+ };
121
+ _this.getDataByType = function (type, response) {
122
+ switch (type) {
123
+ case "text":
124
+ case "json":
125
+ case "blob":
126
+ case "formData":
127
+ case "arrayBuffer":
128
+ return response[type]();
129
+ default:
130
+ return response['json']();
131
+ }
132
+ };
133
+ _this.origin = origin !== null && origin !== void 0 ? origin : '';
134
+ return _this;
135
+ }
136
+ return RequestBase;
137
+ }(Interceptors);
138
+ var RequestInit = function (_super) {
139
+ __extends(RequestInit, _super);
140
+ function RequestInit(origin) {
141
+ var _this = _super.call(this, origin) || this;
142
+ _this.initDefaultParams = function (url, _a) {
143
+ var _b = _a.method,
144
+ method = _b === void 0 ? "GET" : _b,
145
+ _c = _a.query,
146
+ query = _c === void 0 ? {} : _c,
147
+ _d = _a.headers,
148
+ headers = _d === void 0 ? {} : _d,
149
+ _e = _a.body,
150
+ body = _e === void 0 ? null : _e,
151
+ _f = _a.timeout,
152
+ timeout = _f === void 0 ? 30 * 1000 : _f,
153
+ _g = _a.controller,
154
+ controller = _g === void 0 ? new AbortController() : _g,
155
+ _h = _a.type,
156
+ type = _h === void 0 ? "json" : _h,
157
+ others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
158
+ return __assign({ url: urlJoin(_this.fixOrigin(url), query), method: method, 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);
159
+ };
160
+ _this.initFetchParams = function (url, opts) {
161
+ var _a, _b;
162
+ var params = _this.initAbort(_this.initDefaultParams(url, opts));
163
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, params)) !== null && _b !== void 0 ? _b : params;
164
+ };
165
+ _this.initHttpParams = function (url, opts) {
166
+ var _a, _b;
167
+ var params = _this.initAbort(_this.initDefaultParams(url, opts));
168
+ var options = parse(params.url, true);
169
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : params;
170
+ };
171
+ return _this;
172
+ }
173
+ return RequestInit;
174
+ }(RequestBase);
175
+ var Request = function (_super) {
176
+ __extends(Request, _super);
177
+ function Request(origin) {
178
+ var _this = _super.call(this, origin) || this;
179
+ _this.fetch = function (_url, _opts) {
180
+ var _a = defer(),
181
+ promise = _a.promise,
182
+ resolve = _a.resolve,
183
+ reject = _a.reject;
184
+ var _b = _this.initFetchParams(_url, _opts),
185
+ url = _b.url,
186
+ opts = __rest(_b, ["url"]);
187
+ var signal = opts.signal;
188
+ signal.addEventListener('abort', function () {
189
+ return _this.errorFn(reject);
190
+ });
191
+ fetch(url, opts).then(function (response) {
192
+ if (response.status >= 200 && response.status < 300) {
193
+ return _this.getDataByType(opts.type, response);
194
+ }
195
+ return _this.errorFn(reject);
196
+ }).then(function (res) {
197
+ 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);
198
+ }).catch(_this.errorFn(reject)).finally(function () {
199
+ return _this.clearTimer(opts);
200
+ });
201
+ return promise;
202
+ };
203
+ _this.http = function (_url, _opts) {
204
+ var _a = defer(),
205
+ promise = _a.promise,
206
+ resolve = _a.resolve,
207
+ reject = _a.reject;
208
+ var params = _this.initHttpParams(_url, _opts);
209
+ var signal = params.signal;
210
+ var req = request(params, function (response) {
211
+ if (response.statusCode >= 200 && response.statusCode < 300) {
212
+ var data_1 = "";
213
+ response.setEncoding('utf8');
214
+ response.on('data', function (chunk) {
215
+ return data_1 += chunk;
216
+ });
217
+ return response.on("end", function () {
218
+ 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);
219
+ });
220
+ }
221
+ return _this.errorFn(reject)(response === null || response === void 0 ? void 0 : response.statusMessage);
222
+ });
223
+ signal.addEventListener('abort', function () {
224
+ return _this.errorFn(reject)(req.destroy(new Error('request timeout')));
225
+ });
226
+ req.on('error', _this.errorFn(reject));
227
+ req.end();
228
+ return promise;
229
+ };
230
+ _this.GET = function (url, query, _, opts) {
231
+ if (query === void 0) {
232
+ query = {};
233
+ }
234
+ return _this.request(url, __assign({ query: query, method: "GET" }, opts));
235
+ };
236
+ _this.POST = function (url, query, body, opts) {
237
+ if (query === void 0) {
238
+ query = {};
239
+ }
240
+ return _this.request(url, __assign({ query: query, method: "POST", body: body }, opts));
241
+ };
242
+ _this.PUT = function (url, query, body, opts) {
243
+ if (query === void 0) {
244
+ query = {};
245
+ }
246
+ return _this.request(url, __assign({ query: query, method: "PUT", body: body }, opts));
247
+ };
248
+ _this.DELETE = function (url, query, body, opts) {
249
+ if (query === void 0) {
250
+ query = {};
251
+ }
252
+ return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
253
+ };
254
+ _this.OPTIONS = function (url, query, body, opts) {
255
+ if (query === void 0) {
256
+ query = {};
257
+ }
258
+ return _this.request(url, __assign({ query: query, method: "OPTIONS", body: body }, opts));
259
+ };
260
+ _this.HEAD = function (url, query, body, opts) {
261
+ if (query === void 0) {
262
+ query = {};
263
+ }
264
+ return _this.request(url, __assign({ query: query, method: "HEAD", body: body }, opts));
265
+ };
266
+ _this.PATCH = function (url, query, body, opts) {
267
+ if (query === void 0) {
268
+ query = {};
269
+ }
270
+ return _this.request(url, __assign({ query: query, method: "PATCH", body: body }, opts));
271
+ };
272
+ _this.request = _this.requestType();
273
+ return _this;
274
+ }
275
+ return Request;
276
+ }(RequestInit);
277
+ export { Request };
@@ -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 IGetValue = <T>(object: IObject<T> | IObject<T>[IKey], key: string, defaultValue?: any) => IObject<T>[IKey];
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,11 +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 IRequest = {
48
- origin: string;
49
- fixOrigin: <T = string>(origin: T) => IRequest;
50
- envDesc: () => "Window" | "Node";
51
- fetch: () => Promise<Response>;
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>;
52
87
  };
53
- export declare type IGet = (url: string, params: IObject<any>) => Promise<void>;
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;
54
102
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-lib-js",
3
- "version": "1.0.4",
3
+ "version": "1.0.7",
4
4
  "description": "JavaScript工具函数,封装的一些常用的js函数",
5
5
  "main": "./dist/common/index.js",
6
6
  "types": "./dist/common/index.d.ts",
@@ -10,10 +10,11 @@
10
10
  "require": "./dist/common/index.js"
11
11
  },
12
12
  "scripts": {
13
- "debug": "start cmd /k pnpm run build:hot & pnpm run node:hot",
14
- "node:hot": "nodemon ./dist/common/index.js",
13
+ "debug:esm": "start cmd /k pnpm run build:hot:esm",
14
+ "debug:node": "start cmd /k pnpm run build:hot:node & pnpm run node:hot",
15
+ "node:hot": "nodemon ./example/server.js",
15
16
  "build:hot:esm": "tsc -p tsconfig.es.json -w",
16
- "build:hot": "tsc -p tsconfig.json -w",
17
+ "build:hot:node": "tsc -p tsconfig.json -w",
17
18
  "build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
18
19
  "tsc:build": "rm -fr dist && tsc -p tsconfig.json && tsc -p tsconfig.es.json",
19
20
  "publish": "pnpm run build && pnpm publish",
@@ -25,13 +26,18 @@
25
26
  "type": "git",
26
27
  "url": "https://gitee.com/DieHunter/utils-lib-js.git"
27
28
  },
28
- "keywords": ["utils", "tools", "lib"],
29
+ "keywords": [
30
+ "utils",
31
+ "tools",
32
+ "lib"
33
+ ],
29
34
  "author": "",
30
35
  "license": "ISC",
31
36
  "dependencies": {
32
37
  "event-message-center": "^1.0.16"
33
38
  },
34
39
  "devDependencies": {
40
+ "@types/node": "^18.7.1",
35
41
  "babel-cli": "^6.26.0"
36
42
  }
37
43
  }
package/index.html DELETED
@@ -1,15 +0,0 @@
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
- console.log(Request);
13
- </script>
14
- </body>
15
- </html>