feat: 更新打包方式,dts

This commit is contained in:
2024-11-02 22:12:36 +08:00
parent 157fd4878a
commit 61d949cc7c
7 changed files with 202 additions and 230 deletions

54
src/query-browser.ts Normal file
View 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 };