@kevisual/query 0.0.38 → 0.0.40

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;
@@ -35,15 +35,19 @@ const adapter = async (opts = {}, overloadOpts) => {
35
35
  }
36
36
  else {
37
37
  origin = window?.location?.origin || 'http://localhost:51515';
38
- url = new URL(opts.url, origin);
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?.params, ...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 提取到查询参数中, 更容易排查问题
@@ -65,11 +69,13 @@ const adapter = async (opts = {}, overloadOpts) => {
65
69
  body = opts.body; // 如果是文件上传,直接使用 FormData
66
70
  }
67
71
  else {
68
- headers = {
69
- 'Content-Type': 'application/json',
70
- ...headers,
71
- };
72
- body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
72
+ if (opts.body && typeof opts.body === 'object' && !(opts.body instanceof FormData)) {
73
+ headers = {
74
+ 'Content-Type': 'application/json',
75
+ ...headers,
76
+ };
77
+ body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
78
+ }
73
79
  }
74
80
  return fetch(url, {
75
81
  method: method.toUpperCase(),
@@ -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;
@@ -180,6 +180,9 @@ declare class Query {
180
180
  */
181
181
  stop?: boolean;
182
182
  qws: QueryWs;
183
+ /**
184
+ * 默认是 /client/router或者 默认是 /api/router
185
+ */
183
186
  isClient: boolean;
184
187
  constructor(opts?: QueryOptions);
185
188
  setQueryWs(qws: QueryWs): void;
@@ -34,15 +34,19 @@ const adapter = async (opts = {}, overloadOpts) => {
34
34
  }
35
35
  else {
36
36
  origin = window?.location?.origin || 'http://localhost:51515';
37
- url = new URL(opts.url, origin);
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?.params, ...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 提取到查询参数中, 更容易排查问题
@@ -64,11 +68,13 @@ const adapter = async (opts = {}, overloadOpts) => {
64
68
  body = opts.body; // 如果是文件上传,直接使用 FormData
65
69
  }
66
70
  else {
67
- headers = {
68
- 'Content-Type': 'application/json',
69
- ...headers,
70
- };
71
- body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
71
+ if (opts.body && typeof opts.body === 'object' && !(opts.body instanceof FormData)) {
72
+ headers = {
73
+ 'Content-Type': 'application/json',
74
+ ...headers,
75
+ };
76
+ body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
77
+ }
72
78
  }
73
79
  return fetch(url, {
74
80
  method: method.toUpperCase(),
@@ -373,6 +379,9 @@ class Query {
373
379
  stop;
374
380
  // 默认不使用ws
375
381
  qws;
382
+ /**
383
+ * 默认是 /client/router或者 默认是 /api/router
384
+ */
376
385
  isClient = false;
377
386
  constructor(opts) {
378
387
  this.adapter = opts?.adapter || adapter;
@@ -382,7 +391,7 @@ class Query {
382
391
  'Content-Type': 'application/json',
383
392
  };
384
393
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
385
- if (opts.beforeRequest) {
394
+ if (opts?.beforeRequest) {
386
395
  this.beforeRequest = opts.beforeRequest;
387
396
  }
388
397
  else {
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;
@@ -191,6 +191,9 @@ declare class Query {
191
191
  */
192
192
  stop?: boolean;
193
193
  qws: QueryWs;
194
+ /**
195
+ * 默认是 /client/router或者 默认是 /api/router
196
+ */
194
197
  isClient: boolean;
195
198
  constructor(opts?: QueryOptions);
196
199
  setQueryWs(qws: QueryWs): void;
package/dist/query.js CHANGED
@@ -34,15 +34,19 @@ const adapter = async (opts = {}, overloadOpts) => {
34
34
  }
35
35
  else {
36
36
  origin = window?.location?.origin || 'http://localhost:51515';
37
- url = new URL(opts.url, origin);
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?.params, ...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 提取到查询参数中, 更容易排查问题
@@ -64,11 +68,13 @@ const adapter = async (opts = {}, overloadOpts) => {
64
68
  body = opts.body; // 如果是文件上传,直接使用 FormData
65
69
  }
66
70
  else {
67
- headers = {
68
- 'Content-Type': 'application/json',
69
- ...headers,
70
- };
71
- body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
71
+ if (opts.body && typeof opts.body === 'object' && !(opts.body instanceof FormData)) {
72
+ headers = {
73
+ 'Content-Type': 'application/json',
74
+ ...headers,
75
+ };
76
+ body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
77
+ }
72
78
  }
73
79
  return fetch(url, {
74
80
  method: method.toUpperCase(),
@@ -176,6 +182,9 @@ class Query {
176
182
  stop;
177
183
  // 默认不使用ws
178
184
  qws;
185
+ /**
186
+ * 默认是 /client/router或者 默认是 /api/router
187
+ */
179
188
  isClient = false;
180
189
  constructor(opts) {
181
190
  this.adapter = opts?.adapter || adapter;
@@ -185,7 +194,7 @@ class Query {
185
194
  'Content-Type': 'application/json',
186
195
  };
187
196
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
188
- if (opts.beforeRequest) {
197
+ if (opts?.beforeRequest) {
189
198
  this.beforeRequest = opts.beforeRequest;
190
199
  }
191
200
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.38",
3
+ "version": "0.0.40",
4
4
  "main": "dist/query-browser.js",
5
5
  "private": false,
6
6
  "type": "module",
@@ -23,10 +23,10 @@
23
23
  "devDependencies": {
24
24
  "@rollup/plugin-node-resolve": "^16.0.3",
25
25
  "@rollup/plugin-typescript": "^12.3.0",
26
- "rollup": "^4.55.3",
26
+ "rollup": "^4.57.1",
27
27
  "rollup-plugin-dts": "^6.3.0",
28
28
  "typescript": "^5.9.3",
29
- "zustand": "^5.0.10"
29
+ "zustand": "^5.0.11"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"
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>;
@@ -61,14 +61,18 @@ export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit
61
61
  url = new URL(opts.url);
62
62
  } else {
63
63
  origin = window?.location?.origin || 'http://localhost:51515';
64
- url = new URL(opts.url, origin);
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?.params, ...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 提取到查询参数中, 更容易排查问题
@@ -88,11 +92,13 @@ export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit
88
92
  } else if (isPostFile) {
89
93
  body = opts.body as FormData; // 如果是文件上传,直接使用 FormData
90
94
  } else {
91
- headers = {
92
- 'Content-Type': 'application/json',
93
- ...headers,
94
- };
95
- body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
95
+ if (opts.body && typeof opts.body === 'object' && !(opts.body instanceof FormData)) {
96
+ headers = {
97
+ 'Content-Type': 'application/json',
98
+ ...headers,
99
+ };
100
+ body = JSON.stringify(opts.body); // 否则将对象转换为 JSON 字符串
101
+ }
96
102
  }
97
103
  return fetch(url, {
98
104
  method: method.toUpperCase(),
package/src/query.ts CHANGED
@@ -105,6 +105,9 @@ export class Query {
105
105
  stop?: boolean;
106
106
  // 默认不使用ws
107
107
  qws: QueryWs;
108
+ /**
109
+ * 默认是 /client/router或者 默认是 /api/router
110
+ */
108
111
  isClient = false;
109
112
  constructor(opts?: QueryOptions) {
110
113
  this.adapter = opts?.adapter || adapter;
@@ -114,7 +117,7 @@ export class Query {
114
117
  'Content-Type': 'application/json',
115
118
  };
116
119
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
117
- if (opts.beforeRequest) {
120
+ if (opts?.beforeRequest) {
118
121
  this.beforeRequest = opts.beforeRequest;
119
122
  } else {
120
123
  this.beforeRequest = async (opts) => {