@workopilot/webapp-sdk 0.1.1 → 0.1.3
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 +11 -2
- package/dist/http.d.ts +4 -0
- package/dist/http.js +30 -14
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/storage.d.ts +2 -0
- package/dist/storage.js +83 -2
- package/dist/types.d.ts +16 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -18,6 +18,7 @@ const sdk = createWiseAiFrontSdk({
|
|
|
18
18
|
env: 'prod',
|
|
19
19
|
javaBasePath: '/prod-api',
|
|
20
20
|
netBasePath: '/net-api',
|
|
21
|
+
secureStoreKey: 'please-replace-me-with-your-own-key',
|
|
21
22
|
});
|
|
22
23
|
|
|
23
24
|
await sdk.init();
|
|
@@ -34,6 +35,11 @@ await sdk.init();
|
|
|
34
35
|
| `enableEncrypt` | 否 | 读取环境变量 | 是否启用开发环境登录请求加密 |
|
|
35
36
|
| `clientId` | 否 | 读取环境变量 | 登录请求使用的客户端编号 |
|
|
36
37
|
| `rsaPublicKey` | 否 | 读取环境变量 | 启用加密时使用的 RSA 公钥 |
|
|
38
|
+
| `accessToken` | 否 | 空 | 调用方直接提供的 token |
|
|
39
|
+
| `tokenProvider` | 否 | 无 | 自定义同步或异步 token 获取函数 |
|
|
40
|
+
| `tokenQueryParam` | 否 | `runtimeToken` | iframe URL 中承载 token 的 Query 参数名 |
|
|
41
|
+
| `secureStoreKey` | 否 | 读取环境变量 | 解密 WiseAI 生产环境 SecureLS 登录态的密钥 |
|
|
42
|
+
| `secureStoreNamespace` | 否 | 自动扫描 | 明确指定宿主 Pinia 持久化命名空间 |
|
|
37
43
|
|
|
38
44
|
生产环境建议由宿主项目的 `.env.*` 文件管理环境参数,再传给 SDK:
|
|
39
45
|
|
|
@@ -45,13 +51,16 @@ VITE_WISEAI_NET_BASE_PATH=/net-api
|
|
|
45
51
|
VITE_GLOB_ENABLE_ENCRYPT=true
|
|
46
52
|
VITE_GLOB_RSA_PUBLIC_KEY=...
|
|
47
53
|
VITE_GLOB_APP_CLIENT_ID=...
|
|
54
|
+
VITE_APP_STORE_SECURE_KEY=please-replace-me-with-your-own-key
|
|
48
55
|
```
|
|
49
56
|
|
|
50
57
|
## 提供能力
|
|
51
58
|
|
|
52
59
|
### Token 和登录
|
|
53
60
|
|
|
54
|
-
-
|
|
61
|
+
- 按显式 token、`tokenProvider`、URL `runtimeToken`、明文存储、WiseAI SecureLS 存储的顺序获取 token。
|
|
62
|
+
- 兼容参考项目生产环境带版本号的 `*-prod-core-access` 加密 Pinia 登录态。
|
|
63
|
+
- 自动读取和保存开发环境的 `accessToken`。
|
|
55
64
|
- 自动检查 JWT 是否过期。
|
|
56
65
|
- 生产环境没有 token 时,通过 iframe `postMessage` 通知宿主重新登录。
|
|
57
66
|
- 开发环境没有 token 时,自动请求 `{javaBasePath}/auth/code` 获取验证码配置并弹出登录窗口。
|
|
@@ -106,7 +115,7 @@ const menus = await sdk.getRoutePermissions();
|
|
|
106
115
|
默认调用:
|
|
107
116
|
|
|
108
117
|
```text
|
|
109
|
-
GET {netBasePath}/
|
|
118
|
+
GET {netBasePath}/api/PluginApp/GetRouters?appCode={appCode}
|
|
110
119
|
```
|
|
111
120
|
|
|
112
121
|
返回路由菜单权限数据,宿主项目可以根据结果生成左侧菜单和受保护路由。
|
package/dist/http.d.ts
CHANGED
|
@@ -13,9 +13,13 @@ export declare class WiseAiFrontSdk {
|
|
|
13
13
|
private readonly parentWindow?;
|
|
14
14
|
private readonly requestTimeoutMs;
|
|
15
15
|
private readonly rsaPublicKey;
|
|
16
|
+
private readonly secureStoreKey;
|
|
17
|
+
private readonly secureStoreNamespace?;
|
|
16
18
|
private readonly storage?;
|
|
17
19
|
private readonly tokenKey;
|
|
18
20
|
private readonly tokenStorageKey;
|
|
21
|
+
private readonly tokenProvider?;
|
|
22
|
+
private readonly tokenQueryParam;
|
|
19
23
|
private readonly uploadPath;
|
|
20
24
|
private readonly routePermissionPath;
|
|
21
25
|
private readonly loginRequest?;
|
package/dist/http.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { notifyReloginRequired, } from './bridge';
|
|
2
2
|
import { encryptRequestBody, } from './encryption';
|
|
3
3
|
import { isJwtExpired, } from './jwt';
|
|
4
|
-
import { clearToken, persistToken, resolveTokenFromStorage, } from './storage';
|
|
4
|
+
import { clearToken, persistToken, resolveTokenFromLocation, resolveTokenFromStorage, resolveTokensFromSecureStore, } from './storage';
|
|
5
5
|
import { openDevLoginDialog } from './dialog';
|
|
6
6
|
function inferEnvironment() {
|
|
7
7
|
if (typeof import.meta !== 'undefined') {
|
|
@@ -87,7 +87,7 @@ function unwrapEnvelope(body) {
|
|
|
87
87
|
}
|
|
88
88
|
export class WiseAiFrontSdk {
|
|
89
89
|
constructor(options) {
|
|
90
|
-
this.accessToken = '';
|
|
90
|
+
this.accessToken = options.accessToken?.trim() || '';
|
|
91
91
|
this.appCode = options.appCode;
|
|
92
92
|
this.captchaPath = options.captchaPath || '/auth/code';
|
|
93
93
|
this.clientId =
|
|
@@ -103,11 +103,18 @@ export class WiseAiFrontSdk {
|
|
|
103
103
|
this.requestTimeoutMs = options.requestTimeoutMs ?? 30000;
|
|
104
104
|
this.rsaPublicKey =
|
|
105
105
|
options.rsaPublicKey || readImportMetaEnv('VITE_GLOB_RSA_PUBLIC_KEY');
|
|
106
|
+
this.secureStoreKey =
|
|
107
|
+
options.secureStoreKey ||
|
|
108
|
+
readImportMetaEnv('VITE_APP_STORE_SECURE_KEY');
|
|
109
|
+
this.secureStoreNamespace = options.secureStoreNamespace;
|
|
106
110
|
this.storage = options.storage;
|
|
107
111
|
this.tokenKey = options.tokenKey || 'accessToken';
|
|
108
112
|
this.tokenStorageKey = options.tokenStorageKey || 'accessToken';
|
|
113
|
+
this.tokenProvider = options.tokenProvider;
|
|
114
|
+
this.tokenQueryParam = options.tokenQueryParam || 'runtimeToken';
|
|
109
115
|
this.uploadPath = options.uploadPath || '/api/file/upload';
|
|
110
|
-
this.routePermissionPath =
|
|
116
|
+
this.routePermissionPath =
|
|
117
|
+
options.routePermissionPath || '/api/PluginApp/GetRouters';
|
|
111
118
|
this.loginRequest = options.loginRequest;
|
|
112
119
|
}
|
|
113
120
|
get state() {
|
|
@@ -119,19 +126,25 @@ export class WiseAiFrontSdk {
|
|
|
119
126
|
netBasePath: this.netBasePath,
|
|
120
127
|
};
|
|
121
128
|
}
|
|
122
|
-
getCurrentToken() {
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
async getCurrentToken() {
|
|
130
|
+
const providedToken = await this.tokenProvider?.();
|
|
131
|
+
const candidates = [
|
|
132
|
+
this.accessToken,
|
|
133
|
+
providedToken?.trim() || '',
|
|
134
|
+
resolveTokenFromLocation(this.tokenQueryParam),
|
|
135
|
+
resolveTokenFromStorage(this.storage, this.tokenKey, this.tokenStorageKey),
|
|
136
|
+
...resolveTokensFromSecureStore(this.storage, this.secureStoreKey, this.secureStoreNamespace),
|
|
137
|
+
];
|
|
138
|
+
for (const token of candidates) {
|
|
139
|
+
if (token && !isJwtExpired(token)) {
|
|
140
|
+
return token;
|
|
141
|
+
}
|
|
130
142
|
}
|
|
131
|
-
|
|
143
|
+
clearToken(this.storage, this.tokenKey, this.tokenStorageKey);
|
|
144
|
+
return '';
|
|
132
145
|
}
|
|
133
146
|
async ensureToken() {
|
|
134
|
-
const current = this.getCurrentToken();
|
|
147
|
+
const current = await this.getCurrentToken();
|
|
135
148
|
if (current) {
|
|
136
149
|
this.accessToken = current;
|
|
137
150
|
return current;
|
|
@@ -339,12 +352,15 @@ export class WiseAiFrontSdk {
|
|
|
339
352
|
});
|
|
340
353
|
}
|
|
341
354
|
async getRoutePermissions() {
|
|
342
|
-
const response = await this.get(this.routePermissionPath,
|
|
355
|
+
const response = await this.get(this.routePermissionPath, { appCode: this.appCode }, {
|
|
343
356
|
base: 'net',
|
|
344
357
|
});
|
|
345
358
|
if (Array.isArray(response)) {
|
|
346
359
|
return response;
|
|
347
360
|
}
|
|
361
|
+
if (response.code !== undefined && response.code !== 200) {
|
|
362
|
+
throw new Error(response.msg || 'WiseAiFrontSDK: failed to get route permissions');
|
|
363
|
+
}
|
|
348
364
|
return response.data || response.rows || [];
|
|
349
365
|
}
|
|
350
366
|
clearToken() {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { decodeJwtPayload, isJwtExpired } from './jwt';
|
|
2
2
|
export { notifyReloginRequired } from './bridge';
|
|
3
3
|
import { WiseAiFrontSdk } from './http';
|
|
4
|
-
export { persistToken, resolveTokenFromStorage } from './storage';
|
|
5
|
-
export type { ApiBase, HttpMethod, JwtPayload, LoginCredentials, LoginModalOptions, LoginResult, MenuPermissionNode, RequestBodyType, RequestOptions, UploadOptions, WiseAiEnvironment, WiseAiFrontSdkOptions, WiseAiFrontSdkState, } from './types';
|
|
4
|
+
export { persistToken, resolveTokenFromLocation, resolveTokenFromStorage, resolveTokensFromSecureStore, } from './storage';
|
|
5
|
+
export type { ApiBase, HttpMethod, JwtPayload, LoginCredentials, LoginModalOptions, LoginResult, MenuPermissionNode, MenuRouteMeta, RequestBodyType, RequestOptions, UploadOptions, WiseAiEnvironment, WiseAiFrontSdkOptions, WiseAiFrontSdkState, } from './types';
|
|
6
6
|
export { WiseAiFrontSdk } from './http';
|
|
7
7
|
export declare function createWiseAiFrontSdk(options: import('./types').WiseAiFrontSdkOptions): WiseAiFrontSdk;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { decodeJwtPayload, isJwtExpired } from './jwt';
|
|
2
2
|
export { notifyReloginRequired } from './bridge';
|
|
3
3
|
import { WiseAiFrontSdk } from './http';
|
|
4
|
-
export { persistToken, resolveTokenFromStorage } from './storage';
|
|
4
|
+
export { persistToken, resolveTokenFromLocation, resolveTokenFromStorage, resolveTokensFromSecureStore, } from './storage';
|
|
5
5
|
export { WiseAiFrontSdk } from './http';
|
|
6
6
|
export function createWiseAiFrontSdk(options) {
|
|
7
7
|
return new WiseAiFrontSdk(options);
|
package/dist/storage.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export declare function resolveTokenFromStorage(storage: Storage | undefined, tokenKey: string, tokenStorageKey: string): string;
|
|
2
|
+
export declare function resolveTokenFromLocation(queryParam?: string): string;
|
|
3
|
+
export declare function resolveTokensFromSecureStore(storage: Storage | undefined, encryptionSecret: string, namespace?: string): string[];
|
|
2
4
|
export declare function persistToken(storage: Storage | undefined, tokenKey: string, tokenStorageKey: string, token: string): void;
|
|
3
5
|
export declare function clearToken(storage: Storage | undefined, tokenKey: string, tokenStorageKey: string): void;
|
package/dist/storage.js
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
|
+
import SecureLS from 'secure-ls';
|
|
2
|
+
const SecureLSConstructor = (SecureLS.default || SecureLS);
|
|
1
3
|
const ACCESS_TOKEN_KEY = 'accessToken';
|
|
2
4
|
const REFRESH_TOKEN_KEY = 'refreshToken';
|
|
5
|
+
function normalizeToken(value) {
|
|
6
|
+
if (typeof value === 'string') {
|
|
7
|
+
const normalized = value.trim();
|
|
8
|
+
if (!normalized) {
|
|
9
|
+
return '';
|
|
10
|
+
}
|
|
11
|
+
if ((normalized.startsWith('{') && normalized.endsWith('}')) ||
|
|
12
|
+
(normalized.startsWith('"') && normalized.endsWith('"'))) {
|
|
13
|
+
try {
|
|
14
|
+
return normalizeToken(JSON.parse(normalized));
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return normalized;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return normalized;
|
|
21
|
+
}
|
|
22
|
+
if (value && typeof value === 'object') {
|
|
23
|
+
const source = value;
|
|
24
|
+
return normalizeToken(source.accessToken ||
|
|
25
|
+
source.access_token ||
|
|
26
|
+
source.token ||
|
|
27
|
+
source.state);
|
|
28
|
+
}
|
|
29
|
+
return '';
|
|
30
|
+
}
|
|
3
31
|
function getSafeStorage(storage) {
|
|
4
32
|
if (storage) {
|
|
5
33
|
return storage;
|
|
@@ -7,14 +35,19 @@ function getSafeStorage(storage) {
|
|
|
7
35
|
if (typeof window === 'undefined') {
|
|
8
36
|
return null;
|
|
9
37
|
}
|
|
10
|
-
|
|
38
|
+
try {
|
|
39
|
+
return window.localStorage;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
11
44
|
}
|
|
12
45
|
export function resolveTokenFromStorage(storage, tokenKey, tokenStorageKey) {
|
|
13
46
|
const safeStorage = getSafeStorage(storage);
|
|
14
47
|
if (!safeStorage) {
|
|
15
48
|
return '';
|
|
16
49
|
}
|
|
17
|
-
return (safeStorage.getItem(tokenStorageKey) ||
|
|
50
|
+
return normalizeToken(safeStorage.getItem(tokenStorageKey) ||
|
|
18
51
|
safeStorage.getItem(tokenKey) ||
|
|
19
52
|
safeStorage.getItem(`${tokenStorageKey}:${ACCESS_TOKEN_KEY}`) ||
|
|
20
53
|
safeStorage.getItem(`${tokenKey}:${ACCESS_TOKEN_KEY}`) ||
|
|
@@ -22,6 +55,54 @@ export function resolveTokenFromStorage(storage, tokenKey, tokenStorageKey) {
|
|
|
22
55
|
safeStorage.getItem(`${tokenKey}:${REFRESH_TOKEN_KEY}`) ||
|
|
23
56
|
'');
|
|
24
57
|
}
|
|
58
|
+
export function resolveTokenFromLocation(queryParam = 'runtimeToken') {
|
|
59
|
+
if (typeof window === 'undefined') {
|
|
60
|
+
return '';
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
return normalizeToken(new URL(window.location.href).searchParams.get(queryParam));
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export function resolveTokensFromSecureStore(storage, encryptionSecret, namespace) {
|
|
70
|
+
const safeStorage = getSafeStorage(storage);
|
|
71
|
+
if (!safeStorage || !encryptionSecret) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
const explicitKey = namespace ? `${namespace}-core-access` : '';
|
|
75
|
+
const storeKeys = explicitKey
|
|
76
|
+
? [explicitKey]
|
|
77
|
+
: Array.from({ length: safeStorage.length }, (_, index) => safeStorage.key(index))
|
|
78
|
+
.filter((key) => Boolean(key?.endsWith('-core-access')))
|
|
79
|
+
.sort((left, right) => {
|
|
80
|
+
const prodDifference = Number(right.includes('-prod-core-access')) -
|
|
81
|
+
Number(left.includes('-prod-core-access'));
|
|
82
|
+
return prodDifference || right.localeCompare(left);
|
|
83
|
+
});
|
|
84
|
+
const tokens = [];
|
|
85
|
+
for (const storeKey of storeKeys) {
|
|
86
|
+
const storeNamespace = storeKey.slice(0, -'-core-access'.length);
|
|
87
|
+
try {
|
|
88
|
+
const secureStorage = new SecureLSConstructor({
|
|
89
|
+
encodingType: 'aes',
|
|
90
|
+
encryptionSecret,
|
|
91
|
+
isCompression: true,
|
|
92
|
+
metaKey: `${storeNamespace}-secure-meta`,
|
|
93
|
+
storage: safeStorage,
|
|
94
|
+
});
|
|
95
|
+
const token = normalizeToken(secureStorage.get(storeKey));
|
|
96
|
+
if (token && !tokens.includes(token)) {
|
|
97
|
+
tokens.push(token);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// Ignore stores encrypted with another key or using another format.
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return tokens;
|
|
105
|
+
}
|
|
25
106
|
export function persistToken(storage, tokenKey, tokenStorageKey, token) {
|
|
26
107
|
const safeStorage = getSafeStorage(storage);
|
|
27
108
|
if (!safeStorage) {
|
package/dist/types.d.ts
CHANGED
|
@@ -34,11 +34,22 @@ export interface JwtPayload {
|
|
|
34
34
|
[key: string]: unknown;
|
|
35
35
|
}
|
|
36
36
|
export interface MenuPermissionNode {
|
|
37
|
+
alwaysShow?: boolean;
|
|
37
38
|
children?: MenuPermissionNode[];
|
|
39
|
+
component?: string;
|
|
38
40
|
hidden?: boolean;
|
|
39
41
|
icon?: string;
|
|
42
|
+
meta?: MenuRouteMeta;
|
|
40
43
|
name?: string;
|
|
41
44
|
path: string;
|
|
45
|
+
query?: string;
|
|
46
|
+
redirect?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface MenuRouteMeta {
|
|
49
|
+
activeMenu?: string;
|
|
50
|
+
icon?: string;
|
|
51
|
+
link?: string;
|
|
52
|
+
noCache?: boolean;
|
|
42
53
|
title?: string;
|
|
43
54
|
}
|
|
44
55
|
export interface RequestOptions {
|
|
@@ -63,6 +74,7 @@ export interface LoginModalOptions {
|
|
|
63
74
|
title?: string;
|
|
64
75
|
}
|
|
65
76
|
export interface WiseAiFrontSdkOptions {
|
|
77
|
+
accessToken?: string;
|
|
66
78
|
appCode: string;
|
|
67
79
|
captchaPath?: string;
|
|
68
80
|
clientId?: string;
|
|
@@ -80,7 +92,11 @@ export interface WiseAiFrontSdkOptions {
|
|
|
80
92
|
parentWindow?: Window;
|
|
81
93
|
requestTimeoutMs?: number;
|
|
82
94
|
rsaPublicKey?: string;
|
|
95
|
+
secureStoreKey?: string;
|
|
96
|
+
secureStoreNamespace?: string;
|
|
83
97
|
storage?: Storage;
|
|
98
|
+
tokenProvider?: () => null | string | undefined | Promise<null | string | undefined>;
|
|
99
|
+
tokenQueryParam?: string;
|
|
84
100
|
tokenKey?: string;
|
|
85
101
|
tokenStorageKey?: string;
|
|
86
102
|
uploadPath?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@workopilot/webapp-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "WiseAI front-end SDK for iframe-hosted third-party apps",
|
|
5
5
|
"keywords": "wiseai,workopilot",
|
|
6
6
|
"author": "workopilot",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"crypto-js": "^4.2.0",
|
|
27
|
-
"jsencrypt": "^3.3.2"
|
|
27
|
+
"jsencrypt": "^3.3.2",
|
|
28
|
+
"secure-ls": "^2.0.0"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
30
31
|
"@types/crypto-js": "^4.2.2",
|