- 彻底移除旧内部狭隘命名与垫片包袱,一步到位全仓整体重命名: * 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 全部通过,类型检查零错误
81 lines
2.7 KiB
TypeScript
81 lines
2.7 KiB
TypeScript
import type { CorpConfig } from "../config/types.js";
|
|
import { AuthenticationError } from "../errors.js";
|
|
import { createAuthClient } from "./factory.js";
|
|
import {
|
|
getBrowserFingerprintHeaders,
|
|
type BrowserFingerprintOptions,
|
|
} from "./fingerprint.js";
|
|
import {
|
|
canRefreshCorpAccessToken,
|
|
refreshSingleCorpAccessToken,
|
|
} from "../auth/login.js";
|
|
|
|
export interface CreateHoseWebSessionClientOptions {
|
|
/** 额外的指纹或请求头配置 */
|
|
fingerprint?: BrowserFingerprintOptions;
|
|
/** 超时毫秒数 (默认 30000) */
|
|
timeout?: number | false;
|
|
}
|
|
|
|
export type CreateSessionClientOptions = CreateHoseWebSessionClientOptions;
|
|
|
|
/**
|
|
* 获取合思 Web 主站会话所必需的认证请求头与浏览器指纹
|
|
*/
|
|
export function getHoseWebSessionHeaders(
|
|
corp: CorpConfig,
|
|
fingerprintOptions?: BrowserFingerprintOptions
|
|
): Record<string, string> {
|
|
const baseHeaders = getBrowserFingerprintHeaders(fingerprintOptions);
|
|
|
|
return {
|
|
...baseHeaders,
|
|
accesstoken: corp.credentials.accessToken,
|
|
corpid: corp.corpId || corp.credentials.corpId,
|
|
staffid: corp.staffId || corp.credentials.staffId,
|
|
};
|
|
}
|
|
|
|
export const getSessionHeaders = getHoseWebSessionHeaders;
|
|
|
|
/**
|
|
* 创建合思 Web 主站用户会话客户端 (Hose Web Session Client)
|
|
* 直连合思主站 (app.ekuaibao.com),承载单据台账、报销审批流、
|
|
* 费用类型科目、员工通讯录等闭环操作,带 Cookie/Token 双通道自动续期与 401 重试。
|
|
*/
|
|
export async function createHoseWebSessionClient(
|
|
corp: CorpConfig,
|
|
options: CreateHoseWebSessionClientOptions = {}
|
|
) {
|
|
const canRefresh = canRefreshCorpAccessToken(corp);
|
|
|
|
// 1. 预检:若 Token 缺失或即将过期 (提前 refreshTtl 秒),静默无感自动续杯
|
|
if (canRefresh) {
|
|
const ttlMs = (corp.auth?.refreshTtl || 300) * 1000;
|
|
const expiresAt = corp.auth?.expiresAt || corp.session?.expTime;
|
|
if (!corp.credentials.accessToken || (expiresAt && Date.now() >= expiresAt - ttlMs)) {
|
|
await refreshSingleCorpAccessToken(corp);
|
|
}
|
|
}
|
|
|
|
// 2. 具备续期能力(主账号或 OpenAPI 持久化密钥)时启用 401 自动重试与换票;临时凭证直接给出可操作错误
|
|
return createAuthClient({
|
|
prefixUrl: corp.baseUrl || "https://app.ekuaibao.com",
|
|
getHeaders: () => getHoseWebSessionHeaders(corp, options.fingerprint),
|
|
timeout: options.timeout,
|
|
retryUnauthorized: canRefresh,
|
|
...(canRefresh
|
|
? {
|
|
onUnauthorized: async () => {
|
|
await refreshSingleCorpAccessToken(corp);
|
|
},
|
|
}
|
|
: {
|
|
onAuthFailure: () =>
|
|
new AuthenticationError("临时凭证已过期,请使用 hose-use corps add-link 重新导入"),
|
|
}),
|
|
});
|
|
}
|
|
|
|
export const createSessionClient = createHoseWebSessionClient;
|