@jayfong/x-request 2.14.0 → 2.15.0

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
@@ -77,6 +77,16 @@ exports.XRequestFormFile = XRequestFormFile;
77
77
  class XRequest {
78
78
  static getGotOptions(options, responseType) {
79
79
  var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
80
+ let url = options.url;
81
+ if (options.query) {
82
+ const _url = new URL(url);
83
+ const query = options.query instanceof URLSearchParams ? options.query : new URLSearchParams(options.query);
84
+ query.forEach((v, k) => {
85
+ _url.searchParams.set(k, v);
86
+ });
87
+ url = _url.toString();
88
+ }
89
+ options.url = url;
80
90
  const gotOptions = {
81
91
  url: options.url,
82
92
  responseType: responseType === 'stream' ? undefined : responseType,
@@ -117,15 +127,6 @@ class XRequest {
117
127
  return gotOptions;
118
128
  }
119
129
  static get(options, responseType) {
120
- let url = options.url;
121
- if (options.data) {
122
- const _url = new URL(url);
123
- Object.keys(options.data).forEach(key => {
124
- _url.searchParams.set(key, options.data[key]);
125
- });
126
- url = _url.toString();
127
- }
128
- options.url = url;
129
130
  const res = (0, _got.default)({
130
131
  method: 'GET',
131
132
  ...this.getGotOptions(options, responseType || 'buffer')
@@ -209,25 +210,15 @@ class XRequest {
209
210
  this.options = options;
210
211
  }
211
212
  get(options, responseType) {
212
- var _this$options;
213
213
  return XRequest.get({
214
214
  ...this.options,
215
- ...options,
216
- headers: {
217
- ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
218
- ...options.headers
219
- }
215
+ ...options
220
216
  }, responseType);
221
217
  }
222
218
  post(options, responseType) {
223
- var _this$options2;
224
219
  return XRequest.post({
225
220
  ...this.options,
226
- ...options,
227
- headers: {
228
- ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
229
- ...options.headers
230
- }
221
+ ...options
231
222
  }, responseType);
232
223
  }
233
224
  async getText(options) {
package/lib/index.d.ts CHANGED
@@ -11,6 +11,10 @@ export interface XRequestOptions {
11
11
  * 请求地址
12
12
  */
13
13
  url: string;
14
+ /**
15
+ * 查询参数
16
+ */
17
+ query?: URLSearchParams | ConstructorParameters<typeof URLSearchParams>[0];
14
18
  /**
15
19
  * 代理地址
16
20
  */
@@ -52,13 +56,11 @@ export interface XRequestResponse<T> {
52
56
  data: T;
53
57
  }
54
58
  export interface XRequestGetOptions extends XRequestOptions {
55
- data?: Record<string, any>;
56
59
  }
57
60
  export interface XRequestPostOptions extends XRequestOptions {
58
61
  data?: Record<string, any> | FormData | URLSearchParams;
59
62
  }
60
63
  export interface XRequestGetStreamOptions extends XRequestOptions {
61
- data?: Record<string, any>;
62
64
  }
63
65
  export interface XRequestPostStreamOptions extends XRequestOptions {
64
66
  data?: Record<string, any> | FormData | URLSearchParams | FormStream;
package/lib/index.js CHANGED
@@ -68,6 +68,16 @@ export class XRequestFormFile {
68
68
  export class XRequest {
69
69
  static getGotOptions(options, responseType) {
70
70
  var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
71
+ let url = options.url;
72
+ if (options.query) {
73
+ const _url = new URL(url);
74
+ const query = options.query instanceof URLSearchParams ? options.query : new URLSearchParams(options.query);
75
+ query.forEach((v, k) => {
76
+ _url.searchParams.set(k, v);
77
+ });
78
+ url = _url.toString();
79
+ }
80
+ options.url = url;
71
81
  const gotOptions = {
72
82
  url: options.url,
73
83
  responseType: responseType === 'stream' ? undefined : responseType,
@@ -108,15 +118,6 @@ export class XRequest {
108
118
  return gotOptions;
109
119
  }
110
120
  static get(options, responseType) {
111
- let url = options.url;
112
- if (options.data) {
113
- const _url = new URL(url);
114
- Object.keys(options.data).forEach(key => {
115
- _url.searchParams.set(key, options.data[key]);
116
- });
117
- url = _url.toString();
118
- }
119
- options.url = url;
120
121
  const res = got({
121
122
  method: 'GET',
122
123
  ...this.getGotOptions(options, responseType || 'buffer')
@@ -200,25 +201,15 @@ export class XRequest {
200
201
  this.options = options;
201
202
  }
202
203
  get(options, responseType) {
203
- var _this$options;
204
204
  return XRequest.get({
205
205
  ...this.options,
206
- ...options,
207
- headers: {
208
- ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
209
- ...options.headers
210
- }
206
+ ...options
211
207
  }, responseType);
212
208
  }
213
209
  post(options, responseType) {
214
- var _this$options2;
215
210
  return XRequest.post({
216
211
  ...this.options,
217
- ...options,
218
- headers: {
219
- ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
220
- ...options.headers
221
- }
212
+ ...options
222
213
  }, responseType);
223
214
  }
224
215
  async getText(options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-request",
3
- "version": "2.14.0",
3
+ "version": "2.15.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",