feat: 针对请求文件的情况进行优化 (#91)

This commit is contained in:
qlin 2022-01-13 16:25:16 +08:00 committed by GitHub
parent d98f326e5f
commit e00acc4a7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View File

@ -1,6 +1,15 @@
import { isFunction, isObject, isString } from './helpers'; import { isFunction, isObject, isString } from './helpers';
export default async ({ response, responseDataAdaptor }, next) => { export default async ({ response, responseDataAdaptor }, next) => {
// 如果 data 是 blob 并且 content-type 是 application/json自动进行数据处理
if (response.data instanceof Blob && response.headers['content-type'].startsWith('application/json') && response.data.type === 'application/json') {
const rawData = response.data;
try {
response.data = JSON.parse(await response.data.text());
} catch {
response.data = rawData;
}
}
if (isFunction(responseDataAdaptor) && response && (isObject(response.data) || isString(response.data))) { if (isFunction(responseDataAdaptor) && response && (isObject(response.data) || isString(response.data))) {
response.data = responseDataAdaptor(response.data); response.data = responseDataAdaptor(response.data);
} }

View File

@ -65,16 +65,19 @@ export default {
}; };
const post = (id) => { const post = (id) => {
request('/get/api', { id }, { request('/api', { id }, {
responseType: 'blob'
}).then((data) => {
console.log(data);
}); });
}; };
get(1); get(1);
get(2); // get(2);
get(3); // get(3);
post(1); // post(1);
post(2); // post(2);
post(3); post(3);