chore: bump @kevisual/router to version 0.0.75, update QueryClient constructor, and enhance token handling in Query class
This commit is contained in:
@@ -19,23 +19,8 @@ type QueryOpts = {
|
||||
* 前端调用后端QueryRouter, 封装 beforeRequest 和 wss
|
||||
*/
|
||||
export class QueryClient extends Query {
|
||||
tokenName: string;
|
||||
storage: Storage;
|
||||
token: string;
|
||||
constructor(opts?: QueryOptions & { tokenName?: string; storage?: Storage; io?: boolean }) {
|
||||
constructor(opts?: QueryOptions & { io?: boolean }) {
|
||||
super(opts);
|
||||
this.tokenName = opts?.tokenName || 'token';
|
||||
this.storage = opts?.storage || globalThis.localStorage;
|
||||
this.beforeRequest = async (opts) => {
|
||||
const token = this.token || this.getToken();
|
||||
if (token) {
|
||||
opts.headers = {
|
||||
...opts.headers,
|
||||
Authorization: `Bearer ${token}`,
|
||||
};
|
||||
}
|
||||
return opts;
|
||||
};
|
||||
if (opts?.io) {
|
||||
this.createWs();
|
||||
}
|
||||
@@ -43,15 +28,6 @@ export class QueryClient extends Query {
|
||||
createWs(opts?: QueryWsOpts) {
|
||||
this.qws = new QueryWs({ url: this.url, ...opts });
|
||||
}
|
||||
getToken() {
|
||||
return this.storage.getItem(this.tokenName);
|
||||
}
|
||||
saveToken(token: string) {
|
||||
this.storage.setItem(this.tokenName, token);
|
||||
}
|
||||
removeToken() {
|
||||
this.storage.removeItem(this.tokenName);
|
||||
}
|
||||
}
|
||||
// 移除默认生成的实例
|
||||
// export const client = new QueryClient();
|
||||
|
||||
Reference in New Issue
Block a user