@kevisual/query 0.0.21 → 0.0.23

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.
@@ -8,6 +8,7 @@ type AdapterOpts = {
8
8
  method?: Method;
9
9
  isBlob?: boolean;
10
10
  };
11
+ declare const isTextForContentType: (contentType: string | null) => boolean;
11
12
  /**
12
13
  *
13
14
  * @param opts
@@ -20,5 +21,5 @@ declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promis
20
21
  */
21
22
  declare const queryFetch: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
22
23
 
23
- export { adapter, methods, queryFetch };
24
+ export { adapter, isTextForContentType, methods, queryFetch };
24
25
  export type { AdapterOpts, Method };
@@ -1,4 +1,10 @@
1
1
  const methods = ['GET', 'POST'];
2
+ const isTextForContentType = (contentType) => {
3
+ if (!contentType)
4
+ return false;
5
+ const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded'];
6
+ return textTypes.some((type) => contentType.includes(type));
7
+ };
2
8
  /**
3
9
  *
4
10
  * @param opts
@@ -48,8 +54,15 @@ const adapter = async (opts, overloadOpts) => {
48
54
  if (isJson) {
49
55
  return response.json(); // 解析为 JSON
50
56
  }
57
+ else if (isTextForContentType(contentType)) {
58
+ return {
59
+ code: 200,
60
+ status: response.status,
61
+ data: response.text(), // 直接返回文本内容
62
+ };
63
+ }
51
64
  else {
52
- return response.text(); // 解析为文本
65
+ return response;
53
66
  }
54
67
  })
55
68
  .catch((err) => {
@@ -70,4 +83,4 @@ const adapter = async (opts, overloadOpts) => {
70
83
  */
71
84
  const queryFetch = adapter;
72
85
 
73
- export { adapter, methods, queryFetch };
86
+ export { adapter, isTextForContentType, methods, queryFetch };
@@ -197,7 +197,7 @@ declare class Query {
197
197
  * @param fn 处理函数
198
198
  */
199
199
  after(fn: DataOpts['afterResponse']): void;
200
- fetchText(url: string, options?: RequestInit): Promise<Result<any>>;
200
+ fetchText(urlOrOptions?: string | QueryOpts$1, options?: QueryOpts$1): Promise<Result<any>>;
201
201
  }
202
202
 
203
203
  declare class BaseQuery<T extends Query = Query, R extends {
@@ -1,3 +1,9 @@
1
+ const isTextForContentType = (contentType) => {
2
+ if (!contentType)
3
+ return false;
4
+ const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded'];
5
+ return textTypes.some((type) => contentType.includes(type));
6
+ };
1
7
  /**
2
8
  *
3
9
  * @param opts
@@ -47,8 +53,15 @@ const adapter = async (opts, overloadOpts) => {
47
53
  if (isJson) {
48
54
  return response.json(); // 解析为 JSON
49
55
  }
56
+ else if (isTextForContentType(contentType)) {
57
+ return {
58
+ code: 200,
59
+ status: response.status,
60
+ data: response.text(), // 直接返回文本内容
61
+ };
62
+ }
50
63
  else {
51
- return response.text(); // 解析为文本
64
+ return response;
52
65
  }
53
66
  })
54
67
  .catch((err) => {
@@ -454,16 +467,23 @@ class Query {
454
467
  after(fn) {
455
468
  this.afterResponse = fn;
456
469
  }
457
- async fetchText(url, options) {
458
- const res = await fetch(url, {
470
+ async fetchText(urlOrOptions, options) {
471
+ let _options = { ...options };
472
+ if (typeof urlOrOptions === 'string' && !_options.url) {
473
+ _options.url = urlOrOptions;
474
+ }
475
+ if (typeof urlOrOptions === 'object') {
476
+ _options = { ...urlOrOptions, ..._options };
477
+ }
478
+ const res = await adapter({
459
479
  method: 'GET',
460
- ...options,
480
+ ..._options,
461
481
  headers: {
462
482
  ...this.headers,
463
- ...(options?.headers || {}),
483
+ ...(_options?.headers || {}),
464
484
  },
465
485
  });
466
- if (!res.ok) {
486
+ if (res.status !== 200) {
467
487
  return wrapperError({
468
488
  code: res.status,
469
489
  message: `fetch error: ${res.statusText}`,
@@ -475,28 +495,10 @@ class Query {
475
495
  const result = { code: res.status, data, success: res.ok };
476
496
  return setBaseResponse(result);
477
497
  }
478
- if (contentType && contentType.includes('text/html')) {
498
+ if (isTextForContentType(contentType)) {
479
499
  const text = await res.text();
480
500
  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,
501
+ code: res.status || 200,
500
502
  data: text,
501
503
  success: res.ok,
502
504
  };
package/dist/query.d.ts CHANGED
@@ -204,7 +204,7 @@ declare class Query {
204
204
  * @param fn 处理函数
205
205
  */
206
206
  after(fn: DataOpts['afterResponse']): void;
207
- fetchText(url: string, options?: RequestInit): Promise<Result<any>>;
207
+ fetchText(urlOrOptions?: string | QueryOpts, options?: QueryOpts): Promise<Result<any>>;
208
208
  }
209
209
 
210
210
  declare class BaseQuery<T extends Query = Query, R extends {
package/dist/query.js CHANGED
@@ -1,3 +1,9 @@
1
+ const isTextForContentType = (contentType) => {
2
+ if (!contentType)
3
+ return false;
4
+ const textTypes = ['text/', 'xml', 'html', 'javascript', 'css', 'csv', 'plain', 'x-www-form-urlencoded'];
5
+ return textTypes.some((type) => contentType.includes(type));
6
+ };
1
7
  /**
2
8
  *
3
9
  * @param opts
@@ -47,8 +53,15 @@ const adapter = async (opts, overloadOpts) => {
47
53
  if (isJson) {
48
54
  return response.json(); // 解析为 JSON
49
55
  }
56
+ else if (isTextForContentType(contentType)) {
57
+ return {
58
+ code: 200,
59
+ status: response.status,
60
+ data: response.text(), // 直接返回文本内容
61
+ };
62
+ }
50
63
  else {
51
- return response.text(); // 解析为文本
64
+ return response;
52
65
  }
53
66
  })
54
67
  .catch((err) => {
@@ -238,16 +251,23 @@ class Query {
238
251
  after(fn) {
239
252
  this.afterResponse = fn;
240
253
  }
241
- async fetchText(url, options) {
242
- const res = await fetch(url, {
254
+ async fetchText(urlOrOptions, options) {
255
+ let _options = { ...options };
256
+ if (typeof urlOrOptions === 'string' && !_options.url) {
257
+ _options.url = urlOrOptions;
258
+ }
259
+ if (typeof urlOrOptions === 'object') {
260
+ _options = { ...urlOrOptions, ..._options };
261
+ }
262
+ const res = await adapter({
243
263
  method: 'GET',
244
- ...options,
264
+ ..._options,
245
265
  headers: {
246
266
  ...this.headers,
247
- ...(options?.headers || {}),
267
+ ...(_options?.headers || {}),
248
268
  },
249
269
  });
250
- if (!res.ok) {
270
+ if (res.status !== 200) {
251
271
  return wrapperError({
252
272
  code: res.status,
253
273
  message: `fetch error: ${res.statusText}`,
@@ -259,28 +279,10 @@ class Query {
259
279
  const result = { code: res.status, data, success: res.ok };
260
280
  return setBaseResponse(result);
261
281
  }
262
- if (contentType && contentType.includes('text/html')) {
282
+ if (isTextForContentType(contentType)) {
263
283
  const text = await res.text();
264
284
  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,
285
+ code: res.status || 200,
284
286
  data: text,
285
287
  success: res.ok,
286
288
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.21",
3
+ "version": "0.0.23",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",