chore: bump version to 0.0.35, update storage reference to globalThis.localStorage, and add beforeRequest option to QueryOptions

This commit is contained in:
2026-01-03 19:12:56 +08:00
parent 4133b7a360
commit d4db81c58c
3 changed files with 17 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/query",
"version": "0.0.34",
"version": "0.0.35",
"main": "dist/query-browser.js",
"private": false,
"type": "module",

View File

@@ -25,7 +25,7 @@ export class QueryClient extends Query {
constructor(opts?: QueryOptions & { tokenName?: string; storage?: Storage; io?: boolean }) {
super(opts);
this.tokenName = opts?.tokenName || 'token';
this.storage = opts?.storage || localStorage;
this.storage = opts?.storage || globalThis.localStorage;
this.beforeRequest = async (opts) => {
const token = this.token || this.getToken();
if (token) {

View File

@@ -24,6 +24,7 @@ export type QueryOptions = {
headers?: Record<string, string>;
timeout?: number;
isClient?: boolean;
beforeRequest?: Fn;
}
export type Data = {
path?: string;
@@ -113,6 +114,20 @@ export class Query {
'Content-Type': 'application/json',
};
this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
if (opts.beforeRequest) {
this.beforeRequest = opts.beforeRequest;
} else {
this.beforeRequest = async (opts) => {
const token = globalThis?.localStorage?.getItem('token');
if (token) {
opts.headers = {
...opts.headers,
Authorization: `Bearer ${token}`,
};
}
return opts;
};
}
}
setQueryWs(qws: QueryWs) {
this.qws = qws;