chore: bump version to 0.0.49 and update QueryOptions to include baseURL and beforeRequest

This commit is contained in:
2026-02-18 18:46:32 +08:00
parent 05dace0c79
commit e019944c47
2 changed files with 17 additions and 2 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@kevisual/query",
"version": "0.0.48",
"version": "0.0.49",
"type": "module",
"scripts": {
"build": "npm run clean && bun run bun.config.ts",

View File

@@ -20,13 +20,14 @@ export type QueryOpts = {
export type QueryOptions = {
url?: string;
baseURL?: string;
beforeRequest?: Fn;
adapter?: typeof adapter;
headers?: Record<string, string>;
timeout?: number;
isClient?: boolean;
tokenName?: string;
storage?: Storage;
beforeRequest?: Fn;
}
export type Data = {
path?: string;
@@ -67,6 +68,7 @@ export const wrapperError = ({ code, message }: { code?: number; message?: strin
*/
export class Query {
adapter: typeof adapter;
baseURL: string;
url: string;
/**
* 请求前处理函数
@@ -93,6 +95,11 @@ export class Query {
this.storage = opts?.storage || globalThis?.localStorage;
const defaultURL = opts?.isClient ? '/client/router' : '/api/router';
this.url = opts?.url || defaultURL;
if (this.url.startsWith('http')) {
const urlObj = new URL(this.url);
this.baseURL = urlObj.origin;
}
this.baseURL = opts?.baseURL || this.baseURL; // 如果opts中有baseURL优先
this.headers = opts?.headers || {
'Content-Type': 'application/json',
};
@@ -155,6 +162,14 @@ export class Query {
timeout: _timeout,
...rest,
};
const isStartsWithHttp = req.url.startsWith('http');
// 如果是完整的url直接使用, 如果不是完整的url且baseURL存在则拼接baseURL
if (!isStartsWithHttp) {
if (this.baseURL) {
const baseURL = new URL(this.baseURL || globalThis?.location?.origin).origin;
req.url = baseURL + req.url;
}
}
try {
if (_beforeRequest) {
const res = await _beforeRequest(req);