@kevisual/query 0.0.37 → 0.0.39

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,4 +1,4 @@
1
- declare const methods: readonly ["GET", "POST"];
1
+ declare const methods: readonly ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"];
2
2
  type Method = (typeof methods)[number];
3
3
  type AdapterOpts = {
4
4
  url?: string;
@@ -1,4 +1,4 @@
1
- const methods = ['GET', 'POST'];
1
+ const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'];
2
2
  const isTextForContentType = (contentType) => {
3
3
  if (!contentType)
4
4
  return false;
@@ -38,12 +38,16 @@ const adapter = async (opts = {}, overloadOpts) => {
38
38
  url = new URL(opts.url, origin);
39
39
  }
40
40
  const isGet = method === 'GET';
41
+ const oldSearchParams = url.searchParams;
41
42
  if (isGet) {
42
- let searchParams = new URLSearchParams(opts.body);
43
+ let searchParams = new URLSearchParams({ ...Object.fromEntries(oldSearchParams), ...opts.body });
43
44
  url.search = searchParams.toString();
44
45
  }
45
46
  else {
46
- const params = opts.params || {};
47
+ const params = {
48
+ ...Object.fromEntries(oldSearchParams),
49
+ ...opts.params,
50
+ };
47
51
  const searchParams = new URLSearchParams(params);
48
52
  if (typeof opts.body === 'object' && opts.body !== null) {
49
53
  // 浏览器环境下,自动将 body 中的 path 和 key 提取到查询参数中, 更容易排查问题
@@ -103,11 +107,14 @@ const adapter = async (opts = {}, overloadOpts) => {
103
107
  })
104
108
  .catch((err) => {
105
109
  if (err.name === 'AbortError') {
106
- console.log('Request timed out and was aborted');
110
+ return {
111
+ code: 408,
112
+ message: '请求超时',
113
+ };
107
114
  }
108
- console.error(err);
109
115
  return {
110
116
  code: 500,
117
+ message: err.message || '网络错误',
111
118
  };
112
119
  })
113
120
  .finally(() => {
@@ -1,6 +1,6 @@
1
1
  import { StoreApi } from 'zustand/vanilla';
2
2
 
3
- declare const methods: readonly ["GET", "POST"];
3
+ declare const methods: readonly ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"];
4
4
  type Method = (typeof methods)[number];
5
5
  type AdapterOpts = {
6
6
  url?: string;
@@ -37,12 +37,16 @@ const adapter = async (opts = {}, overloadOpts) => {
37
37
  url = new URL(opts.url, origin);
38
38
  }
39
39
  const isGet = method === 'GET';
40
+ const oldSearchParams = url.searchParams;
40
41
  if (isGet) {
41
- let searchParams = new URLSearchParams(opts.body);
42
+ let searchParams = new URLSearchParams({ ...Object.fromEntries(oldSearchParams), ...opts.body });
42
43
  url.search = searchParams.toString();
43
44
  }
44
45
  else {
45
- const params = opts.params || {};
46
+ const params = {
47
+ ...Object.fromEntries(oldSearchParams),
48
+ ...opts.params,
49
+ };
46
50
  const searchParams = new URLSearchParams(params);
47
51
  if (typeof opts.body === 'object' && opts.body !== null) {
48
52
  // 浏览器环境下,自动将 body 中的 path 和 key 提取到查询参数中, 更容易排查问题
@@ -102,11 +106,14 @@ const adapter = async (opts = {}, overloadOpts) => {
102
106
  })
103
107
  .catch((err) => {
104
108
  if (err.name === 'AbortError') {
105
- console.log('Request timed out and was aborted');
109
+ return {
110
+ code: 408,
111
+ message: '请求超时',
112
+ };
106
113
  }
107
- console.error(err);
108
114
  return {
109
115
  code: 500,
116
+ message: err.message || '网络错误',
110
117
  };
111
118
  })
112
119
  .finally(() => {
package/dist/query.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { StoreApi } from 'zustand/vanilla';
2
2
 
3
- declare const methods: readonly ["GET", "POST"];
3
+ declare const methods: readonly ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"];
4
4
  type Method = (typeof methods)[number];
5
5
  type AdapterOpts = {
6
6
  url?: string;
package/dist/query.js CHANGED
@@ -37,12 +37,16 @@ const adapter = async (opts = {}, overloadOpts) => {
37
37
  url = new URL(opts.url, origin);
38
38
  }
39
39
  const isGet = method === 'GET';
40
+ const oldSearchParams = url.searchParams;
40
41
  if (isGet) {
41
- let searchParams = new URLSearchParams(opts.body);
42
+ let searchParams = new URLSearchParams({ ...Object.fromEntries(oldSearchParams), ...opts.body });
42
43
  url.search = searchParams.toString();
43
44
  }
44
45
  else {
45
- const params = opts.params || {};
46
+ const params = {
47
+ ...Object.fromEntries(oldSearchParams),
48
+ ...opts.params,
49
+ };
46
50
  const searchParams = new URLSearchParams(params);
47
51
  if (typeof opts.body === 'object' && opts.body !== null) {
48
52
  // 浏览器环境下,自动将 body 中的 path 和 key 提取到查询参数中, 更容易排查问题
@@ -102,11 +106,14 @@ const adapter = async (opts = {}, overloadOpts) => {
102
106
  })
103
107
  .catch((err) => {
104
108
  if (err.name === 'AbortError') {
105
- console.log('Request timed out and was aborted');
109
+ return {
110
+ code: 408,
111
+ message: '请求超时',
112
+ };
106
113
  }
107
- console.error(err);
108
114
  return {
109
115
  code: 500,
116
+ message: err.message || '网络错误',
110
117
  };
111
118
  })
112
119
  .finally(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.37",
3
+ "version": "0.0.39",
4
4
  "main": "dist/query-browser.js",
5
5
  "private": false,
6
6
  "type": "module",
package/src/adapter.ts CHANGED
@@ -1,4 +1,4 @@
1
- export const methods = ['GET', 'POST'] as const;
1
+ export const methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD'] as const;
2
2
  export type Method = (typeof methods)[number];
3
3
 
4
4
  type SimpleObject = Record<string, any>;
@@ -64,11 +64,15 @@ export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit
64
64
  url = new URL(opts.url, origin);
65
65
  }
66
66
  const isGet = method === 'GET';
67
+ const oldSearchParams = url.searchParams;
67
68
  if (isGet) {
68
- let searchParams = new URLSearchParams(opts.body as SimpleObject);
69
+ let searchParams = new URLSearchParams({ ...Object.fromEntries(oldSearchParams), ...opts.body } as SimpleObject);
69
70
  url.search = searchParams.toString();
70
71
  } else {
71
- const params = opts.params || {};
72
+ const params = {
73
+ ...Object.fromEntries(oldSearchParams),
74
+ ...opts.params,
75
+ }
72
76
  const searchParams = new URLSearchParams(params as SimpleObject);
73
77
  if (typeof opts.body === 'object' && opts.body !== null) {
74
78
  // 浏览器环境下,自动将 body 中的 path 和 key 提取到查询参数中, 更容易排查问题
@@ -124,11 +128,14 @@ export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit
124
128
  })
125
129
  .catch((err) => {
126
130
  if (err.name === 'AbortError') {
127
- console.log('Request timed out and was aborted');
131
+ return {
132
+ code: 408,
133
+ message: '请求超时',
134
+ }
128
135
  }
129
- console.error(err);
130
136
  return {
131
137
  code: 500,
138
+ message: err.message || '网络错误',
132
139
  };
133
140
  })
134
141
  .finally(() => {