@polyv/request-core 2.4.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.
- package/ajax.d.ts +3 -0
- package/ajax.js +27 -10
- package/interface/index.d.ts +5 -0
- package/package.json +1 -1
- package/utils.js +23 -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
|
@@ -148,6 +148,10 @@ export class PolyvRequest {
|
|
|
148
148
|
let newResult = result;
|
|
149
149
|
// 处理插件中的响应拦截
|
|
150
150
|
newResult = await this.__pluginCtrl.interceptPluginResponse(newResult, options);
|
|
151
|
+
if (options.responseInterceptor) {
|
|
152
|
+
const r = await options.responseInterceptor(newResult, options);
|
|
153
|
+
newResult = r || newResult;
|
|
154
|
+
}
|
|
151
155
|
return newResult;
|
|
152
156
|
}
|
|
153
157
|
/**
|
|
@@ -164,6 +168,18 @@ export class PolyvRequest {
|
|
|
164
168
|
}
|
|
165
169
|
return obj;
|
|
166
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
|
+
}
|
|
167
183
|
/** 发起 get 请求 */
|
|
168
184
|
get(url, params, options) {
|
|
169
185
|
return this._request(Object.assign({
|
|
@@ -198,18 +214,19 @@ export class PolyvRequest {
|
|
|
198
214
|
}
|
|
199
215
|
/** 打开链接 */
|
|
200
216
|
async open(url, params, options) {
|
|
201
|
-
|
|
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);
|
|
217
|
+
const requestUrl = await this.__generateOpenUrl(url, params, options);
|
|
211
218
|
window.open(requestUrl, '_blank');
|
|
212
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
|
+
}
|
|
213
230
|
/**
|
|
214
231
|
* 解析响应头
|
|
215
232
|
* @param headerStr 响应头字符串
|
package/interface/index.d.ts
CHANGED
|
@@ -41,6 +41,7 @@ export type RequestType = 'form' | 'json';
|
|
|
41
41
|
* 响应格式。
|
|
42
42
|
*/
|
|
43
43
|
export type ResponseType = 'json' | 'text' | 'blob' | 'arraybuffer';
|
|
44
|
+
export type ResponseInterceptor<Options extends RequestOptions = RequestOptions> = (result: RequestResult, options: Options) => Promise<RequestResult | void> | RequestResult | void;
|
|
44
45
|
/**
|
|
45
46
|
* 请求公用选项
|
|
46
47
|
*/
|
|
@@ -86,6 +87,10 @@ export interface RequestBasicOptions {
|
|
|
86
87
|
* 跨域请求时是否提供凭据
|
|
87
88
|
*/
|
|
88
89
|
withCredentials?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* 响应头拦截
|
|
92
|
+
*/
|
|
93
|
+
responseInterceptor?: ResponseInterceptor;
|
|
89
94
|
}
|
|
90
95
|
export interface RequestCustomOptions {
|
|
91
96
|
}
|
package/package.json
CHANGED
package/utils.js
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 是否是 Blob 或 File 鸭子类型
|
|
3
|
+
* @description 解决 iframe 中 Blob 或 File 类型无法被识别的问题
|
|
4
|
+
* @param value 值
|
|
5
|
+
*/
|
|
6
|
+
function isBlobLike(value) {
|
|
7
|
+
if (!value || typeof value !== 'object') {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
const tag = Object.prototype.toString.call(value);
|
|
11
|
+
if (tag === '[object Blob]' || tag === '[object File]') {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
const blobLike = value;
|
|
15
|
+
return typeof blobLike.size === 'number'
|
|
16
|
+
&& typeof blobLike.type === 'string'
|
|
17
|
+
&& typeof blobLike.slice === 'function'
|
|
18
|
+
&& typeof blobLike.arrayBuffer === 'function';
|
|
19
|
+
}
|
|
1
20
|
/**
|
|
2
21
|
* 是否存在 File 值
|
|
3
22
|
* @param data
|
|
@@ -6,7 +25,10 @@ export function hasFileValue(data) {
|
|
|
6
25
|
for (const key in data) {
|
|
7
26
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
27
|
const val = data[key];
|
|
9
|
-
if (val
|
|
28
|
+
if (isBlobLike(val)) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
if (val instanceof File || val instanceof Blob) {
|
|
10
32
|
return true;
|
|
11
33
|
}
|
|
12
34
|
}
|