@wiajs/request 3.0.31 → 3.0.33
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.
- package/dist/request.cjs +27 -24
- package/dist/request.mjs +27 -24
- package/lib/index.js +21 -20
- package/lib/request.js +3 -3
- package/package.json +1 -1
- package/types/index.d.ts +2 -1
- package/types/request.d.ts +1 -3
package/dist/request.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia request v3.0.
|
|
2
|
+
* wia request v3.0.33
|
|
3
3
|
* (c) 2022-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -636,7 +636,7 @@ class Request extends stream.Duplex {
|
|
|
636
636
|
super();
|
|
637
637
|
const m = this;
|
|
638
638
|
|
|
639
|
-
// log({opts}, '
|
|
639
|
+
// log({opts}, 'new Request')
|
|
640
640
|
|
|
641
641
|
// Initialize the request
|
|
642
642
|
m.sanitizeOptions(opts);
|
|
@@ -1451,8 +1451,8 @@ class Request extends stream.Duplex {
|
|
|
1451
1451
|
* @override - 重写父类方法
|
|
1452
1452
|
* @template T - 需要模板
|
|
1453
1453
|
* @param {T & stream.Writable} dest - The writable stream to which data is written.
|
|
1454
|
-
* @param {Object} [
|
|
1455
|
-
* @param {boolean} [
|
|
1454
|
+
* @param {Object} [opt] - Optional configuration object.
|
|
1455
|
+
* @param {boolean} [opt.end=true] - Whether to end the writable stream when the readable stream ends.
|
|
1456
1456
|
* @returns {T} The destination stream.
|
|
1457
1457
|
*/
|
|
1458
1458
|
pipe(dest, opts = {}) {
|
|
@@ -1645,6 +1645,7 @@ const log = log$2.log({env: `wia:req:${log$2.name((typeof document === 'undefine
|
|
|
1645
1645
|
* @prop {string} [url]
|
|
1646
1646
|
* @prop {'http:' | 'https:'} [protocol]
|
|
1647
1647
|
* @prop {string} [host]
|
|
1648
|
+
* @prop {string} [hostname]
|
|
1648
1649
|
* @prop {string} [family]
|
|
1649
1650
|
* @prop {string} [path]
|
|
1650
1651
|
* @prop {string} [method]
|
|
@@ -1691,7 +1692,7 @@ utils.createErrorType(
|
|
|
1691
1692
|
* @param {string | Opts} uri/opts
|
|
1692
1693
|
* @param {Opts | Cb} [opts] /cb
|
|
1693
1694
|
* @param {Cb} [cb]
|
|
1694
|
-
* @returns {{
|
|
1695
|
+
* @returns {{opt: Opts, cb: Cb}}
|
|
1695
1696
|
*/
|
|
1696
1697
|
function init(uri, opts, cb) {
|
|
1697
1698
|
let R;
|
|
@@ -1727,26 +1728,28 @@ function init(uri, opts, cb) {
|
|
|
1727
1728
|
}
|
|
1728
1729
|
|
|
1729
1730
|
// copy options
|
|
1730
|
-
|
|
1731
|
+
/** @type {Opts} */
|
|
1732
|
+
const opt = {
|
|
1731
1733
|
// @ts-ignore
|
|
1732
1734
|
...uri,
|
|
1733
1735
|
...opts,
|
|
1734
1736
|
};
|
|
1735
1737
|
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
// @ts-ignore
|
|
1739
|
-
if (opts.method) opts.method = opts.method.toUpperCase();
|
|
1738
|
+
if (!utils.isString(opt.host) && !utils.isString(opt.hostname)) opt.hostname = '::1';
|
|
1739
|
+
opt.method = (opt.method ?? 'get').toUpperCase();
|
|
1740
1740
|
|
|
1741
|
-
// @ts-ignore
|
|
1742
1741
|
// follow-redirects does not skip comparison, so it should always succeed for axios -1 unlimited
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1742
|
+
opt.maxBodyLength = opt.maxBodyLength ?? Number.POSITIVE_INFINITY;
|
|
1743
|
+
opt.maxRedirects = opt.maxRedirects ?? 21;
|
|
1744
|
+
if (opt.maxRedirects === 0) opt.followRedirects = false;
|
|
1745
|
+
opt.headers = opt.headers ?? {
|
|
1746
|
+
Accept: 'application/json, text/plain, */*',
|
|
1747
|
+
'User-Agent':
|
|
1748
|
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35',
|
|
1749
|
+
'Accept-Encoding': 'gzip, compress, deflate, br',
|
|
1750
|
+
};
|
|
1748
1751
|
|
|
1749
|
-
R = {
|
|
1752
|
+
R = {opt, cb};
|
|
1750
1753
|
// log({R}, 'init')
|
|
1751
1754
|
} catch (e) {
|
|
1752
1755
|
log.err(e, 'init');
|
|
@@ -1764,24 +1767,24 @@ function init(uri, opts, cb) {
|
|
|
1764
1767
|
maxRedirects: _.maxRedirects,
|
|
1765
1768
|
maxBodyLength: _.maxBodyLength,
|
|
1766
1769
|
* @param {string | Opts} uri /options
|
|
1767
|
-
* @param {Opts | Cb} [
|
|
1770
|
+
* @param {Opts | Cb} [opts] /callback
|
|
1768
1771
|
* @param {Cb} [callback] /null
|
|
1769
1772
|
* @returns {Request}
|
|
1770
1773
|
*/
|
|
1771
|
-
function request(uri,
|
|
1774
|
+
function request(uri, opts, callback) {
|
|
1772
1775
|
let R = null;
|
|
1773
1776
|
|
|
1774
1777
|
try {
|
|
1775
1778
|
// @ts-ignore
|
|
1776
|
-
const {
|
|
1779
|
+
const {opt, cb} = init(uri, opts, callback);
|
|
1777
1780
|
// log.error({uri, options, opts}, 'request')
|
|
1778
1781
|
|
|
1779
|
-
const {data, stream} =
|
|
1782
|
+
const {data, stream} = opt;
|
|
1780
1783
|
// data 在本函数完成处理,不传递到 request
|
|
1781
|
-
|
|
1784
|
+
opt.data = undefined;
|
|
1782
1785
|
|
|
1783
1786
|
// @ts-ignore
|
|
1784
|
-
const req = new Request(
|
|
1787
|
+
const req = new Request(opt, cb);
|
|
1785
1788
|
|
|
1786
1789
|
// 非流模式,自动发送请求,流模式通过流写入发送
|
|
1787
1790
|
if (!stream) {
|
|
@@ -1826,7 +1829,7 @@ function request(uri, options, callback) {
|
|
|
1826
1829
|
}
|
|
1827
1830
|
|
|
1828
1831
|
/**
|
|
1829
|
-
* 执行简单的数据(支持
|
|
1832
|
+
* 执行简单的数据(支持stream)请求
|
|
1830
1833
|
* 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
|
|
1831
1834
|
* 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
|
|
1832
1835
|
* organize params for patch, post, put, head, del
|
package/dist/request.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wia request v3.0.
|
|
2
|
+
* wia request v3.0.33
|
|
3
3
|
* (c) 2022-2024 Sibyl Yu and contributors
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -633,7 +633,7 @@ class Request extends Duplex {
|
|
|
633
633
|
super();
|
|
634
634
|
const m = this;
|
|
635
635
|
|
|
636
|
-
// log({opts}, '
|
|
636
|
+
// log({opts}, 'new Request')
|
|
637
637
|
|
|
638
638
|
// Initialize the request
|
|
639
639
|
m.sanitizeOptions(opts);
|
|
@@ -1448,8 +1448,8 @@ class Request extends Duplex {
|
|
|
1448
1448
|
* @override - 重写父类方法
|
|
1449
1449
|
* @template T - 需要模板
|
|
1450
1450
|
* @param {T & stream.Writable} dest - The writable stream to which data is written.
|
|
1451
|
-
* @param {Object} [
|
|
1452
|
-
* @param {boolean} [
|
|
1451
|
+
* @param {Object} [opt] - Optional configuration object.
|
|
1452
|
+
* @param {boolean} [opt.end=true] - Whether to end the writable stream when the readable stream ends.
|
|
1453
1453
|
* @returns {T} The destination stream.
|
|
1454
1454
|
*/
|
|
1455
1455
|
pipe(dest, opts = {}) {
|
|
@@ -1642,6 +1642,7 @@ const log = log$2({env: `wia:req:${name(import.meta.url)}`}); // __filename
|
|
|
1642
1642
|
* @prop {string} [url]
|
|
1643
1643
|
* @prop {'http:' | 'https:'} [protocol]
|
|
1644
1644
|
* @prop {string} [host]
|
|
1645
|
+
* @prop {string} [hostname]
|
|
1645
1646
|
* @prop {string} [family]
|
|
1646
1647
|
* @prop {string} [path]
|
|
1647
1648
|
* @prop {string} [method]
|
|
@@ -1688,7 +1689,7 @@ utils.createErrorType(
|
|
|
1688
1689
|
* @param {string | Opts} uri/opts
|
|
1689
1690
|
* @param {Opts | Cb} [opts] /cb
|
|
1690
1691
|
* @param {Cb} [cb]
|
|
1691
|
-
* @returns {{
|
|
1692
|
+
* @returns {{opt: Opts, cb: Cb}}
|
|
1692
1693
|
*/
|
|
1693
1694
|
function init(uri, opts, cb) {
|
|
1694
1695
|
let R;
|
|
@@ -1724,26 +1725,28 @@ function init(uri, opts, cb) {
|
|
|
1724
1725
|
}
|
|
1725
1726
|
|
|
1726
1727
|
// copy options
|
|
1727
|
-
|
|
1728
|
+
/** @type {Opts} */
|
|
1729
|
+
const opt = {
|
|
1728
1730
|
// @ts-ignore
|
|
1729
1731
|
...uri,
|
|
1730
1732
|
...opts,
|
|
1731
1733
|
};
|
|
1732
1734
|
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
// @ts-ignore
|
|
1736
|
-
if (opts.method) opts.method = opts.method.toUpperCase();
|
|
1735
|
+
if (!utils.isString(opt.host) && !utils.isString(opt.hostname)) opt.hostname = '::1';
|
|
1736
|
+
opt.method = (opt.method ?? 'get').toUpperCase();
|
|
1737
1737
|
|
|
1738
|
-
// @ts-ignore
|
|
1739
1738
|
// follow-redirects does not skip comparison, so it should always succeed for axios -1 unlimited
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1739
|
+
opt.maxBodyLength = opt.maxBodyLength ?? Number.POSITIVE_INFINITY;
|
|
1740
|
+
opt.maxRedirects = opt.maxRedirects ?? 21;
|
|
1741
|
+
if (opt.maxRedirects === 0) opt.followRedirects = false;
|
|
1742
|
+
opt.headers = opt.headers ?? {
|
|
1743
|
+
Accept: 'application/json, text/plain, */*',
|
|
1744
|
+
'User-Agent':
|
|
1745
|
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35',
|
|
1746
|
+
'Accept-Encoding': 'gzip, compress, deflate, br',
|
|
1747
|
+
};
|
|
1745
1748
|
|
|
1746
|
-
R = {
|
|
1749
|
+
R = {opt, cb};
|
|
1747
1750
|
// log({R}, 'init')
|
|
1748
1751
|
} catch (e) {
|
|
1749
1752
|
log.err(e, 'init');
|
|
@@ -1761,24 +1764,24 @@ function init(uri, opts, cb) {
|
|
|
1761
1764
|
maxRedirects: _.maxRedirects,
|
|
1762
1765
|
maxBodyLength: _.maxBodyLength,
|
|
1763
1766
|
* @param {string | Opts} uri /options
|
|
1764
|
-
* @param {Opts | Cb} [
|
|
1767
|
+
* @param {Opts | Cb} [opts] /callback
|
|
1765
1768
|
* @param {Cb} [callback] /null
|
|
1766
1769
|
* @returns {Request}
|
|
1767
1770
|
*/
|
|
1768
|
-
function request(uri,
|
|
1771
|
+
function request(uri, opts, callback) {
|
|
1769
1772
|
let R = null;
|
|
1770
1773
|
|
|
1771
1774
|
try {
|
|
1772
1775
|
// @ts-ignore
|
|
1773
|
-
const {
|
|
1776
|
+
const {opt, cb} = init(uri, opts, callback);
|
|
1774
1777
|
// log.error({uri, options, opts}, 'request')
|
|
1775
1778
|
|
|
1776
|
-
const {data, stream} =
|
|
1779
|
+
const {data, stream} = opt;
|
|
1777
1780
|
// data 在本函数完成处理,不传递到 request
|
|
1778
|
-
|
|
1781
|
+
opt.data = undefined;
|
|
1779
1782
|
|
|
1780
1783
|
// @ts-ignore
|
|
1781
|
-
const req = new Request(
|
|
1784
|
+
const req = new Request(opt, cb);
|
|
1782
1785
|
|
|
1783
1786
|
// 非流模式,自动发送请求,流模式通过流写入发送
|
|
1784
1787
|
if (!stream) {
|
|
@@ -1823,7 +1826,7 @@ function request(uri, options, callback) {
|
|
|
1823
1826
|
}
|
|
1824
1827
|
|
|
1825
1828
|
/**
|
|
1826
|
-
* 执行简单的数据(支持
|
|
1829
|
+
* 执行简单的数据(支持stream)请求
|
|
1827
1830
|
* 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
|
|
1828
1831
|
* 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
|
|
1829
1832
|
* organize params for patch, post, put, head, del
|
package/lib/index.js
CHANGED
|
@@ -17,6 +17,7 @@ const log = Log({
|
|
|
17
17
|
* @prop {string} [url]
|
|
18
18
|
* @prop {'http:' | 'https:'} [protocol]
|
|
19
19
|
* @prop {string} [host]
|
|
20
|
+
* @prop {string} [hostname]
|
|
20
21
|
* @prop {string} [family]
|
|
21
22
|
* @prop {string} [path]
|
|
22
23
|
* @prop {string} [method]
|
|
@@ -50,7 +51,7 @@ const log = Log({
|
|
|
50
51
|
* @param {string | Opts} uri/opts
|
|
51
52
|
* @param {Opts | Cb} [opts] /cb
|
|
52
53
|
* @param {Cb} [cb]
|
|
53
|
-
* @returns {{
|
|
54
|
+
* @returns {{opt: Opts, cb: Cb}}
|
|
54
55
|
*/ function init(uri, opts, cb) {
|
|
55
56
|
let R;
|
|
56
57
|
try {
|
|
@@ -84,24 +85,24 @@ const log = Log({
|
|
|
84
85
|
opts = {};
|
|
85
86
|
}
|
|
86
87
|
// copy options
|
|
87
|
-
|
|
88
|
+
/** @type {Opts} */ const opt = {
|
|
88
89
|
// @ts-ignore
|
|
89
90
|
...uri,
|
|
90
91
|
...opts
|
|
91
92
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// @ts-ignore
|
|
95
|
-
if (opts.method) opts.method = opts.method.toUpperCase();
|
|
96
|
-
// @ts-ignore
|
|
93
|
+
if (!utils.isString(opt.host) && !utils.isString(opt.hostname)) opt.hostname = '::1';
|
|
94
|
+
opt.method = (opt.method ?? 'get').toUpperCase();
|
|
97
95
|
// follow-redirects does not skip comparison, so it should always succeed for axios -1 unlimited
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
96
|
+
opt.maxBodyLength = opt.maxBodyLength ?? Number.POSITIVE_INFINITY;
|
|
97
|
+
opt.maxRedirects = opt.maxRedirects ?? 21;
|
|
98
|
+
if (opt.maxRedirects === 0) opt.followRedirects = false;
|
|
99
|
+
opt.headers = opt.headers ?? {
|
|
100
|
+
Accept: 'application/json, text/plain, */*',
|
|
101
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.35',
|
|
102
|
+
'Accept-Encoding': 'gzip, compress, deflate, br'
|
|
103
|
+
};
|
|
103
104
|
R = {
|
|
104
|
-
|
|
105
|
+
opt,
|
|
105
106
|
cb
|
|
106
107
|
};
|
|
107
108
|
// log({R}, 'init')
|
|
@@ -119,20 +120,20 @@ const log = Log({
|
|
|
119
120
|
maxRedirects: _.maxRedirects,
|
|
120
121
|
maxBodyLength: _.maxBodyLength,
|
|
121
122
|
* @param {string | Opts} uri /options
|
|
122
|
-
* @param {Opts | Cb} [
|
|
123
|
+
* @param {Opts | Cb} [opts] /callback
|
|
123
124
|
* @param {Cb} [callback] /null
|
|
124
125
|
* @returns {Request}
|
|
125
|
-
*/ function request(uri,
|
|
126
|
+
*/ function request(uri, opts, callback) {
|
|
126
127
|
let R = null;
|
|
127
128
|
try {
|
|
128
129
|
// @ts-ignore
|
|
129
|
-
const {
|
|
130
|
+
const { opt, cb } = init(uri, opts, callback);
|
|
130
131
|
// log.error({uri, options, opts}, 'request')
|
|
131
|
-
const { data, stream } =
|
|
132
|
+
const { data, stream } = opt;
|
|
132
133
|
// data 在本函数完成处理,不传递到 request
|
|
133
|
-
|
|
134
|
+
opt.data = undefined;
|
|
134
135
|
// @ts-ignore
|
|
135
|
-
const req = new Request(
|
|
136
|
+
const req = new Request(opt, cb);
|
|
136
137
|
// 非流模式,自动发送请求,流模式通过流写入发送
|
|
137
138
|
if (!stream) {
|
|
138
139
|
// 发送数据
|
|
@@ -168,7 +169,7 @@ const log = Log({
|
|
|
168
169
|
return R;
|
|
169
170
|
}
|
|
170
171
|
/**
|
|
171
|
-
* 执行简单的数据(支持
|
|
172
|
+
* 执行简单的数据(支持stream)请求
|
|
172
173
|
* 非流模式,直接写入数据流,流模式,由管道触发,或手动调用 end() data.pipe 写入数据
|
|
173
174
|
* 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
|
|
174
175
|
* organize params for patch, post, put, head, del
|
package/lib/request.js
CHANGED
|
@@ -149,7 +149,7 @@ const ConnResetError = utils.createErrorType('ERR_CONNRESET', '连接被重置
|
|
|
149
149
|
, /** @type {stream.Writable[]} */ this.pipedests = [] // pipe dest
|
|
150
150
|
, /** @type {*} */ this.startTimer = null;
|
|
151
151
|
const m = this;
|
|
152
|
-
// log({opts}, '
|
|
152
|
+
// log({opts}, 'new Request')
|
|
153
153
|
// Initialize the request
|
|
154
154
|
m.sanitizeOptions(opts);
|
|
155
155
|
m.opt = opts;
|
|
@@ -818,8 +818,8 @@ const ConnResetError = utils.createErrorType('ERR_CONNRESET', '连接被重置
|
|
|
818
818
|
* @override - 重写父类方法
|
|
819
819
|
* @template T - 需要模板
|
|
820
820
|
* @param {T & stream.Writable} dest - The writable stream to which data is written.
|
|
821
|
-
* @param {Object} [
|
|
822
|
-
* @param {boolean} [
|
|
821
|
+
* @param {Object} [opt] - Optional configuration object.
|
|
822
|
+
* @param {boolean} [opt.end=true] - Whether to end the writable stream when the readable stream ends.
|
|
823
823
|
* @returns {T} The destination stream.
|
|
824
824
|
*/ pipe(dest, opts = {}) {
|
|
825
825
|
const m = this;
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type Opts = {
|
|
|
7
7
|
url?: string;
|
|
8
8
|
protocol?: "http:" | "https:";
|
|
9
9
|
host?: string;
|
|
10
|
+
hostname?: string;
|
|
10
11
|
family?: string;
|
|
11
12
|
path?: string;
|
|
12
13
|
method?: string;
|
|
@@ -24,7 +25,7 @@ export type Opts = {
|
|
|
24
25
|
trackRedirects?: any;
|
|
25
26
|
};
|
|
26
27
|
export type Cb = (res: Response, stream?: stream.Readable) => void;
|
|
27
|
-
declare function request(uri: string | Opts,
|
|
28
|
+
declare function request(uri: string | Opts, opts?: Opts | Cb, callback?: Cb): Request;
|
|
28
29
|
declare namespace request {
|
|
29
30
|
export let get: (url: string | Opts, opts?: Opts | Cb, cb?: Cb) => void;
|
|
30
31
|
export let head: (url: string | Opts, opts?: Opts | Cb, cb?: Cb) => void;
|
package/types/request.d.ts
CHANGED
|
@@ -44,9 +44,7 @@ export default class Request extends stream.Duplex {
|
|
|
44
44
|
processResponse(response: Response): void;
|
|
45
45
|
_isRedirect: boolean;
|
|
46
46
|
processStream(res: Response): Response | stream.Readable;
|
|
47
|
-
override pipe<T>(dest: T & stream.Writable, opts?: {
|
|
48
|
-
end?: boolean;
|
|
49
|
-
}): T;
|
|
47
|
+
override pipe<T>(dest: T & stream.Writable, opts?: {}): T;
|
|
50
48
|
unpipe(dest: stream.Writable): this;
|
|
51
49
|
pipeDest(dest: any): void;
|
|
52
50
|
pause(): this;
|