utils-lib-js 1.0.15 → 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
  }
@@ -170,7 +189,7 @@ var RequestInit = function (_super) {
170
189
  var _a, _b;
171
190
  var params = _this.initAbort(_this.initDefaultParams(url, opts));
172
191
  var options = (0, 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;
192
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : __assign(__assign({}, params), options);
174
193
  };
175
194
  return _this;
176
195
  }
@@ -189,6 +208,9 @@ var Request = function (_super) {
189
208
  url = _b.url,
190
209
  opts = __rest(_b, ["url"]);
191
210
  var signal = opts.signal;
211
+ promise.finally(function () {
212
+ return _this.clearTimer(opts);
213
+ });
192
214
  signal.addEventListener('abort', function () {
193
215
  return _this.errorFn(reject);
194
216
  });
@@ -199,9 +221,7 @@ var Request = function (_super) {
199
221
  return _this.errorFn(reject);
200
222
  }).then(function (res) {
201
223
  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
- });
224
+ }).catch(_this.errorFn(reject));
205
225
  return promise;
206
226
  };
207
227
  _this.http = function (_url, _opts) {
@@ -211,6 +231,9 @@ var Request = function (_super) {
211
231
  reject = _a.reject;
212
232
  var params = _this.initHttpParams(_url, _opts);
213
233
  var signal = params.signal;
234
+ promise.finally(function () {
235
+ return _this.clearTimer(params);
236
+ });
214
237
  var req = (0, http_1.request)(params, function (response) {
215
238
  if ((response === null || response === void 0 ? void 0 : response.statusCode) >= 200 && (response === null || response === void 0 ? void 0 : response.statusCode) < 300) {
216
239
  var data_1 = "";
@@ -219,7 +242,9 @@ var Request = function (_super) {
219
242
  return data_1 += chunk;
220
243
  });
221
244
  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);
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);
223
248
  });
224
249
  }
225
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
  }
@@ -166,7 +185,7 @@ var RequestInit = function (_super) {
166
185
  var _a, _b;
167
186
  var params = _this.initAbort(_this.initDefaultParams(url, opts));
168
187
  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;
188
+ return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : __assign(__assign({}, params), options);
170
189
  };
171
190
  return _this;
172
191
  }
@@ -185,6 +204,9 @@ var Request = function (_super) {
185
204
  url = _b.url,
186
205
  opts = __rest(_b, ["url"]);
187
206
  var signal = opts.signal;
207
+ promise.finally(function () {
208
+ return _this.clearTimer(opts);
209
+ });
188
210
  signal.addEventListener('abort', function () {
189
211
  return _this.errorFn(reject);
190
212
  });
@@ -195,9 +217,7 @@ var Request = function (_super) {
195
217
  return _this.errorFn(reject);
196
218
  }).then(function (res) {
197
219
  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
- });
220
+ }).catch(_this.errorFn(reject));
201
221
  return promise;
202
222
  };
203
223
  _this.http = function (_url, _opts) {
@@ -207,6 +227,9 @@ var Request = function (_super) {
207
227
  reject = _a.reject;
208
228
  var params = _this.initHttpParams(_url, _opts);
209
229
  var signal = params.signal;
230
+ promise.finally(function () {
231
+ return _this.clearTimer(params);
232
+ });
210
233
  var req = request(params, function (response) {
211
234
  if ((response === null || response === void 0 ? void 0 : response.statusCode) >= 200 && (response === null || response === void 0 ? void 0 : response.statusCode) < 300) {
212
235
  var data_1 = "";
@@ -215,7 +238,9 @@ var Request = function (_super) {
215
238
  return data_1 += chunk;
216
239
  });
217
240
  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);
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);
219
244
  });
220
245
  }
221
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,40 +1,40 @@
1
- {
2
- "name": "utils-lib-js",
3
- "version": "1.0.15",
4
- "description": "JavaScript工具函数,封装的一些常用的js函数",
5
- "main": "./dist/common/index.js",
6
- "types": "./dist/common/index.d.ts",
7
- "module": "./dist/esm/index.js",
8
- "exports": {
9
- "import": "./dist/esm/index.js",
10
- "require": "./dist/common/index.js"
11
- },
12
- "scripts": {
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",
18
- "build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
19
- "tsc:build": "rm -fr dist && tsc -p tsconfig.json && tsc -p tsconfig.es.json",
20
- "publish": "pnpm run build && pnpm publish",
21
- "babel:mjs": "babel dist/esm --out-dir dist/esm",
22
- "babel:cjs": "babel dist/common --out-dir dist/common",
23
- "test": "echo \"Error: no test specified\" && exit 1"
24
- },
25
- "repository": {
26
- "type": "git",
27
- "url": "https://gitee.com/DieHunter/utils-lib-js.git"
28
- },
29
- "keywords": [
30
- "utils",
31
- "tools",
32
- "lib"
33
- ],
34
- "author": "",
35
- "license": "ISC",
36
- "devDependencies": {
37
- "@types/node": "^18.7.15",
38
- "babel-cli": "^6.26.0"
39
- }
1
+ {
2
+ "name": "utils-lib-js",
3
+ "version": "1.2.1",
4
+ "description": "JavaScript工具函数,封装的一些常用的js函数",
5
+ "main": "./dist/common/index.js",
6
+ "types": "./dist/common/index.d.ts",
7
+ "module": "./dist/esm/index.js",
8
+ "exports": {
9
+ "import": "./dist/esm/index.js",
10
+ "require": "./dist/common/index.js"
11
+ },
12
+ "scripts": {
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",
18
+ "build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
19
+ "tsc:build": "rm -fr dist && tsc -p tsconfig.json && tsc -p tsconfig.es.json",
20
+ "publish": "pnpm run build && pnpm publish",
21
+ "babel:mjs": "babel dist/esm --out-dir dist/esm",
22
+ "babel:cjs": "babel dist/common --out-dir dist/common",
23
+ "test": "echo \"Error: no test specified\" && exit 1"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://gitee.com/DieHunter/utils-lib-js.git"
28
+ },
29
+ "keywords": [
30
+ "utils",
31
+ "tools",
32
+ "lib"
33
+ ],
34
+ "author": "",
35
+ "license": "ISC",
36
+ "devDependencies": {
37
+ "@types/node": "^18.7.15",
38
+ "babel-cli": "^6.26.0"
39
+ }
40
40
  }