utils-lib-js 1.1.0 → 1.2.1

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.
@@ -21,6 +21,13 @@ declare abstract class RequestBase extends Interceptors implements IRequestBase
21
21
  initAbort: (params: any) => any;
22
22
  requestType: () => (url: any, opts: any) => Promise<void>;
23
23
  getDataByType: (type: any, response: any) => any;
24
+ formatBodyString: (bodyString: any) => {
25
+ text: () => any;
26
+ json: () => any;
27
+ blob: () => IObject<any>;
28
+ formData: () => IObject<any>;
29
+ arrayBuffer: () => IObject<any>;
30
+ };
24
31
  }
25
32
  declare abstract class RequestInit extends RequestBase implements IRequestInit {
26
33
  constructor(origin: any);
@@ -134,6 +134,25 @@ var RequestBase = function (_super) {
134
134
  return response['json']();
135
135
  }
136
136
  };
137
+ _this.formatBodyString = function (bodyString) {
138
+ return {
139
+ text: function () {
140
+ return bodyString;
141
+ },
142
+ json: function () {
143
+ var _a;return (_a = (0, index_js_1.stringToJson)(bodyString)) !== null && _a !== void 0 ? _a : bodyString;
144
+ },
145
+ blob: function () {
146
+ return (0, index_js_1.stringToJson)(bodyString);
147
+ },
148
+ formData: function () {
149
+ return (0, index_js_1.stringToJson)(bodyString);
150
+ },
151
+ arrayBuffer: function () {
152
+ return (0, index_js_1.stringToJson)(bodyString);
153
+ }
154
+ };
155
+ };
137
156
  _this.origin = origin !== null && origin !== void 0 ? origin : '';
138
157
  return _this;
139
158
  }
@@ -223,7 +242,9 @@ var Request = function (_super) {
223
242
  return data_1 += chunk;
224
243
  });
225
244
  return response.on("end", function () {
226
- 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);
245
+ var _a, _b;
246
+ var result = _this.getDataByType(params.type, _this.formatBodyString(data_1));
247
+ resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, result)) !== null && _b !== void 0 ? _b : result);
227
248
  });
228
249
  }
229
250
  return _this.errorFn(reject)(response === null || response === void 0 ? void 0 : response.statusMessage);
@@ -84,6 +84,7 @@ export declare type IRequestBase = {
84
84
  fetch: IRequestBaseFn;
85
85
  http: IRequestBaseFn;
86
86
  getDataByType: (type: IDataType, response: Response) => Promise<any>;
87
+ formatBodyString: (bodyString: string) => IObject<Function>;
87
88
  };
88
89
  export declare type IRequestInit = {
89
90
  initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
@@ -21,6 +21,13 @@ declare abstract class RequestBase extends Interceptors implements IRequestBase
21
21
  initAbort: (params: any) => any;
22
22
  requestType: () => (url: any, opts: any) => Promise<void>;
23
23
  getDataByType: (type: any, response: any) => any;
24
+ formatBodyString: (bodyString: any) => {
25
+ text: () => any;
26
+ json: () => any;
27
+ blob: () => IObject<any>;
28
+ formData: () => IObject<any>;
29
+ arrayBuffer: () => IObject<any>;
30
+ };
24
31
  }
25
32
  declare abstract class RequestInit extends RequestBase implements IRequestInit {
26
33
  constructor(origin: any);
@@ -34,7 +34,7 @@ var __rest = this && this.__rest || function (s, e) {
34
34
  }
35
35
  return t;
36
36
  };
37
- import { urlJoin, defer, jsonToString } from "./index.js";
37
+ import { urlJoin, defer, jsonToString, stringToJson } from "./index.js";
38
38
  import { request } from "http";
39
39
  import { parse } from "url";
40
40
  var Interceptors = function () {
@@ -130,6 +130,25 @@ var RequestBase = function (_super) {
130
130
  return response['json']();
131
131
  }
132
132
  };
133
+ _this.formatBodyString = function (bodyString) {
134
+ return {
135
+ text: function () {
136
+ return bodyString;
137
+ },
138
+ json: function () {
139
+ var _a;return (_a = stringToJson(bodyString)) !== null && _a !== void 0 ? _a : bodyString;
140
+ },
141
+ blob: function () {
142
+ return stringToJson(bodyString);
143
+ },
144
+ formData: function () {
145
+ return stringToJson(bodyString);
146
+ },
147
+ arrayBuffer: function () {
148
+ return stringToJson(bodyString);
149
+ }
150
+ };
151
+ };
133
152
  _this.origin = origin !== null && origin !== void 0 ? origin : '';
134
153
  return _this;
135
154
  }
@@ -219,7 +238,9 @@ var Request = function (_super) {
219
238
  return data_1 += chunk;
220
239
  });
221
240
  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);
241
+ var _a, _b;
242
+ var result = _this.getDataByType(params.type, _this.formatBodyString(data_1));
243
+ resolve((_b = (_a = _this.resFn) === null || _a === void 0 ? void 0 : _a.call(_this, result)) !== null && _b !== void 0 ? _b : result);
223
244
  });
224
245
  }
225
246
  return _this.errorFn(reject)(response === null || response === void 0 ? void 0 : response.statusMessage);
@@ -84,6 +84,7 @@ export declare type IRequestBase = {
84
84
  fetch: IRequestBaseFn;
85
85
  http: IRequestBaseFn;
86
86
  getDataByType: (type: IDataType, response: Response) => Promise<any>;
87
+ formatBodyString: (bodyString: string) => IObject<Function>;
87
88
  };
88
89
  export declare type IRequestInit = {
89
90
  initDefaultParams: (url: IUrl, opts: IRequestOptions) => any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-lib-js",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "JavaScript工具函数,封装的一些常用的js函数",
5
5
  "main": "./dist/common/index.js",
6
6
  "types": "./dist/common/index.d.ts",