@polyv/request-core 2.5.0 → 2.7.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/ajax.d.ts +3 -0
- package/ajax.js +26 -14
- package/axios-interceptor.d.ts +1 -1
- package/package.json +4 -4
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
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
-
import { Request } from '@
|
|
3
|
-
import {
|
|
4
|
-
import { startsWithProtocol } from '@polyv/utils/es/net';
|
|
2
|
+
import { Request, xhrAdapter } from '@polyv/utils-request';
|
|
3
|
+
import { startsWithProtocol } from '@polyv/utils-net';
|
|
5
4
|
import { PluginController } from './plugins/plugin-controller';
|
|
6
5
|
import { buildFormData, hasFileValue } from './utils';
|
|
7
6
|
import { DEFAULT_REQUEST_TYPE, DEFAULT_RESPONSE_TYPE } from './config';
|
|
8
|
-
import { concat } from '@
|
|
7
|
+
import { concat } from '@polyv/utils-querystring';
|
|
9
8
|
let xhrRequest;
|
|
10
9
|
/**
|
|
11
10
|
* API 请求封装
|
|
@@ -168,6 +167,18 @@ export class PolyvRequest {
|
|
|
168
167
|
}
|
|
169
168
|
return obj;
|
|
170
169
|
}
|
|
170
|
+
async __generateOpenUrl(url, params, options) {
|
|
171
|
+
if (this.__isDestroyed) {
|
|
172
|
+
return Promise.reject(new Error('Ajax has been destroyed.'));
|
|
173
|
+
}
|
|
174
|
+
const handleResult = await this.__handleOptions(Object.assign({
|
|
175
|
+
url,
|
|
176
|
+
method: 'GET',
|
|
177
|
+
params: this.__filterParamsData(params),
|
|
178
|
+
}, options));
|
|
179
|
+
const _params = handleResult.options.params || {};
|
|
180
|
+
return concat(handleResult.requestUrl, _params);
|
|
181
|
+
}
|
|
171
182
|
/** 发起 get 请求 */
|
|
172
183
|
get(url, params, options) {
|
|
173
184
|
return this._request(Object.assign({
|
|
@@ -202,18 +213,19 @@ export class PolyvRequest {
|
|
|
202
213
|
}
|
|
203
214
|
/** 打开链接 */
|
|
204
215
|
async open(url, params, options) {
|
|
205
|
-
|
|
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);
|
|
216
|
+
const requestUrl = await this.__generateOpenUrl(url, params, options);
|
|
215
217
|
window.open(requestUrl, '_blank');
|
|
216
218
|
}
|
|
219
|
+
/** 下载链接 */
|
|
220
|
+
async download(url, params, options) {
|
|
221
|
+
const requestUrl = await this.__generateOpenUrl(url, params, options);
|
|
222
|
+
const downloadLink = document.createElement('a');
|
|
223
|
+
downloadLink.setAttribute('download', 'download');
|
|
224
|
+
downloadLink.setAttribute('href', requestUrl);
|
|
225
|
+
document.body.appendChild(downloadLink);
|
|
226
|
+
downloadLink.click();
|
|
227
|
+
document.body.removeChild(downloadLink);
|
|
228
|
+
}
|
|
217
229
|
/**
|
|
218
230
|
* 解析响应头
|
|
219
231
|
* @param headerStr 响应头字符串
|
package/axios-interceptor.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AxiosInstance } from 'axios';
|
|
1
|
+
import type { AxiosInstance } from 'axios';
|
|
2
2
|
import { PolyvRequestConfig, RequestType } from './interface';
|
|
3
3
|
export type SetupAxiosInterceptorConfig = Pick<PolyvRequestConfig, 'baseUrl' | 'timeout' | 'requestPlugins'>;
|
|
4
4
|
declare module 'axios' {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polyv/request-core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"main": "./index.js",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"axios": ">=1.0.0"
|
|
@@ -11,9 +11,9 @@
|
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@
|
|
15
|
-
"@
|
|
16
|
-
"@polyv/utils": "^
|
|
14
|
+
"@polyv/utils-net": "^3.1.0",
|
|
15
|
+
"@polyv/utils-querystring": "^3.1.0",
|
|
16
|
+
"@polyv/utils-request": "^3.1.0",
|
|
17
17
|
"md5": "2.3.0",
|
|
18
18
|
"sha256": "0.2.0"
|
|
19
19
|
},
|