@kevisual/query 0.0.26 → 0.0.28

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.
@@ -7,6 +7,7 @@ type AdapterOpts = {
7
7
  timeout?: number;
8
8
  method?: Method;
9
9
  isBlob?: boolean;
10
+ isText?: boolean;
10
11
  isPostFile?: boolean;
11
12
  };
12
13
  declare const isTextForContentType: (contentType: string | null) => boolean;
@@ -16,11 +17,11 @@ declare const isTextForContentType: (contentType: string | null) => boolean;
16
17
  * @param overloadOpts 覆盖fetch的默认配置
17
18
  * @returns
18
19
  */
19
- declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
20
+ declare const adapter: (opts?: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
20
21
  /**
21
22
  * adapter
22
23
  */
23
- declare const queryFetch: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
24
+ declare const queryFetch: (opts?: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
24
25
 
25
26
  export { adapter, isTextForContentType, methods, queryFetch };
26
27
  export type { AdapterOpts, Method };
@@ -11,16 +11,18 @@ const isTextForContentType = (contentType) => {
11
11
  * @param overloadOpts 覆盖fetch的默认配置
12
12
  * @returns
13
13
  */
14
- const adapter = async (opts, overloadOpts) => {
14
+ const adapter = async (opts = {}, overloadOpts) => {
15
15
  const controller = new AbortController();
16
16
  const signal = controller.signal;
17
17
  const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
18
+ const isText = opts.isText || false; // 是否返回文本内容
18
19
  const isPostFile = opts.isPostFile || false; // 是否为文件上传
19
20
  const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
20
21
  const timer = setTimeout(() => {
21
22
  controller.abort();
22
23
  }, timeout);
23
- let method = overloadOpts?.method || opts.method || 'POST';
24
+ let method = overloadOpts?.method || opts?.method || 'POST';
25
+ let headers = { ...opts?.headers, ...overloadOpts?.headers };
24
26
  let origin = '';
25
27
  let url;
26
28
  if (opts?.url?.startsWith('http')) {
@@ -42,17 +44,18 @@ const adapter = async (opts, overloadOpts) => {
42
44
  body = opts.body; // 如果是文件上传,直接使用 FormData
43
45
  }
44
46
  else {
47
+ headers = {
48
+ 'Content-Type': 'application/json',
49
+ ...headers,
50
+ };
45
51
  body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
46
52
  }
47
53
  return fetch(url, {
48
54
  method: method.toUpperCase(),
49
- headers: {
50
- 'Content-Type': 'application/json',
51
- ...opts.headers,
52
- },
53
55
  signal,
54
- ...overloadOpts,
55
56
  body: body,
57
+ ...overloadOpts,
58
+ headers: headers,
56
59
  })
57
60
  .then(async (response) => {
58
61
  // 获取 Content-Type 头部信息
@@ -62,7 +65,7 @@ const adapter = async (opts, overloadOpts) => {
62
65
  }
63
66
  const isJson = contentType && contentType.includes('application/json');
64
67
  // 判断返回的数据类型
65
- if (isJson) {
68
+ if (isJson && !isText) {
66
69
  return await response.json(); // 解析为 JSON
67
70
  }
68
71
  else if (isTextForContentType(contentType)) {
@@ -9,6 +9,7 @@ type AdapterOpts = {
9
9
  timeout?: number;
10
10
  method?: Method;
11
11
  isBlob?: boolean;
12
+ isText?: boolean;
12
13
  isPostFile?: boolean;
13
14
  };
14
15
  /**
@@ -17,7 +18,7 @@ type AdapterOpts = {
17
18
  * @param overloadOpts 覆盖fetch的默认配置
18
19
  * @returns
19
20
  */
20
- declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
21
+ declare const adapter: (opts?: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
21
22
 
22
23
  type QueryWsStore = {
23
24
  connected: boolean;
@@ -10,16 +10,18 @@ const isTextForContentType = (contentType) => {
10
10
  * @param overloadOpts 覆盖fetch的默认配置
11
11
  * @returns
12
12
  */
13
- const adapter = async (opts, overloadOpts) => {
13
+ const adapter = async (opts = {}, overloadOpts) => {
14
14
  const controller = new AbortController();
15
15
  const signal = controller.signal;
16
16
  const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
17
+ const isText = opts.isText || false; // 是否返回文本内容
17
18
  const isPostFile = opts.isPostFile || false; // 是否为文件上传
18
19
  const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
19
20
  const timer = setTimeout(() => {
20
21
  controller.abort();
21
22
  }, timeout);
22
- let method = overloadOpts?.method || opts.method || 'POST';
23
+ let method = overloadOpts?.method || opts?.method || 'POST';
24
+ let headers = { ...opts?.headers, ...overloadOpts?.headers };
23
25
  let origin = '';
24
26
  let url;
25
27
  if (opts?.url?.startsWith('http')) {
@@ -41,17 +43,18 @@ const adapter = async (opts, overloadOpts) => {
41
43
  body = opts.body; // 如果是文件上传,直接使用 FormData
42
44
  }
43
45
  else {
46
+ headers = {
47
+ 'Content-Type': 'application/json',
48
+ ...headers,
49
+ };
44
50
  body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
45
51
  }
46
52
  return fetch(url, {
47
53
  method: method.toUpperCase(),
48
- headers: {
49
- 'Content-Type': 'application/json',
50
- ...opts.headers,
51
- },
52
54
  signal,
53
- ...overloadOpts,
54
55
  body: body,
56
+ ...overloadOpts,
57
+ headers: headers,
55
58
  })
56
59
  .then(async (response) => {
57
60
  // 获取 Content-Type 头部信息
@@ -61,7 +64,7 @@ const adapter = async (opts, overloadOpts) => {
61
64
  }
62
65
  const isJson = contentType && contentType.includes('application/json');
63
66
  // 判断返回的数据类型
64
- if (isJson) {
67
+ if (isJson && !isText) {
65
68
  return await response.json(); // 解析为 JSON
66
69
  }
67
70
  else if (isTextForContentType(contentType)) {
package/dist/query.d.ts CHANGED
@@ -9,6 +9,7 @@ type AdapterOpts = {
9
9
  timeout?: number;
10
10
  method?: Method;
11
11
  isBlob?: boolean;
12
+ isText?: boolean;
12
13
  isPostFile?: boolean;
13
14
  };
14
15
  /**
@@ -17,7 +18,7 @@ type AdapterOpts = {
17
18
  * @param overloadOpts 覆盖fetch的默认配置
18
19
  * @returns
19
20
  */
20
- declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
21
+ declare const adapter: (opts?: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
21
22
 
22
23
  type QueryWsStore = {
23
24
  connected: boolean;
package/dist/query.js CHANGED
@@ -10,16 +10,18 @@ const isTextForContentType = (contentType) => {
10
10
  * @param overloadOpts 覆盖fetch的默认配置
11
11
  * @returns
12
12
  */
13
- const adapter = async (opts, overloadOpts) => {
13
+ const adapter = async (opts = {}, overloadOpts) => {
14
14
  const controller = new AbortController();
15
15
  const signal = controller.signal;
16
16
  const isBlob = opts.isBlob || false; // 是否返回 Blob 对象
17
+ const isText = opts.isText || false; // 是否返回文本内容
17
18
  const isPostFile = opts.isPostFile || false; // 是否为文件上传
18
19
  const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
19
20
  const timer = setTimeout(() => {
20
21
  controller.abort();
21
22
  }, timeout);
22
- let method = overloadOpts?.method || opts.method || 'POST';
23
+ let method = overloadOpts?.method || opts?.method || 'POST';
24
+ let headers = { ...opts?.headers, ...overloadOpts?.headers };
23
25
  let origin = '';
24
26
  let url;
25
27
  if (opts?.url?.startsWith('http')) {
@@ -41,17 +43,18 @@ const adapter = async (opts, overloadOpts) => {
41
43
  body = opts.body; // 如果是文件上传,直接使用 FormData
42
44
  }
43
45
  else {
46
+ headers = {
47
+ 'Content-Type': 'application/json',
48
+ ...headers,
49
+ };
44
50
  body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
45
51
  }
46
52
  return fetch(url, {
47
53
  method: method.toUpperCase(),
48
- headers: {
49
- 'Content-Type': 'application/json',
50
- ...opts.headers,
51
- },
52
54
  signal,
53
- ...overloadOpts,
54
55
  body: body,
56
+ ...overloadOpts,
57
+ headers: headers,
55
58
  })
56
59
  .then(async (response) => {
57
60
  // 获取 Content-Type 头部信息
@@ -61,7 +64,7 @@ const adapter = async (opts, overloadOpts) => {
61
64
  }
62
65
  const isJson = contentType && contentType.includes('application/json');
63
66
  // 判断返回的数据类型
64
- if (isJson) {
67
+ if (isJson && !isText) {
65
68
  return await response.json(); // 解析为 JSON
66
69
  }
67
70
  else if (isTextForContentType(contentType)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",