@kevisual/query 0.0.20 → 0.0.22

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.
@@ -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: string;
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
  *
@@ -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 (contentType && contentType.includes('application/json')) {
48
+ if (isJson) {
44
49
  return response.json(); // 解析为 JSON
45
50
  }
46
51
  else {
@@ -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: string;
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(urlOrOptions?: string | QueryOpts$1, options?: QueryOpts$1): 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: T;
213
+ query?: T;
210
214
  queryDefine?: R;
211
215
  clientQuery?: T;
212
216
  });
@@ -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 (contentType && contentType.includes('application/json')) {
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,69 @@ class Query {
448
454
  after(fn) {
449
455
  this.afterResponse = fn;
450
456
  }
457
+ async fetchText(urlOrOptions, options) {
458
+ let _options = { ...options };
459
+ if (typeof urlOrOptions === 'string' && !_options.url) {
460
+ _options.url = urlOrOptions;
461
+ }
462
+ if (typeof urlOrOptions === 'object') {
463
+ _options = { ...urlOrOptions, ..._options };
464
+ }
465
+ const res = await adapter({
466
+ method: 'GET',
467
+ ..._options,
468
+ headers: {
469
+ ...this.headers,
470
+ ...(_options?.headers || {}),
471
+ },
472
+ });
473
+ if (!res.ok) {
474
+ return wrapperError({
475
+ code: res.status,
476
+ message: `fetch error: ${res.statusText}`,
477
+ });
478
+ }
479
+ const contentType = res.headers.get('Content-Type');
480
+ if (contentType && contentType.includes('application/json')) {
481
+ const data = await res.json();
482
+ const result = { code: res.status, data, success: res.ok };
483
+ return setBaseResponse(result);
484
+ }
485
+ if (contentType && contentType.includes('text/html')) {
486
+ const text = await res.text();
487
+ const result = {
488
+ code: res.status,
489
+ data: text,
490
+ success: res.ok,
491
+ };
492
+ return setBaseResponse(result);
493
+ }
494
+ if (contentType && contentType.includes('text/plain')) {
495
+ let text = await res.text();
496
+ // 处理特殊情况,比如返回的是纯文本
497
+ if (text.startsWith('{')) {
498
+ try {
499
+ text = JSON.parse(text);
500
+ }
501
+ catch (e) {
502
+ // 如果解析失败,保持原样
503
+ }
504
+ }
505
+ const result = {
506
+ code: res.status,
507
+ data: text,
508
+ success: res.ok,
509
+ };
510
+ return setBaseResponse(result);
511
+ }
512
+ const blob = await res.blob();
513
+ const result = {
514
+ code: res.status,
515
+ data: blob,
516
+ success: res.ok,
517
+ };
518
+ return setBaseResponse(result);
519
+ }
451
520
  }
452
521
  class BaseQuery {
453
522
  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: string;
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) => void;
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(urlOrOptions?: string | QueryOpts, options?: QueryOpts): 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: T;
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 (contentType && contentType.includes('application/json')) {
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,69 @@ class Query {
232
238
  after(fn) {
233
239
  this.afterResponse = fn;
234
240
  }
241
+ async fetchText(urlOrOptions, options) {
242
+ let _options = { ...options };
243
+ if (typeof urlOrOptions === 'string' && !_options.url) {
244
+ _options.url = urlOrOptions;
245
+ }
246
+ if (typeof urlOrOptions === 'object') {
247
+ _options = { ...urlOrOptions, ..._options };
248
+ }
249
+ const res = await adapter({
250
+ method: 'GET',
251
+ ..._options,
252
+ headers: {
253
+ ...this.headers,
254
+ ...(_options?.headers || {}),
255
+ },
256
+ });
257
+ if (!res.ok) {
258
+ return wrapperError({
259
+ code: res.status,
260
+ message: `fetch error: ${res.statusText}`,
261
+ });
262
+ }
263
+ const contentType = res.headers.get('Content-Type');
264
+ if (contentType && contentType.includes('application/json')) {
265
+ const data = await res.json();
266
+ const result = { code: res.status, data, success: res.ok };
267
+ return setBaseResponse(result);
268
+ }
269
+ if (contentType && contentType.includes('text/html')) {
270
+ const text = await res.text();
271
+ const result = {
272
+ code: res.status,
273
+ data: text,
274
+ success: res.ok,
275
+ };
276
+ return setBaseResponse(result);
277
+ }
278
+ if (contentType && contentType.includes('text/plain')) {
279
+ let text = await res.text();
280
+ // 处理特殊情况,比如返回的是纯文本
281
+ if (text.startsWith('{')) {
282
+ try {
283
+ text = JSON.parse(text);
284
+ }
285
+ catch (e) {
286
+ // 如果解析失败,保持原样
287
+ }
288
+ }
289
+ const result = {
290
+ code: res.status,
291
+ data: text,
292
+ success: res.ok,
293
+ };
294
+ return setBaseResponse(result);
295
+ }
296
+ const blob = await res.blob();
297
+ const result = {
298
+ code: res.status,
299
+ data: blob,
300
+ success: res.ok,
301
+ };
302
+ return setBaseResponse(result);
303
+ }
235
304
  }
236
305
  class BaseQuery {
237
306
  query;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.20",
3
+ "version": "0.0.22",
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.40.2",
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.4"
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": "^4.98.0"
62
+ "openai": "^5.0.1"
63
63
  }
64
64
  }