@kevisual/query 0.0.20 → 0.0.21
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 +6 -1
- package/dist/query-browser.d.ts +7 -3
- package/dist/query-browser.js +63 -1
- package/dist/query.d.ts +8 -4
- package/dist/query.js +63 -1
- package/package.json +4 -4
package/dist/query-adapter.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
declare const methods: readonly ["GET", "POST"];
|
|
2
2
|
type Method = (typeof methods)[number];
|
|
3
3
|
type AdapterOpts = {
|
|
4
|
-
url
|
|
4
|
+
url?: string;
|
|
5
5
|
headers?: Record<string, string>;
|
|
6
6
|
body?: Record<string, any>;
|
|
7
7
|
timeout?: number;
|
|
8
8
|
method?: Method;
|
|
9
|
+
isBlob?: boolean;
|
|
9
10
|
};
|
|
10
11
|
/**
|
|
11
12
|
*
|
package/dist/query-adapter.js
CHANGED
|
@@ -8,6 +8,7 @@ const methods = ['GET', 'POST'];
|
|
|
8
8
|
const adapter = async (opts, overloadOpts) => {
|
|
9
9
|
const controller = new AbortController();
|
|
10
10
|
const signal = controller.signal;
|
|
11
|
+
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
|
11
12
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
|
12
13
|
const timer = setTimeout(() => {
|
|
13
14
|
controller.abort();
|
|
@@ -39,8 +40,12 @@ const adapter = async (opts, overloadOpts) => {
|
|
|
39
40
|
.then((response) => {
|
|
40
41
|
// 获取 Content-Type 头部信息
|
|
41
42
|
const contentType = response.headers.get('Content-Type');
|
|
43
|
+
if (isBlob) {
|
|
44
|
+
return response.blob(); // 直接返回 Blob 对象
|
|
45
|
+
}
|
|
46
|
+
const isJson = contentType && contentType.includes('application/json');
|
|
42
47
|
// 判断返回的数据类型
|
|
43
|
-
if (
|
|
48
|
+
if (isJson) {
|
|
44
49
|
return response.json(); // 解析为 JSON
|
|
45
50
|
}
|
|
46
51
|
else {
|
package/dist/query-browser.d.ts
CHANGED
|
@@ -3,11 +3,12 @@ import { StoreApi } from 'zustand/vanilla';
|
|
|
3
3
|
declare const methods: readonly ["GET", "POST"];
|
|
4
4
|
type Method = (typeof methods)[number];
|
|
5
5
|
type AdapterOpts = {
|
|
6
|
-
url
|
|
6
|
+
url?: string;
|
|
7
7
|
headers?: Record<string, string>;
|
|
8
8
|
body?: Record<string, any>;
|
|
9
9
|
timeout?: number;
|
|
10
10
|
method?: Method;
|
|
11
|
+
isBlob?: boolean;
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
13
14
|
*
|
|
@@ -92,10 +93,12 @@ type Fn = (opts: {
|
|
|
92
93
|
}) => Promise<Record<string, any> | false>;
|
|
93
94
|
type QueryOpts$1 = {
|
|
94
95
|
url?: string;
|
|
95
|
-
adapter?: typeof adapter;
|
|
96
96
|
headers?: Record<string, string>;
|
|
97
|
+
body?: Record<string, any>;
|
|
97
98
|
timeout?: number;
|
|
98
99
|
method?: Method;
|
|
100
|
+
isBlob?: boolean;
|
|
101
|
+
adapter?: typeof adapter;
|
|
99
102
|
[key: string]: any;
|
|
100
103
|
};
|
|
101
104
|
type Data = {
|
|
@@ -194,6 +197,7 @@ declare class Query {
|
|
|
194
197
|
* @param fn 处理函数
|
|
195
198
|
*/
|
|
196
199
|
after(fn: DataOpts['afterResponse']): void;
|
|
200
|
+
fetchText(url: string, options?: RequestInit): Promise<Result<any>>;
|
|
197
201
|
}
|
|
198
202
|
|
|
199
203
|
declare class BaseQuery<T extends Query = Query, R extends {
|
|
@@ -206,7 +210,7 @@ declare class BaseQuery<T extends Query = Query, R extends {
|
|
|
206
210
|
query: T;
|
|
207
211
|
queryDefine: R;
|
|
208
212
|
constructor(opts?: {
|
|
209
|
-
query
|
|
213
|
+
query?: T;
|
|
210
214
|
queryDefine?: R;
|
|
211
215
|
clientQuery?: T;
|
|
212
216
|
});
|
package/dist/query-browser.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
const adapter = async (opts, overloadOpts) => {
|
|
8
8
|
const controller = new AbortController();
|
|
9
9
|
const signal = controller.signal;
|
|
10
|
+
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
|
10
11
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
|
11
12
|
const timer = setTimeout(() => {
|
|
12
13
|
controller.abort();
|
|
@@ -38,8 +39,12 @@ const adapter = async (opts, overloadOpts) => {
|
|
|
38
39
|
.then((response) => {
|
|
39
40
|
// 获取 Content-Type 头部信息
|
|
40
41
|
const contentType = response.headers.get('Content-Type');
|
|
42
|
+
if (isBlob) {
|
|
43
|
+
return response.blob(); // 直接返回 Blob 对象
|
|
44
|
+
}
|
|
45
|
+
const isJson = contentType && contentType.includes('application/json');
|
|
41
46
|
// 判断返回的数据类型
|
|
42
|
-
if (
|
|
47
|
+
if (isJson) {
|
|
43
48
|
return response.json(); // 解析为 JSON
|
|
44
49
|
}
|
|
45
50
|
else {
|
|
@@ -293,6 +298,7 @@ const setBaseResponse = (res) => {
|
|
|
293
298
|
fn?.();
|
|
294
299
|
}
|
|
295
300
|
};
|
|
301
|
+
return res;
|
|
296
302
|
};
|
|
297
303
|
const wrapperError = ({ code, message }) => {
|
|
298
304
|
const result = {
|
|
@@ -448,6 +454,62 @@ class Query {
|
|
|
448
454
|
after(fn) {
|
|
449
455
|
this.afterResponse = fn;
|
|
450
456
|
}
|
|
457
|
+
async fetchText(url, options) {
|
|
458
|
+
const res = await fetch(url, {
|
|
459
|
+
method: 'GET',
|
|
460
|
+
...options,
|
|
461
|
+
headers: {
|
|
462
|
+
...this.headers,
|
|
463
|
+
...(options?.headers || {}),
|
|
464
|
+
},
|
|
465
|
+
});
|
|
466
|
+
if (!res.ok) {
|
|
467
|
+
return wrapperError({
|
|
468
|
+
code: res.status,
|
|
469
|
+
message: `fetch error: ${res.statusText}`,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
const contentType = res.headers.get('Content-Type');
|
|
473
|
+
if (contentType && contentType.includes('application/json')) {
|
|
474
|
+
const data = await res.json();
|
|
475
|
+
const result = { code: res.status, data, success: res.ok };
|
|
476
|
+
return setBaseResponse(result);
|
|
477
|
+
}
|
|
478
|
+
if (contentType && contentType.includes('text/html')) {
|
|
479
|
+
const text = await res.text();
|
|
480
|
+
const result = {
|
|
481
|
+
code: res.status,
|
|
482
|
+
data: text,
|
|
483
|
+
success: res.ok,
|
|
484
|
+
};
|
|
485
|
+
return setBaseResponse(result);
|
|
486
|
+
}
|
|
487
|
+
if (contentType && contentType.includes('text/plain')) {
|
|
488
|
+
let text = await res.text();
|
|
489
|
+
// 处理特殊情况,比如返回的是纯文本
|
|
490
|
+
if (text.startsWith('{')) {
|
|
491
|
+
try {
|
|
492
|
+
text = JSON.parse(text);
|
|
493
|
+
}
|
|
494
|
+
catch (e) {
|
|
495
|
+
// 如果解析失败,保持原样
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
const result = {
|
|
499
|
+
code: res.status,
|
|
500
|
+
data: text,
|
|
501
|
+
success: res.ok,
|
|
502
|
+
};
|
|
503
|
+
return setBaseResponse(result);
|
|
504
|
+
}
|
|
505
|
+
const blob = await res.blob();
|
|
506
|
+
const result = {
|
|
507
|
+
code: res.status,
|
|
508
|
+
data: blob,
|
|
509
|
+
success: res.ok,
|
|
510
|
+
};
|
|
511
|
+
return setBaseResponse(result);
|
|
512
|
+
}
|
|
451
513
|
}
|
|
452
514
|
class BaseQuery {
|
|
453
515
|
query;
|
package/dist/query.d.ts
CHANGED
|
@@ -3,11 +3,12 @@ import { StoreApi } from 'zustand/vanilla';
|
|
|
3
3
|
declare const methods: readonly ["GET", "POST"];
|
|
4
4
|
type Method = (typeof methods)[number];
|
|
5
5
|
type AdapterOpts = {
|
|
6
|
-
url
|
|
6
|
+
url?: string;
|
|
7
7
|
headers?: Record<string, string>;
|
|
8
8
|
body?: Record<string, any>;
|
|
9
9
|
timeout?: number;
|
|
10
10
|
method?: Method;
|
|
11
|
+
isBlob?: boolean;
|
|
11
12
|
};
|
|
12
13
|
/**
|
|
13
14
|
*
|
|
@@ -92,10 +93,12 @@ type Fn = (opts: {
|
|
|
92
93
|
}) => Promise<Record<string, any> | false>;
|
|
93
94
|
type QueryOpts = {
|
|
94
95
|
url?: string;
|
|
95
|
-
adapter?: typeof adapter;
|
|
96
96
|
headers?: Record<string, string>;
|
|
97
|
+
body?: Record<string, any>;
|
|
97
98
|
timeout?: number;
|
|
98
99
|
method?: Method;
|
|
100
|
+
isBlob?: boolean;
|
|
101
|
+
adapter?: typeof adapter;
|
|
99
102
|
[key: string]: any;
|
|
100
103
|
};
|
|
101
104
|
type Data = {
|
|
@@ -135,7 +138,7 @@ type DataOpts = Partial<QueryOpts> & {
|
|
|
135
138
|
* showError 是 如果 success 为 false 且 noMsg 为 false, 则调用 showError
|
|
136
139
|
* @param res 响应
|
|
137
140
|
*/
|
|
138
|
-
declare const setBaseResponse: (res: Result) =>
|
|
141
|
+
declare const setBaseResponse: (res: Partial<Result>) => Result;
|
|
139
142
|
declare const wrapperError: ({ code, message }: {
|
|
140
143
|
code?: number;
|
|
141
144
|
message?: string;
|
|
@@ -201,6 +204,7 @@ declare class Query {
|
|
|
201
204
|
* @param fn 处理函数
|
|
202
205
|
*/
|
|
203
206
|
after(fn: DataOpts['afterResponse']): void;
|
|
207
|
+
fetchText(url: string, options?: RequestInit): Promise<Result<any>>;
|
|
204
208
|
}
|
|
205
209
|
|
|
206
210
|
declare class BaseQuery<T extends Query = Query, R extends {
|
|
@@ -213,7 +217,7 @@ declare class BaseQuery<T extends Query = Query, R extends {
|
|
|
213
217
|
query: T;
|
|
214
218
|
queryDefine: R;
|
|
215
219
|
constructor(opts?: {
|
|
216
|
-
query
|
|
220
|
+
query?: T;
|
|
217
221
|
queryDefine?: R;
|
|
218
222
|
clientQuery?: T;
|
|
219
223
|
});
|
package/dist/query.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
const adapter = async (opts, overloadOpts) => {
|
|
8
8
|
const controller = new AbortController();
|
|
9
9
|
const signal = controller.signal;
|
|
10
|
+
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
|
10
11
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
|
11
12
|
const timer = setTimeout(() => {
|
|
12
13
|
controller.abort();
|
|
@@ -38,8 +39,12 @@ const adapter = async (opts, overloadOpts) => {
|
|
|
38
39
|
.then((response) => {
|
|
39
40
|
// 获取 Content-Type 头部信息
|
|
40
41
|
const contentType = response.headers.get('Content-Type');
|
|
42
|
+
if (isBlob) {
|
|
43
|
+
return response.blob(); // 直接返回 Blob 对象
|
|
44
|
+
}
|
|
45
|
+
const isJson = contentType && contentType.includes('application/json');
|
|
41
46
|
// 判断返回的数据类型
|
|
42
|
-
if (
|
|
47
|
+
if (isJson) {
|
|
43
48
|
return response.json(); // 解析为 JSON
|
|
44
49
|
}
|
|
45
50
|
else {
|
|
@@ -77,6 +82,7 @@ const setBaseResponse = (res) => {
|
|
|
77
82
|
fn?.();
|
|
78
83
|
}
|
|
79
84
|
};
|
|
85
|
+
return res;
|
|
80
86
|
};
|
|
81
87
|
const wrapperError = ({ code, message }) => {
|
|
82
88
|
const result = {
|
|
@@ -232,6 +238,62 @@ class Query {
|
|
|
232
238
|
after(fn) {
|
|
233
239
|
this.afterResponse = fn;
|
|
234
240
|
}
|
|
241
|
+
async fetchText(url, options) {
|
|
242
|
+
const res = await fetch(url, {
|
|
243
|
+
method: 'GET',
|
|
244
|
+
...options,
|
|
245
|
+
headers: {
|
|
246
|
+
...this.headers,
|
|
247
|
+
...(options?.headers || {}),
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
if (!res.ok) {
|
|
251
|
+
return wrapperError({
|
|
252
|
+
code: res.status,
|
|
253
|
+
message: `fetch error: ${res.statusText}`,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
const contentType = res.headers.get('Content-Type');
|
|
257
|
+
if (contentType && contentType.includes('application/json')) {
|
|
258
|
+
const data = await res.json();
|
|
259
|
+
const result = { code: res.status, data, success: res.ok };
|
|
260
|
+
return setBaseResponse(result);
|
|
261
|
+
}
|
|
262
|
+
if (contentType && contentType.includes('text/html')) {
|
|
263
|
+
const text = await res.text();
|
|
264
|
+
const result = {
|
|
265
|
+
code: res.status,
|
|
266
|
+
data: text,
|
|
267
|
+
success: res.ok,
|
|
268
|
+
};
|
|
269
|
+
return setBaseResponse(result);
|
|
270
|
+
}
|
|
271
|
+
if (contentType && contentType.includes('text/plain')) {
|
|
272
|
+
let text = await res.text();
|
|
273
|
+
// 处理特殊情况,比如返回的是纯文本
|
|
274
|
+
if (text.startsWith('{')) {
|
|
275
|
+
try {
|
|
276
|
+
text = JSON.parse(text);
|
|
277
|
+
}
|
|
278
|
+
catch (e) {
|
|
279
|
+
// 如果解析失败,保持原样
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const result = {
|
|
283
|
+
code: res.status,
|
|
284
|
+
data: text,
|
|
285
|
+
success: res.ok,
|
|
286
|
+
};
|
|
287
|
+
return setBaseResponse(result);
|
|
288
|
+
}
|
|
289
|
+
const blob = await res.blob();
|
|
290
|
+
const result = {
|
|
291
|
+
code: res.status,
|
|
292
|
+
data: blob,
|
|
293
|
+
success: res.ok,
|
|
294
|
+
};
|
|
295
|
+
return setBaseResponse(result);
|
|
296
|
+
}
|
|
235
297
|
}
|
|
236
298
|
class BaseQuery {
|
|
237
299
|
query;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kevisual/query",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.21",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
|
27
27
|
"@rollup/plugin-typescript": "^12.1.2",
|
|
28
|
-
"rollup": "^4.
|
|
28
|
+
"rollup": "^4.41.1",
|
|
29
29
|
"rollup-plugin-dts": "^6.2.1",
|
|
30
30
|
"ts-node": "^10.9.2",
|
|
31
31
|
"tslib": "^2.8.1",
|
|
32
32
|
"typescript": "^5.8.3",
|
|
33
|
-
"zustand": "^5.0.
|
|
33
|
+
"zustand": "^5.0.5"
|
|
34
34
|
},
|
|
35
35
|
"packageManager": "yarn@1.22.22",
|
|
36
36
|
"publishConfig": {
|
|
@@ -59,6 +59,6 @@
|
|
|
59
59
|
}
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"openai": "^
|
|
62
|
+
"openai": "^5.0.1"
|
|
63
63
|
}
|
|
64
64
|
}
|