@jayfong/x-request 2.14.0 → 2.15.1

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,14 @@ exports.XRequestFormFile = XRequestFormFile;
77
77
  class XRequest {
78
78
  static getGotOptions(options, responseType) {
79
79
  var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
80
+ if (options.query) {
81
+ const url = new URL(options.url);
82
+ const query = options.query instanceof URLSearchParams ? options.query : new URLSearchParams(options.query);
83
+ query.forEach((v, k) => {
84
+ url.searchParams.set(k, v);
85
+ });
86
+ options.url = url.toString();
87
+ }
80
88
  const gotOptions = {
81
89
  url: options.url,
82
90
  responseType: responseType === 'stream' ? undefined : responseType,
@@ -117,15 +125,6 @@ class XRequest {
117
125
  return gotOptions;
118
126
  }
119
127
  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
128
  const res = (0, _got.default)({
130
129
  method: 'GET',
131
130
  ...this.getGotOptions(options, responseType || 'buffer')
@@ -209,25 +208,15 @@ class XRequest {
209
208
  this.options = options;
210
209
  }
211
210
  get(options, responseType) {
212
- var _this$options;
213
211
  return XRequest.get({
214
212
  ...this.options,
215
- ...options,
216
- headers: {
217
- ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
218
- ...options.headers
219
- }
213
+ ...options
220
214
  }, responseType);
221
215
  }
222
216
  post(options, responseType) {
223
- var _this$options2;
224
217
  return XRequest.post({
225
218
  ...this.options,
226
- ...options,
227
- headers: {
228
- ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
229
- ...options.headers
230
- }
219
+ ...options
231
220
  }, responseType);
232
221
  }
233
222
  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,14 @@ 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
+ if (options.query) {
72
+ const url = new URL(options.url);
73
+ const query = options.query instanceof URLSearchParams ? options.query : new URLSearchParams(options.query);
74
+ query.forEach((v, k) => {
75
+ url.searchParams.set(k, v);
76
+ });
77
+ options.url = url.toString();
78
+ }
71
79
  const gotOptions = {
72
80
  url: options.url,
73
81
  responseType: responseType === 'stream' ? undefined : responseType,
@@ -108,15 +116,6 @@ export class XRequest {
108
116
  return gotOptions;
109
117
  }
110
118
  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
119
  const res = got({
121
120
  method: 'GET',
122
121
  ...this.getGotOptions(options, responseType || 'buffer')
@@ -200,25 +199,15 @@ export class XRequest {
200
199
  this.options = options;
201
200
  }
202
201
  get(options, responseType) {
203
- var _this$options;
204
202
  return XRequest.get({
205
203
  ...this.options,
206
- ...options,
207
- headers: {
208
- ...((_this$options = this.options) == null ? void 0 : _this$options.headers),
209
- ...options.headers
210
- }
204
+ ...options
211
205
  }, responseType);
212
206
  }
213
207
  post(options, responseType) {
214
- var _this$options2;
215
208
  return XRequest.post({
216
209
  ...this.options,
217
- ...options,
218
- headers: {
219
- ...((_this$options2 = this.options) == null ? void 0 : _this$options2.headers),
220
- ...options.headers
221
- }
210
+ ...options
222
211
  }, responseType);
223
212
  }
224
213
  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.1",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",