@kevisual/query 0.0.22 → 0.0.24
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 +2 -1
- package/dist/query-adapter.js +15 -2
- package/dist/query-browser.js +15 -47
- package/dist/query.js +15 -47
- package/package.json +1 -1
package/dist/query-adapter.d.ts
CHANGED
|
@@ -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 };
|
package/dist/query-adapter.js
CHANGED
|
@@ -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
|
|
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 };
|
package/dist/query-browser.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
|
|
64
|
+
return response;
|
|
52
65
|
}
|
|
53
66
|
})
|
|
54
67
|
.catch((err) => {
|
|
@@ -470,52 +483,7 @@ class Query {
|
|
|
470
483
|
...(_options?.headers || {}),
|
|
471
484
|
},
|
|
472
485
|
});
|
|
473
|
-
|
|
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);
|
|
486
|
+
return setBaseResponse(res);
|
|
519
487
|
}
|
|
520
488
|
}
|
|
521
489
|
class BaseQuery {
|
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
|
|
64
|
+
return response;
|
|
52
65
|
}
|
|
53
66
|
})
|
|
54
67
|
.catch((err) => {
|
|
@@ -254,52 +267,7 @@ class Query {
|
|
|
254
267
|
...(_options?.headers || {}),
|
|
255
268
|
},
|
|
256
269
|
});
|
|
257
|
-
|
|
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);
|
|
270
|
+
return setBaseResponse(res);
|
|
303
271
|
}
|
|
304
272
|
}
|
|
305
273
|
class BaseQuery {
|