@jayfong/x-request 2.12.14

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.
@@ -0,0 +1,305 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+
5
+ exports.__esModule = true;
6
+ exports.XRequestFormUrlencoded = exports.XRequestFormStream = exports.XRequestFormFile = exports.XRequestFormData = exports.XRequest = void 0;
7
+
8
+ var _assert = _interopRequireDefault(require("assert"));
9
+
10
+ var _formData = _interopRequireDefault(require("form-data"));
11
+
12
+ var _formstream = _interopRequireDefault(require("formstream"));
13
+
14
+ var _fs = _interopRequireDefault(require("fs"));
15
+
16
+ var _got = _interopRequireDefault(require("got"));
17
+
18
+ var _vtils = require("vtils");
19
+
20
+ var _proxyAgent = require("proxy-agent");
21
+
22
+ var _stream = require("stream");
23
+
24
+ class XRequestFormUrlencoded extends URLSearchParams {}
25
+
26
+ exports.XRequestFormUrlencoded = XRequestFormUrlencoded;
27
+
28
+ class XRequestFormData extends _formData.default {
29
+ constructor(cbOrObject) {
30
+ super();
31
+
32
+ if (cbOrObject) {
33
+ if (typeof cbOrObject === 'function') {
34
+ cbOrObject(this);
35
+ } else {
36
+ Object.keys(cbOrObject).forEach(key => {
37
+ const value = cbOrObject[key];
38
+
39
+ if (value instanceof XRequestFormFile) {
40
+ var _value$options, _value$options2;
41
+
42
+ this.append(key, typeof value.file === 'string' ? _fs.default.createReadStream(value.file) : value.file, {
43
+ filename: (_value$options = value.options) == null ? void 0 : _value$options.name,
44
+ contentType: (_value$options2 = value.options) == null ? void 0 : _value$options2.type
45
+ });
46
+ } else {
47
+ this.append(key, value);
48
+ }
49
+ });
50
+ }
51
+ }
52
+ }
53
+
54
+ }
55
+
56
+ exports.XRequestFormData = XRequestFormData;
57
+
58
+ class XRequestFormStream extends _formstream.default {
59
+ constructor(cbOrObject) {
60
+ super();
61
+
62
+ if (cbOrObject) {
63
+ if (typeof cbOrObject === 'function') {
64
+ cbOrObject(this);
65
+ } else {
66
+ Object.keys(cbOrObject).forEach(key => {
67
+ const value = cbOrObject[key];
68
+
69
+ if (value instanceof XRequestFormFile) {
70
+ if (typeof value.file === 'string') {
71
+ var _value$options3;
72
+
73
+ this.file(key, value.file, (_value$options3 = value.options) == null ? void 0 : _value$options3.name);
74
+ } else if (Buffer.isBuffer(value.file)) {
75
+ this.buffer(key, value.file, value.options.name, value.options.type);
76
+ } else if (value.file instanceof _stream.Readable) {
77
+ this.stream(key, value.file, value.options.name, value.options.type);
78
+ }
79
+ } else {
80
+ this.field(key, value);
81
+ }
82
+ });
83
+ }
84
+ }
85
+ }
86
+
87
+ }
88
+
89
+ exports.XRequestFormStream = XRequestFormStream;
90
+
91
+ class XRequestFormFile {
92
+ constructor(file, options) {
93
+ this.file = file;
94
+ this.options = options;
95
+
96
+ if (Buffer.isBuffer(file) || file instanceof _stream.Readable) {
97
+ (0, _assert.default)(options == null ? void 0 : options.name, 'options.name is required');
98
+ }
99
+ }
100
+
101
+ }
102
+
103
+ exports.XRequestFormFile = XRequestFormFile;
104
+
105
+ class XRequest {
106
+ static getGotOptions(options, responseType) {
107
+ var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
108
+
109
+ const gotOptions = {
110
+ url: options.url,
111
+ responseType: responseType === 'stream' ? undefined : responseType,
112
+ https: {
113
+ rejectUnauthorized: false
114
+ },
115
+ isStream: responseType === 'stream'
116
+ };
117
+
118
+ if (options.headers) {
119
+ gotOptions.headers = options.headers;
120
+ }
121
+
122
+ if (options.followRedirect != null) {
123
+ gotOptions.followRedirect = options.followRedirect;
124
+ }
125
+
126
+ if (options.timeoutMs) {
127
+ gotOptions.timeout = options.timeoutMs;
128
+ }
129
+
130
+ if (options.proxyUrl) {
131
+ const proxyAgent = new _proxyAgent.ProxyAgent({
132
+ getProxyForUrl: () => options.proxyUrl
133
+ });
134
+ gotOptions.agent = {
135
+ http: proxyAgent,
136
+ https: proxyAgent,
137
+ http2: proxyAgent
138
+ };
139
+ }
140
+
141
+ if (options.cookieJar) {
142
+ gotOptions.cookieJar = options.cookieJar;
143
+ } // 头处理
144
+
145
+
146
+ gotOptions.headers = (0, _vtils.mapKeys)(gotOptions.headers || {}, (v, k) => k.toLowerCase());
147
+ (_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';
148
+
149
+ if (options.autoReferer && gotOptions.headers['referer'] == null) {
150
+ gotOptions.headers['referer'] = options.url;
151
+ }
152
+
153
+ return gotOptions;
154
+ }
155
+
156
+ static getRaw(options, responseType) {
157
+ let url = options.url;
158
+
159
+ if (options.data) {
160
+ const _url = new URL(url);
161
+
162
+ Object.keys(options.data).forEach(key => {
163
+ _url.searchParams.set(key, options.data[key]);
164
+ });
165
+ url = _url.toString();
166
+ }
167
+
168
+ options.url = url;
169
+ const res = (0, _got.default)({
170
+ method: 'GET',
171
+ ...this.getGotOptions(options, responseType || 'buffer')
172
+ });
173
+
174
+ if (responseType === 'stream') {
175
+ return res;
176
+ }
177
+
178
+ return res.then(res => ({
179
+ status: res.statusCode,
180
+ headers: res.headers,
181
+ data: res.body
182
+ }));
183
+ }
184
+
185
+ static postRaw(options, responseType) {
186
+ const gotOptions = this.getGotOptions(options, responseType || 'buffer');
187
+
188
+ if (responseType === 'stream' && options.data instanceof _formstream.default) {
189
+ gotOptions.headers = options.data.headers(gotOptions.headers);
190
+ }
191
+
192
+ const res = (0, _got.default)({
193
+ method: 'POST',
194
+ ...(options.data == null ? {
195
+ body: ''
196
+ } : options.data instanceof _formData.default ? {
197
+ body: options.data
198
+ } : options.data instanceof URLSearchParams ? {
199
+ form: options.data
200
+ } : options.data instanceof _formstream.default ? {} : {
201
+ json: options.data
202
+ }),
203
+ ...gotOptions
204
+ });
205
+
206
+ if (responseType === 'stream' && options.data instanceof _formstream.default) {
207
+ options.data.pipe(res);
208
+ }
209
+
210
+ if (responseType === 'stream') {
211
+ return res;
212
+ }
213
+
214
+ return res.then(res => ({
215
+ status: res.statusCode,
216
+ headers: res.headers,
217
+ data: res.body
218
+ }));
219
+ }
220
+
221
+ static async getText(options) {
222
+ return this.getRaw(options, 'text');
223
+ }
224
+
225
+ static async postText(options) {
226
+ return this.postRaw(options, 'text');
227
+ }
228
+
229
+ static async get(options) {
230
+ return this.getRaw(options, 'json');
231
+ }
232
+
233
+ static async post(options) {
234
+ return this.postRaw(options, 'json');
235
+ }
236
+
237
+ static getStream(options) {
238
+ return this.getRaw(options, 'stream');
239
+ }
240
+
241
+ static postStream(options) {
242
+ return this.postRaw(options, 'stream');
243
+ }
244
+
245
+ static isTimeoutError(err) {
246
+ return err instanceof _got.default.TimeoutError;
247
+ }
248
+
249
+ constructor(options) {
250
+ this.options = options;
251
+ }
252
+
253
+ getRaw(options, responseType) {
254
+ var _this$options;
255
+
256
+ return XRequest.getRaw({ ...this.options,
257
+ ...options,
258
+ headers: { ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
259
+ ...options.headers
260
+ }
261
+ }, responseType);
262
+ }
263
+
264
+ postRaw(options, responseType) {
265
+ var _this$options2;
266
+
267
+ return XRequest.postRaw({ ...this.options,
268
+ ...options,
269
+ headers: { ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
270
+ ...options.headers
271
+ }
272
+ }, responseType);
273
+ }
274
+
275
+ async getText(options) {
276
+ return this.getRaw(options, 'text');
277
+ }
278
+
279
+ async postText(options) {
280
+ return this.postRaw(options, 'text');
281
+ }
282
+
283
+ async get(options) {
284
+ return this.getRaw(options, 'json');
285
+ }
286
+
287
+ async post(options) {
288
+ return this.postRaw(options, 'json');
289
+ }
290
+
291
+ getStream(options) {
292
+ return this.getRaw(options, 'stream');
293
+ }
294
+
295
+ postStream(options) {
296
+ return this.postRaw(options, 'stream');
297
+ }
298
+
299
+ isTimeoutError(err) {
300
+ return XRequest.isTimeoutError(err);
301
+ }
302
+
303
+ }
304
+
305
+ exports.XRequest = XRequest;
@@ -0,0 +1 @@
1
+ "use strict";
package/lib/index.d.ts ADDED
@@ -0,0 +1,131 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import FormData from 'form-data';
4
+ import FormStream from 'formstream';
5
+ import Request from 'got/dist/source/core';
6
+ import { CookieJar } from 'tough-cookie';
7
+ import { OmitStrict } from 'vtils/types';
8
+ import { Readable } from 'stream';
9
+ export interface XRequestOptions {
10
+ /**
11
+ * 请求地址
12
+ */
13
+ url: string;
14
+ /**
15
+ * 代理地址
16
+ */
17
+ proxyUrl?: string;
18
+ /**
19
+ * 超时毫秒数
20
+ */
21
+ timeoutMs?: number;
22
+ /**
23
+ * 头
24
+ */
25
+ headers?: Record<string, any>;
26
+ /**
27
+ * 是否跟随跳转
28
+ *
29
+ * @default true
30
+ */
31
+ followRedirect?: boolean;
32
+ /**
33
+ * COOKIE 容器
34
+ */
35
+ cookieJar?: CookieJar;
36
+ /**
37
+ * 用户代理
38
+ *
39
+ * @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"
40
+ */
41
+ userAgent?: string;
42
+ /**
43
+ * 是否自动设置来源地址,设为 true 后若未手动设置来源页则将把请求地址设为来源地址
44
+ *
45
+ * @default false
46
+ */
47
+ autoReferer?: boolean;
48
+ }
49
+ export interface XRequestResponse<T> {
50
+ status: number;
51
+ headers: Record<string, any>;
52
+ data: T;
53
+ }
54
+ export interface XRequestGetOptions extends XRequestOptions {
55
+ data?: Record<string, any>;
56
+ }
57
+ export interface XRequestPostOptions extends XRequestOptions {
58
+ data?: Record<string, any> | FormData | URLSearchParams;
59
+ }
60
+ export interface XRequestGetStreamOptions extends XRequestOptions {
61
+ data?: Record<string, any>;
62
+ }
63
+ export interface XRequestPostStreamOptions extends XRequestOptions {
64
+ data?: Record<string, any> | FormData | URLSearchParams | FormStream;
65
+ }
66
+ export declare class XRequestFormUrlencoded extends URLSearchParams {
67
+ }
68
+ export declare class XRequestFormData extends FormData {
69
+ constructor(cbOrObject?: ((formData: FormData) => FormData) | Record<string, any>);
70
+ }
71
+ export declare class XRequestFormStream extends FormStream {
72
+ constructor(cbOrObject?: ((formStream: FormStream) => FormStream) | Record<string, any>);
73
+ }
74
+ export declare class XRequestFormFile {
75
+ file: any;
76
+ options?: {
77
+ name?: string;
78
+ type?: string;
79
+ };
80
+ constructor(file: Buffer, options: {
81
+ name: string;
82
+ type?: string;
83
+ });
84
+ constructor(file: Readable, options: {
85
+ name: string;
86
+ type?: string;
87
+ });
88
+ constructor(file: string, options?: {
89
+ name?: string;
90
+ type?: string;
91
+ });
92
+ }
93
+ export declare class XRequest {
94
+ private options?;
95
+ private static getGotOptions;
96
+ static getRaw(options: XRequestGetStreamOptions): Promise<XRequestResponse<Buffer>>;
97
+ static getRaw(options: XRequestGetStreamOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
98
+ static getRaw(options: XRequestGetStreamOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
99
+ static getRaw<T>(options: XRequestGetStreamOptions, responseType: 'json'): Promise<XRequestResponse<T>>;
100
+ static getRaw(options: XRequestGetStreamOptions, responseType: 'stream'): Request;
101
+ static postRaw(options: XRequestPostOptions): Promise<XRequestResponse<Buffer>>;
102
+ static postRaw(options: XRequestPostOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
103
+ static postRaw(options: XRequestPostOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
104
+ static postRaw<T>(options: XRequestPostOptions, responseType: 'json'): Promise<XRequestResponse<T>>;
105
+ static postRaw(options: XRequestPostOptions, responseType: 'stream'): Request;
106
+ static getText(options: XRequestGetStreamOptions): Promise<XRequestResponse<string>>;
107
+ static postText(options: XRequestPostOptions): Promise<XRequestResponse<string>>;
108
+ static get<T>(options: XRequestGetStreamOptions): Promise<XRequestResponse<T>>;
109
+ static post<T>(options: XRequestPostOptions): Promise<XRequestResponse<T>>;
110
+ static getStream(options: XRequestGetOptions): Request;
111
+ static postStream(options: XRequestPostStreamOptions): Request;
112
+ static isTimeoutError(err: any): boolean;
113
+ constructor(options?: OmitStrict<XRequestOptions, 'url'>);
114
+ getRaw(options: XRequestGetStreamOptions): Promise<XRequestResponse<Buffer>>;
115
+ getRaw(options: XRequestGetStreamOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
116
+ getRaw(options: XRequestGetStreamOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
117
+ getRaw<T>(options: XRequestGetStreamOptions, responseType: 'json'): Promise<XRequestResponse<T>>;
118
+ getRaw(options: XRequestGetStreamOptions, responseType: 'stream'): Request;
119
+ postRaw(options: XRequestPostOptions): Promise<XRequestResponse<Buffer>>;
120
+ postRaw(options: XRequestPostOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
121
+ postRaw(options: XRequestPostOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
122
+ postRaw<T>(options: XRequestPostOptions, responseType: 'json'): Promise<XRequestResponse<T>>;
123
+ postRaw(options: XRequestPostOptions, responseType: 'stream'): Request;
124
+ getText(options: XRequestGetStreamOptions): Promise<XRequestResponse<string>>;
125
+ postText(options: XRequestPostOptions): Promise<XRequestResponse<string>>;
126
+ get<T>(options: XRequestGetStreamOptions): Promise<XRequestResponse<T>>;
127
+ post<T>(options: XRequestPostOptions): Promise<XRequestResponse<T>>;
128
+ getStream(options: XRequestGetOptions): Request;
129
+ postStream(options: XRequestPostStreamOptions): Request;
130
+ isTimeoutError(err: any): boolean;
131
+ }
package/lib/index.js ADDED
@@ -0,0 +1,276 @@
1
+ import assert from 'assert';
2
+ import FormData from 'form-data';
3
+ import FormStream from 'formstream';
4
+ import fs from 'fs';
5
+ import got from 'got';
6
+ import { mapKeys } from 'vtils';
7
+ import { ProxyAgent } from 'proxy-agent';
8
+ import { Readable } from 'stream';
9
+ export class XRequestFormUrlencoded extends URLSearchParams {}
10
+ export class XRequestFormData extends FormData {
11
+ constructor(cbOrObject) {
12
+ super();
13
+
14
+ if (cbOrObject) {
15
+ if (typeof cbOrObject === 'function') {
16
+ cbOrObject(this);
17
+ } else {
18
+ Object.keys(cbOrObject).forEach(key => {
19
+ const value = cbOrObject[key];
20
+
21
+ if (value instanceof XRequestFormFile) {
22
+ var _value$options, _value$options2;
23
+
24
+ this.append(key, typeof value.file === 'string' ? fs.createReadStream(value.file) : value.file, {
25
+ filename: (_value$options = value.options) == null ? void 0 : _value$options.name,
26
+ contentType: (_value$options2 = value.options) == null ? void 0 : _value$options2.type
27
+ });
28
+ } else {
29
+ this.append(key, value);
30
+ }
31
+ });
32
+ }
33
+ }
34
+ }
35
+
36
+ }
37
+ export class XRequestFormStream extends FormStream {
38
+ constructor(cbOrObject) {
39
+ super();
40
+
41
+ if (cbOrObject) {
42
+ if (typeof cbOrObject === 'function') {
43
+ cbOrObject(this);
44
+ } else {
45
+ Object.keys(cbOrObject).forEach(key => {
46
+ const value = cbOrObject[key];
47
+
48
+ if (value instanceof XRequestFormFile) {
49
+ if (typeof value.file === 'string') {
50
+ var _value$options3;
51
+
52
+ this.file(key, value.file, (_value$options3 = value.options) == null ? void 0 : _value$options3.name);
53
+ } else if (Buffer.isBuffer(value.file)) {
54
+ this.buffer(key, value.file, value.options.name, value.options.type);
55
+ } else if (value.file instanceof Readable) {
56
+ this.stream(key, value.file, value.options.name, value.options.type);
57
+ }
58
+ } else {
59
+ this.field(key, value);
60
+ }
61
+ });
62
+ }
63
+ }
64
+ }
65
+
66
+ }
67
+ export class XRequestFormFile {
68
+ constructor(file, options) {
69
+ this.file = file;
70
+ this.options = options;
71
+
72
+ if (Buffer.isBuffer(file) || file instanceof Readable) {
73
+ assert(options == null ? void 0 : options.name, 'options.name is required');
74
+ }
75
+ }
76
+
77
+ }
78
+ export class XRequest {
79
+ static getGotOptions(options, responseType) {
80
+ var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
81
+
82
+ const gotOptions = {
83
+ url: options.url,
84
+ responseType: responseType === 'stream' ? undefined : responseType,
85
+ https: {
86
+ rejectUnauthorized: false
87
+ },
88
+ isStream: responseType === 'stream'
89
+ };
90
+
91
+ if (options.headers) {
92
+ gotOptions.headers = options.headers;
93
+ }
94
+
95
+ if (options.followRedirect != null) {
96
+ gotOptions.followRedirect = options.followRedirect;
97
+ }
98
+
99
+ if (options.timeoutMs) {
100
+ gotOptions.timeout = options.timeoutMs;
101
+ }
102
+
103
+ if (options.proxyUrl) {
104
+ const proxyAgent = new ProxyAgent({
105
+ getProxyForUrl: () => options.proxyUrl
106
+ });
107
+ gotOptions.agent = {
108
+ http: proxyAgent,
109
+ https: proxyAgent,
110
+ http2: proxyAgent
111
+ };
112
+ }
113
+
114
+ if (options.cookieJar) {
115
+ gotOptions.cookieJar = options.cookieJar;
116
+ } // 头处理
117
+
118
+
119
+ gotOptions.headers = mapKeys(gotOptions.headers || {}, (v, k) => k.toLowerCase());
120
+ (_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';
121
+
122
+ if (options.autoReferer && gotOptions.headers['referer'] == null) {
123
+ gotOptions.headers['referer'] = options.url;
124
+ }
125
+
126
+ return gotOptions;
127
+ }
128
+
129
+ static getRaw(options, responseType) {
130
+ let url = options.url;
131
+
132
+ if (options.data) {
133
+ const _url = new URL(url);
134
+
135
+ Object.keys(options.data).forEach(key => {
136
+ _url.searchParams.set(key, options.data[key]);
137
+ });
138
+ url = _url.toString();
139
+ }
140
+
141
+ options.url = url;
142
+ const res = got({
143
+ method: 'GET',
144
+ ...this.getGotOptions(options, responseType || 'buffer')
145
+ });
146
+
147
+ if (responseType === 'stream') {
148
+ return res;
149
+ }
150
+
151
+ return res.then(res => ({
152
+ status: res.statusCode,
153
+ headers: res.headers,
154
+ data: res.body
155
+ }));
156
+ }
157
+
158
+ static postRaw(options, responseType) {
159
+ const gotOptions = this.getGotOptions(options, responseType || 'buffer');
160
+
161
+ if (responseType === 'stream' && options.data instanceof FormStream) {
162
+ gotOptions.headers = options.data.headers(gotOptions.headers);
163
+ }
164
+
165
+ const res = got({
166
+ method: 'POST',
167
+ ...(options.data == null ? {
168
+ body: ''
169
+ } : options.data instanceof FormData ? {
170
+ body: options.data
171
+ } : options.data instanceof URLSearchParams ? {
172
+ form: options.data
173
+ } : options.data instanceof FormStream ? {} : {
174
+ json: options.data
175
+ }),
176
+ ...gotOptions
177
+ });
178
+
179
+ if (responseType === 'stream' && options.data instanceof FormStream) {
180
+ options.data.pipe(res);
181
+ }
182
+
183
+ if (responseType === 'stream') {
184
+ return res;
185
+ }
186
+
187
+ return res.then(res => ({
188
+ status: res.statusCode,
189
+ headers: res.headers,
190
+ data: res.body
191
+ }));
192
+ }
193
+
194
+ static async getText(options) {
195
+ return this.getRaw(options, 'text');
196
+ }
197
+
198
+ static async postText(options) {
199
+ return this.postRaw(options, 'text');
200
+ }
201
+
202
+ static async get(options) {
203
+ return this.getRaw(options, 'json');
204
+ }
205
+
206
+ static async post(options) {
207
+ return this.postRaw(options, 'json');
208
+ }
209
+
210
+ static getStream(options) {
211
+ return this.getRaw(options, 'stream');
212
+ }
213
+
214
+ static postStream(options) {
215
+ return this.postRaw(options, 'stream');
216
+ }
217
+
218
+ static isTimeoutError(err) {
219
+ return err instanceof got.TimeoutError;
220
+ }
221
+
222
+ constructor(options) {
223
+ this.options = options;
224
+ }
225
+
226
+ getRaw(options, responseType) {
227
+ var _this$options;
228
+
229
+ return XRequest.getRaw({ ...this.options,
230
+ ...options,
231
+ headers: { ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
232
+ ...options.headers
233
+ }
234
+ }, responseType);
235
+ }
236
+
237
+ postRaw(options, responseType) {
238
+ var _this$options2;
239
+
240
+ return XRequest.postRaw({ ...this.options,
241
+ ...options,
242
+ headers: { ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
243
+ ...options.headers
244
+ }
245
+ }, responseType);
246
+ }
247
+
248
+ async getText(options) {
249
+ return this.getRaw(options, 'text');
250
+ }
251
+
252
+ async postText(options) {
253
+ return this.postRaw(options, 'text');
254
+ }
255
+
256
+ async get(options) {
257
+ return this.getRaw(options, 'json');
258
+ }
259
+
260
+ async post(options) {
261
+ return this.postRaw(options, 'json');
262
+ }
263
+
264
+ getStream(options) {
265
+ return this.getRaw(options, 'stream');
266
+ }
267
+
268
+ postStream(options) {
269
+ return this.postRaw(options, 'stream');
270
+ }
271
+
272
+ isTimeoutError(err) {
273
+ return XRequest.isTimeoutError(err);
274
+ }
275
+
276
+ }
@@ -0,0 +1,49 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ declare module 'formstream' {
4
+ import { Stream, Readable } from 'stream';
5
+ class FormStream extends Stream {
6
+ /**
7
+ * Add a normal field to the form.
8
+ *
9
+ * @param name Name of field
10
+ * @param value Value of field
11
+ */
12
+ field(name: string, value: string): this;
13
+ /**
14
+ * Add a local file to be uploaded to the form.
15
+ *
16
+ * @param name Name of file field
17
+ * @param filepath Local path of the file to be uploaded
18
+ * @param filename Name of the file (will be the base name of filepath if empty)
19
+ * @param filesize Size of the file (will not generate Content-Length header if not specified)
20
+ */
21
+ file(name: string, filepath: string, filename?: string, filesize?: number): this;
22
+ /**
23
+ * Add a buffer as a file to upload.
24
+ *
25
+ * @param name Name of field
26
+ * @param buffer The buffer to be uploaded
27
+ * @param filename The file name that tells the remote server
28
+ * @param contentType Content-Type (aka. MIME Type) of content (will be infered with filename if empty)
29
+ */
30
+ buffer(name: string, buffer: Buffer, filename: string, contentType?: string): this;
31
+ /**
32
+ * Add a readable stream as a file to upload. Event 'error' will be emitted if an error occured.
33
+ *
34
+ * @param name Name of field
35
+ * @param stream A readable stream to be piped
36
+ * @param filename The file name that tells the remote server
37
+ * @param contentType Content-Type (aka. MIME Type) of content (will be infered with filename if empty)
38
+ * @param size Size of the stream (will not generate Content-Length header if not specified)
39
+ */
40
+ stream(name: string, stream: Readable, filename: string, contentType?: string, size?: number): this;
41
+ /**
42
+ * Get headers for the request.
43
+ *
44
+ * @param additionalHeaders Additional headers
45
+ */
46
+ headers(additionalHeaders?: Record<string, any>): Record<string, any>;
47
+ }
48
+ export = FormStream;
49
+ }
package/lib/typings.js ADDED
File without changes
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@jayfong/x-request",
3
+ "version": "2.12.14",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "license": "ISC",
8
+ "sideEffects": false,
9
+ "main": "lib/_cjs/index.js",
10
+ "module": "lib/index.js",
11
+ "types": "lib/index.d.ts",
12
+ "files": [
13
+ "lib"
14
+ ],
15
+ "scripts": {
16
+ "build": "haoma compile"
17
+ },
18
+ "dependencies": {
19
+ "form-data": "^4.0.0",
20
+ "formstream": "^1.2.0",
21
+ "got": "^11.8.2",
22
+ "proxy-agent": "^6.2.2",
23
+ "tough-cookie": "^4.1.3",
24
+ "vtils": "^4.85.3"
25
+ },
26
+ "devDependencies": {
27
+ "get-port": "^7.0.0",
28
+ "proxy": "^2.1.1"
29
+ }
30
+ }