@polyv/request-core 2.5.0 → 2.6.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 +23 -10
  3. package/package.json +1 -1
package/ajax.d.ts CHANGED
@@ -42,6 +42,7 @@ export declare class PolyvRequest<Options extends RequestOptions = RequestOption
42
42
  * @param target 目标对象
43
43
  */
44
44
  private __filterParamsData;
45
+ private __generateOpenUrl;
45
46
  /** 发起 get 请求 */
46
47
  get<R = unknown, P = object>(url: string, params: P, options?: Options): Promise<R>;
47
48
  /** 发起 post 请求 */
@@ -52,6 +53,8 @@ export declare class PolyvRequest<Options extends RequestOptions = RequestOption
52
53
  put<R = unknown, D = object>(url: string, data: D, options?: Options): Promise<R>;
53
54
  /** 打开链接 */
54
55
  open<P = object>(url: string, params: P, options?: Options): Promise<void>;
56
+ /** 下载链接 */
57
+ download<P = object>(url: string, params: P, options?: Options): Promise<void>;
55
58
  /**
56
59
  * 解析响应头
57
60
  * @param headerStr 响应头字符串
package/ajax.js CHANGED
@@ -168,6 +168,18 @@ export class PolyvRequest {
168
168
  }
169
169
  return obj;
170
170
  }
171
+ async __generateOpenUrl(url, params, options) {
172
+ if (this.__isDestroyed) {
173
+ return Promise.reject(new Error('Ajax has been destroyed.'));
174
+ }
175
+ const handleResult = await this.__handleOptions(Object.assign({
176
+ url,
177
+ method: 'GET',
178
+ params: this.__filterParamsData(params),
179
+ }, options));
180
+ const _params = handleResult.options.params || {};
181
+ return concat(handleResult.requestUrl, _params);
182
+ }
171
183
  /** 发起 get 请求 */
172
184
  get(url, params, options) {
173
185
  return this._request(Object.assign({
@@ -202,18 +214,19 @@ export class PolyvRequest {
202
214
  }
203
215
  /** 打开链接 */
204
216
  async open(url, params, options) {
205
- if (this.__isDestroyed) {
206
- return Promise.reject(new Error('Ajax has been destroyed.'));
207
- }
208
- const handleResult = await this.__handleOptions(Object.assign({
209
- url,
210
- method: 'GET',
211
- params: this.__filterParamsData(params),
212
- }, options));
213
- const _params = handleResult.options.params || {};
214
- const requestUrl = concat(handleResult.requestUrl, _params);
217
+ const requestUrl = await this.__generateOpenUrl(url, params, options);
215
218
  window.open(requestUrl, '_blank');
216
219
  }
220
+ /** 下载链接 */
221
+ async download(url, params, options) {
222
+ const requestUrl = await this.__generateOpenUrl(url, params, options);
223
+ const downloadLink = document.createElement('a');
224
+ downloadLink.setAttribute('download', 'download');
225
+ downloadLink.setAttribute('href', requestUrl);
226
+ document.body.appendChild(downloadLink);
227
+ downloadLink.click();
228
+ document.body.removeChild(downloadLink);
229
+ }
217
230
  /**
218
231
  * 解析响应头
219
232
  * @param headerStr 响应头字符串
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polyv/request-core",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "main": "./index.js",
5
5
  "peerDependencies": {
6
6
  "axios": ">=1.0.0"