cvitool 1.0.753 → 1.0.754

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.
@@ -24,7 +24,7 @@ interface reqOptions extends baseReqOptions {
24
24
  query?: querystring.ParsedUrlQueryInput;
25
25
  body?: {
26
26
  [key: string]: any;
27
- };
27
+ } | string;
28
28
  retries?: number;
29
29
  retryInterval?: number;
30
30
  [key: string]: any;
@@ -33,7 +33,7 @@ interface methodReqOptions extends baseReqOptions {
33
33
  query?: querystring.ParsedUrlQueryInput;
34
34
  body?: {
35
35
  [key: string]: any;
36
- };
36
+ } | string;
37
37
  retries?: number;
38
38
  retryInterval?: number;
39
39
  [key: string]: any;
@@ -55,7 +55,7 @@ interface reqSendMultiPartOptions extends baseReqOptions {
55
55
  interface ResData {
56
56
  reqUrl: string;
57
57
  resHeaders: CustomObject;
58
- resBody: http.IncomingMessage | CustomObject | string | Buffer | null;
58
+ resBody: http.IncomingMessage | Readable | CustomObject | string | Buffer | null;
59
59
  }
60
60
  /**
61
61
  * 通用json请求
package/build/src/hgo.js CHANGED
@@ -21,6 +21,7 @@ exports.del = del;
21
21
  const querystring = require("querystring");
22
22
  const https = require("https");
23
23
  const http = require("http");
24
+ const cutil_1 = require("./cutil");
24
25
  function buildTimeOutErr(type, timeout, reqUrl) {
25
26
  const err = new Error();
26
27
  err.name = 'timeoutError';
@@ -51,12 +52,13 @@ function request(url, options) {
51
52
  url = url + '?' + querystring.stringify(query);
52
53
  }
53
54
  }
54
- const data = JSON.stringify(body);
55
- const isbodyEmpty = Object.keys(body).length === 0;
55
+ const isStrBodyType = (0, cutil_1.getValueType)(body) === 'string';
56
+ const data = (isStrBodyType ? body : JSON.stringify(body));
57
+ const isBodyEmpty = data.length === 0 || data === '{}';
56
58
  let baseHeaders = {};
57
- if (!isbodyEmpty) {
59
+ if (!isBodyEmpty) {
58
60
  baseHeaders = {
59
- 'Content-Type': 'application/json; charset=utf-8',
61
+ 'Content-Type': isStrBodyType ? 'text/plain; charset=utf-8' : 'application/json; charset=utf-8',
60
62
  'Content-Length': Buffer.byteLength(data)
61
63
  };
62
64
  }
@@ -75,7 +77,7 @@ function request(url, options) {
75
77
  req.destroy();
76
78
  reject(e);
77
79
  });
78
- if (isbodyEmpty) {
80
+ if (isBodyEmpty) {
79
81
  req.end();
80
82
  }
81
83
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cvitool",
3
- "version": "1.0.753",
3
+ "version": "1.0.754",
4
4
  "description": "cvitool",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/hgo.ts CHANGED
@@ -2,6 +2,7 @@ import * as querystring from 'querystring';
2
2
  import * as https from 'https';
3
3
  import * as http from 'http';
4
4
  import { Readable } from 'stream';
5
+ import { getValueType } from './cutil';
5
6
 
6
7
  type Method = 'get' | 'put' | 'post' | 'delete' | 'patch' | 'head';
7
8
  type ResType = 'json' | 'buffer' | 'stream' | 'text';
@@ -28,7 +29,7 @@ interface reqOptions extends baseReqOptions {
28
29
  query?: querystring.ParsedUrlQueryInput,
29
30
  body?: {
30
31
  [key: string]: any
31
- },
32
+ } | string,
32
33
  retries?: number,
33
34
  retryInterval?: number,
34
35
  [key: string]: any
@@ -38,7 +39,7 @@ interface methodReqOptions extends baseReqOptions {
38
39
  query?: querystring.ParsedUrlQueryInput,
39
40
  body?: {
40
41
  [key: string]: any
41
- },
42
+ } | string,
42
43
  retries?: number,
43
44
  retryInterval?: number,
44
45
  [key: string]: any
@@ -64,7 +65,7 @@ interface reqSendMultiPartOptions extends baseReqOptions {
64
65
  interface ResData {
65
66
  reqUrl: string,
66
67
  resHeaders: CustomObject,
67
- resBody: http.IncomingMessage | CustomObject | string | Buffer | null
68
+ resBody: http.IncomingMessage | Readable | CustomObject | string | Buffer | null
68
69
  }
69
70
 
70
71
  function buildTimeOutErr(type: 'connectTimeOut' | 'readTimeOut', timeout: number, reqUrl?: string) {
@@ -100,12 +101,13 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
100
101
  url = url + '?' + querystring.stringify(query);
101
102
  }
102
103
  }
103
- const data = JSON.stringify(body);
104
- const isbodyEmpty = Object.keys(body).length === 0;
104
+ const isStrBodyType = getValueType(body) === 'string';
105
+ const data = (isStrBodyType ? body : JSON.stringify(body)) as string;
106
+ const isBodyEmpty = data.length === 0 || data === '{}';
105
107
  let baseHeaders: CustomObject = {};
106
- if (!isbodyEmpty) {
108
+ if (!isBodyEmpty) {
107
109
  baseHeaders = {
108
- 'Content-Type': 'application/json; charset=utf-8',
110
+ 'Content-Type': isStrBodyType ? 'text/plain; charset=utf-8' : 'application/json; charset=utf-8',
109
111
  'Content-Length': Buffer.byteLength(data)
110
112
  };
111
113
  }
@@ -124,7 +126,7 @@ async function request(url: string, options?: reqOptions): Promise<ResData> {
124
126
  req.destroy();
125
127
  reject(e);
126
128
  });
127
- if (isbodyEmpty) {
129
+ if (isBodyEmpty) {
128
130
  req.end();
129
131
  } else {
130
132
  req.write(data, (e) => {