@kevisual/query 0.0.34 → 0.0.36

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.
@@ -34,16 +34,28 @@ const adapter = async (opts = {}, overloadOpts) => {
34
34
  url = new URL(opts.url);
35
35
  }
36
36
  else {
37
- origin = window?.location?.origin || 'http://localhost:51015';
37
+ origin = window?.location?.origin || 'http://localhost:51515';
38
38
  url = new URL(opts.url, origin);
39
39
  }
40
40
  const isGet = method === 'GET';
41
41
  if (isGet) {
42
- url.search = new URLSearchParams(opts.body).toString();
42
+ let searchParams = new URLSearchParams(opts.body);
43
+ url.search = searchParams.toString();
43
44
  }
44
45
  else {
45
46
  const params = opts.params || {};
46
- url.search = new URLSearchParams(params).toString();
47
+ const searchParams = new URLSearchParams(params);
48
+ if (typeof opts.body === 'object' && opts.body !== null) {
49
+ // 浏览器环境下,自动将 body 中的 path 和 key 提取到查询参数中, 更容易排查问题
50
+ let body = opts.body || {};
51
+ if (!params.path && body?.path) {
52
+ params.path = body.path;
53
+ if (body?.key) {
54
+ params.key = body.key;
55
+ }
56
+ }
57
+ }
58
+ url.search = searchParams.toString();
47
59
  }
48
60
  let body = undefined;
49
61
  if (isGet) {
@@ -118,6 +118,7 @@ type QueryOptions = {
118
118
  headers?: Record<string, string>;
119
119
  timeout?: number;
120
120
  isClient?: boolean;
121
+ beforeRequest?: Fn;
121
122
  };
122
123
  type Data = {
123
124
  path?: string;
@@ -33,16 +33,28 @@ const adapter = async (opts = {}, overloadOpts) => {
33
33
  url = new URL(opts.url);
34
34
  }
35
35
  else {
36
- origin = window?.location?.origin || 'http://localhost:51015';
36
+ origin = window?.location?.origin || 'http://localhost:51515';
37
37
  url = new URL(opts.url, origin);
38
38
  }
39
39
  const isGet = method === 'GET';
40
40
  if (isGet) {
41
- url.search = new URLSearchParams(opts.body).toString();
41
+ let searchParams = new URLSearchParams(opts.body);
42
+ url.search = searchParams.toString();
42
43
  }
43
44
  else {
44
45
  const params = opts.params || {};
45
- url.search = new URLSearchParams(params).toString();
46
+ const searchParams = new URLSearchParams(params);
47
+ if (typeof opts.body === 'object' && opts.body !== null) {
48
+ // 浏览器环境下,自动将 body 中的 path 和 key 提取到查询参数中, 更容易排查问题
49
+ let body = opts.body || {};
50
+ if (!params.path && body?.path) {
51
+ params.path = body.path;
52
+ if (body?.key) {
53
+ params.key = body.key;
54
+ }
55
+ }
56
+ }
57
+ url.search = searchParams.toString();
46
58
  }
47
59
  let body = undefined;
48
60
  if (isGet) {
@@ -367,6 +379,21 @@ class Query {
367
379
  'Content-Type': 'application/json',
368
380
  };
369
381
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
382
+ if (opts.beforeRequest) {
383
+ this.beforeRequest = opts.beforeRequest;
384
+ }
385
+ else {
386
+ this.beforeRequest = async (opts) => {
387
+ const token = globalThis?.localStorage?.getItem('token');
388
+ if (token) {
389
+ opts.headers = {
390
+ ...opts.headers,
391
+ Authorization: `Bearer ${token}`,
392
+ };
393
+ }
394
+ return opts;
395
+ };
396
+ }
370
397
  }
371
398
  setQueryWs(qws) {
372
399
  this.qws = qws;
@@ -549,7 +576,7 @@ class QueryClient extends Query {
549
576
  constructor(opts) {
550
577
  super(opts);
551
578
  this.tokenName = opts?.tokenName || 'token';
552
- this.storage = opts?.storage || localStorage;
579
+ this.storage = opts?.storage || globalThis.localStorage;
553
580
  this.beforeRequest = async (opts) => {
554
581
  const token = this.token || this.getToken();
555
582
  if (token) {
package/dist/query.d.ts CHANGED
@@ -118,6 +118,7 @@ type QueryOptions = {
118
118
  headers?: Record<string, string>;
119
119
  timeout?: number;
120
120
  isClient?: boolean;
121
+ beforeRequest?: Fn;
121
122
  };
122
123
  type Data = {
123
124
  path?: string;
package/dist/query.js CHANGED
@@ -33,16 +33,28 @@ const adapter = async (opts = {}, overloadOpts) => {
33
33
  url = new URL(opts.url);
34
34
  }
35
35
  else {
36
- origin = window?.location?.origin || 'http://localhost:51015';
36
+ origin = window?.location?.origin || 'http://localhost:51515';
37
37
  url = new URL(opts.url, origin);
38
38
  }
39
39
  const isGet = method === 'GET';
40
40
  if (isGet) {
41
- url.search = new URLSearchParams(opts.body).toString();
41
+ let searchParams = new URLSearchParams(opts.body);
42
+ url.search = searchParams.toString();
42
43
  }
43
44
  else {
44
45
  const params = opts.params || {};
45
- url.search = new URLSearchParams(params).toString();
46
+ const searchParams = new URLSearchParams(params);
47
+ if (typeof opts.body === 'object' && opts.body !== null) {
48
+ // 浏览器环境下,自动将 body 中的 path 和 key 提取到查询参数中, 更容易排查问题
49
+ let body = opts.body || {};
50
+ if (!params.path && body?.path) {
51
+ params.path = body.path;
52
+ if (body?.key) {
53
+ params.key = body.key;
54
+ }
55
+ }
56
+ }
57
+ url.search = searchParams.toString();
46
58
  }
47
59
  let body = undefined;
48
60
  if (isGet) {
@@ -170,6 +182,21 @@ class Query {
170
182
  'Content-Type': 'application/json',
171
183
  };
172
184
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
185
+ if (opts.beforeRequest) {
186
+ this.beforeRequest = opts.beforeRequest;
187
+ }
188
+ else {
189
+ this.beforeRequest = async (opts) => {
190
+ const token = globalThis?.localStorage?.getItem('token');
191
+ if (token) {
192
+ opts.headers = {
193
+ ...opts.headers,
194
+ Authorization: `Bearer ${token}`,
195
+ };
196
+ }
197
+ return opts;
198
+ };
199
+ }
173
200
  }
174
201
  setQueryWs(qws) {
175
202
  this.qws = qws;
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.34",
3
+ "version": "0.0.36",
4
4
  "main": "dist/query-browser.js",
5
5
  "private": false,
6
6
  "type": "module",
7
7
  "scripts": {
8
8
  "build": "npm run clean && rollup -c",
9
- "build:app": "npm run build && rsync dist/* ../deploy/dist",
10
9
  "dev:lib": "rollup -c -w",
11
10
  "clean": "rm -rf dist"
12
11
  },
@@ -24,10 +23,10 @@
24
23
  "devDependencies": {
25
24
  "@rollup/plugin-node-resolve": "^16.0.3",
26
25
  "@rollup/plugin-typescript": "^12.3.0",
27
- "rollup": "^4.54.0",
26
+ "rollup": "^4.55.3",
28
27
  "rollup-plugin-dts": "^6.3.0",
29
28
  "typescript": "^5.9.3",
30
- "zustand": "^5.0.9"
29
+ "zustand": "^5.0.10"
31
30
  },
32
31
  "publishConfig": {
33
32
  "access": "public"
package/src/adapter.ts CHANGED
@@ -60,15 +60,27 @@ export const adapter = async (opts: AdapterOpts = {}, overloadOpts?: RequestInit
60
60
  if (opts?.url?.startsWith('http')) {
61
61
  url = new URL(opts.url);
62
62
  } else {
63
- origin = window?.location?.origin || 'http://localhost:51015';
63
+ origin = window?.location?.origin || 'http://localhost:51515';
64
64
  url = new URL(opts.url, origin);
65
65
  }
66
66
  const isGet = method === 'GET';
67
67
  if (isGet) {
68
- url.search = new URLSearchParams(opts.body as SimpleObject).toString();
68
+ let searchParams = new URLSearchParams(opts.body as SimpleObject);
69
+ url.search = searchParams.toString();
69
70
  } else {
70
71
  const params = opts.params || {};
71
- url.search = new URLSearchParams(params as SimpleObject).toString();
72
+ const searchParams = new URLSearchParams(params as SimpleObject);
73
+ if (typeof opts.body === 'object' && opts.body !== null) {
74
+ // 浏览器环境下,自动将 body 中的 path 和 key 提取到查询参数中, 更容易排查问题
75
+ let body = opts.body as Record<string, any> || {};
76
+ if (!params.path && body?.path) {
77
+ params.path = body.path;
78
+ if (body?.key) {
79
+ params.key = body.key;
80
+ }
81
+ }
82
+ }
83
+ url.search = searchParams.toString();
72
84
  }
73
85
  let body: string | FormData | undefined = undefined;
74
86
  if (isGet) {
@@ -25,7 +25,7 @@ export class QueryClient extends Query {
25
25
  constructor(opts?: QueryOptions & { tokenName?: string; storage?: Storage; io?: boolean }) {
26
26
  super(opts);
27
27
  this.tokenName = opts?.tokenName || 'token';
28
- this.storage = opts?.storage || localStorage;
28
+ this.storage = opts?.storage || globalThis.localStorage;
29
29
  this.beforeRequest = async (opts) => {
30
30
  const token = this.token || this.getToken();
31
31
  if (token) {
package/src/query.ts CHANGED
@@ -24,6 +24,7 @@ export type QueryOptions = {
24
24
  headers?: Record<string, string>;
25
25
  timeout?: number;
26
26
  isClient?: boolean;
27
+ beforeRequest?: Fn;
27
28
  }
28
29
  export type Data = {
29
30
  path?: string;
@@ -113,6 +114,20 @@ export class Query {
113
114
  'Content-Type': 'application/json',
114
115
  };
115
116
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
117
+ if (opts.beforeRequest) {
118
+ this.beforeRequest = opts.beforeRequest;
119
+ } else {
120
+ this.beforeRequest = async (opts) => {
121
+ const token = globalThis?.localStorage?.getItem('token');
122
+ if (token) {
123
+ opts.headers = {
124
+ ...opts.headers,
125
+ Authorization: `Bearer ${token}`,
126
+ };
127
+ }
128
+ return opts;
129
+ };
130
+ }
116
131
  }
117
132
  setQueryWs(qws: QueryWs) {
118
133
  this.qws = qws;