@jayfong/x-request 2.12.14 → 2.12.15

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/index.js CHANGED
@@ -1,44 +1,29 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
4
  exports.__esModule = true;
6
5
  exports.XRequestFormUrlencoded = exports.XRequestFormStream = exports.XRequestFormFile = exports.XRequestFormData = exports.XRequest = void 0;
7
-
8
6
  var _assert = _interopRequireDefault(require("assert"));
9
-
10
7
  var _formData = _interopRequireDefault(require("form-data"));
11
-
12
8
  var _formstream = _interopRequireDefault(require("formstream"));
13
-
14
9
  var _fs = _interopRequireDefault(require("fs"));
15
-
16
10
  var _got = _interopRequireDefault(require("got"));
17
-
18
11
  var _vtils = require("vtils");
19
-
20
12
  var _proxyAgent = require("proxy-agent");
21
-
22
13
  var _stream = require("stream");
23
-
24
14
  class XRequestFormUrlencoded extends URLSearchParams {}
25
-
26
15
  exports.XRequestFormUrlencoded = XRequestFormUrlencoded;
27
-
28
16
  class XRequestFormData extends _formData.default {
29
17
  constructor(cbOrObject) {
30
18
  super();
31
-
32
19
  if (cbOrObject) {
33
20
  if (typeof cbOrObject === 'function') {
34
21
  cbOrObject(this);
35
22
  } else {
36
23
  Object.keys(cbOrObject).forEach(key => {
37
24
  const value = cbOrObject[key];
38
-
39
25
  if (value instanceof XRequestFormFile) {
40
26
  var _value$options, _value$options2;
41
-
42
27
  this.append(key, typeof value.file === 'string' ? _fs.default.createReadStream(value.file) : value.file, {
43
28
  filename: (_value$options = value.options) == null ? void 0 : _value$options.name,
44
29
  contentType: (_value$options2 = value.options) == null ? void 0 : _value$options2.type
@@ -50,26 +35,20 @@ class XRequestFormData extends _formData.default {
50
35
  }
51
36
  }
52
37
  }
53
-
54
38
  }
55
-
56
39
  exports.XRequestFormData = XRequestFormData;
57
-
58
40
  class XRequestFormStream extends _formstream.default {
59
41
  constructor(cbOrObject) {
60
42
  super();
61
-
62
43
  if (cbOrObject) {
63
44
  if (typeof cbOrObject === 'function') {
64
45
  cbOrObject(this);
65
46
  } else {
66
47
  Object.keys(cbOrObject).forEach(key => {
67
48
  const value = cbOrObject[key];
68
-
69
49
  if (value instanceof XRequestFormFile) {
70
50
  if (typeof value.file === 'string') {
71
51
  var _value$options3;
72
-
73
52
  this.file(key, value.file, (_value$options3 = value.options) == null ? void 0 : _value$options3.name);
74
53
  } else if (Buffer.isBuffer(value.file)) {
75
54
  this.buffer(key, value.file, value.options.name, value.options.type);
@@ -83,29 +62,21 @@ class XRequestFormStream extends _formstream.default {
83
62
  }
84
63
  }
85
64
  }
86
-
87
65
  }
88
-
89
66
  exports.XRequestFormStream = XRequestFormStream;
90
-
91
67
  class XRequestFormFile {
92
68
  constructor(file, options) {
93
69
  this.file = file;
94
70
  this.options = options;
95
-
96
71
  if (Buffer.isBuffer(file) || file instanceof _stream.Readable) {
97
72
  (0, _assert.default)(options == null ? void 0 : options.name, 'options.name is required');
98
73
  }
99
74
  }
100
-
101
75
  }
102
-
103
76
  exports.XRequestFormFile = XRequestFormFile;
104
-
105
77
  class XRequest {
106
78
  static getGotOptions(options, responseType) {
107
79
  var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
108
-
109
80
  const gotOptions = {
110
81
  url: options.url,
111
82
  responseType: responseType === 'stream' ? undefined : responseType,
@@ -114,19 +85,15 @@ class XRequest {
114
85
  },
115
86
  isStream: responseType === 'stream'
116
87
  };
117
-
118
88
  if (options.headers) {
119
89
  gotOptions.headers = options.headers;
120
90
  }
121
-
122
91
  if (options.followRedirect != null) {
123
92
  gotOptions.followRedirect = options.followRedirect;
124
93
  }
125
-
126
94
  if (options.timeoutMs) {
127
95
  gotOptions.timeout = options.timeoutMs;
128
96
  }
129
-
130
97
  if (options.proxyUrl) {
131
98
  const proxyAgent = new _proxyAgent.ProxyAgent({
132
99
  getProxyForUrl: () => options.proxyUrl
@@ -137,58 +104,46 @@ class XRequest {
137
104
  http2: proxyAgent
138
105
  };
139
106
  }
140
-
141
107
  if (options.cookieJar) {
142
108
  gotOptions.cookieJar = options.cookieJar;
143
- } // 头处理
144
-
109
+ }
145
110
 
111
+ // 头处理
146
112
  gotOptions.headers = (0, _vtils.mapKeys)(gotOptions.headers || {}, (v, k) => k.toLowerCase());
147
113
  (_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
114
  if (options.autoReferer && gotOptions.headers['referer'] == null) {
150
115
  gotOptions.headers['referer'] = options.url;
151
116
  }
152
-
153
117
  return gotOptions;
154
118
  }
155
-
156
119
  static getRaw(options, responseType) {
157
120
  let url = options.url;
158
-
159
121
  if (options.data) {
160
122
  const _url = new URL(url);
161
-
162
123
  Object.keys(options.data).forEach(key => {
163
124
  _url.searchParams.set(key, options.data[key]);
164
125
  });
165
126
  url = _url.toString();
166
127
  }
167
-
168
128
  options.url = url;
169
129
  const res = (0, _got.default)({
170
130
  method: 'GET',
171
131
  ...this.getGotOptions(options, responseType || 'buffer')
172
132
  });
173
-
174
133
  if (responseType === 'stream') {
175
134
  return res;
176
135
  }
177
-
178
136
  return res.then(res => ({
179
137
  status: res.statusCode,
180
138
  headers: res.headers,
181
139
  data: res.body
182
140
  }));
183
141
  }
184
-
185
142
  static postRaw(options, responseType) {
186
143
  const gotOptions = this.getGotOptions(options, responseType || 'buffer');
187
-
188
144
  if (responseType === 'stream' && options.data instanceof _formstream.default) {
189
145
  gotOptions.headers = options.data.headers(gotOptions.headers);
190
146
  }
191
-
192
147
  const res = (0, _got.default)({
193
148
  method: 'POST',
194
149
  ...(options.data == null ? {
@@ -202,104 +157,84 @@ class XRequest {
202
157
  }),
203
158
  ...gotOptions
204
159
  });
205
-
206
160
  if (responseType === 'stream' && options.data instanceof _formstream.default) {
207
161
  options.data.pipe(res);
208
162
  }
209
-
210
163
  if (responseType === 'stream') {
211
164
  return res;
212
165
  }
213
-
214
166
  return res.then(res => ({
215
167
  status: res.statusCode,
216
168
  headers: res.headers,
217
169
  data: res.body
218
170
  }));
219
171
  }
220
-
221
172
  static async getText(options) {
222
173
  return this.getRaw(options, 'text');
223
174
  }
224
-
225
175
  static async postText(options) {
226
176
  return this.postRaw(options, 'text');
227
177
  }
228
-
229
178
  static async get(options) {
230
179
  return this.getRaw(options, 'json');
231
180
  }
232
-
233
181
  static async post(options) {
234
182
  return this.postRaw(options, 'json');
235
183
  }
236
-
237
184
  static getStream(options) {
238
185
  return this.getRaw(options, 'stream');
239
186
  }
240
-
241
187
  static postStream(options) {
242
188
  return this.postRaw(options, 'stream');
243
189
  }
244
-
245
190
  static isTimeoutError(err) {
246
191
  return err instanceof _got.default.TimeoutError;
247
192
  }
248
-
249
193
  constructor(options) {
250
194
  this.options = options;
251
195
  }
252
-
253
196
  getRaw(options, responseType) {
254
197
  var _this$options;
255
-
256
- return XRequest.getRaw({ ...this.options,
198
+ return XRequest.getRaw({
199
+ ...this.options,
257
200
  ...options,
258
- headers: { ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
201
+ headers: {
202
+ ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
259
203
  ...options.headers
260
204
  }
261
205
  }, responseType);
262
206
  }
263
-
264
207
  postRaw(options, responseType) {
265
208
  var _this$options2;
266
-
267
- return XRequest.postRaw({ ...this.options,
209
+ return XRequest.postRaw({
210
+ ...this.options,
268
211
  ...options,
269
- headers: { ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
212
+ headers: {
213
+ ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
270
214
  ...options.headers
271
215
  }
272
216
  }, responseType);
273
217
  }
274
-
275
218
  async getText(options) {
276
219
  return this.getRaw(options, 'text');
277
220
  }
278
-
279
221
  async postText(options) {
280
222
  return this.postRaw(options, 'text');
281
223
  }
282
-
283
224
  async get(options) {
284
225
  return this.getRaw(options, 'json');
285
226
  }
286
-
287
227
  async post(options) {
288
228
  return this.postRaw(options, 'json');
289
229
  }
290
-
291
230
  getStream(options) {
292
231
  return this.getRaw(options, 'stream');
293
232
  }
294
-
295
233
  postStream(options) {
296
234
  return this.postRaw(options, 'stream');
297
235
  }
298
-
299
236
  isTimeoutError(err) {
300
237
  return XRequest.isTimeoutError(err);
301
238
  }
302
-
303
239
  }
304
-
305
240
  exports.XRequest = XRequest;
package/lib/index.js CHANGED
@@ -10,17 +10,14 @@ export class XRequestFormUrlencoded extends URLSearchParams {}
10
10
  export class XRequestFormData extends FormData {
11
11
  constructor(cbOrObject) {
12
12
  super();
13
-
14
13
  if (cbOrObject) {
15
14
  if (typeof cbOrObject === 'function') {
16
15
  cbOrObject(this);
17
16
  } else {
18
17
  Object.keys(cbOrObject).forEach(key => {
19
18
  const value = cbOrObject[key];
20
-
21
19
  if (value instanceof XRequestFormFile) {
22
20
  var _value$options, _value$options2;
23
-
24
21
  this.append(key, typeof value.file === 'string' ? fs.createReadStream(value.file) : value.file, {
25
22
  filename: (_value$options = value.options) == null ? void 0 : _value$options.name,
26
23
  contentType: (_value$options2 = value.options) == null ? void 0 : _value$options2.type
@@ -32,23 +29,19 @@ export class XRequestFormData extends FormData {
32
29
  }
33
30
  }
34
31
  }
35
-
36
32
  }
37
33
  export class XRequestFormStream extends FormStream {
38
34
  constructor(cbOrObject) {
39
35
  super();
40
-
41
36
  if (cbOrObject) {
42
37
  if (typeof cbOrObject === 'function') {
43
38
  cbOrObject(this);
44
39
  } else {
45
40
  Object.keys(cbOrObject).forEach(key => {
46
41
  const value = cbOrObject[key];
47
-
48
42
  if (value instanceof XRequestFormFile) {
49
43
  if (typeof value.file === 'string') {
50
44
  var _value$options3;
51
-
52
45
  this.file(key, value.file, (_value$options3 = value.options) == null ? void 0 : _value$options3.name);
53
46
  } else if (Buffer.isBuffer(value.file)) {
54
47
  this.buffer(key, value.file, value.options.name, value.options.type);
@@ -62,23 +55,19 @@ export class XRequestFormStream extends FormStream {
62
55
  }
63
56
  }
64
57
  }
65
-
66
58
  }
67
59
  export class XRequestFormFile {
68
60
  constructor(file, options) {
69
61
  this.file = file;
70
62
  this.options = options;
71
-
72
63
  if (Buffer.isBuffer(file) || file instanceof Readable) {
73
64
  assert(options == null ? void 0 : options.name, 'options.name is required');
74
65
  }
75
66
  }
76
-
77
67
  }
78
68
  export class XRequest {
79
69
  static getGotOptions(options, responseType) {
80
70
  var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
81
-
82
71
  const gotOptions = {
83
72
  url: options.url,
84
73
  responseType: responseType === 'stream' ? undefined : responseType,
@@ -87,19 +76,15 @@ export class XRequest {
87
76
  },
88
77
  isStream: responseType === 'stream'
89
78
  };
90
-
91
79
  if (options.headers) {
92
80
  gotOptions.headers = options.headers;
93
81
  }
94
-
95
82
  if (options.followRedirect != null) {
96
83
  gotOptions.followRedirect = options.followRedirect;
97
84
  }
98
-
99
85
  if (options.timeoutMs) {
100
86
  gotOptions.timeout = options.timeoutMs;
101
87
  }
102
-
103
88
  if (options.proxyUrl) {
104
89
  const proxyAgent = new ProxyAgent({
105
90
  getProxyForUrl: () => options.proxyUrl
@@ -110,58 +95,46 @@ export class XRequest {
110
95
  http2: proxyAgent
111
96
  };
112
97
  }
113
-
114
98
  if (options.cookieJar) {
115
99
  gotOptions.cookieJar = options.cookieJar;
116
- } // 头处理
117
-
100
+ }
118
101
 
102
+ // 头处理
119
103
  gotOptions.headers = mapKeys(gotOptions.headers || {}, (v, k) => k.toLowerCase());
120
104
  (_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
105
  if (options.autoReferer && gotOptions.headers['referer'] == null) {
123
106
  gotOptions.headers['referer'] = options.url;
124
107
  }
125
-
126
108
  return gotOptions;
127
109
  }
128
-
129
110
  static getRaw(options, responseType) {
130
111
  let url = options.url;
131
-
132
112
  if (options.data) {
133
113
  const _url = new URL(url);
134
-
135
114
  Object.keys(options.data).forEach(key => {
136
115
  _url.searchParams.set(key, options.data[key]);
137
116
  });
138
117
  url = _url.toString();
139
118
  }
140
-
141
119
  options.url = url;
142
120
  const res = got({
143
121
  method: 'GET',
144
122
  ...this.getGotOptions(options, responseType || 'buffer')
145
123
  });
146
-
147
124
  if (responseType === 'stream') {
148
125
  return res;
149
126
  }
150
-
151
127
  return res.then(res => ({
152
128
  status: res.statusCode,
153
129
  headers: res.headers,
154
130
  data: res.body
155
131
  }));
156
132
  }
157
-
158
133
  static postRaw(options, responseType) {
159
134
  const gotOptions = this.getGotOptions(options, responseType || 'buffer');
160
-
161
135
  if (responseType === 'stream' && options.data instanceof FormStream) {
162
136
  gotOptions.headers = options.data.headers(gotOptions.headers);
163
137
  }
164
-
165
138
  const res = got({
166
139
  method: 'POST',
167
140
  ...(options.data == null ? {
@@ -175,102 +148,83 @@ export class XRequest {
175
148
  }),
176
149
  ...gotOptions
177
150
  });
178
-
179
151
  if (responseType === 'stream' && options.data instanceof FormStream) {
180
152
  options.data.pipe(res);
181
153
  }
182
-
183
154
  if (responseType === 'stream') {
184
155
  return res;
185
156
  }
186
-
187
157
  return res.then(res => ({
188
158
  status: res.statusCode,
189
159
  headers: res.headers,
190
160
  data: res.body
191
161
  }));
192
162
  }
193
-
194
163
  static async getText(options) {
195
164
  return this.getRaw(options, 'text');
196
165
  }
197
-
198
166
  static async postText(options) {
199
167
  return this.postRaw(options, 'text');
200
168
  }
201
-
202
169
  static async get(options) {
203
170
  return this.getRaw(options, 'json');
204
171
  }
205
-
206
172
  static async post(options) {
207
173
  return this.postRaw(options, 'json');
208
174
  }
209
-
210
175
  static getStream(options) {
211
176
  return this.getRaw(options, 'stream');
212
177
  }
213
-
214
178
  static postStream(options) {
215
179
  return this.postRaw(options, 'stream');
216
180
  }
217
-
218
181
  static isTimeoutError(err) {
219
182
  return err instanceof got.TimeoutError;
220
183
  }
221
-
222
184
  constructor(options) {
223
185
  this.options = options;
224
186
  }
225
-
226
187
  getRaw(options, responseType) {
227
188
  var _this$options;
228
-
229
- return XRequest.getRaw({ ...this.options,
189
+ return XRequest.getRaw({
190
+ ...this.options,
230
191
  ...options,
231
- headers: { ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
192
+ headers: {
193
+ ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
232
194
  ...options.headers
233
195
  }
234
196
  }, responseType);
235
197
  }
236
-
237
198
  postRaw(options, responseType) {
238
199
  var _this$options2;
239
-
240
- return XRequest.postRaw({ ...this.options,
200
+ return XRequest.postRaw({
201
+ ...this.options,
241
202
  ...options,
242
- headers: { ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
203
+ headers: {
204
+ ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
243
205
  ...options.headers
244
206
  }
245
207
  }, responseType);
246
208
  }
247
-
248
209
  async getText(options) {
249
210
  return this.getRaw(options, 'text');
250
211
  }
251
-
252
212
  async postText(options) {
253
213
  return this.postRaw(options, 'text');
254
214
  }
255
-
256
215
  async get(options) {
257
216
  return this.getRaw(options, 'json');
258
217
  }
259
-
260
218
  async post(options) {
261
219
  return this.postRaw(options, 'json');
262
220
  }
263
-
264
221
  getStream(options) {
265
222
  return this.getRaw(options, 'stream');
266
223
  }
267
-
268
224
  postStream(options) {
269
225
  return this.postRaw(options, 'stream');
270
226
  }
271
-
272
227
  isTimeoutError(err) {
273
228
  return XRequest.isTimeoutError(err);
274
229
  }
275
-
276
230
  }
package/package.json CHANGED
@@ -1,9 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-request",
3
- "version": "2.12.14",
4
- "publishConfig": {
5
- "access": "public"
6
- },
3
+ "version": "2.12.15",
7
4
  "license": "ISC",
8
5
  "sideEffects": false,
9
6
  "main": "lib/_cjs/index.js",
@@ -24,7 +21,10 @@
24
21
  "vtils": "^4.85.3"
25
22
  },
26
23
  "devDependencies": {
27
- "get-port": "^7.0.0",
24
+ "get-port": "^5.1.1",
28
25
  "proxy": "^2.1.1"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
29
  }
30
30
  }