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",
|
"name": "@kevisual/query",
|
||||||
"version": "0.0.34",
|
"version": "0.0.35",
|
||||||
"main": "dist/query-browser.js",
|
"main": "dist/query-browser.js",
|
||||||
"private": false,
|
"private": false,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export class QueryClient extends Query {
|
|||||||
constructor(opts?: QueryOptions & { tokenName?: string; storage?: Storage; io?: boolean }) {
|
constructor(opts?: QueryOptions & { tokenName?: string; storage?: Storage; io?: boolean }) {
|
||||||
super(opts);
|
super(opts);
|
||||||
this.tokenName = opts?.tokenName || 'token';
|
this.tokenName = opts?.tokenName || 'token';
|
||||||
this.storage = opts?.storage || localStorage;
|
this.storage = opts?.storage || globalThis.localStorage;
|
||||||
this.beforeRequest = async (opts) => {
|
this.beforeRequest = async (opts) => {
|
||||||
const token = this.token || this.getToken();
|
const token = this.token || this.getToken();
|
||||||
if (token) {
|
if (token) {
|
||||||
|
|||||||
15
src/query.ts
15
src/query.ts
@@ -24,6 +24,7 @@ export type QueryOptions = {
|
|||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
isClient?: boolean;
|
isClient?: boolean;
|
||||||
|
beforeRequest?: Fn;
|
||||||
}
|
}
|
||||||
export type Data = {
|
export type Data = {
|
||||||
path?: string;
|
path?: string;
|
||||||
@@ -113,6 +114,20 @@ export class Query {
|
|||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
};
|
};
|
||||||
this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
|
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) {
|
setQueryWs(qws: QueryWs) {
|
||||||
this.qws = qws;
|
this.qws = qws;
|
||||||
|
|||||||
Reference in New Issue
Block a user