utils-lib-js 1.4.1 → 1.4.3

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.
@@ -166,11 +166,11 @@ var classDecorator = function (params) {
166
166
  };
167
167
  exports.classDecorator = classDecorator;
168
168
  var stringToJson = function (target) {
169
- if ((0, index_js_1.getType)(target) !== "string") return {};
169
+ if ((0, index_js_1.getType)(target) !== "string") return null;
170
170
  try {
171
171
  return JSON.parse(target);
172
172
  } catch (error) {
173
- return {};
173
+ return null;
174
174
  }
175
175
  };
176
176
  exports.stringToJson = stringToJson;
@@ -28,8 +28,9 @@ declare abstract class RequestInit extends RequestBase implements IRequestInit {
28
28
  constructor(origin: any);
29
29
  abstract fetch(url: any, opts: any): Promise<void>;
30
30
  abstract http(url: any, opts: any): Promise<void>;
31
- initDefaultParams: (url: IUrl, { method, query, headers, body, timeout, controller, type, ...others }: {
31
+ initDefaultParams: ({ url, method, query, headers, body, timeout, controller, type, ...others }: {
32
32
  [x: string]: any;
33
+ url?: string;
33
34
  method?: string;
34
35
  query?: {};
35
36
  headers?: {};
@@ -176,36 +176,40 @@ var RequestInit = function (_super) {
176
176
  __extends(RequestInit, _super);
177
177
  function RequestInit(origin) {
178
178
  var _this = _super.call(this, origin) || this;
179
- _this.initDefaultParams = function (url, _a) {
180
- var _b = _a.method,
181
- method = _b === void 0 ? "GET" : _b,
182
- _c = _a.query,
183
- query = _c === void 0 ? {} : _c,
184
- _d = _a.headers,
185
- headers = _d === void 0 ? {} : _d,
186
- _e = _a.body,
187
- body = _e === void 0 ? null : _e,
188
- _f = _a.timeout,
189
- timeout = _f === void 0 ? 30 * 1000 : _f,
190
- _g = _a.controller,
191
- controller = _g === void 0 ? new CustomAbortController() : _g,
192
- _h = _a.type,
193
- type = _h === void 0 ? "json" : _h,
194
- others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
179
+ _this.initDefaultParams = function (_a) {
180
+ var _b = _a.url,
181
+ url = _b === void 0 ? "/" : _b,
182
+ _c = _a.method,
183
+ method = _c === void 0 ? "GET" : _c,
184
+ _d = _a.query,
185
+ query = _d === void 0 ? {} : _d,
186
+ _e = _a.headers,
187
+ headers = _e === void 0 ? {} : _e,
188
+ _f = _a.body,
189
+ body = _f === void 0 ? null : _f,
190
+ _g = _a.timeout,
191
+ timeout = _g === void 0 ? 30 * 1000 : _g,
192
+ _h = _a.controller,
193
+ controller = _h === void 0 ? new CustomAbortController() : _h,
194
+ _j = _a.type,
195
+ type = _j === void 0 ? "json" : _j,
196
+ others = __rest(_a, ["url", "method", "query", "headers", "body", "timeout", "controller", "type"]);
195
197
  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);
196
198
  };
197
199
  _this.initFetchParams = function (url, opts) {
198
200
  var _a, _b;
199
- var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
200
- var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, _temp)) !== null && _b !== void 0 ? _b : _temp;
201
- return params;
201
+ var __p = __assign({ url: url }, opts);
202
+ var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __p)) !== null && _b !== void 0 ? _b : __p;
203
+ var _temp = _this.initAbort(_this.initDefaultParams(params));
204
+ return _temp;
202
205
  };
203
206
  _this.initHttpParams = function (url, opts) {
204
207
  var _a, _b;
205
- var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
206
- var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, _temp)) !== null && _b !== void 0 ? _b : _temp;
208
+ var __p = __assign({ url: url }, opts);
209
+ var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __p)) !== null && _b !== void 0 ? _b : __p;
210
+ var _temp = _this.initAbort(_this.initDefaultParams(params));
207
211
  var options = parse(params.url, true);
208
- return __assign(__assign({}, params), options);
212
+ return __assign(__assign({}, _temp), options);
209
213
  };
210
214
  return _this;
211
215
  }
@@ -26,7 +26,7 @@ export declare type ICreateObject = <T, U extends T>(source: T) => U;
26
26
  export declare type IInherit = <T extends Function>(source: T, target?: Function) => Function;
27
27
  export declare type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
28
28
  export declare type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void;
29
- export declare type IStringToJson = (target: string) => IObject<any>;
29
+ export declare type IStringToJson = (target: string) => IObject<any> | null;
30
30
  export declare type IJsonToString = (target: IObject<any>) => string;
31
31
  export declare type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
32
32
  export declare type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
@@ -87,7 +87,7 @@ export declare type IRequestBase = {
87
87
  formatBodyString: (bodyString: string) => IObject<Function>;
88
88
  };
89
89
  export declare type IRequestInit = {
90
- initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
90
+ initDefaultParams: (opts: IRequestOptions) => any;
91
91
  initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
92
92
  initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
93
93
  };
@@ -151,11 +151,11 @@ export var classDecorator = function (params) {
151
151
  };
152
152
  };
153
153
  export var stringToJson = function (target) {
154
- if (getType(target) !== "string") return {};
154
+ if (getType(target) !== "string") return null;
155
155
  try {
156
156
  return JSON.parse(target);
157
157
  } catch (error) {
158
- return {};
158
+ return null;
159
159
  }
160
160
  };
161
161
  export var jsonToString = function (target) {
@@ -28,8 +28,9 @@ declare abstract class RequestInit extends RequestBase implements IRequestInit {
28
28
  constructor(origin: any);
29
29
  abstract fetch(url: any, opts: any): Promise<void>;
30
30
  abstract http(url: any, opts: any): Promise<void>;
31
- initDefaultParams: (url: IUrl, { method, query, headers, body, timeout, controller, type, ...others }: {
31
+ initDefaultParams: ({ url, method, query, headers, body, timeout, controller, type, ...others }: {
32
32
  [x: string]: any;
33
+ url?: string;
33
34
  method?: string;
34
35
  query?: {};
35
36
  headers?: {};
@@ -172,36 +172,40 @@ var RequestInit = function (_super) {
172
172
  __extends(RequestInit, _super);
173
173
  function RequestInit(origin) {
174
174
  var _this = _super.call(this, origin) || this;
175
- _this.initDefaultParams = function (url, _a) {
176
- var _b = _a.method,
177
- method = _b === void 0 ? "GET" : _b,
178
- _c = _a.query,
179
- query = _c === void 0 ? {} : _c,
180
- _d = _a.headers,
181
- headers = _d === void 0 ? {} : _d,
182
- _e = _a.body,
183
- body = _e === void 0 ? null : _e,
184
- _f = _a.timeout,
185
- timeout = _f === void 0 ? 30 * 1000 : _f,
186
- _g = _a.controller,
187
- controller = _g === void 0 ? new CustomAbortController() : _g,
188
- _h = _a.type,
189
- type = _h === void 0 ? "json" : _h,
190
- others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
175
+ _this.initDefaultParams = function (_a) {
176
+ var _b = _a.url,
177
+ url = _b === void 0 ? "/" : _b,
178
+ _c = _a.method,
179
+ method = _c === void 0 ? "GET" : _c,
180
+ _d = _a.query,
181
+ query = _d === void 0 ? {} : _d,
182
+ _e = _a.headers,
183
+ headers = _e === void 0 ? {} : _e,
184
+ _f = _a.body,
185
+ body = _f === void 0 ? null : _f,
186
+ _g = _a.timeout,
187
+ timeout = _g === void 0 ? 30 * 1000 : _g,
188
+ _h = _a.controller,
189
+ controller = _h === void 0 ? new CustomAbortController() : _h,
190
+ _j = _a.type,
191
+ type = _j === void 0 ? "json" : _j,
192
+ others = __rest(_a, ["url", "method", "query", "headers", "body", "timeout", "controller", "type"]);
191
193
  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);
192
194
  };
193
195
  _this.initFetchParams = function (url, opts) {
194
196
  var _a, _b;
195
- var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
196
- var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, _temp)) !== null && _b !== void 0 ? _b : _temp;
197
- return params;
197
+ var __p = __assign({ url: url }, opts);
198
+ var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __p)) !== null && _b !== void 0 ? _b : __p;
199
+ var _temp = _this.initAbort(_this.initDefaultParams(params));
200
+ return _temp;
198
201
  };
199
202
  _this.initHttpParams = function (url, opts) {
200
203
  var _a, _b;
201
- var _temp = _this.initAbort(_this.initDefaultParams(url, opts));
202
- var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, _temp)) !== null && _b !== void 0 ? _b : _temp;
204
+ var __p = __assign({ url: url }, opts);
205
+ var params = (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __p)) !== null && _b !== void 0 ? _b : __p;
206
+ var _temp = _this.initAbort(_this.initDefaultParams(params));
203
207
  var options = parse(params.url, true);
204
- return __assign(__assign({}, params), options);
208
+ return __assign(__assign({}, _temp), options);
205
209
  };
206
210
  return _this;
207
211
  }
@@ -26,7 +26,7 @@ export declare type ICreateObject = <T, U extends T>(source: T) => U;
26
26
  export declare type IInherit = <T extends Function>(source: T, target?: Function) => Function;
27
27
  export declare type IGetInstance = (classProto: IInstance<FunctionConstructor>, overwrite?: boolean, ...params: any[]) => Function;
28
28
  export declare type IClassDecorator = (params: IObject<any>) => <TFunction extends Function>(target: TFunction) => void;
29
- export declare type IStringToJson = (target: string) => IObject<any>;
29
+ export declare type IStringToJson = (target: string) => IObject<any> | null;
30
30
  export declare type IJsonToString = (target: IObject<any>) => string;
31
31
  export declare type IThrottle = (fn: Function, time: number) => (...args: any[]) => void;
32
32
  export declare type IDebounce = (fn: Function, time: number) => (...args: any[]) => void;
@@ -87,7 +87,7 @@ export declare type IRequestBase = {
87
87
  formatBodyString: (bodyString: string) => IObject<Function>;
88
88
  };
89
89
  export declare type IRequestInit = {
90
- initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
90
+ initDefaultParams: (opts: IRequestOptions) => any;
91
91
  initFetchParams: (url: IUrl, opts: IRequestOptions) => any;
92
92
  initHttpParams: (url: IUrl, opts: IRequestOptions) => any;
93
93
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-lib-js",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "JavaScript工具函数,封装的一些常用的js函数",
5
5
  "main": "./dist/common/index.js",
6
6
  "types": "./dist/common/index.d.ts",