@polyv/request-core 2.2.0 → 2.4.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.
Files changed (3) hide show
  1. package/ajax.d.ts +3 -0
  2. package/ajax.js +29 -7
  3. package/package.json +3 -2
package/ajax.d.ts CHANGED
@@ -25,6 +25,7 @@ export declare class PolyvRequest<Options extends RequestOptions = RequestOption
25
25
  * 生成请求地址,插入 baseUrl
26
26
  */
27
27
  generateUrl(options: Options): string;
28
+ private __handleOptions;
28
29
  /** 发起请求 */
29
30
  protected _request<R = unknown>(options: Options): Promise<R>;
30
31
  private __beforeSend;
@@ -49,6 +50,8 @@ export declare class PolyvRequest<Options extends RequestOptions = RequestOption
49
50
  delete<R = unknown, D = object>(url: string, data: D, options?: Options): Promise<R>;
50
51
  /** 发起 put 请求 */
51
52
  put<R = unknown, D = object>(url: string, data: D, options?: Options): Promise<R>;
53
+ /** 打开链接 */
54
+ open<P = object>(url: string, params: P, options?: Options): Promise<void>;
52
55
  /**
53
56
  * 解析响应头
54
57
  * @param headerStr 响应头字符串
package/ajax.js CHANGED
@@ -5,6 +5,7 @@ import { startsWithProtocol } from '@polyv/utils/es/net';
5
5
  import { PluginController } from './plugins/plugin-controller';
6
6
  import { buildFormData, hasFileValue } from './utils';
7
7
  import { DEFAULT_REQUEST_TYPE, DEFAULT_RESPONSE_TYPE } from './config';
8
+ import { concat } from '@just4/querystring';
8
9
  let xhrRequest;
9
10
  /**
10
11
  * API 请求封装
@@ -65,11 +66,7 @@ export class PolyvRequest {
65
66
  }
66
67
  return baseUrl + url;
67
68
  }
68
- /** 发起请求 */
69
- async _request(options) {
70
- if (this.__isDestroyed) {
71
- return Promise.reject(new Error('Ajax has been destroyed.'));
72
- }
69
+ async __handleOptions(options) {
73
70
  let _options = { ...options };
74
71
  // 处理请求地址
75
72
  const orignRequestUrl = this.generateUrl(_options);
@@ -84,12 +81,23 @@ export class PolyvRequest {
84
81
  _options = await this.__interceptRequest(_options);
85
82
  // 处理后的请求地址
86
83
  const requestUrl = _options.requestUrl || orignRequestUrl;
84
+ return {
85
+ options: _options,
86
+ requestUrl,
87
+ };
88
+ }
89
+ /** 发起请求 */
90
+ async _request(options) {
91
+ if (this.__isDestroyed) {
92
+ return Promise.reject(new Error('Ajax has been destroyed.'));
93
+ }
94
+ const handleResult = await this.__handleOptions(options);
87
95
  // 做最后一步处理
88
- _options = this.__beforeSend(_options);
96
+ const _options = this.__beforeSend(handleResult.options);
89
97
  let result;
90
98
  try {
91
99
  // 调用请求库
92
- const justRes = await this.__xhrRequest.send(requestUrl, {
100
+ const justRes = await this.__xhrRequest.send(handleResult.requestUrl, {
93
101
  ..._options,
94
102
  });
95
103
  const headers = this.__parseResponseHeaders(justRes.xhr.getAllResponseHeaders());
@@ -188,6 +196,20 @@ export class PolyvRequest {
188
196
  data: this.__filterParamsData(data),
189
197
  }, options));
190
198
  }
199
+ /** 打开链接 */
200
+ async open(url, params, options) {
201
+ if (this.__isDestroyed) {
202
+ return Promise.reject(new Error('Ajax has been destroyed.'));
203
+ }
204
+ const handleResult = await this.__handleOptions(Object.assign({
205
+ url,
206
+ method: 'GET',
207
+ params: this.__filterParamsData(params),
208
+ }, options));
209
+ const _params = handleResult.options.params || {};
210
+ const requestUrl = concat(handleResult.requestUrl, _params);
211
+ window.open(requestUrl, '_blank');
212
+ }
191
213
  /**
192
214
  * 解析响应头
193
215
  * @param headerStr 响应头字符串
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/request-core",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "main": "./index.js",
5
5
  "peerDependencies": {
6
6
  "axios": ">=1.0.0"
@@ -11,7 +11,8 @@
11
11
  }
12
12
  },
13
13
  "dependencies": {
14
- "@just4/request": "^0.6.0",
14
+ "@just4/querystring": "^2.0.0",
15
+ "@just4/request": "^1.0.0",
15
16
  "@polyv/utils": "^2.6.0",
16
17
  "md5": "2.3.0",
17
18
  "sha256": "0.2.0"