@jayfong/x-server 2.12.4 → 2.12.6
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/lib/_cjs/services/request.js +31 -13
- package/lib/services/request.d.ts +30 -7
- package/lib/services/request.js +30 -13
- package/package.json +1 -1
|
@@ -11,6 +11,8 @@ var _formstream = _interopRequireDefault(require("formstream"));
|
|
|
11
11
|
|
|
12
12
|
var _got = _interopRequireDefault(require("got"));
|
|
13
13
|
|
|
14
|
+
var _vtils = require("vtils");
|
|
15
|
+
|
|
14
16
|
var _proxyAgent = require("proxy-agent");
|
|
15
17
|
|
|
16
18
|
class RequestFormUrlencoded extends URLSearchParams {}
|
|
@@ -44,13 +46,11 @@ class RequestFormStream extends _formstream.default {
|
|
|
44
46
|
exports.RequestFormStream = RequestFormStream;
|
|
45
47
|
|
|
46
48
|
class RequestService {
|
|
47
|
-
constructor(options) {
|
|
48
|
-
this.options = options;
|
|
49
|
-
this.serviceName = 'request';
|
|
50
|
-
}
|
|
51
|
-
|
|
52
49
|
static getGotOptions(options, responseType) {
|
|
50
|
+
var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
|
|
51
|
+
|
|
53
52
|
const gotOptions = {
|
|
53
|
+
url: options.url,
|
|
54
54
|
responseType: responseType === 'stream' ? undefined : responseType,
|
|
55
55
|
rejectUnauthorized: false,
|
|
56
56
|
isStream: responseType === 'stream'
|
|
@@ -79,12 +79,16 @@ class RequestService {
|
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
if (options.encoding) {
|
|
83
|
-
gotOptions.encoding = options.encoding;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
82
|
if (options.cookieJar) {
|
|
87
83
|
gotOptions.cookieJar = options.cookieJar;
|
|
84
|
+
} // 头处理
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
gotOptions.headers = (0, _vtils.mapKeys)(gotOptions.headers || {}, (v, k) => k.toLowerCase());
|
|
88
|
+
(_gotOptions$headers$_ = (_gotOptions$headers = gotOptions.headers)[_userAgent = 'user-agent']) != null ? _gotOptions$headers$_ : _gotOptions$headers[_userAgent] = (_options$userAgent = options.userAgent) != null ? _options$userAgent : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36';
|
|
89
|
+
|
|
90
|
+
if (options.autoReferer && gotOptions.headers['referer'] == null) {
|
|
91
|
+
gotOptions.headers['referer'] = options.url;
|
|
88
92
|
}
|
|
89
93
|
|
|
90
94
|
return gotOptions;
|
|
@@ -102,8 +106,8 @@ class RequestService {
|
|
|
102
106
|
url = _url.toString();
|
|
103
107
|
}
|
|
104
108
|
|
|
109
|
+
options.url = url;
|
|
105
110
|
const res = (0, _got.default)({
|
|
106
|
-
url: url,
|
|
107
111
|
method: 'GET',
|
|
108
112
|
...this.getGotOptions(options, responseType || 'buffer')
|
|
109
113
|
});
|
|
@@ -127,7 +131,6 @@ class RequestService {
|
|
|
127
131
|
}
|
|
128
132
|
|
|
129
133
|
const res = (0, _got.default)({
|
|
130
|
-
url: options.url,
|
|
131
134
|
method: 'POST',
|
|
132
135
|
...(options.data == null ? {
|
|
133
136
|
body: ''
|
|
@@ -184,15 +187,30 @@ class RequestService {
|
|
|
184
187
|
return err instanceof _got.default.TimeoutError;
|
|
185
188
|
}
|
|
186
189
|
|
|
190
|
+
constructor(options) {
|
|
191
|
+
this.options = options;
|
|
192
|
+
this.serviceName = 'request';
|
|
193
|
+
}
|
|
194
|
+
|
|
187
195
|
getRaw(options, responseType) {
|
|
196
|
+
var _this$options;
|
|
197
|
+
|
|
188
198
|
return RequestService.getRaw({ ...this.options,
|
|
189
|
-
...options
|
|
199
|
+
...options,
|
|
200
|
+
headers: { ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
|
|
201
|
+
...options.headers
|
|
202
|
+
}
|
|
190
203
|
}, responseType);
|
|
191
204
|
}
|
|
192
205
|
|
|
193
206
|
postRaw(options, responseType) {
|
|
207
|
+
var _this$options2;
|
|
208
|
+
|
|
194
209
|
return RequestService.postRaw({ ...this.options,
|
|
195
|
-
...options
|
|
210
|
+
...options,
|
|
211
|
+
headers: { ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
|
|
212
|
+
...options.headers
|
|
213
|
+
}
|
|
196
214
|
}, responseType);
|
|
197
215
|
}
|
|
198
216
|
|
|
@@ -4,19 +4,46 @@ import FormStream from 'formstream';
|
|
|
4
4
|
import Request from 'got/dist/source/core';
|
|
5
5
|
import { BaseService } from './base';
|
|
6
6
|
import { CookieJar } from 'tough-cookie';
|
|
7
|
+
import { OmitStrict } from 'vtils/types';
|
|
7
8
|
export interface RequestServiceOptions {
|
|
9
|
+
/**
|
|
10
|
+
* 请求地址
|
|
11
|
+
*/
|
|
12
|
+
url: string;
|
|
13
|
+
/**
|
|
14
|
+
* 代理地址
|
|
15
|
+
*/
|
|
8
16
|
proxyUrl?: string;
|
|
17
|
+
/**
|
|
18
|
+
* 超时毫秒数
|
|
19
|
+
*/
|
|
9
20
|
timeoutMs?: number;
|
|
21
|
+
/**
|
|
22
|
+
* 头
|
|
23
|
+
*/
|
|
10
24
|
headers?: Record<string, any>;
|
|
11
25
|
/**
|
|
26
|
+
* 是否跟随跳转
|
|
27
|
+
*
|
|
12
28
|
* @default true
|
|
13
29
|
*/
|
|
14
30
|
followRedirect?: boolean;
|
|
15
31
|
/**
|
|
16
|
-
*
|
|
32
|
+
* COOKIE 容器
|
|
17
33
|
*/
|
|
18
|
-
encoding?: BufferEncoding;
|
|
19
34
|
cookieJar?: CookieJar;
|
|
35
|
+
/**
|
|
36
|
+
* 用户代理
|
|
37
|
+
*
|
|
38
|
+
* @default "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
|
|
39
|
+
*/
|
|
40
|
+
userAgent?: string;
|
|
41
|
+
/**
|
|
42
|
+
* 是否自动设置来源地址,设为 true 后若未手动设置来源页则将把请求地址设为来源地址
|
|
43
|
+
*
|
|
44
|
+
* @default false
|
|
45
|
+
*/
|
|
46
|
+
autoReferer?: boolean;
|
|
20
47
|
}
|
|
21
48
|
export interface RequestServiceResponse<T> {
|
|
22
49
|
status: number;
|
|
@@ -24,19 +51,15 @@ export interface RequestServiceResponse<T> {
|
|
|
24
51
|
data: T;
|
|
25
52
|
}
|
|
26
53
|
export interface RequestServiceGetOptions extends RequestServiceOptions {
|
|
27
|
-
url: string;
|
|
28
54
|
data?: Record<string, any>;
|
|
29
55
|
}
|
|
30
56
|
export interface RequestServicePostOptions extends RequestServiceOptions {
|
|
31
|
-
url: string;
|
|
32
57
|
data?: Record<string, any> | FormData | URLSearchParams;
|
|
33
58
|
}
|
|
34
59
|
export interface RequestServiceGetStreamOptions extends RequestServiceOptions {
|
|
35
|
-
url: string;
|
|
36
60
|
data?: Record<string, any>;
|
|
37
61
|
}
|
|
38
62
|
export interface RequestServicePostStreamOptions extends RequestServiceOptions {
|
|
39
|
-
url: string;
|
|
40
63
|
data?: Record<string, any> | FormData | URLSearchParams | FormStream;
|
|
41
64
|
}
|
|
42
65
|
export declare class RequestFormUrlencoded extends URLSearchParams {
|
|
@@ -50,7 +73,6 @@ export declare class RequestFormStream extends FormStream {
|
|
|
50
73
|
export declare class RequestService implements BaseService {
|
|
51
74
|
private options?;
|
|
52
75
|
serviceName: string;
|
|
53
|
-
constructor(options?: RequestServiceOptions);
|
|
54
76
|
private static getGotOptions;
|
|
55
77
|
static getRaw(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<Buffer>>;
|
|
56
78
|
static getRaw(options: RequestServiceGetStreamOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
|
|
@@ -69,6 +91,7 @@ export declare class RequestService implements BaseService {
|
|
|
69
91
|
static getStream(options: RequestServiceGetOptions): Request;
|
|
70
92
|
static postStream(options: RequestServicePostStreamOptions): Request;
|
|
71
93
|
static isTimeoutError(err: any): boolean;
|
|
94
|
+
constructor(options?: OmitStrict<RequestServiceOptions, 'url'>);
|
|
72
95
|
getRaw(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<Buffer>>;
|
|
73
96
|
getRaw(options: RequestServiceGetStreamOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
|
|
74
97
|
getRaw(options: RequestServiceGetStreamOptions, responseType: 'text'): Promise<RequestServiceResponse<string>>;
|
package/lib/services/request.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import FormData from 'form-data';
|
|
2
2
|
import FormStream from 'formstream';
|
|
3
3
|
import got from 'got';
|
|
4
|
+
import { mapKeys } from 'vtils';
|
|
4
5
|
import { ProxyAgent } from 'proxy-agent';
|
|
5
6
|
export class RequestFormUrlencoded extends URLSearchParams {}
|
|
6
7
|
export class RequestFormData extends FormData {
|
|
@@ -24,13 +25,11 @@ export class RequestFormStream extends FormStream {
|
|
|
24
25
|
|
|
25
26
|
}
|
|
26
27
|
export class RequestService {
|
|
27
|
-
constructor(options) {
|
|
28
|
-
this.options = options;
|
|
29
|
-
this.serviceName = 'request';
|
|
30
|
-
}
|
|
31
|
-
|
|
32
28
|
static getGotOptions(options, responseType) {
|
|
29
|
+
var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
|
|
30
|
+
|
|
33
31
|
const gotOptions = {
|
|
32
|
+
url: options.url,
|
|
34
33
|
responseType: responseType === 'stream' ? undefined : responseType,
|
|
35
34
|
rejectUnauthorized: false,
|
|
36
35
|
isStream: responseType === 'stream'
|
|
@@ -59,12 +58,16 @@ export class RequestService {
|
|
|
59
58
|
};
|
|
60
59
|
}
|
|
61
60
|
|
|
62
|
-
if (options.encoding) {
|
|
63
|
-
gotOptions.encoding = options.encoding;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
61
|
if (options.cookieJar) {
|
|
67
62
|
gotOptions.cookieJar = options.cookieJar;
|
|
63
|
+
} // 头处理
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
gotOptions.headers = mapKeys(gotOptions.headers || {}, (v, k) => k.toLowerCase());
|
|
67
|
+
(_gotOptions$headers$_ = (_gotOptions$headers = gotOptions.headers)[_userAgent = 'user-agent']) != null ? _gotOptions$headers$_ : _gotOptions$headers[_userAgent] = (_options$userAgent = options.userAgent) != null ? _options$userAgent : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36';
|
|
68
|
+
|
|
69
|
+
if (options.autoReferer && gotOptions.headers['referer'] == null) {
|
|
70
|
+
gotOptions.headers['referer'] = options.url;
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
return gotOptions;
|
|
@@ -82,8 +85,8 @@ export class RequestService {
|
|
|
82
85
|
url = _url.toString();
|
|
83
86
|
}
|
|
84
87
|
|
|
88
|
+
options.url = url;
|
|
85
89
|
const res = got({
|
|
86
|
-
url: url,
|
|
87
90
|
method: 'GET',
|
|
88
91
|
...this.getGotOptions(options, responseType || 'buffer')
|
|
89
92
|
});
|
|
@@ -107,7 +110,6 @@ export class RequestService {
|
|
|
107
110
|
}
|
|
108
111
|
|
|
109
112
|
const res = got({
|
|
110
|
-
url: options.url,
|
|
111
113
|
method: 'POST',
|
|
112
114
|
...(options.data == null ? {
|
|
113
115
|
body: ''
|
|
@@ -164,15 +166,30 @@ export class RequestService {
|
|
|
164
166
|
return err instanceof got.TimeoutError;
|
|
165
167
|
}
|
|
166
168
|
|
|
169
|
+
constructor(options) {
|
|
170
|
+
this.options = options;
|
|
171
|
+
this.serviceName = 'request';
|
|
172
|
+
}
|
|
173
|
+
|
|
167
174
|
getRaw(options, responseType) {
|
|
175
|
+
var _this$options;
|
|
176
|
+
|
|
168
177
|
return RequestService.getRaw({ ...this.options,
|
|
169
|
-
...options
|
|
178
|
+
...options,
|
|
179
|
+
headers: { ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
|
|
180
|
+
...options.headers
|
|
181
|
+
}
|
|
170
182
|
}, responseType);
|
|
171
183
|
}
|
|
172
184
|
|
|
173
185
|
postRaw(options, responseType) {
|
|
186
|
+
var _this$options2;
|
|
187
|
+
|
|
174
188
|
return RequestService.postRaw({ ...this.options,
|
|
175
|
-
...options
|
|
189
|
+
...options,
|
|
190
|
+
headers: { ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
|
|
191
|
+
...options.headers
|
|
192
|
+
}
|
|
176
193
|
}, responseType);
|
|
177
194
|
}
|
|
178
195
|
|