@kevisual/query 0.0.34 → 0.0.35

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.
@@ -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;
@@ -367,6 +367,21 @@ class Query {
367
367
  'Content-Type': 'application/json',
368
368
  };
369
369
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
370
+ if (opts.beforeRequest) {
371
+ this.beforeRequest = opts.beforeRequest;
372
+ }
373
+ else {
374
+ this.beforeRequest = async (opts) => {
375
+ const token = globalThis?.localStorage?.getItem('token');
376
+ if (token) {
377
+ opts.headers = {
378
+ ...opts.headers,
379
+ Authorization: `Bearer ${token}`,
380
+ };
381
+ }
382
+ return opts;
383
+ };
384
+ }
370
385
  }
371
386
  setQueryWs(qws) {
372
387
  this.qws = qws;
@@ -549,7 +564,7 @@ class QueryClient extends Query {
549
564
  constructor(opts) {
550
565
  super(opts);
551
566
  this.tokenName = opts?.tokenName || 'token';
552
- this.storage = opts?.storage || localStorage;
567
+ this.storage = opts?.storage || globalThis.localStorage;
553
568
  this.beforeRequest = async (opts) => {
554
569
  const token = this.token || this.getToken();
555
570
  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
@@ -170,6 +170,21 @@ class Query {
170
170
  'Content-Type': 'application/json',
171
171
  };
172
172
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
173
+ if (opts.beforeRequest) {
174
+ this.beforeRequest = opts.beforeRequest;
175
+ }
176
+ else {
177
+ this.beforeRequest = async (opts) => {
178
+ const token = globalThis?.localStorage?.getItem('token');
179
+ if (token) {
180
+ opts.headers = {
181
+ ...opts.headers,
182
+ Authorization: `Bearer ${token}`,
183
+ };
184
+ }
185
+ return opts;
186
+ };
187
+ }
173
188
  }
174
189
  setQueryWs(qws) {
175
190
  this.qws = qws;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.34",
3
+ "version": "0.0.35",
4
4
  "main": "dist/query-browser.js",
5
5
  "private": false,
6
6
  "type": "module",
@@ -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;