@kevisual/query 0.0.29 → 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.
@@ -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;
@@ -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 (isBlob) {
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) {
@@ -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
  /**
@@ -154,7 +169,13 @@ declare const wrapperError: ({ code, message }: {
154
169
  declare class Query {
155
170
  adapter: typeof adapter;
156
171
  url: string;
172
+ /**
173
+ * 请求前处理函数
174
+ */
157
175
  beforeRequest?: DataOpts['beforeRequest'];
176
+ /**
177
+ * 请求后处理函数
178
+ */
158
179
  afterResponse?: DataOpts['afterResponse'];
159
180
  headers?: Record<string, string>;
160
181
  timeout?: number;
@@ -188,40 +209,18 @@ declare class Query {
188
209
  */
189
210
  post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>>;
190
211
  /**
191
- * 请求前处理,设置请求前处理函数
212
+ * 设置请求前处理,设置请求前处理函数
192
213
  * @param fn 处理函数
193
214
  */
194
215
  before(fn: DataOpts['beforeRequest']): void;
195
216
  /**
196
- * 请求后处理,设置请求后处理函数
217
+ * 设置请求后处理,设置请求后处理函数
197
218
  * @param fn 处理函数
198
219
  */
199
220
  after(fn: DataOpts['afterResponse']): void;
200
221
  fetchText(urlOrOptions?: string | QueryOpts$1, options?: QueryOpts$1): Promise<Result<any>>;
201
222
  }
202
223
 
203
- declare class BaseQuery<T extends Query = Query, R extends {
204
- queryChain?: any;
205
- query?: any;
206
- } = {
207
- queryChain: any;
208
- query?: T;
209
- }> {
210
- query: T;
211
- queryDefine: R;
212
- constructor(opts?: {
213
- query?: T;
214
- queryDefine?: R;
215
- clientQuery?: T;
216
- });
217
- get chain(): R['queryChain'];
218
- post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
219
- get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
220
- }
221
- declare class ClientQuery extends Query {
222
- constructor(opts?: QueryOpts$1);
223
- }
224
-
225
224
  type QueryOpts = {
226
225
  url?: string;
227
226
  adapter?: typeof adapter;
@@ -246,5 +245,5 @@ declare class QueryClient extends Query {
246
245
  removeToken(): void;
247
246
  }
248
247
 
249
- export { BaseQuery, ClientQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
248
+ export { Query, QueryClient, QueryWs, adapter, wrapperError };
250
249
  export type { Data, DataOpts, QueryOpts, QueryWsOpts, Result };
@@ -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 (isBlob) {
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;
@@ -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 { BaseQuery, ClientQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
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
  /**
@@ -161,7 +176,13 @@ declare const wrapperError: ({ code, message }: {
161
176
  declare class Query {
162
177
  adapter: typeof adapter;
163
178
  url: string;
179
+ /**
180
+ * 请求前处理函数
181
+ */
164
182
  beforeRequest?: DataOpts['beforeRequest'];
183
+ /**
184
+ * 请求后处理函数
185
+ */
165
186
  afterResponse?: DataOpts['afterResponse'];
166
187
  headers?: Record<string, string>;
167
188
  timeout?: number;
@@ -195,39 +216,17 @@ declare class Query {
195
216
  */
196
217
  post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>>;
197
218
  /**
198
- * 请求前处理,设置请求前处理函数
219
+ * 设置请求前处理,设置请求前处理函数
199
220
  * @param fn 处理函数
200
221
  */
201
222
  before(fn: DataOpts['beforeRequest']): void;
202
223
  /**
203
- * 请求后处理,设置请求后处理函数
224
+ * 设置请求后处理,设置请求后处理函数
204
225
  * @param fn 处理函数
205
226
  */
206
227
  after(fn: DataOpts['afterResponse']): void;
207
228
  fetchText(urlOrOptions?: string | QueryOpts, options?: QueryOpts): Promise<Result<any>>;
208
229
  }
209
230
 
210
- declare class BaseQuery<T extends Query = Query, R extends {
211
- queryChain?: any;
212
- query?: any;
213
- } = {
214
- queryChain: any;
215
- query?: T;
216
- }> {
217
- query: T;
218
- queryDefine: R;
219
- constructor(opts?: {
220
- query?: T;
221
- queryDefine?: R;
222
- clientQuery?: T;
223
- });
224
- get chain(): R['queryChain'];
225
- post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
226
- get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
227
- }
228
- declare class ClientQuery extends Query {
229
- constructor(opts?: QueryOpts);
230
- }
231
-
232
- export { BaseQuery, ClientQuery, Query, adapter, setBaseResponse, wrapperError };
231
+ export { Query, adapter, setBaseResponse, wrapperError };
233
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 (isBlob) {
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;
@@ -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 { BaseQuery, ClientQuery, Query, adapter, setBaseResponse, wrapperError };
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.29",
4
- "main": "dist/index.js",
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.1",
27
- "@rollup/plugin-typescript": "^12.1.2",
28
- "rollup": "^4.41.1",
29
- "rollup-plugin-dts": "^6.2.1",
30
- "ts-node": "^10.9.2",
31
- "tslib": "^2.8.1",
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
  }
@@ -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 };