feat: 更新打包方式,dts
This commit is contained in:
54
src/query-browser.ts
Normal file
54
src/query-browser.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { adapter } from './adapter.ts';
|
||||
import { QueryWs, QueryWsOpts } from './ws.ts';
|
||||
export { QueryOpts, QueryWs };
|
||||
import { Query } from './query.ts';
|
||||
|
||||
type QueryOpts = {
|
||||
url?: string;
|
||||
adapter?: typeof adapter;
|
||||
headers?: Record<string, string>;
|
||||
timeout?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* 前端调用后端QueryRouter
|
||||
*/
|
||||
export class QueryClient<U = any, V = any> extends Query<U, V> {
|
||||
tokenName: string;
|
||||
storage: Storage;
|
||||
token: string;
|
||||
qws: QueryWs;
|
||||
constructor(opts?: QueryOpts & { tokenName?: string; storage?: Storage; io?: boolean }) {
|
||||
super(opts);
|
||||
this.tokenName = opts?.tokenName || 'token';
|
||||
this.storage = opts?.storage || 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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
|
||||
export { adapter };
|
||||
Reference in New Issue
Block a user