封装评查点列表的接口,完成删除和查找

This commit is contained in:
2025-04-03 19:59:57 +08:00
parent 2bde2bd76e
commit 145aec6aa6
5 changed files with 959 additions and 342 deletions
+23 -8
View File
@@ -24,7 +24,8 @@ export interface PostgrestParams {
*/
function logPostgrestQuery(endpoint: string, params?: QueryParams): void {
if (process.env.NODE_ENV !== 'production') {
const baseUrl = 'http://172.16.0.119:9000/admin';
// const baseUrl = 'http://172.16.0.119:9000/admin';
const baseUrl = 'http://172.18.0.100:3000';
console.log('\n📦 PostgREST 查询日志 ========================');
console.log(`📦 API 端点: ${baseUrl}/${endpoint}`);
@@ -355,24 +356,38 @@ export async function postgrestPut<T, D = Record<string, unknown>>(endpoint: str
/**
* 发送 DELETE 请求到 PostgresREST 接口
* @param endpoint 端点
* @param params 查询参数,用于指定要删除的记录
* @returns 响应数据
*/
export async function postgrestDelete<T>(endpoint: string): Promise<{data: T; error?: never} | {data?: never; error: string; status?: number}> {
export async function postgrestDelete<T>(endpoint: string, params?: PostgrestParams): Promise<{data: T; error?: never} | {data?: never; error: string; status?: number}> {
try {
// 确保端点没有前导斜杠
const apiEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
// 打印查询信息(DELETE请求只打印端点)
logPostgrestQuery(apiEndpoint);
// 转换查询参数
const queryParams = params ? transformParams(params) : {};
// 提取并移除自定义头部参数
const headers: Record<string, string> = {
'Prefer': 'return=representation', // 默认请求返回被删除的记录
...(params?.headers || {})
};
// 清除查询参数中的headers属性,避免将其作为URL参数
if (queryParams.headers) {
delete queryParams.headers;
}
// 打印查询信息
logPostgrestQuery(apiEndpoint, queryParams);
const response = await apiRequest<T>(
apiEndpoint,
{
method: 'DELETE',
headers: {
'Prefer': 'return=representation'
}
}
headers
},
queryParams
);
if (response.error) {