/** * @file API Bridge 类型定义 — RequestBuilder 接口 + 认证 + 响应处理 * @description 定义 ApiBridgeClient 与请求构建函数之间的通用契约。 * 连接器通过 requestBuilderId 指定使用哪个 builder 构建 fetch 请求参数。 */ /** 构建请求函数的输入上下文 */ export interface BuildRequestContext { /** 工具名称 */ toolName: string; /** 工具调用参数 */ args: Record; /** apiMapping 中该工具的配置项 */ mapping: Record; /** 扩展配置(从 MCPServerConfig.extra 透传) */ extra: Record; } /** 构建请求函数的返回值 — fetch 所需的完整参数 */ export interface FetchRequestOptions { /** 请求 URL */ url: string; /** fetch 初始化选项 */ init: RequestInit; } /** * 请求构建函数 * * 由连接器定义,负责将工具调用参数转换为完整的 fetch 请求。 * ApiBridgeClient 仅作为执行器调用此函数并发送返回的请求。 */ export type RequestBuilderFn = (ctx: BuildRequestContext) => Promise; /** * 认证配置 * * 支持四种认证方式: * - `bearer`: HTTP Bearer Token(Authorization: Bearer xxx) * - `basic`: HTTP Basic Auth(Authorization: Basic base64(user:pass)) * - `apiKey`: 自定义 API Key Header(如 X-API-Key: xxx) * - `custom`: 完全自定义 Header 名和值 * * value 支持 `${env.VAR_NAME}` 模板,运行时从环境变量读取敏感凭证。 */ export interface AuthConfig { /** 认证方式 */ type: 'bearer' | 'basic' | 'apiKey' | 'custom'; /** * 认证凭证值 * - bearer: token 值 * - basic: base64(user:pass) 编码值 * - apiKey: key 值 * - custom: header 值 * 支持 `${env.VAR_NAME}` 模板语法 */ value: string; /** apiKey/custom 模式下的 header 名称(如 X-API-Key) */ name?: string; /** 额外自定义 header 列表 */ extraHeaders?: { name: string; value: string; }[]; } /** * 响应处理配置 * * 控制 ApiBridgeClient 在 fetch 成功后如何加工响应数据: * - `extract`: JSONPath 表达式提取有效数据(丢弃外层包装) * - `errorField` + `errorMessage`: 业务错误码检测 * * 示例: * ```json * { * "extract": "$.data.items", * "errorField": "$.errCode", * "errorMessage": "$.errMsg" * } * ``` * 当返回 `{ "errCode": 1001, "errMsg": "not found", "data": null }` 时, * 自动抛出 `Business error [1001]: not found` */ export interface ResponseConfig { /** JSONPath 表达式,提取有效数据(如 $.data.items),留空则返回完整响应 */ extract?: string; /** * 错误码字段路径(JSONPath 表达式,如 $.errCode) * 当该字段值不为 0/null/undefined 时,视为业务错误并抛出异常 */ errorField?: string; /** 错误消息字段路径(JSONPath 表达式,如 $.errMsg),与 errorField 配合使用 */ errorMessage?: string; /** * 对数组结果逐项套用模板,渲染成可读文本(如把 references[] 变成 markdown)。 * 模板语法复用 ${param},例如 '## ${title}\n${url}\n${content}'。 * 仅当 extract 返回数组时生效;渲染后各项以 join 连接。 */ map?: { template: string; }; /** map 逐项结果的连接符,默认 '\n\n' */ join?: string; /** * 对标量/单对象结果套用模板(extract 返回非数组时生效),如 '${summary}'。 * 留空则按 JSON 返回。 */ template?: string; } //# sourceMappingURL=api-bridge-types.d.ts.map