@kevisual/api 0.0.50 → 0.0.52
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.
- package/dist/query-ai.js +48 -32
- package/dist/query-app.js +48 -32
- package/dist/query-config.js +46 -30
- package/dist/query-login.js +46 -43
- package/dist/query-mark.js +46 -30
- package/dist/query-proxy.js +7709 -1083
- package/dist/query-resources.js +21 -17
- package/dist/query-secret.js +46 -30
- package/dist/query-shop.js +48 -32
- package/dist/store-mark.js +45 -29
- package/dist/utils-node.d.ts +36 -0
- package/dist/utils-node.js +1871 -0
- package/dist/utils.d.ts +4 -1
- package/dist/utils.js +1807 -4
- package/package.json +6 -4
- package/query/query-login/query-login.ts +0 -3
- package/query/query-proxy/router-api-proxy.ts +1 -1
- package/query/utils/random.ts +5 -2
package/dist/query-ai.js
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
|
-
// node_modules/.pnpm/@kevisual+query@0.0.
|
|
1
|
+
// node_modules/.pnpm/@kevisual+query@0.0.47/node_modules/@kevisual/query/dist/query-browser.js
|
|
2
2
|
var isTextForContentType = (contentType) => {
|
|
3
3
|
if (!contentType)
|
|
4
4
|
return false;
|
|
5
|
-
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
|
|
5
|
+
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md", "json"];
|
|
6
6
|
return textTypes.some((type) => contentType.includes(type));
|
|
7
7
|
};
|
|
8
8
|
var adapter = async (opts = {}, overloadOpts) => {
|
|
9
9
|
const controller = new AbortController;
|
|
10
10
|
const signal = controller.signal;
|
|
11
11
|
const isPostFile = opts.isPostFile || false;
|
|
12
|
-
let responseType = opts.responseType || "json";
|
|
13
|
-
if (opts.isBlob) {
|
|
14
|
-
responseType = "blob";
|
|
15
|
-
} else if (opts.isText) {
|
|
16
|
-
responseType = "text";
|
|
17
|
-
}
|
|
18
12
|
const timeout = opts.timeout || 60000 * 3;
|
|
19
13
|
const timer = setTimeout(() => {
|
|
20
14
|
controller.abort();
|
|
@@ -26,7 +20,7 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
26
20
|
if (opts?.url?.startsWith("http")) {
|
|
27
21
|
url = new URL(opts.url);
|
|
28
22
|
} else {
|
|
29
|
-
origin =
|
|
23
|
+
origin = globalThis?.location?.origin || "http://localhost:51515";
|
|
30
24
|
url = new URL(opts?.url || "", origin);
|
|
31
25
|
}
|
|
32
26
|
const isGet = method === "GET";
|
|
@@ -73,21 +67,31 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
73
67
|
headers
|
|
74
68
|
}).then(async (response) => {
|
|
75
69
|
const contentType = response.headers.get("Content-Type");
|
|
76
|
-
if (responseType === "blob") {
|
|
77
|
-
return await response.blob();
|
|
78
|
-
}
|
|
79
|
-
const isText = responseType === "text";
|
|
80
70
|
const isJson = contentType && contentType.includes("application/json");
|
|
81
|
-
|
|
82
|
-
|
|
71
|
+
const isSuccess = response.ok;
|
|
72
|
+
if (isJson) {
|
|
73
|
+
const json = await response.json();
|
|
74
|
+
if (json?.code) {
|
|
75
|
+
return json;
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
code: isSuccess ? 200 : response.status,
|
|
79
|
+
status: response.status,
|
|
80
|
+
data: json
|
|
81
|
+
};
|
|
83
82
|
} else if (isTextForContentType(contentType)) {
|
|
84
83
|
return {
|
|
85
|
-
code: response.status,
|
|
84
|
+
code: isSuccess ? 200 : response.status,
|
|
86
85
|
status: response.status,
|
|
87
86
|
data: await response.text()
|
|
88
87
|
};
|
|
89
88
|
} else {
|
|
90
|
-
return
|
|
89
|
+
return {
|
|
90
|
+
code: isSuccess ? 200 : response.status,
|
|
91
|
+
status: response.status,
|
|
92
|
+
data: "非文本非JSON响应, 请手动处理response。",
|
|
93
|
+
response
|
|
94
|
+
};
|
|
91
95
|
}
|
|
92
96
|
}).catch((err) => {
|
|
93
97
|
if (err.name === "AbortError") {
|
|
@@ -107,10 +111,7 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
107
111
|
var wrapperError = ({ code, message }) => {
|
|
108
112
|
const result = {
|
|
109
113
|
code: code || 500,
|
|
110
|
-
|
|
111
|
-
message: message || "api request error",
|
|
112
|
-
showError: (fn) => {},
|
|
113
|
-
noMsg: true
|
|
114
|
+
message: message || "请求错误"
|
|
114
115
|
};
|
|
115
116
|
return result;
|
|
116
117
|
};
|
|
@@ -124,9 +125,13 @@ class Query {
|
|
|
124
125
|
timeout;
|
|
125
126
|
stop;
|
|
126
127
|
qws;
|
|
127
|
-
|
|
128
|
+
tokenName;
|
|
129
|
+
storage;
|
|
130
|
+
token;
|
|
128
131
|
constructor(opts) {
|
|
129
132
|
this.adapter = opts?.adapter || adapter;
|
|
133
|
+
this.tokenName = opts?.tokenName || "token";
|
|
134
|
+
this.storage = opts?.storage || globalThis?.localStorage;
|
|
130
135
|
const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
|
|
131
136
|
this.url = opts?.url || defaultURL;
|
|
132
137
|
this.headers = opts?.headers || {
|
|
@@ -137,7 +142,7 @@ class Query {
|
|
|
137
142
|
this.beforeRequest = opts.beforeRequest;
|
|
138
143
|
} else {
|
|
139
144
|
this.beforeRequest = async (opts2) => {
|
|
140
|
-
const token =
|
|
145
|
+
const token = this.token || this.storage?.getItem?.(this.tokenName);
|
|
141
146
|
if (token) {
|
|
142
147
|
opts2.headers = {
|
|
143
148
|
...opts2.headers,
|
|
@@ -178,7 +183,7 @@ class Query {
|
|
|
178
183
|
if (res === false) {
|
|
179
184
|
return wrapperError({
|
|
180
185
|
code: 500,
|
|
181
|
-
message: "
|
|
186
|
+
message: "请求取消",
|
|
182
187
|
req
|
|
183
188
|
});
|
|
184
189
|
}
|
|
@@ -187,12 +192,13 @@ class Query {
|
|
|
187
192
|
console.error("request beforeFn error", e, req);
|
|
188
193
|
return wrapperError({
|
|
189
194
|
code: 500,
|
|
190
|
-
message: "
|
|
195
|
+
message: "请求在请求前处理时发生错误",
|
|
196
|
+
req
|
|
191
197
|
});
|
|
192
198
|
}
|
|
193
199
|
if (this.stop && !options?.noStop) {
|
|
194
200
|
const that = this;
|
|
195
|
-
await new Promise((resolve) => {
|
|
201
|
+
const res = await new Promise((resolve) => {
|
|
196
202
|
let timer = 0;
|
|
197
203
|
const detect = setInterval(() => {
|
|
198
204
|
if (!that.stop) {
|
|
@@ -200,11 +206,20 @@ class Query {
|
|
|
200
206
|
resolve(true);
|
|
201
207
|
}
|
|
202
208
|
timer++;
|
|
203
|
-
if (timer >
|
|
204
|
-
console.error("
|
|
209
|
+
if (timer > 5) {
|
|
210
|
+
console.error("等待请求失败:", req.url, timer);
|
|
211
|
+
clearInterval(detect);
|
|
212
|
+
resolve(false);
|
|
205
213
|
}
|
|
206
214
|
}, 1000);
|
|
207
215
|
});
|
|
216
|
+
if (!res) {
|
|
217
|
+
return wrapperError({
|
|
218
|
+
code: 500,
|
|
219
|
+
message: "请求取消,可能是因为用户未登录或者token过期",
|
|
220
|
+
req
|
|
221
|
+
});
|
|
222
|
+
}
|
|
208
223
|
}
|
|
209
224
|
return _adapter(req).then(async (res) => {
|
|
210
225
|
try {
|
|
@@ -217,10 +232,11 @@ class Query {
|
|
|
217
232
|
}
|
|
218
233
|
return res;
|
|
219
234
|
} catch (e) {
|
|
220
|
-
console.error("
|
|
235
|
+
console.error("请求在响应后处理时发生错误", e, req);
|
|
221
236
|
return wrapperError({
|
|
222
237
|
code: 500,
|
|
223
|
-
message: "
|
|
238
|
+
message: "请求在响应后处理时发生错误",
|
|
239
|
+
req
|
|
224
240
|
});
|
|
225
241
|
}
|
|
226
242
|
});
|
|
@@ -257,7 +273,7 @@ class Query {
|
|
|
257
273
|
}
|
|
258
274
|
}
|
|
259
275
|
|
|
260
|
-
// node_modules/.pnpm/@kevisual+router@0.0.
|
|
276
|
+
// node_modules/.pnpm/@kevisual+router@0.0.75/node_modules/@kevisual/router/dist/router-define.js
|
|
261
277
|
class Chain {
|
|
262
278
|
object;
|
|
263
279
|
app;
|
|
@@ -391,7 +407,7 @@ var appDefine = QueryUtil.create({
|
|
|
391
407
|
}
|
|
392
408
|
});
|
|
393
409
|
|
|
394
|
-
// node_modules/.pnpm/@kevisual+query@0.0.
|
|
410
|
+
// node_modules/.pnpm/@kevisual+query@0.0.47/node_modules/@kevisual/query/dist/query.js
|
|
395
411
|
class BaseQuery {
|
|
396
412
|
query;
|
|
397
413
|
queryDefine;
|
package/dist/query-app.js
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
|
-
// node_modules/.pnpm/@kevisual+query@0.0.
|
|
1
|
+
// node_modules/.pnpm/@kevisual+query@0.0.47/node_modules/@kevisual/query/dist/query-browser.js
|
|
2
2
|
var isTextForContentType = (contentType) => {
|
|
3
3
|
if (!contentType)
|
|
4
4
|
return false;
|
|
5
|
-
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
|
|
5
|
+
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md", "json"];
|
|
6
6
|
return textTypes.some((type) => contentType.includes(type));
|
|
7
7
|
};
|
|
8
8
|
var adapter = async (opts = {}, overloadOpts) => {
|
|
9
9
|
const controller = new AbortController;
|
|
10
10
|
const signal = controller.signal;
|
|
11
11
|
const isPostFile = opts.isPostFile || false;
|
|
12
|
-
let responseType = opts.responseType || "json";
|
|
13
|
-
if (opts.isBlob) {
|
|
14
|
-
responseType = "blob";
|
|
15
|
-
} else if (opts.isText) {
|
|
16
|
-
responseType = "text";
|
|
17
|
-
}
|
|
18
12
|
const timeout = opts.timeout || 60000 * 3;
|
|
19
13
|
const timer = setTimeout(() => {
|
|
20
14
|
controller.abort();
|
|
@@ -26,7 +20,7 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
26
20
|
if (opts?.url?.startsWith("http")) {
|
|
27
21
|
url = new URL(opts.url);
|
|
28
22
|
} else {
|
|
29
|
-
origin =
|
|
23
|
+
origin = globalThis?.location?.origin || "http://localhost:51515";
|
|
30
24
|
url = new URL(opts?.url || "", origin);
|
|
31
25
|
}
|
|
32
26
|
const isGet = method === "GET";
|
|
@@ -73,21 +67,31 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
73
67
|
headers
|
|
74
68
|
}).then(async (response) => {
|
|
75
69
|
const contentType = response.headers.get("Content-Type");
|
|
76
|
-
if (responseType === "blob") {
|
|
77
|
-
return await response.blob();
|
|
78
|
-
}
|
|
79
|
-
const isText = responseType === "text";
|
|
80
70
|
const isJson = contentType && contentType.includes("application/json");
|
|
81
|
-
|
|
82
|
-
|
|
71
|
+
const isSuccess = response.ok;
|
|
72
|
+
if (isJson) {
|
|
73
|
+
const json = await response.json();
|
|
74
|
+
if (json?.code) {
|
|
75
|
+
return json;
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
code: isSuccess ? 200 : response.status,
|
|
79
|
+
status: response.status,
|
|
80
|
+
data: json
|
|
81
|
+
};
|
|
83
82
|
} else if (isTextForContentType(contentType)) {
|
|
84
83
|
return {
|
|
85
|
-
code: response.status,
|
|
84
|
+
code: isSuccess ? 200 : response.status,
|
|
86
85
|
status: response.status,
|
|
87
86
|
data: await response.text()
|
|
88
87
|
};
|
|
89
88
|
} else {
|
|
90
|
-
return
|
|
89
|
+
return {
|
|
90
|
+
code: isSuccess ? 200 : response.status,
|
|
91
|
+
status: response.status,
|
|
92
|
+
data: "非文本非JSON响应, 请手动处理response。",
|
|
93
|
+
response
|
|
94
|
+
};
|
|
91
95
|
}
|
|
92
96
|
}).catch((err) => {
|
|
93
97
|
if (err.name === "AbortError") {
|
|
@@ -107,10 +111,7 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
107
111
|
var wrapperError = ({ code, message }) => {
|
|
108
112
|
const result = {
|
|
109
113
|
code: code || 500,
|
|
110
|
-
|
|
111
|
-
message: message || "api request error",
|
|
112
|
-
showError: (fn) => {},
|
|
113
|
-
noMsg: true
|
|
114
|
+
message: message || "请求错误"
|
|
114
115
|
};
|
|
115
116
|
return result;
|
|
116
117
|
};
|
|
@@ -124,9 +125,13 @@ class Query {
|
|
|
124
125
|
timeout;
|
|
125
126
|
stop;
|
|
126
127
|
qws;
|
|
127
|
-
|
|
128
|
+
tokenName;
|
|
129
|
+
storage;
|
|
130
|
+
token;
|
|
128
131
|
constructor(opts) {
|
|
129
132
|
this.adapter = opts?.adapter || adapter;
|
|
133
|
+
this.tokenName = opts?.tokenName || "token";
|
|
134
|
+
this.storage = opts?.storage || globalThis?.localStorage;
|
|
130
135
|
const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
|
|
131
136
|
this.url = opts?.url || defaultURL;
|
|
132
137
|
this.headers = opts?.headers || {
|
|
@@ -137,7 +142,7 @@ class Query {
|
|
|
137
142
|
this.beforeRequest = opts.beforeRequest;
|
|
138
143
|
} else {
|
|
139
144
|
this.beforeRequest = async (opts2) => {
|
|
140
|
-
const token =
|
|
145
|
+
const token = this.token || this.storage?.getItem?.(this.tokenName);
|
|
141
146
|
if (token) {
|
|
142
147
|
opts2.headers = {
|
|
143
148
|
...opts2.headers,
|
|
@@ -178,7 +183,7 @@ class Query {
|
|
|
178
183
|
if (res === false) {
|
|
179
184
|
return wrapperError({
|
|
180
185
|
code: 500,
|
|
181
|
-
message: "
|
|
186
|
+
message: "请求取消",
|
|
182
187
|
req
|
|
183
188
|
});
|
|
184
189
|
}
|
|
@@ -187,12 +192,13 @@ class Query {
|
|
|
187
192
|
console.error("request beforeFn error", e, req);
|
|
188
193
|
return wrapperError({
|
|
189
194
|
code: 500,
|
|
190
|
-
message: "
|
|
195
|
+
message: "请求在请求前处理时发生错误",
|
|
196
|
+
req
|
|
191
197
|
});
|
|
192
198
|
}
|
|
193
199
|
if (this.stop && !options?.noStop) {
|
|
194
200
|
const that = this;
|
|
195
|
-
await new Promise((resolve) => {
|
|
201
|
+
const res = await new Promise((resolve) => {
|
|
196
202
|
let timer = 0;
|
|
197
203
|
const detect = setInterval(() => {
|
|
198
204
|
if (!that.stop) {
|
|
@@ -200,11 +206,20 @@ class Query {
|
|
|
200
206
|
resolve(true);
|
|
201
207
|
}
|
|
202
208
|
timer++;
|
|
203
|
-
if (timer >
|
|
204
|
-
console.error("
|
|
209
|
+
if (timer > 5) {
|
|
210
|
+
console.error("等待请求失败:", req.url, timer);
|
|
211
|
+
clearInterval(detect);
|
|
212
|
+
resolve(false);
|
|
205
213
|
}
|
|
206
214
|
}, 1000);
|
|
207
215
|
});
|
|
216
|
+
if (!res) {
|
|
217
|
+
return wrapperError({
|
|
218
|
+
code: 500,
|
|
219
|
+
message: "请求取消,可能是因为用户未登录或者token过期",
|
|
220
|
+
req
|
|
221
|
+
});
|
|
222
|
+
}
|
|
208
223
|
}
|
|
209
224
|
return _adapter(req).then(async (res) => {
|
|
210
225
|
try {
|
|
@@ -217,10 +232,11 @@ class Query {
|
|
|
217
232
|
}
|
|
218
233
|
return res;
|
|
219
234
|
} catch (e) {
|
|
220
|
-
console.error("
|
|
235
|
+
console.error("请求在响应后处理时发生错误", e, req);
|
|
221
236
|
return wrapperError({
|
|
222
237
|
code: 500,
|
|
223
|
-
message: "
|
|
238
|
+
message: "请求在响应后处理时发生错误",
|
|
239
|
+
req
|
|
224
240
|
});
|
|
225
241
|
}
|
|
226
242
|
});
|
|
@@ -257,7 +273,7 @@ class Query {
|
|
|
257
273
|
}
|
|
258
274
|
}
|
|
259
275
|
|
|
260
|
-
// node_modules/.pnpm/@kevisual+router@0.0.
|
|
276
|
+
// node_modules/.pnpm/@kevisual+router@0.0.75/node_modules/@kevisual/router/dist/router-define.js
|
|
261
277
|
class Chain {
|
|
262
278
|
object;
|
|
263
279
|
app;
|
|
@@ -465,7 +481,7 @@ var userAppDefine = QueryUtil.create({
|
|
|
465
481
|
}
|
|
466
482
|
});
|
|
467
483
|
|
|
468
|
-
// node_modules/.pnpm/@kevisual+query@0.0.
|
|
484
|
+
// node_modules/.pnpm/@kevisual+query@0.0.47/node_modules/@kevisual/query/dist/query.js
|
|
469
485
|
class BaseQuery {
|
|
470
486
|
query;
|
|
471
487
|
queryDefine;
|
package/dist/query-config.js
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
|
-
// node_modules/.pnpm/@kevisual+query@0.0.
|
|
1
|
+
// node_modules/.pnpm/@kevisual+query@0.0.47/node_modules/@kevisual/query/dist/query-browser.js
|
|
2
2
|
var isTextForContentType = (contentType) => {
|
|
3
3
|
if (!contentType)
|
|
4
4
|
return false;
|
|
5
|
-
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
|
|
5
|
+
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md", "json"];
|
|
6
6
|
return textTypes.some((type) => contentType.includes(type));
|
|
7
7
|
};
|
|
8
8
|
var adapter = async (opts = {}, overloadOpts) => {
|
|
9
9
|
const controller = new AbortController;
|
|
10
10
|
const signal = controller.signal;
|
|
11
11
|
const isPostFile = opts.isPostFile || false;
|
|
12
|
-
let responseType = opts.responseType || "json";
|
|
13
|
-
if (opts.isBlob) {
|
|
14
|
-
responseType = "blob";
|
|
15
|
-
} else if (opts.isText) {
|
|
16
|
-
responseType = "text";
|
|
17
|
-
}
|
|
18
12
|
const timeout = opts.timeout || 60000 * 3;
|
|
19
13
|
const timer = setTimeout(() => {
|
|
20
14
|
controller.abort();
|
|
@@ -26,7 +20,7 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
26
20
|
if (opts?.url?.startsWith("http")) {
|
|
27
21
|
url = new URL(opts.url);
|
|
28
22
|
} else {
|
|
29
|
-
origin =
|
|
23
|
+
origin = globalThis?.location?.origin || "http://localhost:51515";
|
|
30
24
|
url = new URL(opts?.url || "", origin);
|
|
31
25
|
}
|
|
32
26
|
const isGet = method === "GET";
|
|
@@ -73,21 +67,31 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
73
67
|
headers
|
|
74
68
|
}).then(async (response) => {
|
|
75
69
|
const contentType = response.headers.get("Content-Type");
|
|
76
|
-
if (responseType === "blob") {
|
|
77
|
-
return await response.blob();
|
|
78
|
-
}
|
|
79
|
-
const isText = responseType === "text";
|
|
80
70
|
const isJson = contentType && contentType.includes("application/json");
|
|
81
|
-
|
|
82
|
-
|
|
71
|
+
const isSuccess = response.ok;
|
|
72
|
+
if (isJson) {
|
|
73
|
+
const json = await response.json();
|
|
74
|
+
if (json?.code) {
|
|
75
|
+
return json;
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
code: isSuccess ? 200 : response.status,
|
|
79
|
+
status: response.status,
|
|
80
|
+
data: json
|
|
81
|
+
};
|
|
83
82
|
} else if (isTextForContentType(contentType)) {
|
|
84
83
|
return {
|
|
85
|
-
code: response.status,
|
|
84
|
+
code: isSuccess ? 200 : response.status,
|
|
86
85
|
status: response.status,
|
|
87
86
|
data: await response.text()
|
|
88
87
|
};
|
|
89
88
|
} else {
|
|
90
|
-
return
|
|
89
|
+
return {
|
|
90
|
+
code: isSuccess ? 200 : response.status,
|
|
91
|
+
status: response.status,
|
|
92
|
+
data: "非文本非JSON响应, 请手动处理response。",
|
|
93
|
+
response
|
|
94
|
+
};
|
|
91
95
|
}
|
|
92
96
|
}).catch((err) => {
|
|
93
97
|
if (err.name === "AbortError") {
|
|
@@ -107,10 +111,7 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
107
111
|
var wrapperError = ({ code, message }) => {
|
|
108
112
|
const result = {
|
|
109
113
|
code: code || 500,
|
|
110
|
-
|
|
111
|
-
message: message || "api request error",
|
|
112
|
-
showError: (fn) => {},
|
|
113
|
-
noMsg: true
|
|
114
|
+
message: message || "请求错误"
|
|
114
115
|
};
|
|
115
116
|
return result;
|
|
116
117
|
};
|
|
@@ -124,9 +125,13 @@ class Query {
|
|
|
124
125
|
timeout;
|
|
125
126
|
stop;
|
|
126
127
|
qws;
|
|
127
|
-
|
|
128
|
+
tokenName;
|
|
129
|
+
storage;
|
|
130
|
+
token;
|
|
128
131
|
constructor(opts) {
|
|
129
132
|
this.adapter = opts?.adapter || adapter;
|
|
133
|
+
this.tokenName = opts?.tokenName || "token";
|
|
134
|
+
this.storage = opts?.storage || globalThis?.localStorage;
|
|
130
135
|
const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
|
|
131
136
|
this.url = opts?.url || defaultURL;
|
|
132
137
|
this.headers = opts?.headers || {
|
|
@@ -137,7 +142,7 @@ class Query {
|
|
|
137
142
|
this.beforeRequest = opts.beforeRequest;
|
|
138
143
|
} else {
|
|
139
144
|
this.beforeRequest = async (opts2) => {
|
|
140
|
-
const token =
|
|
145
|
+
const token = this.token || this.storage?.getItem?.(this.tokenName);
|
|
141
146
|
if (token) {
|
|
142
147
|
opts2.headers = {
|
|
143
148
|
...opts2.headers,
|
|
@@ -178,7 +183,7 @@ class Query {
|
|
|
178
183
|
if (res === false) {
|
|
179
184
|
return wrapperError({
|
|
180
185
|
code: 500,
|
|
181
|
-
message: "
|
|
186
|
+
message: "请求取消",
|
|
182
187
|
req
|
|
183
188
|
});
|
|
184
189
|
}
|
|
@@ -187,12 +192,13 @@ class Query {
|
|
|
187
192
|
console.error("request beforeFn error", e, req);
|
|
188
193
|
return wrapperError({
|
|
189
194
|
code: 500,
|
|
190
|
-
message: "
|
|
195
|
+
message: "请求在请求前处理时发生错误",
|
|
196
|
+
req
|
|
191
197
|
});
|
|
192
198
|
}
|
|
193
199
|
if (this.stop && !options?.noStop) {
|
|
194
200
|
const that = this;
|
|
195
|
-
await new Promise((resolve) => {
|
|
201
|
+
const res = await new Promise((resolve) => {
|
|
196
202
|
let timer = 0;
|
|
197
203
|
const detect = setInterval(() => {
|
|
198
204
|
if (!that.stop) {
|
|
@@ -200,11 +206,20 @@ class Query {
|
|
|
200
206
|
resolve(true);
|
|
201
207
|
}
|
|
202
208
|
timer++;
|
|
203
|
-
if (timer >
|
|
204
|
-
console.error("
|
|
209
|
+
if (timer > 5) {
|
|
210
|
+
console.error("等待请求失败:", req.url, timer);
|
|
211
|
+
clearInterval(detect);
|
|
212
|
+
resolve(false);
|
|
205
213
|
}
|
|
206
214
|
}, 1000);
|
|
207
215
|
});
|
|
216
|
+
if (!res) {
|
|
217
|
+
return wrapperError({
|
|
218
|
+
code: 500,
|
|
219
|
+
message: "请求取消,可能是因为用户未登录或者token过期",
|
|
220
|
+
req
|
|
221
|
+
});
|
|
222
|
+
}
|
|
208
223
|
}
|
|
209
224
|
return _adapter(req).then(async (res) => {
|
|
210
225
|
try {
|
|
@@ -217,10 +232,11 @@ class Query {
|
|
|
217
232
|
}
|
|
218
233
|
return res;
|
|
219
234
|
} catch (e) {
|
|
220
|
-
console.error("
|
|
235
|
+
console.error("请求在响应后处理时发生错误", e, req);
|
|
221
236
|
return wrapperError({
|
|
222
237
|
code: 500,
|
|
223
|
-
message: "
|
|
238
|
+
message: "请求在响应后处理时发生错误",
|
|
239
|
+
req
|
|
224
240
|
});
|
|
225
241
|
}
|
|
226
242
|
});
|