@autional/shared 0.1.0-rc.59 → 0.1.0-rc.61
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/package.json
CHANGED
|
@@ -193,6 +193,20 @@ describe('createFetchClient', () => {
|
|
|
193
193
|
expect(onUnauthorized).not.toHaveBeenCalled();
|
|
194
194
|
});
|
|
195
195
|
|
|
196
|
+
|
|
197
|
+
it('refresh 收到 401 错误本身(可按业务码决定要不要续期)', async () => {
|
|
198
|
+
stubFetch(() => jsonResponse({ code: '30000001', detail: 'secret not found' }, 401));
|
|
199
|
+
const refresh = vi.fn(async (_error: FetchApiError) => null);
|
|
200
|
+
const onUnauthorized = vi.fn();
|
|
201
|
+
const client = createFetchClient({ baseUrl: '/api', refresh, onUnauthorized });
|
|
202
|
+
|
|
203
|
+
await expect(client.get('/x')).rejects.toMatchObject({ status: 401 });
|
|
204
|
+
|
|
205
|
+
expect(refresh).toHaveBeenCalledTimes(1);
|
|
206
|
+
expect(refresh.mock.calls[0]![0]).toMatchObject({ status: 401, code: '30000001' });
|
|
207
|
+
expect(onUnauthorized).toHaveBeenCalledTimes(1);
|
|
208
|
+
});
|
|
209
|
+
|
|
196
210
|
it('401 + refresh 返回 null:不重试,抛 401 并通知一次', async () => {
|
|
197
211
|
stubFetch(() => jsonResponse({ message: 'expired' }, 401));
|
|
198
212
|
const onUnauthorized = vi.fn();
|
package/src/api/fetch-client.ts
CHANGED
|
@@ -60,8 +60,12 @@ export interface FetchClientOptions {
|
|
|
60
60
|
* 可救回的 401 静默续期,不打扰用户。
|
|
61
61
|
*/
|
|
62
62
|
onUnauthorized?: (error: FetchApiError) => void;
|
|
63
|
-
/**
|
|
64
|
-
|
|
63
|
+
/**
|
|
64
|
+
* 401 静默续期:返回新 token 则重试一次;返回 null/抛错则放弃。
|
|
65
|
+
* 入参是本次 401 的错误(含 `code` / `body`)—— 调用方可据此区分「会话过期」与
|
|
66
|
+
* 「业务 401」(如密钥服务的 `30000001 secret not found` 不该续期、更不该踢下线)。
|
|
67
|
+
*/
|
|
68
|
+
refresh?: (error: FetchApiError) => Promise<string | null>;
|
|
65
69
|
/** 默认超时,20s(与两站现状一致)。 */
|
|
66
70
|
timeoutMs?: number;
|
|
67
71
|
/**
|
|
@@ -249,7 +253,7 @@ export function createFetchClient(options: FetchClientOptions): FetchClient {
|
|
|
249
253
|
if (!retried && refresh) {
|
|
250
254
|
let nextToken: string | null = null;
|
|
251
255
|
try {
|
|
252
|
-
nextToken = await refresh();
|
|
256
|
+
nextToken = await refresh(unauthorized);
|
|
253
257
|
} catch {
|
|
254
258
|
nextToken = null;
|
|
255
259
|
}
|