chore: bump version to 0.0.35, update storage reference to globalThis.localStorage, and add beforeRequest option to QueryOptions
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@kevisual/query",
|
||||
"version": "0.0.34",
|
||||
"version": "0.0.35",
|
||||
"main": "dist/query-browser.js",
|
||||
"private": false,
|
||||
"type": "module",
|
||||
|
||||
@@ -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) {
|
||||
|
||||
15
src/query.ts
15
src/query.ts
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user