feat:完成dify知识库文档基础CRUD模块
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Dify Dataset 服务端 API 模块
|
||||
*
|
||||
* 提供 Node.js 服务端调用 FastAPI 后端的基础功能
|
||||
* Dify 的 DATASET_API_KEY 由 FastAPI 后端管理,前端只负责转发请求
|
||||
*
|
||||
* 调用链路:
|
||||
* Remix Server → FastAPI /dify_dataset/* → Dify Knowledge API
|
||||
*
|
||||
* @module api/dify-dataset/client.server
|
||||
*/
|
||||
|
||||
import { API_BASE_URL } from '~/config/api-config';
|
||||
|
||||
// ============================================================================
|
||||
// 配置
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Dify Dataset API 代理地址
|
||||
* 通过 FastAPI 后端的 /dify_dataset 路由代理访问 Dify Knowledge API
|
||||
* Dify 的认证(DATASET_API_KEY)由 FastAPI 后端处理
|
||||
*/
|
||||
const DIFY_DATASET_API_URL = `${API_BASE_URL}/dify_dataset`;
|
||||
|
||||
// ============================================================================
|
||||
// 基础请求函数
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Dify Dataset API 基础请求函数
|
||||
*
|
||||
* 使用用户 JWT 认证通过 FastAPI 代理访问 Dify Knowledge API
|
||||
* FastAPI 后端会验证 JWT 并添加 Dify DATASET_API_KEY
|
||||
*
|
||||
* @param endpoint - API 端点路径
|
||||
* @param options - fetch 请求选项
|
||||
* @param jwt - 用户 JWT 认证令牌
|
||||
* @returns Response 对象
|
||||
*/
|
||||
export async function difyDatasetFetch(
|
||||
endpoint: string,
|
||||
options: RequestInit = {},
|
||||
jwt?: string
|
||||
): Promise<Response> {
|
||||
const url = `${DIFY_DATASET_API_URL}/${endpoint.replace(/^\//, '')}`;
|
||||
|
||||
const headers: HeadersInit = {
|
||||
...options.headers,
|
||||
};
|
||||
|
||||
// 如果不是 FormData,设置 Content-Type 为 JSON
|
||||
if (!(options.body instanceof FormData)) {
|
||||
(headers as Record<string, string>)['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
if (jwt) {
|
||||
(headers as Record<string, string>)['Authorization'] = `Bearer ${jwt}`;
|
||||
} else {
|
||||
console.warn('[Dify Dataset] 没有提供 JWT,FastAPI 请求可能失败');
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
console.error('[Dify Dataset] API 转发错误:', {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
error: errorText
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
throw new Error('JWT认证失败,请重新登录');
|
||||
}
|
||||
|
||||
throw new Error(`Dify Dataset API Error: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
Reference in New Issue
Block a user