@kevisual/query 0.0.24 → 0.0.26
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/dist/query-adapter.d.ts +2 -1
- package/dist/query-adapter.js +16 -5
- package/dist/query-ai.d.ts +6 -2
- package/dist/query-ai.js +4424 -4363
- package/dist/query-browser.d.ts +4 -2
- package/dist/query-browser.js +16 -5
- package/dist/query.d.ts +4 -2
- package/dist/query.js +16 -5
- package/package.json +1 -1
- package/readme.md +19 -3
package/dist/query-browser.d.ts
CHANGED
|
@@ -5,10 +5,11 @@ type Method = (typeof methods)[number];
|
|
|
5
5
|
type AdapterOpts = {
|
|
6
6
|
url?: string;
|
|
7
7
|
headers?: Record<string, string>;
|
|
8
|
-
body?: Record<string, any
|
|
8
|
+
body?: Record<string, any> | FormData;
|
|
9
9
|
timeout?: number;
|
|
10
10
|
method?: Method;
|
|
11
11
|
isBlob?: boolean;
|
|
12
|
+
isPostFile?: boolean;
|
|
12
13
|
};
|
|
13
14
|
/**
|
|
14
15
|
*
|
|
@@ -94,10 +95,11 @@ type Fn = (opts: {
|
|
|
94
95
|
type QueryOpts$1 = {
|
|
95
96
|
url?: string;
|
|
96
97
|
headers?: Record<string, string>;
|
|
97
|
-
body?: Record<string, any
|
|
98
|
+
body?: Record<string, any> | FormData;
|
|
98
99
|
timeout?: number;
|
|
99
100
|
method?: Method;
|
|
100
101
|
isBlob?: boolean;
|
|
102
|
+
isPostFile?: boolean;
|
|
101
103
|
adapter?: typeof adapter;
|
|
102
104
|
[key: string]: any;
|
|
103
105
|
};
|
package/dist/query-browser.js
CHANGED
|
@@ -14,6 +14,7 @@ const adapter = async (opts, overloadOpts) => {
|
|
|
14
14
|
const controller = new AbortController();
|
|
15
15
|
const signal = controller.signal;
|
|
16
16
|
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
|
17
|
+
const isPostFile = opts.isPostFile || false; // 是否为文件上传
|
|
17
18
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
|
18
19
|
const timer = setTimeout(() => {
|
|
19
20
|
controller.abort();
|
|
@@ -32,6 +33,16 @@ const adapter = async (opts, overloadOpts) => {
|
|
|
32
33
|
if (isGet) {
|
|
33
34
|
url.search = new URLSearchParams(opts.body).toString();
|
|
34
35
|
}
|
|
36
|
+
let body = undefined;
|
|
37
|
+
if (isGet) {
|
|
38
|
+
body = undefined;
|
|
39
|
+
}
|
|
40
|
+
else if (isPostFile) {
|
|
41
|
+
body = opts.body; // 如果是文件上传,直接使用 FormData
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
|
|
45
|
+
}
|
|
35
46
|
return fetch(url, {
|
|
36
47
|
method: method.toUpperCase(),
|
|
37
48
|
headers: {
|
|
@@ -40,24 +51,24 @@ const adapter = async (opts, overloadOpts) => {
|
|
|
40
51
|
},
|
|
41
52
|
signal,
|
|
42
53
|
...overloadOpts,
|
|
43
|
-
body:
|
|
54
|
+
body: body,
|
|
44
55
|
})
|
|
45
|
-
.then((response) => {
|
|
56
|
+
.then(async (response) => {
|
|
46
57
|
// 获取 Content-Type 头部信息
|
|
47
58
|
const contentType = response.headers.get('Content-Type');
|
|
48
59
|
if (isBlob) {
|
|
49
|
-
return response.blob(); // 直接返回 Blob 对象
|
|
60
|
+
return await response.blob(); // 直接返回 Blob 对象
|
|
50
61
|
}
|
|
51
62
|
const isJson = contentType && contentType.includes('application/json');
|
|
52
63
|
// 判断返回的数据类型
|
|
53
64
|
if (isJson) {
|
|
54
|
-
return response.json(); // 解析为 JSON
|
|
65
|
+
return await response.json(); // 解析为 JSON
|
|
55
66
|
}
|
|
56
67
|
else if (isTextForContentType(contentType)) {
|
|
57
68
|
return {
|
|
58
69
|
code: 200,
|
|
59
70
|
status: response.status,
|
|
60
|
-
data: response.text(), // 直接返回文本内容
|
|
71
|
+
data: await response.text(), // 直接返回文本内容
|
|
61
72
|
};
|
|
62
73
|
}
|
|
63
74
|
else {
|
package/dist/query.d.ts
CHANGED
|
@@ -5,10 +5,11 @@ type Method = (typeof methods)[number];
|
|
|
5
5
|
type AdapterOpts = {
|
|
6
6
|
url?: string;
|
|
7
7
|
headers?: Record<string, string>;
|
|
8
|
-
body?: Record<string, any
|
|
8
|
+
body?: Record<string, any> | FormData;
|
|
9
9
|
timeout?: number;
|
|
10
10
|
method?: Method;
|
|
11
11
|
isBlob?: boolean;
|
|
12
|
+
isPostFile?: boolean;
|
|
12
13
|
};
|
|
13
14
|
/**
|
|
14
15
|
*
|
|
@@ -94,10 +95,11 @@ type Fn = (opts: {
|
|
|
94
95
|
type QueryOpts = {
|
|
95
96
|
url?: string;
|
|
96
97
|
headers?: Record<string, string>;
|
|
97
|
-
body?: Record<string, any
|
|
98
|
+
body?: Record<string, any> | FormData;
|
|
98
99
|
timeout?: number;
|
|
99
100
|
method?: Method;
|
|
100
101
|
isBlob?: boolean;
|
|
102
|
+
isPostFile?: boolean;
|
|
101
103
|
adapter?: typeof adapter;
|
|
102
104
|
[key: string]: any;
|
|
103
105
|
};
|
package/dist/query.js
CHANGED
|
@@ -14,6 +14,7 @@ const adapter = async (opts, overloadOpts) => {
|
|
|
14
14
|
const controller = new AbortController();
|
|
15
15
|
const signal = controller.signal;
|
|
16
16
|
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
|
17
|
+
const isPostFile = opts.isPostFile || false; // 是否为文件上传
|
|
17
18
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
|
18
19
|
const timer = setTimeout(() => {
|
|
19
20
|
controller.abort();
|
|
@@ -32,6 +33,16 @@ const adapter = async (opts, overloadOpts) => {
|
|
|
32
33
|
if (isGet) {
|
|
33
34
|
url.search = new URLSearchParams(opts.body).toString();
|
|
34
35
|
}
|
|
36
|
+
let body = undefined;
|
|
37
|
+
if (isGet) {
|
|
38
|
+
body = undefined;
|
|
39
|
+
}
|
|
40
|
+
else if (isPostFile) {
|
|
41
|
+
body = opts.body; // 如果是文件上传,直接使用 FormData
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
|
|
45
|
+
}
|
|
35
46
|
return fetch(url, {
|
|
36
47
|
method: method.toUpperCase(),
|
|
37
48
|
headers: {
|
|
@@ -40,24 +51,24 @@ const adapter = async (opts, overloadOpts) => {
|
|
|
40
51
|
},
|
|
41
52
|
signal,
|
|
42
53
|
...overloadOpts,
|
|
43
|
-
body:
|
|
54
|
+
body: body,
|
|
44
55
|
})
|
|
45
|
-
.then((response) => {
|
|
56
|
+
.then(async (response) => {
|
|
46
57
|
// 获取 Content-Type 头部信息
|
|
47
58
|
const contentType = response.headers.get('Content-Type');
|
|
48
59
|
if (isBlob) {
|
|
49
|
-
return response.blob(); // 直接返回 Blob 对象
|
|
60
|
+
return await response.blob(); // 直接返回 Blob 对象
|
|
50
61
|
}
|
|
51
62
|
const isJson = contentType && contentType.includes('application/json');
|
|
52
63
|
// 判断返回的数据类型
|
|
53
64
|
if (isJson) {
|
|
54
|
-
return response.json(); // 解析为 JSON
|
|
65
|
+
return await response.json(); // 解析为 JSON
|
|
55
66
|
}
|
|
56
67
|
else if (isTextForContentType(contentType)) {
|
|
57
68
|
return {
|
|
58
69
|
code: 200,
|
|
59
70
|
status: response.status,
|
|
60
|
-
data: response.text(), // 直接返回文本内容
|
|
71
|
+
data: await response.text(), // 直接返回文本内容
|
|
61
72
|
};
|
|
62
73
|
}
|
|
63
74
|
else {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# query
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
对 fetch 功能的的一部分功能的封装。主要处理header的token的提交和response中的处理json。
|
|
4
4
|
|
|
5
5
|
主要目的:请求路径默认`/api/router`,使用`post`,`post`的数据分流使用`path`和`key`.
|
|
6
6
|
|
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
|
|
9
9
|
## query
|
|
10
10
|
|
|
11
|
-
|
|
12
11
|
```ts
|
|
13
12
|
const query = new Query();
|
|
14
13
|
const res = await query.post({
|
|
@@ -35,4 +34,21 @@ type Data = {
|
|
|
35
34
|
type DataOpts = Partial<QueryOpts> & {
|
|
36
35
|
beforeRequest?: Fn;
|
|
37
36
|
};
|
|
38
|
-
```
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## 适配的@kevisual/router的代码
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { App } from '@kevisual/router';
|
|
43
|
+
const app = new App();
|
|
44
|
+
|
|
45
|
+
app
|
|
46
|
+
.route({
|
|
47
|
+
path: 'demo',
|
|
48
|
+
key: '1',
|
|
49
|
+
})
|
|
50
|
+
.define(async (ctx) => {
|
|
51
|
+
ctx.body = 234;
|
|
52
|
+
})
|
|
53
|
+
.addTo(app);
|
|
54
|
+
```
|