@kevisual/query 0.0.48 → 0.0.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -107,13 +107,14 @@ type QueryOpts = {
107
107
  } & AdapterOpts;
108
108
  type QueryOptions = {
109
109
  url?: string;
110
+ baseURL?: string;
111
+ beforeRequest?: Fn;
110
112
  adapter?: typeof adapter;
111
113
  headers?: Record<string, string>;
112
114
  timeout?: number;
113
115
  isClient?: boolean;
114
116
  tokenName?: string;
115
117
  storage?: Storage;
116
- beforeRequest?: Fn;
117
118
  };
118
119
  type Data = {
119
120
  path?: string;
@@ -149,6 +150,7 @@ type DataOpts = Partial<QueryOpts> & {
149
150
  */
150
151
  declare class Query {
151
152
  adapter: typeof adapter;
153
+ baseURL: string;
152
154
  url: string;
153
155
  /**
154
156
  * 请求前处理函数
package/dist/query-api.js CHANGED
@@ -120,6 +120,7 @@ var wrapperError = ({ code, message }) => {
120
120
 
121
121
  class Query {
122
122
  adapter;
123
+ baseURL;
123
124
  url;
124
125
  beforeRequest;
125
126
  afterResponse;
@@ -136,6 +137,11 @@ class Query {
136
137
  this.storage = opts?.storage || globalThis?.localStorage;
137
138
  const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
138
139
  this.url = opts?.url || defaultURL;
140
+ if (this.url.startsWith("http")) {
141
+ const urlObj = new URL(this.url);
142
+ this.baseURL = urlObj.origin;
143
+ }
144
+ this.baseURL = opts?.baseURL || this.baseURL;
139
145
  this.headers = opts?.headers || {
140
146
  "Content-Type": "application/json"
141
147
  };
@@ -179,6 +185,13 @@ class Query {
179
185
  timeout: _timeout,
180
186
  ...rest
181
187
  };
188
+ const isStartsWithHttp = req.url.startsWith("http");
189
+ if (!isStartsWithHttp) {
190
+ if (this.baseURL) {
191
+ const baseURL = new URL(this.baseURL || globalThis?.location?.origin).origin;
192
+ req.url = baseURL + req.url;
193
+ }
194
+ }
182
195
  try {
183
196
  if (_beforeRequest) {
184
197
  const res = await _beforeRequest(req);
@@ -106,13 +106,14 @@ type QueryOpts$1 = {
106
106
  } & AdapterOpts;
107
107
  type QueryOptions = {
108
108
  url?: string;
109
+ baseURL?: string;
110
+ beforeRequest?: Fn;
109
111
  adapter?: typeof adapter;
110
112
  headers?: Record<string, string>;
111
113
  timeout?: number;
112
114
  isClient?: boolean;
113
115
  tokenName?: string;
114
116
  storage?: Storage;
115
- beforeRequest?: Fn;
116
117
  };
117
118
  type Data = {
118
119
  path?: string;
@@ -155,6 +156,7 @@ declare const wrapperError: ({ code, message }: {
155
156
  */
156
157
  declare class Query {
157
158
  adapter: typeof adapter;
159
+ baseURL: string;
158
160
  url: string;
159
161
  /**
160
162
  * 请求前处理函数
@@ -317,6 +317,7 @@ var wrapperError = ({ code, message }) => {
317
317
 
318
318
  class Query {
319
319
  adapter;
320
+ baseURL;
320
321
  url;
321
322
  beforeRequest;
322
323
  afterResponse;
@@ -333,6 +334,11 @@ class Query {
333
334
  this.storage = opts?.storage || globalThis?.localStorage;
334
335
  const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
335
336
  this.url = opts?.url || defaultURL;
337
+ if (this.url.startsWith("http")) {
338
+ const urlObj = new URL(this.url);
339
+ this.baseURL = urlObj.origin;
340
+ }
341
+ this.baseURL = opts?.baseURL || this.baseURL;
336
342
  this.headers = opts?.headers || {
337
343
  "Content-Type": "application/json"
338
344
  };
@@ -376,6 +382,13 @@ class Query {
376
382
  timeout: _timeout,
377
383
  ...rest
378
384
  };
385
+ const isStartsWithHttp = req.url.startsWith("http");
386
+ if (!isStartsWithHttp) {
387
+ if (this.baseURL) {
388
+ const baseURL = new URL(this.baseURL || globalThis?.location?.origin).origin;
389
+ req.url = baseURL + req.url;
390
+ }
391
+ }
379
392
  try {
380
393
  if (_beforeRequest) {
381
394
  const res = await _beforeRequest(req);
package/dist/query.d.ts CHANGED
@@ -106,13 +106,14 @@ type QueryOpts = {
106
106
  } & AdapterOpts;
107
107
  type QueryOptions = {
108
108
  url?: string;
109
+ baseURL?: string;
110
+ beforeRequest?: Fn;
109
111
  adapter?: typeof adapter;
110
112
  headers?: Record<string, string>;
111
113
  timeout?: number;
112
114
  isClient?: boolean;
113
115
  tokenName?: string;
114
116
  storage?: Storage;
115
- beforeRequest?: Fn;
116
117
  };
117
118
  type Data = {
118
119
  path?: string;
@@ -155,6 +156,7 @@ declare const wrapperError: ({ code, message }: {
155
156
  */
156
157
  declare class Query {
157
158
  adapter: typeof adapter;
159
+ baseURL: string;
158
160
  url: string;
159
161
  /**
160
162
  * 请求前处理函数
package/dist/query.js CHANGED
@@ -120,6 +120,7 @@ var wrapperError = ({ code, message }) => {
120
120
 
121
121
  class Query {
122
122
  adapter;
123
+ baseURL;
123
124
  url;
124
125
  beforeRequest;
125
126
  afterResponse;
@@ -136,6 +137,11 @@ class Query {
136
137
  this.storage = opts?.storage || globalThis?.localStorage;
137
138
  const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
138
139
  this.url = opts?.url || defaultURL;
140
+ if (this.url.startsWith("http")) {
141
+ const urlObj = new URL(this.url);
142
+ this.baseURL = urlObj.origin;
143
+ }
144
+ this.baseURL = opts?.baseURL || this.baseURL;
139
145
  this.headers = opts?.headers || {
140
146
  "Content-Type": "application/json"
141
147
  };
@@ -179,6 +185,13 @@ class Query {
179
185
  timeout: _timeout,
180
186
  ...rest
181
187
  };
188
+ const isStartsWithHttp = req.url.startsWith("http");
189
+ if (!isStartsWithHttp) {
190
+ if (this.baseURL) {
191
+ const baseURL = new URL(this.baseURL || globalThis?.location?.origin).origin;
192
+ req.url = baseURL + req.url;
193
+ }
194
+ }
182
195
  try {
183
196
  if (_beforeRequest) {
184
197
  const res = await _beforeRequest(req);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.48",
3
+ "version": "0.0.49",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "npm run clean && bun run bun.config.ts",
package/src/query.ts CHANGED
@@ -20,13 +20,14 @@ export type QueryOpts = {
20
20
 
21
21
  export type QueryOptions = {
22
22
  url?: string;
23
+ baseURL?: string;
24
+ beforeRequest?: Fn;
23
25
  adapter?: typeof adapter;
24
26
  headers?: Record<string, string>;
25
27
  timeout?: number;
26
28
  isClient?: boolean;
27
29
  tokenName?: string;
28
30
  storage?: Storage;
29
- beforeRequest?: Fn;
30
31
  }
31
32
  export type Data = {
32
33
  path?: string;
@@ -67,6 +68,7 @@ export const wrapperError = ({ code, message }: { code?: number; message?: strin
67
68
  */
68
69
  export class Query {
69
70
  adapter: typeof adapter;
71
+ baseURL: string;
70
72
  url: string;
71
73
  /**
72
74
  * 请求前处理函数
@@ -93,6 +95,11 @@ export class Query {
93
95
  this.storage = opts?.storage || globalThis?.localStorage;
94
96
  const defaultURL = opts?.isClient ? '/client/router' : '/api/router';
95
97
  this.url = opts?.url || defaultURL;
98
+ if (this.url.startsWith('http')) {
99
+ const urlObj = new URL(this.url);
100
+ this.baseURL = urlObj.origin;
101
+ }
102
+ this.baseURL = opts?.baseURL || this.baseURL; // 如果opts中有baseURL优先
96
103
  this.headers = opts?.headers || {
97
104
  'Content-Type': 'application/json',
98
105
  };
@@ -155,6 +162,14 @@ export class Query {
155
162
  timeout: _timeout,
156
163
  ...rest,
157
164
  };
165
+ const isStartsWithHttp = req.url.startsWith('http');
166
+ // 如果是完整的url,直接使用, 如果不是完整的url,且baseURL存在,则拼接baseURL
167
+ if (!isStartsWithHttp) {
168
+ if (this.baseURL) {
169
+ const baseURL = new URL(this.baseURL || globalThis?.location?.origin).origin;
170
+ req.url = baseURL + req.url;
171
+ }
172
+ }
158
173
  try {
159
174
  if (_beforeRequest) {
160
175
  const res = await _beforeRequest(req);