fix: fix adapter

This commit is contained in:
熊潇 2025-06-03 21:12:33 +08:00
parent 27adb05c39
commit 452f821d06
3 changed files with 24 additions and 8 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@kevisual/query",
"version": "0.0.24",
"version": "0.0.25",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",

View File

@ -1,6 +1,6 @@
# query
应的 fetch 内容的一部分功能的封装
fetch 功能的的一部分功能的封装。主要处理header的token的提交和response中的处理json
主要目的:请求路径默认`/api/router`,使用`post`,`post`的数据分流使用`path``key`.
@ -8,7 +8,6 @@
## query
```ts
const query = new Query();
const res = await query.post({
@ -36,3 +35,20 @@ type DataOpts = Partial<QueryOpts> & {
beforeRequest?: Fn;
};
```
## 适配的@kevisual/router的代码
```ts
import { App } from '@kevisual/router';
const app = new App();
app
.route({
path: 'demo',
key: '1',
})
.define(async (ctx) => {
ctx.body = 234;
})
.addTo(app);
```

View File

@ -50,21 +50,21 @@ export const adapter = async (opts: AdapterOpts, overloadOpts?: RequestInit) =>
...overloadOpts,
body: isGet ? undefined : JSON.stringify(opts.body),
})
.then((response) => {
.then(async (response) => {
// 获取 Content-Type 头部信息
const contentType = response.headers.get('Content-Type');
if (isBlob) {
return response.blob(); // 直接返回 Blob 对象
return await response.blob(); // 直接返回 Blob 对象
}
const isJson = contentType && contentType.includes('application/json');
// 判断返回的数据类型
if (isJson) {
return response.json(); // 解析为 JSON
return await response.json(); // 解析为 JSON
} else if (isTextForContentType(contentType)) {
return {
code: 200,
status: response.status,
data: response.text(), // 直接返回文本内容
data: await response.text(), // 直接返回文本内容
};
} else {
return response;