chore: bump version to 0.0.49 and update QueryOptions to include baseURL and beforeRequest
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@kevisual/query",
|
"name": "@kevisual/query",
|
||||||
"version": "0.0.48",
|
"version": "0.0.49",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npm run clean && bun run bun.config.ts",
|
"build": "npm run clean && bun run bun.config.ts",
|
||||||
|
|||||||
17
src/query.ts
17
src/query.ts
@@ -20,13 +20,14 @@ export type QueryOpts = {
|
|||||||
|
|
||||||
export type QueryOptions = {
|
export type QueryOptions = {
|
||||||
url?: string;
|
url?: string;
|
||||||
|
baseURL?: string;
|
||||||
|
beforeRequest?: Fn;
|
||||||
adapter?: typeof adapter;
|
adapter?: typeof adapter;
|
||||||
headers?: Record<string, string>;
|
headers?: Record<string, string>;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
isClient?: boolean;
|
isClient?: boolean;
|
||||||
tokenName?: string;
|
tokenName?: string;
|
||||||
storage?: Storage;
|
storage?: Storage;
|
||||||
beforeRequest?: Fn;
|
|
||||||
}
|
}
|
||||||
export type Data = {
|
export type Data = {
|
||||||
path?: string;
|
path?: string;
|
||||||
@@ -67,6 +68,7 @@ export const wrapperError = ({ code, message }: { code?: number; message?: strin
|
|||||||
*/
|
*/
|
||||||
export class Query {
|
export class Query {
|
||||||
adapter: typeof adapter;
|
adapter: typeof adapter;
|
||||||
|
baseURL: string;
|
||||||
url: string;
|
url: string;
|
||||||
/**
|
/**
|
||||||
* 请求前处理函数
|
* 请求前处理函数
|
||||||
@@ -93,6 +95,11 @@ export class Query {
|
|||||||
this.storage = opts?.storage || globalThis?.localStorage;
|
this.storage = opts?.storage || globalThis?.localStorage;
|
||||||
const defaultURL = opts?.isClient ? '/client/router' : '/api/router';
|
const defaultURL = opts?.isClient ? '/client/router' : '/api/router';
|
||||||
this.url = opts?.url || defaultURL;
|
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 || {
|
this.headers = opts?.headers || {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
};
|
};
|
||||||
@@ -155,6 +162,14 @@ export class Query {
|
|||||||
timeout: _timeout,
|
timeout: _timeout,
|
||||||
...rest,
|
...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 {
|
try {
|
||||||
if (_beforeRequest) {
|
if (_beforeRequest) {
|
||||||
const res = await _beforeRequest(req);
|
const res = await _beforeRequest(req);
|
||||||
|
|||||||
Reference in New Issue
Block a user