@workopilot/webapp-sdk 0.1.6 → 0.1.7
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/README.md +19 -0
- package/dist/http-error.d.ts +8 -0
- package/dist/http-error.js +93 -0
- package/dist/http.js +7 -10
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -95,6 +95,25 @@ const data = await sdk.request('/some/api', {
|
|
|
95
95
|
|
|
96
96
|
支持 `get`、`postJson` 和泛化 `request`。泛化请求支持自定义接口前缀、请求方法、查询参数、请求体类型和请求头。
|
|
97
97
|
|
|
98
|
+
### 接口错误信息(0.1.7 起)
|
|
99
|
+
|
|
100
|
+
HTTP 非 2xx 响应优先使用 JSON 中的 `error.message`、`data.message`、`message`、`msg` 作为错误消息,忽略空值和非字符串。非 HTML 纯文本最多展示前 300 个字符;没有可用文案时显示 `请求失败(HTTP 状态码)`。业务接口、文件上传下载、流式请求以及开发登录和验证码接口均适用。
|
|
101
|
+
|
|
102
|
+
抛出的 `WiseAiHttpError` 继承 `Error`,可以直接通过 `error.message` 展示后端错误,同时通过 `error.status` 和 `error.body` 获取状态码和响应内容。错误响应最多读取 64 KiB,超限后取消剩余响应体。401 重新登录行为保持不变,业务请求不自动重试。
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import { WiseAiHttpError } from '@workopilot/webapp-sdk';
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
await sdk.postJson('/orders/save', { id: 'order-id' });
|
|
109
|
+
} catch (error) {
|
|
110
|
+
if (error instanceof WiseAiHttpError) {
|
|
111
|
+
// 例如:error.message 为“当前订单已关闭,无法修改”。
|
|
112
|
+
showError(error.message);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
98
117
|
### 文件上传
|
|
99
118
|
|
|
100
119
|
```ts
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** 保留接口状态码和响应内容,message 可直接用于页面错误提示。 */
|
|
2
|
+
export declare class WiseAiHttpError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
readonly body: unknown;
|
|
5
|
+
constructor(message: string, status: number, body: unknown);
|
|
6
|
+
}
|
|
7
|
+
/** 读取非成功响应;错误体不可读或没有错误文案时保留 HTTP 状态码。 */
|
|
8
|
+
export declare function createHttpError(response: Response): Promise<WiseAiHttpError>;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const MAX_ERROR_BODY_BYTES = 64 * 1024;
|
|
2
|
+
const MAX_TEXT_MESSAGE_LENGTH = 300;
|
|
3
|
+
/** 保留接口状态码和响应内容,message 可直接用于页面错误提示。 */
|
|
4
|
+
export class WiseAiHttpError extends Error {
|
|
5
|
+
constructor(message, status, body) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'WiseAiHttpError';
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.body = body;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function asRecord(value) {
|
|
13
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
function getJsonMessage(body) {
|
|
19
|
+
const record = asRecord(body);
|
|
20
|
+
const candidates = [
|
|
21
|
+
asRecord(record?.error)?.message,
|
|
22
|
+
asRecord(record?.data)?.message,
|
|
23
|
+
record?.message,
|
|
24
|
+
record?.msg,
|
|
25
|
+
];
|
|
26
|
+
for (const value of candidates) {
|
|
27
|
+
if (typeof value === 'string' && value.trim()) {
|
|
28
|
+
return value.trim();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
async function readErrorText(response) {
|
|
34
|
+
if (!response.body) {
|
|
35
|
+
return '';
|
|
36
|
+
}
|
|
37
|
+
const reader = response.body.getReader();
|
|
38
|
+
const decoder = new TextDecoder();
|
|
39
|
+
let remaining = MAX_ERROR_BODY_BYTES;
|
|
40
|
+
let text = '';
|
|
41
|
+
let complete = false;
|
|
42
|
+
try {
|
|
43
|
+
while (remaining > 0) {
|
|
44
|
+
const { done, value } = await reader.read();
|
|
45
|
+
if (done) {
|
|
46
|
+
complete = true;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
const chunk = value.subarray(0, remaining);
|
|
50
|
+
text += decoder.decode(chunk, { stream: true });
|
|
51
|
+
remaining -= chunk.byteLength;
|
|
52
|
+
}
|
|
53
|
+
return text + decoder.decode();
|
|
54
|
+
}
|
|
55
|
+
finally {
|
|
56
|
+
try {
|
|
57
|
+
if (!complete) {
|
|
58
|
+
await reader.cancel();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
reader.releaseLock();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** 读取非成功响应;错误体不可读或没有错误文案时保留 HTTP 状态码。 */
|
|
67
|
+
export async function createHttpError(response) {
|
|
68
|
+
let body;
|
|
69
|
+
let message;
|
|
70
|
+
try {
|
|
71
|
+
const text = (await readErrorText(response)).trim();
|
|
72
|
+
if (text) {
|
|
73
|
+
try {
|
|
74
|
+
body = JSON.parse(text);
|
|
75
|
+
message = getJsonMessage(body);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
body = text;
|
|
79
|
+
const contentType = response.headers.get('content-type') || '';
|
|
80
|
+
const isText = !contentType || /text\/|json/i.test(contentType);
|
|
81
|
+
const isHtml = /html/i.test(contentType) || /<(?:!doctype|\/?[a-z])[^>]*>/i.test(text);
|
|
82
|
+
const looksLikeJson = text.startsWith('{') || text.startsWith('[');
|
|
83
|
+
if (isText && !isHtml && !looksLikeJson) {
|
|
84
|
+
message = text.slice(0, MAX_TEXT_MESSAGE_LENGTH);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
// 响应体读取失败也不能掩盖 HTTP 错误或阻止 401 重新登录。
|
|
91
|
+
}
|
|
92
|
+
return new WiseAiHttpError(message || `请求失败(HTTP ${response.status})`, response.status, body);
|
|
93
|
+
}
|
package/dist/http.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { fetchManagedResponse, getDownloadFilename, readResponse } from './response-stream';
|
|
2
|
+
import { createHttpError } from './http-error';
|
|
2
3
|
import { notifyReloginRequired, } from './bridge';
|
|
3
4
|
import { encryptRequestBody, } from './encryption';
|
|
4
5
|
import { isJwtExpired, } from './jwt';
|
|
@@ -231,7 +232,7 @@ export class WiseAiFrontSdk {
|
|
|
231
232
|
method: 'POST',
|
|
232
233
|
});
|
|
233
234
|
if (!response.ok) {
|
|
234
|
-
throw
|
|
235
|
+
throw await createHttpError(response);
|
|
235
236
|
}
|
|
236
237
|
const data = (await response.json());
|
|
237
238
|
const loginResult = unwrapEnvelope(data);
|
|
@@ -286,7 +287,7 @@ export class WiseAiFrontSdk {
|
|
|
286
287
|
method: 'GET',
|
|
287
288
|
});
|
|
288
289
|
if (!response.ok) {
|
|
289
|
-
throw
|
|
290
|
+
throw await createHttpError(response);
|
|
290
291
|
}
|
|
291
292
|
const body = (await response.json());
|
|
292
293
|
return unwrapEnvelope(body);
|
|
@@ -325,15 +326,11 @@ export class WiseAiFrontSdk {
|
|
|
325
326
|
signal: options.signal,
|
|
326
327
|
}, options.timeoutMs ?? this.requestTimeoutMs);
|
|
327
328
|
if (!response.ok) {
|
|
328
|
-
|
|
329
|
-
|
|
329
|
+
const error = await createHttpError(response);
|
|
330
|
+
if (response.status === 401) {
|
|
331
|
+
await this.handleUnauthorized(token);
|
|
330
332
|
}
|
|
331
|
-
|
|
332
|
-
if (response.status === 401) {
|
|
333
|
-
await this.handleUnauthorized(token);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
throw new Error(`WiseAiFrontSDK: request failed with ${response.status}`);
|
|
333
|
+
throw error;
|
|
337
334
|
}
|
|
338
335
|
return readResponse(response, options.responseType);
|
|
339
336
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { decodeJwtPayload, isJwtExpired } from './jwt';
|
|
2
2
|
export { notifyReloginRequired } from './bridge';
|
|
3
|
+
export { WiseAiHttpError } from './http-error';
|
|
3
4
|
import { WiseAiFrontSdk } from './http';
|
|
4
5
|
export { persistToken, resolveTokenFromLocation, resolveTokenFromStorage, resolveTokensFromSecureStore, } from './storage';
|
|
5
6
|
export type { ApiBase, DownloadResult, HttpMethod, JwtPayload, LoginCredentials, LoginModalOptions, LoginResult, MenuPermissionNode, MenuRouteMeta, RequestBodyType, RequestOptions, UploadOptions, WiseAiEnvironment, WiseAiFrontSdkOptions, WiseAiFrontSdkState, } from './types';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { decodeJwtPayload, isJwtExpired } from './jwt';
|
|
2
2
|
export { notifyReloginRequired } from './bridge';
|
|
3
|
+
export { WiseAiHttpError } from './http-error';
|
|
3
4
|
import { WiseAiFrontSdk } from './http';
|
|
4
5
|
export { persistToken, resolveTokenFromLocation, resolveTokenFromStorage, resolveTokensFromSecureStore, } from './storage';
|
|
5
6
|
export { WiseAiFrontSdk } from './http';
|