@kevisual/query 0.0.22 → 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 };
@@ -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) => {
@@ -470,7 +483,7 @@ class Query {
470
483
  ...(_options?.headers || {}),
471
484
  },
472
485
  });
473
- if (!res.ok) {
486
+ if (res.status !== 200) {
474
487
  return wrapperError({
475
488
  code: res.status,
476
489
  message: `fetch error: ${res.statusText}`,
@@ -482,28 +495,10 @@ class Query {
482
495
  const result = { code: res.status, data, success: res.ok };
483
496
  return setBaseResponse(result);
484
497
  }
485
- if (contentType && contentType.includes('text/html')) {
498
+ if (isTextForContentType(contentType)) {
486
499
  const text = await res.text();
487
500
  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,
501
+ code: res.status || 200,
507
502
  data: text,
508
503
  success: res.ok,
509
504
  };
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) => {
@@ -254,7 +267,7 @@ class Query {
254
267
  ...(_options?.headers || {}),
255
268
  },
256
269
  });
257
- if (!res.ok) {
270
+ if (res.status !== 200) {
258
271
  return wrapperError({
259
272
  code: res.status,
260
273
  message: `fetch error: ${res.statusText}`,
@@ -266,28 +279,10 @@ class Query {
266
279
  const result = { code: res.status, data, success: res.ok };
267
280
  return setBaseResponse(result);
268
281
  }
269
- if (contentType && contentType.includes('text/html')) {
282
+ if (isTextForContentType(contentType)) {
270
283
  const text = await res.text();
271
284
  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,
285
+ code: res.status || 200,
291
286
  data: text,
292
287
  success: res.ok,
293
288
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",