- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名: * session-client.ts ➔ hose-web-session-client.ts (合思 Web 主站会话客户端) * script-engine-client.ts ➔ piper-workflow-client.ts (Piper 工作流引擎客户端) * unity-client.ts ➔ unity-easo-client.ts (Unity / EASO 运营中台与动态集群客户端) * sso-client.ts ➔ sso-auth-client.ts (SSO 与扫码鉴权客户端) - 新增 hose-docs-client.ts 纳为第 7 大通信底座客户端,收口 docs.ekuaibao.com 文档站点通信 - 端正 Piper 工作流引擎定位,完整覆盖工作流创建、保存、发布、108 类节点编排与 Job 流转 - 彻底整体替换全仓 import 引用与测试套件,新增 clients-semantics.test.ts 自动化校验 - 全量自动化测试 150 files 1155 tests 全部通过,类型检查零错误
66 lines
2.3 KiB
TypeScript
66 lines
2.3 KiB
TypeScript
import ky, { type KyInstance } from "ky";
|
|
import { AuthenticationError } from "../errors.js";
|
|
|
|
export const DEFAULT_ASSIST_PLATFORM_API_URL = "https://unity.ekuaibao.com/api";
|
|
|
|
export interface AssistPlatformClientOptions {
|
|
token: string;
|
|
baseUrl?: string;
|
|
hoseAccessToken?: string;
|
|
timeout?: number;
|
|
}
|
|
|
|
export type CreateAssistPlatformClientOptions = AssistPlatformClientOptions;
|
|
|
|
|
|
export function createAssistPlatformClient(options: AssistPlatformClientOptions): KyInstance {
|
|
if (!options.token || !options.token.trim()) {
|
|
throw new AuthenticationError("缺少协助平台凭证 Token,请先登录协助平台");
|
|
}
|
|
|
|
const baseUrl = options.baseUrl || DEFAULT_ASSIST_PLATFORM_API_URL;
|
|
const cookieParts: string[] = [];
|
|
if (options.hoseAccessToken) {
|
|
cookieParts.push(`hose-access-token=${options.hoseAccessToken}`);
|
|
}
|
|
|
|
return ky.create({
|
|
prefix: baseUrl,
|
|
timeout: options.timeout ?? 30000,
|
|
headers: {
|
|
accept: "application/json",
|
|
"content-type": "application/json;charset=UTF-8",
|
|
token: options.token.trim(),
|
|
referer: "https://unity.ekuaibao.com/platform-charge/index.html",
|
|
...(cookieParts.length > 0 ? { cookie: cookieParts.join("; ") } : {}),
|
|
},
|
|
hooks: {
|
|
afterResponse: [
|
|
async ({ response }) => {
|
|
if (response.status === 401 || response.status === 403) {
|
|
throw new AuthenticationError("协助平台凭证已过期或无效,请重新登录协助平台");
|
|
}
|
|
const contentType = response.headers.get("content-type");
|
|
if (contentType && contentType.includes("application/json")) {
|
|
try {
|
|
const clone = response.clone();
|
|
const json = (await clone.json()) as any;
|
|
if (
|
|
json?.errorCode === 403 ||
|
|
(typeof json?.errorMessage === "string" && json.errorMessage.includes("token过期"))
|
|
) {
|
|
throw new AuthenticationError(
|
|
"协助平台凭证已过期或无效,请运行: hose-use assist-platform login 重新扫码登录"
|
|
);
|
|
}
|
|
} catch (err) {
|
|
if (err instanceof AuthenticationError) throw err;
|
|
}
|
|
}
|
|
return response;
|
|
},
|
|
],
|
|
},
|
|
});
|
|
}
|