@kevisual/query 0.0.28 → 0.0.30
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 +15 -0
- package/dist/query-adapter.js +14 -4
- package/dist/query-browser.d.ts +29 -33
- package/dist/query-browser.js +25 -39
- package/dist/query-ws.js +1 -1
- package/dist/query.d.ts +29 -33
- package/dist/query.js +24 -38
- package/package.json +8 -16
- package/dist/query-ai.d.ts +0 -30
- package/dist/query-ai.js +0 -6612
package/dist/query-adapter.d.ts
CHANGED
|
@@ -3,11 +3,26 @@ type Method = (typeof methods)[number];
|
|
|
3
3
|
type AdapterOpts = {
|
|
4
4
|
url?: string;
|
|
5
5
|
headers?: Record<string, string>;
|
|
6
|
+
/**
|
|
7
|
+
* 只用户POST请求,传递的查询参数,
|
|
8
|
+
* GET请求默认方body自己转化为查询参数
|
|
9
|
+
*/
|
|
10
|
+
params?: Record<string, any>;
|
|
6
11
|
body?: Record<string, any> | FormData;
|
|
7
12
|
timeout?: number;
|
|
8
13
|
method?: Method;
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated use responseType
|
|
16
|
+
*/
|
|
9
17
|
isBlob?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated use responseType
|
|
20
|
+
*/
|
|
10
21
|
isText?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* 响应类型,
|
|
24
|
+
* */
|
|
25
|
+
responseType?: 'json' | 'text' | 'blob';
|
|
11
26
|
isPostFile?: boolean;
|
|
12
27
|
};
|
|
13
28
|
declare const isTextForContentType: (contentType: string | null) => boolean;
|
package/dist/query-adapter.js
CHANGED
|
@@ -2,7 +2,7 @@ const methods = ['GET', 'POST'];
|
|
|
2
2
|
const isTextForContentType = (contentType) => {
|
|
3
3
|
if (!contentType)
|
|
4
4
|
return false;
|
|
5
|
-
const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded'];
|
|
5
|
+
const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded', 'md'];
|
|
6
6
|
return textTypes.some((type) => contentType.includes(type));
|
|
7
7
|
};
|
|
8
8
|
/**
|
|
@@ -14,9 +14,14 @@ const isTextForContentType = (contentType) => {
|
|
|
14
14
|
const adapter = async (opts = {}, overloadOpts) => {
|
|
15
15
|
const controller = new AbortController();
|
|
16
16
|
const signal = controller.signal;
|
|
17
|
-
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
|
18
|
-
const isText = opts.isText || false; // 是否返回文本内容
|
|
19
17
|
const isPostFile = opts.isPostFile || false; // 是否为文件上传
|
|
18
|
+
let responseType = opts.responseType || 'json'; // 响应类型
|
|
19
|
+
if (opts.isBlob) {
|
|
20
|
+
responseType = 'blob';
|
|
21
|
+
}
|
|
22
|
+
else if (opts.isText) {
|
|
23
|
+
responseType = 'text';
|
|
24
|
+
}
|
|
20
25
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
|
21
26
|
const timer = setTimeout(() => {
|
|
22
27
|
controller.abort();
|
|
@@ -36,6 +41,10 @@ const adapter = async (opts = {}, overloadOpts) => {
|
|
|
36
41
|
if (isGet) {
|
|
37
42
|
url.search = new URLSearchParams(opts.body).toString();
|
|
38
43
|
}
|
|
44
|
+
else {
|
|
45
|
+
const params = opts.params || {};
|
|
46
|
+
url.search = new URLSearchParams(params).toString();
|
|
47
|
+
}
|
|
39
48
|
let body = undefined;
|
|
40
49
|
if (isGet) {
|
|
41
50
|
body = undefined;
|
|
@@ -60,9 +69,10 @@ const adapter = async (opts = {}, overloadOpts) => {
|
|
|
60
69
|
.then(async (response) => {
|
|
61
70
|
// 获取 Content-Type 头部信息
|
|
62
71
|
const contentType = response.headers.get('Content-Type');
|
|
63
|
-
if (
|
|
72
|
+
if (responseType === 'blob') {
|
|
64
73
|
return await response.blob(); // 直接返回 Blob 对象
|
|
65
74
|
}
|
|
75
|
+
const isText = responseType === 'text';
|
|
66
76
|
const isJson = contentType && contentType.includes('application/json');
|
|
67
77
|
// 判断返回的数据类型
|
|
68
78
|
if (isJson && !isText) {
|
package/dist/query-browser.d.ts
CHANGED
|
@@ -5,11 +5,26 @@ type Method = (typeof methods)[number];
|
|
|
5
5
|
type AdapterOpts = {
|
|
6
6
|
url?: string;
|
|
7
7
|
headers?: Record<string, string>;
|
|
8
|
+
/**
|
|
9
|
+
* 只用户POST请求,传递的查询参数,
|
|
10
|
+
* GET请求默认方body自己转化为查询参数
|
|
11
|
+
*/
|
|
12
|
+
params?: Record<string, any>;
|
|
8
13
|
body?: Record<string, any> | FormData;
|
|
9
14
|
timeout?: number;
|
|
10
15
|
method?: Method;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated use responseType
|
|
18
|
+
*/
|
|
11
19
|
isBlob?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated use responseType
|
|
22
|
+
*/
|
|
12
23
|
isText?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* 响应类型,
|
|
26
|
+
* */
|
|
27
|
+
responseType?: 'json' | 'text' | 'blob';
|
|
13
28
|
isPostFile?: boolean;
|
|
14
29
|
};
|
|
15
30
|
/**
|
|
@@ -94,16 +109,9 @@ type Fn = (opts: {
|
|
|
94
109
|
timeout?: number;
|
|
95
110
|
}) => Promise<Record<string, any> | false>;
|
|
96
111
|
type QueryOpts$1 = {
|
|
97
|
-
url?: string;
|
|
98
|
-
headers?: Record<string, string>;
|
|
99
|
-
body?: Record<string, any> | FormData;
|
|
100
|
-
timeout?: number;
|
|
101
|
-
method?: Method;
|
|
102
|
-
isBlob?: boolean;
|
|
103
|
-
isPostFile?: boolean;
|
|
104
112
|
adapter?: typeof adapter;
|
|
105
113
|
[key: string]: any;
|
|
106
|
-
};
|
|
114
|
+
} & AdapterOpts;
|
|
107
115
|
type Data = {
|
|
108
116
|
path?: string;
|
|
109
117
|
key?: string;
|
|
@@ -134,6 +142,10 @@ type DataOpts = Partial<QueryOpts$1> & {
|
|
|
134
142
|
res?: any;
|
|
135
143
|
fetch?: any;
|
|
136
144
|
}) => Promise<Result<S>>;
|
|
145
|
+
/**
|
|
146
|
+
* 是否在stop的时候不请求
|
|
147
|
+
*/
|
|
148
|
+
noStop?: boolean;
|
|
137
149
|
};
|
|
138
150
|
declare const wrapperError: ({ code, message }: {
|
|
139
151
|
code?: number;
|
|
@@ -157,7 +169,13 @@ declare const wrapperError: ({ code, message }: {
|
|
|
157
169
|
declare class Query {
|
|
158
170
|
adapter: typeof adapter;
|
|
159
171
|
url: string;
|
|
172
|
+
/**
|
|
173
|
+
* 请求前处理函数
|
|
174
|
+
*/
|
|
160
175
|
beforeRequest?: DataOpts['beforeRequest'];
|
|
176
|
+
/**
|
|
177
|
+
* 请求后处理函数
|
|
178
|
+
*/
|
|
161
179
|
afterResponse?: DataOpts['afterResponse'];
|
|
162
180
|
headers?: Record<string, string>;
|
|
163
181
|
timeout?: number;
|
|
@@ -191,40 +209,18 @@ declare class Query {
|
|
|
191
209
|
*/
|
|
192
210
|
post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>>;
|
|
193
211
|
/**
|
|
194
|
-
*
|
|
212
|
+
* 设置请求前处理,设置请求前处理函数
|
|
195
213
|
* @param fn 处理函数
|
|
196
214
|
*/
|
|
197
215
|
before(fn: DataOpts['beforeRequest']): void;
|
|
198
216
|
/**
|
|
199
|
-
*
|
|
217
|
+
* 设置请求后处理,设置请求后处理函数
|
|
200
218
|
* @param fn 处理函数
|
|
201
219
|
*/
|
|
202
220
|
after(fn: DataOpts['afterResponse']): void;
|
|
203
221
|
fetchText(urlOrOptions?: string | QueryOpts$1, options?: QueryOpts$1): Promise<Result<any>>;
|
|
204
222
|
}
|
|
205
223
|
|
|
206
|
-
declare class BaseQuery<T extends Query = Query, R extends {
|
|
207
|
-
queryChain?: any;
|
|
208
|
-
query?: any;
|
|
209
|
-
} = {
|
|
210
|
-
queryChain: any;
|
|
211
|
-
query?: T;
|
|
212
|
-
}> {
|
|
213
|
-
query: T;
|
|
214
|
-
queryDefine: R;
|
|
215
|
-
constructor(opts?: {
|
|
216
|
-
query?: T;
|
|
217
|
-
queryDefine?: R;
|
|
218
|
-
clientQuery?: T;
|
|
219
|
-
});
|
|
220
|
-
get chain(): R['queryChain'];
|
|
221
|
-
post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
222
|
-
get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
223
|
-
}
|
|
224
|
-
declare class ClientQuery extends Query {
|
|
225
|
-
constructor(opts?: QueryOpts$1);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
224
|
type QueryOpts = {
|
|
229
225
|
url?: string;
|
|
230
226
|
adapter?: typeof adapter;
|
|
@@ -249,5 +245,5 @@ declare class QueryClient extends Query {
|
|
|
249
245
|
removeToken(): void;
|
|
250
246
|
}
|
|
251
247
|
|
|
252
|
-
export {
|
|
248
|
+
export { Query, QueryClient, QueryWs, adapter, wrapperError };
|
|
253
249
|
export type { Data, DataOpts, QueryOpts, QueryWsOpts, Result };
|
package/dist/query-browser.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const isTextForContentType = (contentType) => {
|
|
2
2
|
if (!contentType)
|
|
3
3
|
return false;
|
|
4
|
-
const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded'];
|
|
4
|
+
const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded', 'md'];
|
|
5
5
|
return textTypes.some((type) => contentType.includes(type));
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
@@ -13,9 +13,14 @@ const isTextForContentType = (contentType) => {
|
|
|
13
13
|
const adapter = async (opts = {}, overloadOpts) => {
|
|
14
14
|
const controller = new AbortController();
|
|
15
15
|
const signal = controller.signal;
|
|
16
|
-
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
|
17
|
-
const isText = opts.isText || false; // 是否返回文本内容
|
|
18
16
|
const isPostFile = opts.isPostFile || false; // 是否为文件上传
|
|
17
|
+
let responseType = opts.responseType || 'json'; // 响应类型
|
|
18
|
+
if (opts.isBlob) {
|
|
19
|
+
responseType = 'blob';
|
|
20
|
+
}
|
|
21
|
+
else if (opts.isText) {
|
|
22
|
+
responseType = 'text';
|
|
23
|
+
}
|
|
19
24
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
|
20
25
|
const timer = setTimeout(() => {
|
|
21
26
|
controller.abort();
|
|
@@ -35,6 +40,10 @@ const adapter = async (opts = {}, overloadOpts) => {
|
|
|
35
40
|
if (isGet) {
|
|
36
41
|
url.search = new URLSearchParams(opts.body).toString();
|
|
37
42
|
}
|
|
43
|
+
else {
|
|
44
|
+
const params = opts.params || {};
|
|
45
|
+
url.search = new URLSearchParams(params).toString();
|
|
46
|
+
}
|
|
38
47
|
let body = undefined;
|
|
39
48
|
if (isGet) {
|
|
40
49
|
body = undefined;
|
|
@@ -59,9 +68,10 @@ const adapter = async (opts = {}, overloadOpts) => {
|
|
|
59
68
|
.then(async (response) => {
|
|
60
69
|
// 获取 Content-Type 头部信息
|
|
61
70
|
const contentType = response.headers.get('Content-Type');
|
|
62
|
-
if (
|
|
71
|
+
if (responseType === 'blob') {
|
|
63
72
|
return await response.blob(); // 直接返回 Blob 对象
|
|
64
73
|
}
|
|
74
|
+
const isText = responseType === 'text';
|
|
65
75
|
const isJson = contentType && contentType.includes('application/json');
|
|
66
76
|
// 判断返回的数据类型
|
|
67
77
|
if (isJson && !isText) {
|
|
@@ -113,7 +123,7 @@ const createStoreImpl = (createState) => {
|
|
|
113
123
|
const initialState = state = createState(setState, getState, api);
|
|
114
124
|
return api;
|
|
115
125
|
};
|
|
116
|
-
const createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;
|
|
126
|
+
const createStore = ((createState) => createState ? createStoreImpl(createState) : createStoreImpl);
|
|
117
127
|
|
|
118
128
|
const parseWsUrl = (url) => {
|
|
119
129
|
try {
|
|
@@ -351,7 +361,13 @@ const wrapperError = ({ code, message }) => {
|
|
|
351
361
|
class Query {
|
|
352
362
|
adapter;
|
|
353
363
|
url;
|
|
364
|
+
/**
|
|
365
|
+
* 请求前处理函数
|
|
366
|
+
*/
|
|
354
367
|
beforeRequest;
|
|
368
|
+
/**
|
|
369
|
+
* 请求后处理函数
|
|
370
|
+
*/
|
|
355
371
|
afterResponse;
|
|
356
372
|
headers;
|
|
357
373
|
timeout;
|
|
@@ -431,7 +447,7 @@ class Query {
|
|
|
431
447
|
code: 500,
|
|
432
448
|
message: 'api request beforeFn error'});
|
|
433
449
|
}
|
|
434
|
-
if (this.stop) {
|
|
450
|
+
if (this.stop && !options?.noStop) {
|
|
435
451
|
const that = this;
|
|
436
452
|
await new Promise((resolve) => {
|
|
437
453
|
let timer = 0;
|
|
@@ -468,14 +484,14 @@ class Query {
|
|
|
468
484
|
});
|
|
469
485
|
}
|
|
470
486
|
/**
|
|
471
|
-
*
|
|
487
|
+
* 设置请求前处理,设置请求前处理函数
|
|
472
488
|
* @param fn 处理函数
|
|
473
489
|
*/
|
|
474
490
|
before(fn) {
|
|
475
491
|
this.beforeRequest = fn;
|
|
476
492
|
}
|
|
477
493
|
/**
|
|
478
|
-
*
|
|
494
|
+
* 设置请求后处理,设置请求后处理函数
|
|
479
495
|
* @param fn 处理函数
|
|
480
496
|
*/
|
|
481
497
|
after(fn) {
|
|
@@ -500,36 +516,6 @@ class Query {
|
|
|
500
516
|
return setBaseResponse(res);
|
|
501
517
|
}
|
|
502
518
|
}
|
|
503
|
-
class BaseQuery {
|
|
504
|
-
query;
|
|
505
|
-
queryDefine;
|
|
506
|
-
constructor(opts) {
|
|
507
|
-
if (opts?.clientQuery) {
|
|
508
|
-
this.query = opts.clientQuery;
|
|
509
|
-
}
|
|
510
|
-
else {
|
|
511
|
-
this.query = opts?.query;
|
|
512
|
-
}
|
|
513
|
-
if (opts.queryDefine) {
|
|
514
|
-
this.queryDefine = opts.queryDefine;
|
|
515
|
-
this.queryDefine.query = this.query;
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
get chain() {
|
|
519
|
-
return this.queryDefine.queryChain;
|
|
520
|
-
}
|
|
521
|
-
post(data, options) {
|
|
522
|
-
return this.query.post(data, options);
|
|
523
|
-
}
|
|
524
|
-
get(data, options) {
|
|
525
|
-
return this.query.get(data, options);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
class ClientQuery extends Query {
|
|
529
|
-
constructor(opts) {
|
|
530
|
-
super({ ...opts, url: opts?.url || '/client/router' });
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
519
|
|
|
534
520
|
/**
|
|
535
521
|
* 前端调用后端QueryRouter, 封装 beforeRequest 和 wss
|
|
@@ -572,4 +558,4 @@ class QueryClient extends Query {
|
|
|
572
558
|
// 移除默认生成的实例
|
|
573
559
|
// export const client = new QueryClient();
|
|
574
560
|
|
|
575
|
-
export {
|
|
561
|
+
export { Query, QueryClient, QueryWs, adapter, wrapperError };
|
package/dist/query-ws.js
CHANGED
|
@@ -19,7 +19,7 @@ const createStoreImpl = (createState) => {
|
|
|
19
19
|
const initialState = state = createState(setState, getState, api);
|
|
20
20
|
return api;
|
|
21
21
|
};
|
|
22
|
-
const createStore = (createState) => createState ? createStoreImpl(createState) : createStoreImpl;
|
|
22
|
+
const createStore = ((createState) => createState ? createStoreImpl(createState) : createStoreImpl);
|
|
23
23
|
|
|
24
24
|
const parseWsUrl = (url) => {
|
|
25
25
|
try {
|
package/dist/query.d.ts
CHANGED
|
@@ -5,11 +5,26 @@ type Method = (typeof methods)[number];
|
|
|
5
5
|
type AdapterOpts = {
|
|
6
6
|
url?: string;
|
|
7
7
|
headers?: Record<string, string>;
|
|
8
|
+
/**
|
|
9
|
+
* 只用户POST请求,传递的查询参数,
|
|
10
|
+
* GET请求默认方body自己转化为查询参数
|
|
11
|
+
*/
|
|
12
|
+
params?: Record<string, any>;
|
|
8
13
|
body?: Record<string, any> | FormData;
|
|
9
14
|
timeout?: number;
|
|
10
15
|
method?: Method;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated use responseType
|
|
18
|
+
*/
|
|
11
19
|
isBlob?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated use responseType
|
|
22
|
+
*/
|
|
12
23
|
isText?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* 响应类型,
|
|
26
|
+
* */
|
|
27
|
+
responseType?: 'json' | 'text' | 'blob';
|
|
13
28
|
isPostFile?: boolean;
|
|
14
29
|
};
|
|
15
30
|
/**
|
|
@@ -94,16 +109,9 @@ type Fn = (opts: {
|
|
|
94
109
|
timeout?: number;
|
|
95
110
|
}) => Promise<Record<string, any> | false>;
|
|
96
111
|
type QueryOpts = {
|
|
97
|
-
url?: string;
|
|
98
|
-
headers?: Record<string, string>;
|
|
99
|
-
body?: Record<string, any> | FormData;
|
|
100
|
-
timeout?: number;
|
|
101
|
-
method?: Method;
|
|
102
|
-
isBlob?: boolean;
|
|
103
|
-
isPostFile?: boolean;
|
|
104
112
|
adapter?: typeof adapter;
|
|
105
113
|
[key: string]: any;
|
|
106
|
-
};
|
|
114
|
+
} & AdapterOpts;
|
|
107
115
|
type Data = {
|
|
108
116
|
path?: string;
|
|
109
117
|
key?: string;
|
|
@@ -134,6 +142,10 @@ type DataOpts = Partial<QueryOpts> & {
|
|
|
134
142
|
res?: any;
|
|
135
143
|
fetch?: any;
|
|
136
144
|
}) => Promise<Result<S>>;
|
|
145
|
+
/**
|
|
146
|
+
* 是否在stop的时候不请求
|
|
147
|
+
*/
|
|
148
|
+
noStop?: boolean;
|
|
137
149
|
};
|
|
138
150
|
/**
|
|
139
151
|
* 设置基础响应, 设置 success 和 showError,
|
|
@@ -164,7 +176,13 @@ declare const wrapperError: ({ code, message }: {
|
|
|
164
176
|
declare class Query {
|
|
165
177
|
adapter: typeof adapter;
|
|
166
178
|
url: string;
|
|
179
|
+
/**
|
|
180
|
+
* 请求前处理函数
|
|
181
|
+
*/
|
|
167
182
|
beforeRequest?: DataOpts['beforeRequest'];
|
|
183
|
+
/**
|
|
184
|
+
* 请求后处理函数
|
|
185
|
+
*/
|
|
168
186
|
afterResponse?: DataOpts['afterResponse'];
|
|
169
187
|
headers?: Record<string, string>;
|
|
170
188
|
timeout?: number;
|
|
@@ -198,39 +216,17 @@ declare class Query {
|
|
|
198
216
|
*/
|
|
199
217
|
post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>>;
|
|
200
218
|
/**
|
|
201
|
-
*
|
|
219
|
+
* 设置请求前处理,设置请求前处理函数
|
|
202
220
|
* @param fn 处理函数
|
|
203
221
|
*/
|
|
204
222
|
before(fn: DataOpts['beforeRequest']): void;
|
|
205
223
|
/**
|
|
206
|
-
*
|
|
224
|
+
* 设置请求后处理,设置请求后处理函数
|
|
207
225
|
* @param fn 处理函数
|
|
208
226
|
*/
|
|
209
227
|
after(fn: DataOpts['afterResponse']): void;
|
|
210
228
|
fetchText(urlOrOptions?: string | QueryOpts, options?: QueryOpts): Promise<Result<any>>;
|
|
211
229
|
}
|
|
212
230
|
|
|
213
|
-
|
|
214
|
-
queryChain?: any;
|
|
215
|
-
query?: any;
|
|
216
|
-
} = {
|
|
217
|
-
queryChain: any;
|
|
218
|
-
query?: T;
|
|
219
|
-
}> {
|
|
220
|
-
query: T;
|
|
221
|
-
queryDefine: R;
|
|
222
|
-
constructor(opts?: {
|
|
223
|
-
query?: T;
|
|
224
|
-
queryDefine?: R;
|
|
225
|
-
clientQuery?: T;
|
|
226
|
-
});
|
|
227
|
-
get chain(): R['queryChain'];
|
|
228
|
-
post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
229
|
-
get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
230
|
-
}
|
|
231
|
-
declare class ClientQuery extends Query {
|
|
232
|
-
constructor(opts?: QueryOpts);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
export { BaseQuery, ClientQuery, Query, adapter, setBaseResponse, wrapperError };
|
|
231
|
+
export { Query, adapter, setBaseResponse, wrapperError };
|
|
236
232
|
export type { Data, DataOpts, Fn, QueryOpts, Result };
|
package/dist/query.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const isTextForContentType = (contentType) => {
|
|
2
2
|
if (!contentType)
|
|
3
3
|
return false;
|
|
4
|
-
const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded'];
|
|
4
|
+
const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded', 'md'];
|
|
5
5
|
return textTypes.some((type) => contentType.includes(type));
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
@@ -13,9 +13,14 @@ const isTextForContentType = (contentType) => {
|
|
|
13
13
|
const adapter = async (opts = {}, overloadOpts) => {
|
|
14
14
|
const controller = new AbortController();
|
|
15
15
|
const signal = controller.signal;
|
|
16
|
-
const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
|
|
17
|
-
const isText = opts.isText || false; // 是否返回文本内容
|
|
18
16
|
const isPostFile = opts.isPostFile || false; // 是否为文件上传
|
|
17
|
+
let responseType = opts.responseType || 'json'; // 响应类型
|
|
18
|
+
if (opts.isBlob) {
|
|
19
|
+
responseType = 'blob';
|
|
20
|
+
}
|
|
21
|
+
else if (opts.isText) {
|
|
22
|
+
responseType = 'text';
|
|
23
|
+
}
|
|
19
24
|
const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
|
20
25
|
const timer = setTimeout(() => {
|
|
21
26
|
controller.abort();
|
|
@@ -35,6 +40,10 @@ const adapter = async (opts = {}, overloadOpts) => {
|
|
|
35
40
|
if (isGet) {
|
|
36
41
|
url.search = new URLSearchParams(opts.body).toString();
|
|
37
42
|
}
|
|
43
|
+
else {
|
|
44
|
+
const params = opts.params || {};
|
|
45
|
+
url.search = new URLSearchParams(params).toString();
|
|
46
|
+
}
|
|
38
47
|
let body = undefined;
|
|
39
48
|
if (isGet) {
|
|
40
49
|
body = undefined;
|
|
@@ -59,9 +68,10 @@ const adapter = async (opts = {}, overloadOpts) => {
|
|
|
59
68
|
.then(async (response) => {
|
|
60
69
|
// 获取 Content-Type 头部信息
|
|
61
70
|
const contentType = response.headers.get('Content-Type');
|
|
62
|
-
if (
|
|
71
|
+
if (responseType === 'blob') {
|
|
63
72
|
return await response.blob(); // 直接返回 Blob 对象
|
|
64
73
|
}
|
|
74
|
+
const isText = responseType === 'text';
|
|
65
75
|
const isJson = contentType && contentType.includes('application/json');
|
|
66
76
|
// 判断返回的数据类型
|
|
67
77
|
if (isJson && !isText) {
|
|
@@ -135,7 +145,13 @@ const wrapperError = ({ code, message }) => {
|
|
|
135
145
|
class Query {
|
|
136
146
|
adapter;
|
|
137
147
|
url;
|
|
148
|
+
/**
|
|
149
|
+
* 请求前处理函数
|
|
150
|
+
*/
|
|
138
151
|
beforeRequest;
|
|
152
|
+
/**
|
|
153
|
+
* 请求后处理函数
|
|
154
|
+
*/
|
|
139
155
|
afterResponse;
|
|
140
156
|
headers;
|
|
141
157
|
timeout;
|
|
@@ -215,7 +231,7 @@ class Query {
|
|
|
215
231
|
code: 500,
|
|
216
232
|
message: 'api request beforeFn error'});
|
|
217
233
|
}
|
|
218
|
-
if (this.stop) {
|
|
234
|
+
if (this.stop && !options?.noStop) {
|
|
219
235
|
const that = this;
|
|
220
236
|
await new Promise((resolve) => {
|
|
221
237
|
let timer = 0;
|
|
@@ -252,14 +268,14 @@ class Query {
|
|
|
252
268
|
});
|
|
253
269
|
}
|
|
254
270
|
/**
|
|
255
|
-
*
|
|
271
|
+
* 设置请求前处理,设置请求前处理函数
|
|
256
272
|
* @param fn 处理函数
|
|
257
273
|
*/
|
|
258
274
|
before(fn) {
|
|
259
275
|
this.beforeRequest = fn;
|
|
260
276
|
}
|
|
261
277
|
/**
|
|
262
|
-
*
|
|
278
|
+
* 设置请求后处理,设置请求后处理函数
|
|
263
279
|
* @param fn 处理函数
|
|
264
280
|
*/
|
|
265
281
|
after(fn) {
|
|
@@ -284,35 +300,5 @@ class Query {
|
|
|
284
300
|
return setBaseResponse(res);
|
|
285
301
|
}
|
|
286
302
|
}
|
|
287
|
-
class BaseQuery {
|
|
288
|
-
query;
|
|
289
|
-
queryDefine;
|
|
290
|
-
constructor(opts) {
|
|
291
|
-
if (opts?.clientQuery) {
|
|
292
|
-
this.query = opts.clientQuery;
|
|
293
|
-
}
|
|
294
|
-
else {
|
|
295
|
-
this.query = opts?.query;
|
|
296
|
-
}
|
|
297
|
-
if (opts.queryDefine) {
|
|
298
|
-
this.queryDefine = opts.queryDefine;
|
|
299
|
-
this.queryDefine.query = this.query;
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
get chain() {
|
|
303
|
-
return this.queryDefine.queryChain;
|
|
304
|
-
}
|
|
305
|
-
post(data, options) {
|
|
306
|
-
return this.query.post(data, options);
|
|
307
|
-
}
|
|
308
|
-
get(data, options) {
|
|
309
|
-
return this.query.get(data, options);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
class ClientQuery extends Query {
|
|
313
|
-
constructor(opts) {
|
|
314
|
-
super({ ...opts, url: opts?.url || '/client/router' });
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
303
|
|
|
318
|
-
export {
|
|
304
|
+
export { Query, adapter, setBaseResponse, wrapperError };
|
package/package.json
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kevisual/query",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"main": "dist/
|
|
5
|
-
"module": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
3
|
+
"version": "0.0.30",
|
|
4
|
+
"main": "dist/query-browser.js",
|
|
7
5
|
"private": false,
|
|
8
6
|
"type": "module",
|
|
9
7
|
"scripts": {
|
|
@@ -23,16 +21,13 @@
|
|
|
23
21
|
"license": "ISC",
|
|
24
22
|
"description": "",
|
|
25
23
|
"devDependencies": {
|
|
26
|
-
"@rollup/plugin-node-resolve": "^16.0.
|
|
27
|
-
"@rollup/plugin-typescript": "^12.
|
|
28
|
-
"rollup": "^4.
|
|
29
|
-
"rollup-plugin-dts": "^6.
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"typescript": "^5.8.3",
|
|
33
|
-
"zustand": "^5.0.5"
|
|
24
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
25
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
26
|
+
"rollup": "^4.53.3",
|
|
27
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
28
|
+
"typescript": "^5.9.3",
|
|
29
|
+
"zustand": "^5.0.9"
|
|
34
30
|
},
|
|
35
|
-
"packageManager": "yarn@1.22.22",
|
|
36
31
|
"publishConfig": {
|
|
37
32
|
"access": "public"
|
|
38
33
|
},
|
|
@@ -57,8 +52,5 @@
|
|
|
57
52
|
"import": "./dist/query-ai.js",
|
|
58
53
|
"require": "./dist/query-ai.js"
|
|
59
54
|
}
|
|
60
|
-
},
|
|
61
|
-
"dependencies": {
|
|
62
|
-
"openai": "^5.0.1"
|
|
63
55
|
}
|
|
64
56
|
}
|
package/dist/query-ai.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import OpenAI, { ClientOptions } from 'openai';
|
|
2
|
-
export { default as OpenAI } from 'openai';
|
|
3
|
-
import { RequestOptions } from 'openai/core.mjs';
|
|
4
|
-
|
|
5
|
-
type QueryOpts = {
|
|
6
|
-
/**
|
|
7
|
-
* OpenAI model name, example: deepseek-chat
|
|
8
|
-
*/
|
|
9
|
-
model: string;
|
|
10
|
-
/**
|
|
11
|
-
* OpenAI client options
|
|
12
|
-
* QueryAi.init() will be called with these options
|
|
13
|
-
*/
|
|
14
|
-
openAiOpts?: ClientOptions;
|
|
15
|
-
openai?: OpenAI;
|
|
16
|
-
};
|
|
17
|
-
declare class QueryAI {
|
|
18
|
-
private openai;
|
|
19
|
-
model?: string;
|
|
20
|
-
constructor(opts?: QueryOpts);
|
|
21
|
-
init(opts: ClientOptions): void;
|
|
22
|
-
query(prompt: string, opts?: RequestOptions): Promise<OpenAI.Chat.Completions.ChatCompletion & {
|
|
23
|
-
_request_id?: string | null;
|
|
24
|
-
}>;
|
|
25
|
-
queryAsync(prompt: string, opts?: RequestOptions): Promise<OpenAI.Chat.Completions.ChatCompletion & {
|
|
26
|
-
_request_id?: string | null;
|
|
27
|
-
}>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export { QueryAI };
|