@workopilot/webapp-sdk 0.1.5 → 0.1.6

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 CHANGED
@@ -64,6 +64,9 @@ VITE_APP_STORE_SECURE_KEY=please-replace-me-with-your-own-key
64
64
  - 自动检查 JWT 是否过期。
65
65
  - 生产环境没有 token 时,通过 iframe `postMessage` 通知宿主重新登录。
66
66
  - 开发环境没有 token 时,自动请求 `{javaBasePath}/auth/code` 获取验证码配置并弹出登录窗口。
67
+ - 业务请求返回 HTTP 401 时,清理失效 token;生产环境调用 `window.parent.location.reload()` 刷新父页面,并发送 `RELOGIN_REQUIRED` 消息及触发 `onReloginRequired`(`reason: 'unauthorized'`)。跨域限制导致刷新失败时,由宿主响应消息或回调处理重新登录。
68
+ - 开发环境收到 HTTP 401 时复用现有登录窗口,登录成功后保存新 token。并发请求共享同一次登录;生产环境同一 SDK 实例只触发一次刷新。
69
+ - 收到 401 的业务请求仍按失败返回,不自动重试;登录成功后,后续请求使用新 token。验证码和登录接口本身失败时直接报错,不递归弹出登录窗口。
67
70
  - 根据 `captchaEnabled` 决定是否显示验证码。
68
71
  - 登录接口默认使用 `POST {javaBasePath}/auth/login`,请求体不包含 `appCode`。
69
72
  - 支持按参考项目规则使用 AES + RSA 加密登录请求。
package/dist/http.d.ts CHANGED
@@ -24,11 +24,16 @@ export declare class WiseAiFrontSdk {
24
24
  private readonly routePermissionPath;
25
25
  private readonly loginRequest?;
26
26
  private accessToken;
27
+ private loginPromise?;
28
+ private parentReloadRequested;
29
+ private readonly rejectedTokens;
27
30
  constructor(options: WiseAiFrontSdkOptions);
28
31
  get state(): WiseAiFrontSdkState;
29
32
  private getCurrentToken;
30
33
  private ensureToken;
31
34
  private loginInDev;
35
+ private handleUnauthorized;
36
+ private performDevLogin;
32
37
  private buildLoginRequest;
33
38
  private openLoginModal;
34
39
  private fetchCaptchaConfig;
package/dist/http.js CHANGED
@@ -78,6 +78,8 @@ function unwrapEnvelope(body) {
78
78
  }
79
79
  export class WiseAiFrontSdk {
80
80
  constructor(options) {
81
+ this.parentReloadRequested = false;
82
+ this.rejectedTokens = new Set();
81
83
  this.accessToken = options.accessToken?.trim() || '';
82
84
  this.appCode = options.appCode;
83
85
  this.captchaPath = options.captchaPath || '/auth/code';
@@ -127,7 +129,7 @@ export class WiseAiFrontSdk {
127
129
  ...resolveTokensFromSecureStore(this.storage, this.secureStoreKey, this.secureStoreNamespace),
128
130
  ];
129
131
  for (const token of candidates) {
130
- if (token && !isJwtExpired(token)) {
132
+ if (token && !this.rejectedTokens.has(token) && !isJwtExpired(token)) {
131
133
  return token;
132
134
  }
133
135
  }
@@ -135,6 +137,12 @@ export class WiseAiFrontSdk {
135
137
  return '';
136
138
  }
137
139
  async ensureToken() {
140
+ if (this.loginPromise) {
141
+ return this.loginPromise;
142
+ }
143
+ if (this.parentReloadRequested) {
144
+ throw new Error('WiseAiFrontSDK: relogin required in prod environment');
145
+ }
138
146
  const current = await this.getCurrentToken();
139
147
  if (current) {
140
148
  this.accessToken = current;
@@ -154,6 +162,50 @@ export class WiseAiFrontSdk {
154
162
  return token;
155
163
  }
156
164
  async loginInDev() {
165
+ if (!this.loginPromise) {
166
+ this.loginPromise = this.performDevLogin()
167
+ .then((token) => {
168
+ this.accessToken = token;
169
+ this.rejectedTokens.delete(token);
170
+ return token;
171
+ })
172
+ .finally(() => {
173
+ this.loginPromise = undefined;
174
+ });
175
+ }
176
+ return this.loginPromise;
177
+ }
178
+ async handleUnauthorized(token) {
179
+ if (this.loginPromise) {
180
+ await this.loginPromise;
181
+ return;
182
+ }
183
+ // 忽略旧请求迟到的 401,避免清除重新登录后取得的 token。
184
+ if (this.accessToken !== token || this.parentReloadRequested) {
185
+ return;
186
+ }
187
+ this.rejectedTokens.add(token);
188
+ this.clearToken();
189
+ if (this.env === 'dev') {
190
+ await this.loginInDev();
191
+ return;
192
+ }
193
+ this.parentReloadRequested = true;
194
+ try {
195
+ // 默认 parentWindow 为 window.parent;跨域时由宿主消息处理兜底。
196
+ this.parentWindow?.location.reload();
197
+ }
198
+ catch {
199
+ // 浏览器同源策略可能禁止 iframe 直接刷新父页面。
200
+ }
201
+ notifyReloginRequired(this.parentWindow, this.appCode, this.env, 'unauthorized');
202
+ this.onReloginRequired?.({
203
+ appCode: this.appCode,
204
+ env: this.env,
205
+ reason: 'unauthorized',
206
+ });
207
+ }
208
+ async performDevLogin() {
157
209
  if (this.loginRequest) {
158
210
  const result = await this.loginRequest(await this.openLoginModal());
159
211
  const token = result.access_token || result.accessToken || '';
@@ -273,7 +325,14 @@ export class WiseAiFrontSdk {
273
325
  signal: options.signal,
274
326
  }, options.timeoutMs ?? this.requestTimeoutMs);
275
327
  if (!response.ok) {
276
- await response.body?.cancel();
328
+ try {
329
+ await response.body?.cancel();
330
+ }
331
+ finally {
332
+ if (response.status === 401) {
333
+ await this.handleUnauthorized(token);
334
+ }
335
+ }
277
336
  throw new Error(`WiseAiFrontSDK: request failed with ${response.status}`);
278
337
  }
279
338
  return readResponse(response, options.responseType);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@workopilot/webapp-sdk",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "WiseAI front-end SDK for iframe-hosted third-party apps",
5
5
  "keywords": "wiseai,workopilot",
6
6
  "author": "workopilot",