@kevisual/api 0.0.51 → 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/package.json +4 -4
- package/query/query-login/query-login.ts +0 -3
- package/query/query-proxy/router-api-proxy.ts +1 -1
package/dist/query-resources.js
CHANGED
|
@@ -446,23 +446,17 @@ var require_spark_md5 = __commonJS((exports, module) => {
|
|
|
446
446
|
});
|
|
447
447
|
});
|
|
448
448
|
|
|
449
|
-
// node_modules/.pnpm/@kevisual+query@0.0.
|
|
449
|
+
// node_modules/.pnpm/@kevisual+query@0.0.47/node_modules/@kevisual/query/dist/query-browser.js
|
|
450
450
|
var isTextForContentType = (contentType) => {
|
|
451
451
|
if (!contentType)
|
|
452
452
|
return false;
|
|
453
|
-
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
|
|
453
|
+
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md", "json"];
|
|
454
454
|
return textTypes.some((type) => contentType.includes(type));
|
|
455
455
|
};
|
|
456
456
|
var adapter = async (opts = {}, overloadOpts) => {
|
|
457
457
|
const controller = new AbortController;
|
|
458
458
|
const signal = controller.signal;
|
|
459
459
|
const isPostFile = opts.isPostFile || false;
|
|
460
|
-
let responseType = opts.responseType || "json";
|
|
461
|
-
if (opts.isBlob) {
|
|
462
|
-
responseType = "blob";
|
|
463
|
-
} else if (opts.isText) {
|
|
464
|
-
responseType = "text";
|
|
465
|
-
}
|
|
466
460
|
const timeout = opts.timeout || 60000 * 3;
|
|
467
461
|
const timer = setTimeout(() => {
|
|
468
462
|
controller.abort();
|
|
@@ -474,7 +468,7 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
474
468
|
if (opts?.url?.startsWith("http")) {
|
|
475
469
|
url = new URL(opts.url);
|
|
476
470
|
} else {
|
|
477
|
-
origin =
|
|
471
|
+
origin = globalThis?.location?.origin || "http://localhost:51515";
|
|
478
472
|
url = new URL(opts?.url || "", origin);
|
|
479
473
|
}
|
|
480
474
|
const isGet = method === "GET";
|
|
@@ -521,21 +515,31 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
521
515
|
headers
|
|
522
516
|
}).then(async (response) => {
|
|
523
517
|
const contentType = response.headers.get("Content-Type");
|
|
524
|
-
if (responseType === "blob") {
|
|
525
|
-
return await response.blob();
|
|
526
|
-
}
|
|
527
|
-
const isText = responseType === "text";
|
|
528
518
|
const isJson = contentType && contentType.includes("application/json");
|
|
529
|
-
|
|
530
|
-
|
|
519
|
+
const isSuccess = response.ok;
|
|
520
|
+
if (isJson) {
|
|
521
|
+
const json = await response.json();
|
|
522
|
+
if (json?.code) {
|
|
523
|
+
return json;
|
|
524
|
+
}
|
|
525
|
+
return {
|
|
526
|
+
code: isSuccess ? 200 : response.status,
|
|
527
|
+
status: response.status,
|
|
528
|
+
data: json
|
|
529
|
+
};
|
|
531
530
|
} else if (isTextForContentType(contentType)) {
|
|
532
531
|
return {
|
|
533
|
-
code: response.status,
|
|
532
|
+
code: isSuccess ? 200 : response.status,
|
|
534
533
|
status: response.status,
|
|
535
534
|
data: await response.text()
|
|
536
535
|
};
|
|
537
536
|
} else {
|
|
538
|
-
return
|
|
537
|
+
return {
|
|
538
|
+
code: isSuccess ? 200 : response.status,
|
|
539
|
+
status: response.status,
|
|
540
|
+
data: "非文本非JSON响应, 请手动处理response。",
|
|
541
|
+
response
|
|
542
|
+
};
|
|
539
543
|
}
|
|
540
544
|
}).catch((err) => {
|
|
541
545
|
if (err.name === "AbortError") {
|
package/dist/query-secret.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
|
});
|
package/dist/query-shop.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;
|
|
@@ -406,7 +422,7 @@ var shopDefine = QueryUtil.create({
|
|
|
406
422
|
}
|
|
407
423
|
});
|
|
408
424
|
|
|
409
|
-
// node_modules/.pnpm/@kevisual+query@0.0.
|
|
425
|
+
// node_modules/.pnpm/@kevisual+query@0.0.47/node_modules/@kevisual/query/dist/query.js
|
|
410
426
|
class BaseQuery {
|
|
411
427
|
query;
|
|
412
428
|
queryDefine;
|
package/dist/store-mark.js
CHANGED
|
@@ -5,19 +5,13 @@ import { create } from "zustand";
|
|
|
5
5
|
var isTextForContentType = (contentType) => {
|
|
6
6
|
if (!contentType)
|
|
7
7
|
return false;
|
|
8
|
-
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md"];
|
|
8
|
+
const textTypes = ["text/", "xml", "html", "javascript", "css", "csv", "plain", "x-www-form-urlencoded", "md", "json"];
|
|
9
9
|
return textTypes.some((type) => contentType.includes(type));
|
|
10
10
|
};
|
|
11
11
|
var adapter = async (opts = {}, overloadOpts) => {
|
|
12
12
|
const controller = new AbortController;
|
|
13
13
|
const signal = controller.signal;
|
|
14
14
|
const isPostFile = opts.isPostFile || false;
|
|
15
|
-
let responseType = opts.responseType || "json";
|
|
16
|
-
if (opts.isBlob) {
|
|
17
|
-
responseType = "blob";
|
|
18
|
-
} else if (opts.isText) {
|
|
19
|
-
responseType = "text";
|
|
20
|
-
}
|
|
21
15
|
const timeout = opts.timeout || 60000 * 3;
|
|
22
16
|
const timer = setTimeout(() => {
|
|
23
17
|
controller.abort();
|
|
@@ -29,7 +23,7 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
29
23
|
if (opts?.url?.startsWith("http")) {
|
|
30
24
|
url = new URL(opts.url);
|
|
31
25
|
} else {
|
|
32
|
-
origin =
|
|
26
|
+
origin = globalThis?.location?.origin || "http://localhost:51515";
|
|
33
27
|
url = new URL(opts?.url || "", origin);
|
|
34
28
|
}
|
|
35
29
|
const isGet = method === "GET";
|
|
@@ -76,21 +70,31 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
76
70
|
headers
|
|
77
71
|
}).then(async (response) => {
|
|
78
72
|
const contentType = response.headers.get("Content-Type");
|
|
79
|
-
if (responseType === "blob") {
|
|
80
|
-
return await response.blob();
|
|
81
|
-
}
|
|
82
|
-
const isText = responseType === "text";
|
|
83
73
|
const isJson = contentType && contentType.includes("application/json");
|
|
84
|
-
|
|
85
|
-
|
|
74
|
+
const isSuccess = response.ok;
|
|
75
|
+
if (isJson) {
|
|
76
|
+
const json = await response.json();
|
|
77
|
+
if (json?.code) {
|
|
78
|
+
return json;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
code: isSuccess ? 200 : response.status,
|
|
82
|
+
status: response.status,
|
|
83
|
+
data: json
|
|
84
|
+
};
|
|
86
85
|
} else if (isTextForContentType(contentType)) {
|
|
87
86
|
return {
|
|
88
|
-
code: response.status,
|
|
87
|
+
code: isSuccess ? 200 : response.status,
|
|
89
88
|
status: response.status,
|
|
90
89
|
data: await response.text()
|
|
91
90
|
};
|
|
92
91
|
} else {
|
|
93
|
-
return
|
|
92
|
+
return {
|
|
93
|
+
code: isSuccess ? 200 : response.status,
|
|
94
|
+
status: response.status,
|
|
95
|
+
data: "非文本非JSON响应, 请手动处理response。",
|
|
96
|
+
response
|
|
97
|
+
};
|
|
94
98
|
}
|
|
95
99
|
}).catch((err) => {
|
|
96
100
|
if (err.name === "AbortError") {
|
|
@@ -110,10 +114,7 @@ var adapter = async (opts = {}, overloadOpts) => {
|
|
|
110
114
|
var wrapperError = ({ code, message }) => {
|
|
111
115
|
const result = {
|
|
112
116
|
code: code || 500,
|
|
113
|
-
|
|
114
|
-
message: message || "api request error",
|
|
115
|
-
showError: (fn) => {},
|
|
116
|
-
noMsg: true
|
|
117
|
+
message: message || "请求错误"
|
|
117
118
|
};
|
|
118
119
|
return result;
|
|
119
120
|
};
|
|
@@ -127,9 +128,13 @@ class Query {
|
|
|
127
128
|
timeout;
|
|
128
129
|
stop;
|
|
129
130
|
qws;
|
|
130
|
-
|
|
131
|
+
tokenName;
|
|
132
|
+
storage;
|
|
133
|
+
token;
|
|
131
134
|
constructor(opts) {
|
|
132
135
|
this.adapter = opts?.adapter || adapter;
|
|
136
|
+
this.tokenName = opts?.tokenName || "token";
|
|
137
|
+
this.storage = opts?.storage || globalThis?.localStorage;
|
|
133
138
|
const defaultURL = opts?.isClient ? "/client/router" : "/api/router";
|
|
134
139
|
this.url = opts?.url || defaultURL;
|
|
135
140
|
this.headers = opts?.headers || {
|
|
@@ -140,7 +145,7 @@ class Query {
|
|
|
140
145
|
this.beforeRequest = opts.beforeRequest;
|
|
141
146
|
} else {
|
|
142
147
|
this.beforeRequest = async (opts2) => {
|
|
143
|
-
const token =
|
|
148
|
+
const token = this.token || this.storage?.getItem?.(this.tokenName);
|
|
144
149
|
if (token) {
|
|
145
150
|
opts2.headers = {
|
|
146
151
|
...opts2.headers,
|
|
@@ -181,7 +186,7 @@ class Query {
|
|
|
181
186
|
if (res === false) {
|
|
182
187
|
return wrapperError({
|
|
183
188
|
code: 500,
|
|
184
|
-
message: "
|
|
189
|
+
message: "请求取消",
|
|
185
190
|
req
|
|
186
191
|
});
|
|
187
192
|
}
|
|
@@ -190,12 +195,13 @@ class Query {
|
|
|
190
195
|
console.error("request beforeFn error", e, req);
|
|
191
196
|
return wrapperError({
|
|
192
197
|
code: 500,
|
|
193
|
-
message: "
|
|
198
|
+
message: "请求在请求前处理时发生错误",
|
|
199
|
+
req
|
|
194
200
|
});
|
|
195
201
|
}
|
|
196
202
|
if (this.stop && !options?.noStop) {
|
|
197
203
|
const that = this;
|
|
198
|
-
await new Promise((resolve) => {
|
|
204
|
+
const res = await new Promise((resolve) => {
|
|
199
205
|
let timer = 0;
|
|
200
206
|
const detect = setInterval(() => {
|
|
201
207
|
if (!that.stop) {
|
|
@@ -203,11 +209,20 @@ class Query {
|
|
|
203
209
|
resolve(true);
|
|
204
210
|
}
|
|
205
211
|
timer++;
|
|
206
|
-
if (timer >
|
|
207
|
-
console.error("
|
|
212
|
+
if (timer > 5) {
|
|
213
|
+
console.error("等待请求失败:", req.url, timer);
|
|
214
|
+
clearInterval(detect);
|
|
215
|
+
resolve(false);
|
|
208
216
|
}
|
|
209
217
|
}, 1000);
|
|
210
218
|
});
|
|
219
|
+
if (!res) {
|
|
220
|
+
return wrapperError({
|
|
221
|
+
code: 500,
|
|
222
|
+
message: "请求取消,可能是因为用户未登录或者token过期",
|
|
223
|
+
req
|
|
224
|
+
});
|
|
225
|
+
}
|
|
211
226
|
}
|
|
212
227
|
return _adapter(req).then(async (res) => {
|
|
213
228
|
try {
|
|
@@ -220,10 +235,11 @@ class Query {
|
|
|
220
235
|
}
|
|
221
236
|
return res;
|
|
222
237
|
} catch (e) {
|
|
223
|
-
console.error("
|
|
238
|
+
console.error("请求在响应后处理时发生错误", e, req);
|
|
224
239
|
return wrapperError({
|
|
225
240
|
code: 500,
|
|
226
|
-
message: "
|
|
241
|
+
message: "请求在响应后处理时发生错误",
|
|
242
|
+
req
|
|
227
243
|
});
|
|
228
244
|
}
|
|
229
245
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kevisual/api",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.52",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "mod.ts",
|
|
6
6
|
"scripts": {
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
"keywords": [],
|
|
19
19
|
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
|
20
20
|
"license": "MIT",
|
|
21
|
-
"packageManager": "pnpm@10.
|
|
21
|
+
"packageManager": "pnpm@10.30.0",
|
|
22
22
|
"type": "module",
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@kevisual/cache": "^0.0.5",
|
|
25
25
|
"@kevisual/code-builder": "^0.0.6",
|
|
26
|
-
"@kevisual/query": "^0.0.
|
|
26
|
+
"@kevisual/query": "^0.0.47",
|
|
27
27
|
"@kevisual/remote-app": "^0.0.4",
|
|
28
|
-
"@kevisual/router": "^0.0.
|
|
28
|
+
"@kevisual/router": "^0.0.75",
|
|
29
29
|
"@kevisual/types": "^0.0.12",
|
|
30
30
|
"@kevisual/use-config": "^1.0.30",
|
|
31
31
|
"@types/bun": "^1.3.9",
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Query, BaseQuery } from '@kevisual/query';
|
|
2
2
|
import type { Result, DataOpts } from '@kevisual/query/query';
|
|
3
|
-
import { setBaseResponse } from '@kevisual/query/query';
|
|
4
3
|
import { LoginCacheStore, CacheStore } from './login-cache.ts';
|
|
5
4
|
import { Cache } from './login-cache.ts';
|
|
6
5
|
import { BaseLoad } from '@kevisual/load';
|
|
@@ -166,7 +165,6 @@ export class QueryLogin extends BaseQuery {
|
|
|
166
165
|
{ key: 'refreshToken', data },
|
|
167
166
|
{
|
|
168
167
|
afterResponse: async (response, ctx) => {
|
|
169
|
-
setBaseResponse(response);
|
|
170
168
|
return response as any;
|
|
171
169
|
},
|
|
172
170
|
},
|
|
@@ -200,7 +198,6 @@ export class QueryLogin extends BaseQuery {
|
|
|
200
198
|
body: body,
|
|
201
199
|
headers: { ...headers, Authorization: `Bearer ${accessToken}` },
|
|
202
200
|
});
|
|
203
|
-
setBaseResponse(res);
|
|
204
201
|
return res;
|
|
205
202
|
}
|
|
206
203
|
} else {
|
|
@@ -47,7 +47,7 @@ export const initApi = async (opts: {
|
|
|
47
47
|
metadata: metadata,
|
|
48
48
|
}).define(async (ctx) => {
|
|
49
49
|
const msg = { ...ctx.query };
|
|
50
|
-
if (msg.token === undefined && token !== undefined) {
|
|
50
|
+
if (msg.token === undefined && token !== undefined && !_isBrowser) {
|
|
51
51
|
msg.token = token;
|
|
52
52
|
}
|
|
53
53
|
const res = await query.post<any>({ path: r.path, key: r.key, ...msg });
|