refactor(core): 统一 HTTP 工厂与认证模块,支持 401 自动重试
- factory.ts 只负责创建 HTTP client 和 401 retry 逻辑 - auth 模块(ekb/easo/piper/sealai)只负责 token/header 获取 - ekb.ts 新增 createEkbClient - sealai.ts 新增 createSealaiClient,内部使用 createAuthClient - easo.ts/piper.ts 改用 createAuthClient,token 交换失败抛 AuthenticationError - server.ts 使用 createEkbClient / createSealaiClient / getEasoClient
This commit is contained in:
1 parent
62afd6906e
commit
c9e0fba265
6 files changed
+151
-62
No files matched your search
+36
-12
@@ -1,19 +1,26 @@
|
||||
import ky from "ky";
|
||||
import { CorpConfig } from "../../core/config/types.js";
|
||||
import { getCorpTokens, setCorpTokens } from "../../core/auth/token-store.js";
|
||||
import { createAuthClient } from "../../core/http/factory.js";
|
||||
import { AuthenticationError } from "../../core/errors.js";
|
||||
|
||||
const EASO_BASE_URL = "https://unity.ekuaibao.com";
|
||||
const BUSINESS_CENTER_URL = "https://business-center.ekuaibao.com";
|
||||
const EKB_BASE_URL = "https://app.ekuaibao.com";
|
||||
|
||||
// 内存缓存
|
||||
const easoMemoryCache = new Map<string, { token: string; hoseToken: string; expiresAt: number }>();
|
||||
const easoMemoryCache = new Map<
|
||||
string,
|
||||
{ token: string; hoseToken: string; expiresAt: number }
|
||||
>();
|
||||
|
||||
function isExpired(entry: { expiresAt: number }, refreshTtl: number): boolean {
|
||||
return Date.now() >= entry.expiresAt - refreshTtl * 1000;
|
||||
}
|
||||
|
||||
async function fetchHoseToken(accessToken: string, corpId: string): Promise<string> {
|
||||
async function fetchHoseToken(
|
||||
accessToken: string,
|
||||
corpId: string
|
||||
): Promise<string> {
|
||||
const response = await ky
|
||||
.get(`${BUSINESS_CENTER_URL}/auth/oauth2/grant/ekb/accessToken`, {
|
||||
searchParams: {
|
||||
@@ -23,6 +30,9 @@ async function fetchHoseToken(accessToken: string, corpId: string): Promise<stri
|
||||
},
|
||||
})
|
||||
.json<{ data: string }>();
|
||||
if (!response.data) {
|
||||
throw new AuthenticationError("Failed to fetch hose-access-token");
|
||||
}
|
||||
return response.data;
|
||||
}
|
||||
|
||||
@@ -41,7 +51,11 @@ async function fetchUnityJwt(
|
||||
},
|
||||
})
|
||||
.json<{ id?: string; value?: string }>();
|
||||
return response.id || response.value || "";
|
||||
const token = response.id || response.value;
|
||||
if (!token) {
|
||||
throw new AuthenticationError("Failed to fetch UNITY JWT");
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
async function fetchEasoRealmToken(
|
||||
@@ -58,11 +72,15 @@ async function fetchEasoRealmToken(
|
||||
},
|
||||
})
|
||||
.json<{ value?: { accessToken?: string } }>();
|
||||
return response.value?.accessToken || "";
|
||||
const token = response.value?.accessToken;
|
||||
if (!token) {
|
||||
throw new AuthenticationError("Failed to fetch EASO realm token");
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
export async function getEasoClient(corp: CorpConfig): Promise<{
|
||||
client: typeof ky;
|
||||
client: ReturnType<typeof createAuthClient>;
|
||||
}> {
|
||||
const refreshTtl = corp.auth?.refreshTtl ?? 300;
|
||||
const cacheKey = `${corp.id}:easo`;
|
||||
@@ -71,13 +89,16 @@ export async function getEasoClient(corp: CorpConfig): Promise<{
|
||||
const mem = easoMemoryCache.get(cacheKey);
|
||||
if (mem && !isExpired(mem, refreshTtl)) {
|
||||
return {
|
||||
client: ky.create({
|
||||
prefix: EASO_BASE_URL,
|
||||
headers: {
|
||||
client: createAuthClient({
|
||||
prefixUrl: EASO_BASE_URL,
|
||||
getHeaders: () => ({
|
||||
Authorization: `Bearer ${mem.token}`,
|
||||
accesstoken: corp.credentials.accessToken,
|
||||
corporationid: corp.credentials.corpId,
|
||||
hoseaccesstoken: mem.hoseToken,
|
||||
}),
|
||||
onUnauthorized: () => {
|
||||
easoMemoryCache.delete(cacheKey);
|
||||
},
|
||||
}),
|
||||
};
|
||||
@@ -108,13 +129,16 @@ export async function getEasoClient(corp: CorpConfig): Promise<{
|
||||
easoMemoryCache.set(cacheKey, entry);
|
||||
|
||||
return {
|
||||
client: ky.create({
|
||||
prefix: EASO_BASE_URL,
|
||||
headers: {
|
||||
client: createAuthClient({
|
||||
prefixUrl: EASO_BASE_URL,
|
||||
getHeaders: () => ({
|
||||
Authorization: `Bearer ${easoToken}`,
|
||||
accesstoken: corp.credentials.accessToken,
|
||||
corporationid: corp.credentials.corpId,
|
||||
hoseaccesstoken: hoseToken,
|
||||
}),
|
||||
onUnauthorized: () => {
|
||||
easoMemoryCache.delete(cacheKey);
|
||||
},
|
||||
}),
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user