utils-lib-js 1.0.3 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/common/array.d.ts +1 -1
  2. package/dist/common/array.js +14 -25
  3. package/dist/common/base.d.ts +2 -2
  4. package/dist/common/base.js +49 -43
  5. package/dist/common/element.d.ts +1 -1
  6. package/dist/common/element.js +13 -21
  7. package/dist/common/event.d.ts +1 -1
  8. package/dist/common/event.js +46 -43
  9. package/dist/common/function.d.ts +1 -1
  10. package/dist/common/function.js +60 -66
  11. package/dist/common/index.d.ts +10 -11
  12. package/dist/common/index.js +26 -30
  13. package/dist/common/object.d.ts +3 -1
  14. package/dist/common/object.js +175 -167
  15. package/dist/common/request.d.ts +65 -11
  16. package/dist/common/request.js +225 -26
  17. package/dist/common/static.js +17 -18
  18. package/dist/common/storage.js +36 -34
  19. package/dist/common/types.d.ts +57 -8
  20. package/dist/common/types.js +2 -3
  21. package/dist/esm/array.d.ts +1 -1
  22. package/dist/esm/array.js +8 -18
  23. package/dist/esm/base.d.ts +2 -2
  24. package/dist/esm/base.js +41 -35
  25. package/dist/esm/element.d.ts +1 -1
  26. package/dist/esm/element.js +9 -16
  27. package/dist/esm/event.d.ts +1 -1
  28. package/dist/esm/event.js +38 -34
  29. package/dist/esm/function.d.ts +1 -1
  30. package/dist/esm/function.js +53 -58
  31. package/dist/esm/index.d.ts +10 -11
  32. package/dist/esm/index.js +10 -11
  33. package/dist/esm/object.d.ts +3 -1
  34. package/dist/esm/object.js +159 -152
  35. package/dist/esm/request.d.ts +56 -9
  36. package/dist/esm/request.js +199 -18
  37. package/dist/esm/static.js +14 -14
  38. package/dist/esm/storage.js +30 -27
  39. package/dist/esm/types.d.ts +47 -6
  40. package/dist/esm/types.js +1 -1
  41. package/package.json +12 -5
  42. package/tsconfig.es.json +3 -7
@@ -1,18 +1,199 @@
1
- var Request = function () {
2
- function Request() {}
3
- Request.prototype.fixOrigin = function (origin) {
4
- return origin;
5
- };
6
- Request.prototype.envDesc = function () {
7
- if (Window) {
8
- return "Window";
9
- }
10
- return "Node";
11
- };
12
- return Request;
13
- }();
14
- export { Request };
15
- export var GET = function () {};
16
- export var POST = function () {};
17
- export var PUT = function () {};
18
- export var DELETE = function () {};
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = function (d, b) {
3
+ extendStatics = Object.setPrototypeOf ||
4
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
+ return extendStatics(d, b);
7
+ };
8
+ return function (d, b) {
9
+ if (typeof b !== "function" && b !== null)
10
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
+ extendStatics(d, b);
12
+ function __() { this.constructor = d; }
13
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
+ };
15
+ })();
16
+ var __assign = (this && this.__assign) || function () {
17
+ __assign = Object.assign || function(t) {
18
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
19
+ s = arguments[i];
20
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
21
+ t[p] = s[p];
22
+ }
23
+ return t;
24
+ };
25
+ return __assign.apply(this, arguments);
26
+ };
27
+ var __rest = (this && this.__rest) || function (s, e) {
28
+ var t = {};
29
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
30
+ t[p] = s[p];
31
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
32
+ 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]))
34
+ t[p[i]] = s[p[i]];
35
+ }
36
+ return t;
37
+ };
38
+ import { urlJoin, defer, jsonToString } from "./index.js";
39
+ import { request } from "node:http";
40
+ var Interceptors = (function () {
41
+ function Interceptors() {
42
+ }
43
+ Interceptors.prototype.use = function (type, fn) {
44
+ switch (type) {
45
+ case "request":
46
+ this.requestSuccess = fn;
47
+ break;
48
+ case "response":
49
+ this.responseSuccess = fn;
50
+ break;
51
+ case "error":
52
+ this.error = fn;
53
+ break;
54
+ }
55
+ return this;
56
+ };
57
+ Object.defineProperty(Interceptors.prototype, "reqFn", {
58
+ get: function () {
59
+ return this.requestSuccess;
60
+ },
61
+ enumerable: false,
62
+ configurable: true
63
+ });
64
+ Object.defineProperty(Interceptors.prototype, "resFn", {
65
+ get: function () {
66
+ return this.responseSuccess;
67
+ },
68
+ enumerable: false,
69
+ configurable: true
70
+ });
71
+ Object.defineProperty(Interceptors.prototype, "errFn", {
72
+ get: function () {
73
+ return this.error;
74
+ },
75
+ enumerable: false,
76
+ configurable: true
77
+ });
78
+ return Interceptors;
79
+ }());
80
+ export { Interceptors };
81
+ var RequestBase = (function (_super) {
82
+ __extends(RequestBase, _super);
83
+ function RequestBase(origin) {
84
+ var _this = _super.call(this) || this;
85
+ _this.chackUrl = function (url) {
86
+ return url.startsWith('/');
87
+ };
88
+ _this.fixOrigin = function (fixStr) {
89
+ if (_this.chackUrl(fixStr))
90
+ return _this.origin + fixStr;
91
+ return fixStr;
92
+ };
93
+ _this.envDesc = function () {
94
+ if (typeof Window !== "undefined") {
95
+ return "Window";
96
+ }
97
+ return "Node";
98
+ };
99
+ _this.errorFn = function (reject) { return function (err) { var _a, _b; return reject((_b = (_a = _this.errFn) === null || _a === void 0 ? void 0 : _a.call(_this, err)) !== null && _b !== void 0 ? _b : err); }; };
100
+ _this.clearTimer = function (opts) { return !!opts.timer && (clearTimeout(opts.timer), opts.timer = null); };
101
+ _this.requestType = function () {
102
+ switch (_this.envDesc()) {
103
+ case "Window":
104
+ return _this.fetch;
105
+ case "Node":
106
+ return _this.http;
107
+ }
108
+ };
109
+ _this.initDefaultParams = function (url, _a) {
110
+ var _b = _a.method, method = _b === void 0 ? "GET" : _b, _c = _a.query, query = _c === void 0 ? {} : _c, _d = _a.headers, headers = _d === void 0 ? {} : _d, _e = _a.body, body = _e === void 0 ? null : _e, _f = _a.timeout, timeout = _f === void 0 ? 30 * 1000 : _f, _g = _a.controller, controller = _g === void 0 ? new AbortController() : _g, _h = _a.type, type = _h === void 0 ? "json" : _h, others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
111
+ return (__assign({ url: urlJoin(_this.fixOrigin(url), query), method: method, headers: headers, body: method === "GET" ? null : jsonToString(body), timeout: timeout, signal: controller.signal, controller: controller, type: type, timer: null }, others));
112
+ };
113
+ _this.initFetchParams = function (url, opts) {
114
+ var _a, _b;
115
+ var params = _this.initDefaultParams(url, opts);
116
+ var controller = params.controller, timer = params.timer, timeout = params.timeout;
117
+ !!!timer && (params.timer = setTimeout(function () { return controller.abort(); }, timeout));
118
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, params)) !== null && _b !== void 0 ? _b : params;
119
+ };
120
+ _this.initHttpParams = function (url, opts) {
121
+ var _a, _b;
122
+ var params = _this.initDefaultParams(url, opts);
123
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, params)) !== null && _b !== void 0 ? _b : params;
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
+ export { RequestBase };
143
+ var Request = (function (_super) {
144
+ __extends(Request, _super);
145
+ function Request(origin) {
146
+ var _this = _super.call(this, origin) || this;
147
+ _this.fetch = function (_url, _opts) {
148
+ var _a = defer(), promise = _a.promise, resolve = _a.resolve, reject = _a.reject;
149
+ var _b = _this.initFetchParams(_url, _opts), url = _b.url, opts = __rest(_b, ["url"]);
150
+ var signal = opts.signal;
151
+ signal.addEventListener('abort', _this.errorFn(reject));
152
+ fetch(url, opts).then(function (response) {
153
+ if (response.status >= 200 && response.status < 300) {
154
+ return _this.getDataByType(opts.type, response);
155
+ }
156
+ return _this.errorFn(reject);
157
+ }).then(function (res) { var _a, _b; return resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, res)) !== null && _b !== void 0 ? _b : res); }).catch(_this.errorFn(reject)).finally(function () { return _this.clearTimer(opts); });
158
+ return promise;
159
+ };
160
+ _this.http = function (_url, _opts) {
161
+ var _a = defer(), promise = _a.promise, resolve = _a.resolve, reject = _a.reject;
162
+ var _b = _this.initFetchParams(_url, _opts), url = _b.url, opts = __rest(_b, ["url"]);
163
+ request(url, opts, resolve);
164
+ return promise;
165
+ };
166
+ _this.GET = function (url, query, _, opts) {
167
+ if (query === void 0) { query = {}; }
168
+ return _this.request(url, __assign({ query: query, method: "GET" }, opts));
169
+ };
170
+ _this.POST = function (url, query, body, opts) {
171
+ if (query === void 0) { query = {}; }
172
+ return _this.request(url, __assign({ query: query, method: "POST", body: body }, opts));
173
+ };
174
+ _this.PUT = function (url, query, body, opts) {
175
+ if (query === void 0) { query = {}; }
176
+ return _this.request(url, __assign({ query: query, method: "PUT", body: body }, opts));
177
+ };
178
+ _this.DELETE = function (url, query, body, opts) {
179
+ if (query === void 0) { query = {}; }
180
+ return _this.request(url, __assign({ query: query, method: "DELETE", body: body }, opts));
181
+ };
182
+ _this.OPTIONS = function (url, query, body, opts) {
183
+ if (query === void 0) { query = {}; }
184
+ return _this.request(url, __assign({ query: query, method: "OPTIONS", body: body }, opts));
185
+ };
186
+ _this.HEAD = function (url, query, body, opts) {
187
+ if (query === void 0) { query = {}; }
188
+ return _this.request(url, __assign({ query: query, method: "HEAD", body: body }, opts));
189
+ };
190
+ _this.PATCH = function (url, query, body, opts) {
191
+ if (query === void 0) { query = {}; }
192
+ return _this.request(url, __assign({ query: query, method: "PATCH", body: body }, opts));
193
+ };
194
+ _this.request = _this.requestType();
195
+ return _this;
196
+ }
197
+ return Request;
198
+ }(RequestBase));
199
+ export { Request };
@@ -1,14 +1,14 @@
1
- export var types;
2
- (function (types) {
3
- types["[object Array]"] = "array";
4
- types["[object Object]"] = "object";
5
- types["[object Function]"] = "function";
6
- types["[object Set]"] = "set";
7
- types["[object Map]"] = "map";
8
- types["[object WeakMap]"] = "weakMap";
9
- types["[object WeakSet]"] = "weakSet";
10
- types["[object Date]"] = "date";
11
- types["[object RegExp]"] = "regExp";
12
- types["[object Math]"] = "math";
13
- })(types || (types = {}));
14
- ;
1
+ export var types;
2
+ (function (types) {
3
+ types["[object Array]"] = "array";
4
+ types["[object Object]"] = "object";
5
+ types["[object Function]"] = "function";
6
+ types["[object Set]"] = "set";
7
+ types["[object Map]"] = "map";
8
+ types["[object WeakMap]"] = "weakMap";
9
+ types["[object WeakSet]"] = "weakSet";
10
+ types["[object Date]"] = "date";
11
+ types["[object RegExp]"] = "regExp";
12
+ types["[object Math]"] = "math";
13
+ })(types || (types = {}));
14
+ ;
@@ -1,27 +1,30 @@
1
- export var setStorage = function (key, val) {
2
- try {
3
- localStorage.setItem(key, JSON.stringify(val));
4
- } catch (error) {
5
- console.error(error);
6
- }
7
- };
8
- export var getStorage = function (key) {
9
- try {
10
- var str = localStorage.getItem(key);
11
- if (str === null || str === undefined) {
12
- return null;
13
- }
14
- return JSON.parse(str);
15
- } catch (error) {
16
- console.error(error);
17
- return null;
18
- }
19
- };
20
- export var clearStorage = function (key) {
21
- try {
22
- key && localStorage.removeItem(key);
23
- !key && localStorage.clear();
24
- } catch (error) {
25
- console.error(error);
26
- }
27
- };
1
+ export var setStorage = function (key, val) {
2
+ try {
3
+ localStorage.setItem(key, JSON.stringify(val));
4
+ }
5
+ catch (error) {
6
+ console.error(error);
7
+ }
8
+ };
9
+ export var getStorage = function (key) {
10
+ try {
11
+ var str = localStorage.getItem(key);
12
+ if (str === null || str === undefined) {
13
+ return null;
14
+ }
15
+ return JSON.parse(str);
16
+ }
17
+ catch (error) {
18
+ console.error(error);
19
+ return null;
20
+ }
21
+ };
22
+ export var clearStorage = function (key) {
23
+ try {
24
+ key && localStorage.removeItem(key);
25
+ !key && localStorage.clear();
26
+ }
27
+ catch (error) {
28
+ console.error(error);
29
+ }
30
+ };
@@ -4,8 +4,8 @@ export interface IObject<T> {
4
4
  }
5
5
  export interface IPromise extends IObject<any> {
6
6
  promise: Promise<void>;
7
- resolve: () => unknown;
8
- reject: () => unknown;
7
+ resolve: (res: any) => unknown;
8
+ reject: (err: any) => unknown;
9
9
  }
10
10
  export declare type IInstance<T> = {
11
11
  _instance: Function;
@@ -15,7 +15,8 @@ export declare type IRandomNum = (min: number, max: number, bool?: boolean) => n
15
15
  export declare type IUrlSplit = (url: string) => IObject<any>;
16
16
  export declare type IUrlJoin = (url: string, query: object) => string;
17
17
  export declare type IGetType<T> = (data: any) => typeof data | T[keyof T] | "null";
18
- export declare type 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,10 +47,48 @@ 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 = {
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
+ controller?: AbortController;
62
+ timeout?: number;
63
+ timer?: number | unknown | null;
64
+ [key: string]: any;
65
+ };
66
+ export declare type IInterceptors = {
67
+ use(type: "request" | "response" | "error", fn: Function): void;
68
+ get reqFn(): Function;
69
+ get resFn(): Function;
70
+ get errFn(): Function;
71
+ };
72
+ export declare type IRequestBase = {
48
73
  origin: string;
49
- fixOrigin: <T = string>(origin: T) => T;
74
+ fixOrigin: (fixStr: string) => string;
50
75
  envDesc: () => "Window" | "Node";
76
+ chackUrl: (url: string) => boolean;
77
+ fetch: (url: string, opts: IRequestOptions) => Promise<any>;
78
+ http: (url: string, opts: IRequestOptions) => Promise<any>;
79
+ initDefaultParams: (url: string, opts: IRequestOptions) => any;
80
+ initFetchParams: (url: string, opts: IRequestOptions) => any;
81
+ initHttpParams: (url: string, opts: IRequestOptions) => any;
82
+ getDataByType: (type: IDataType, response: Response) => Promise<any>;
51
83
  };
52
- export declare type IGet = (url: string, params: IObject<any>) => Promise<void>;
84
+ export declare type IRequest = {
85
+ request: Function;
86
+ GET: IRequestFn;
87
+ POST: IRequestFn;
88
+ DELETE: IRequestFn;
89
+ PUT: IRequestFn;
90
+ OPTIONS: IRequestFn;
91
+ HEAD: IRequestFn;
92
+ PATCH: IRequestFn;
93
+ } & IRequestBase;
53
94
  export {};
package/dist/esm/types.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-lib-js",
3
- "version": "1.0.3",
3
+ "version": "1.0.6",
4
4
  "description": "JavaScript工具函数,封装的一些常用的js函数",
5
5
  "main": "./dist/common/index.js",
6
6
  "types": "./dist/common/index.d.ts",
@@ -10,9 +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",
15
- "build:hot": "tsc -p tsconfig.json -w",
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",
16
+ "build:hot:esm": "tsc -p tsconfig.es.json -w",
17
+ "build:hot:node": "tsc -p tsconfig.json -w",
16
18
  "build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
17
19
  "tsc:build": "rm -fr dist && tsc -p tsconfig.json && tsc -p tsconfig.es.json",
18
20
  "publish": "pnpm run build && pnpm publish",
@@ -24,13 +26,18 @@
24
26
  "type": "git",
25
27
  "url": "https://gitee.com/DieHunter/utils-lib-js.git"
26
28
  },
27
- "keywords": ["utils", "tools", "lib"],
29
+ "keywords": [
30
+ "utils",
31
+ "tools",
32
+ "lib"
33
+ ],
28
34
  "author": "",
29
35
  "license": "ISC",
30
36
  "dependencies": {
31
37
  "event-message-center": "^1.0.16"
32
38
  },
33
39
  "devDependencies": {
40
+ "@types/node": "^18.7.1",
34
41
  "babel-cli": "^6.26.0"
35
42
  }
36
43
  }
package/tsconfig.es.json CHANGED
@@ -1,12 +1,8 @@
1
1
  {
2
2
  "extends": "./tsconfig",
3
3
  "compilerOptions": {
4
- "module": "es2015",
4
+ "module": "ES6",
5
5
  "outDir": "dist/esm",
6
- "moduleResolution": "node"
7
- },
8
- "exclude": [
9
- "**/__tests__/**",
10
- "**/example/**"
11
- ]
6
+ "rootDir": "src"
7
+ }
12
8
  }