fix: fix bugs
This commit is contained in:
parent
66905f13a9
commit
27adb05c39
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/query",
|
"name": "@kevisual/query",
|
||||||
"version": "0.0.22",
|
"version": "0.0.24",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.js",
|
"module": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
@ -8,7 +8,11 @@ export type AdapterOpts = {
|
|||||||
method?: Method;
|
method?: Method;
|
||||||
isBlob?: boolean; // 是否返回 Blob 对象
|
isBlob?: boolean; // 是否返回 Blob 对象
|
||||||
};
|
};
|
||||||
|
export const isTextForContentType = (contentType: string | null) => {
|
||||||
|
if (!contentType) return false;
|
||||||
|
const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded'];
|
||||||
|
return textTypes.some((type) => contentType.includes(type));
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param opts
|
* @param opts
|
||||||
@ -56,8 +60,14 @@ export const adapter = async (opts: AdapterOpts, overloadOpts?: RequestInit) =>
|
|||||||
// 判断返回的数据类型
|
// 判断返回的数据类型
|
||||||
if (isJson) {
|
if (isJson) {
|
||||||
return response.json(); // 解析为 JSON
|
return response.json(); // 解析为 JSON
|
||||||
|
} else if (isTextForContentType(contentType)) {
|
||||||
|
return {
|
||||||
|
code: 200,
|
||||||
|
status: response.status,
|
||||||
|
data: response.text(), // 直接返回文本内容
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
return response.text(); // 解析为文本
|
return response;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
48
src/query.ts
48
src/query.ts
@ -1,4 +1,4 @@
|
|||||||
import { adapter, Method } from './adapter.ts';
|
import { adapter, isTextForContentType, Method } from './adapter.ts';
|
||||||
import type { QueryWs } from './ws.ts';
|
import type { QueryWs } from './ws.ts';
|
||||||
/**
|
/**
|
||||||
* 请求前处理函数
|
* 请求前处理函数
|
||||||
@ -247,51 +247,7 @@ export class Query {
|
|||||||
...(_options?.headers || {}),
|
...(_options?.headers || {}),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (!res.ok) {
|
return setBaseResponse(res);
|
||||||
return wrapperError({
|
|
||||||
code: res.status,
|
|
||||||
message: `fetch error: ${res.statusText}`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const contentType = res.headers.get('Content-Type');
|
|
||||||
if (contentType && contentType.includes('application/json')) {
|
|
||||||
const data = await res.json();
|
|
||||||
const result = { code: res.status, data, success: res.ok };
|
|
||||||
return setBaseResponse(result);
|
|
||||||
}
|
|
||||||
if (contentType && contentType.includes('text/html')) {
|
|
||||||
const text = await res.text();
|
|
||||||
const result: Partial<Result> = {
|
|
||||||
code: res.status,
|
|
||||||
data: text,
|
|
||||||
success: res.ok,
|
|
||||||
};
|
|
||||||
return setBaseResponse(result);
|
|
||||||
}
|
|
||||||
if (contentType && contentType.includes('text/plain')) {
|
|
||||||
let text = await res.text();
|
|
||||||
// 处理特殊情况,比如返回的是纯文本
|
|
||||||
if (text.startsWith('{')) {
|
|
||||||
try {
|
|
||||||
text = JSON.parse(text);
|
|
||||||
} catch (e) {
|
|
||||||
// 如果解析失败,保持原样
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const result: Partial<Result> = {
|
|
||||||
code: res.status,
|
|
||||||
data: text,
|
|
||||||
success: res.ok,
|
|
||||||
};
|
|
||||||
return setBaseResponse(result);
|
|
||||||
}
|
|
||||||
const blob = await res.blob();
|
|
||||||
const result: Partial<Result> = {
|
|
||||||
code: res.status,
|
|
||||||
data: blob,
|
|
||||||
success: res.ok,
|
|
||||||
};
|
|
||||||
return setBaseResponse(result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user