删除client文件,添加部分progrest的console输入内容
This commit is contained in:
+53
-27
@@ -6,14 +6,22 @@ import { handleApiError } from './error-handler';
|
||||
* PostgresREST 特定的查询参数接口
|
||||
*/
|
||||
export interface PostgrestParams {
|
||||
// 查询参数
|
||||
select?: string;
|
||||
// 排序参数
|
||||
order?: string;
|
||||
// 分页参数
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
// 过滤参数
|
||||
filter?: Record<string, unknown>;
|
||||
schema?: string; // 指定 PostgreSQL schema
|
||||
or?: Array<Record<string, unknown>> | string; // 支持OR条件查询
|
||||
[key: string]: unknown; // 允许添加其他参数
|
||||
// 指定 PostgreSQL schema
|
||||
schema?: string;
|
||||
// 支持OR条件查询
|
||||
or?: Array<Record<string, unknown>> | string;
|
||||
// 其他参数
|
||||
[key: string]: unknown;
|
||||
// 自定义头部参数
|
||||
headers?: Record<string, string>;
|
||||
}
|
||||
|
||||
@@ -45,8 +53,9 @@ function decodeUrlForDisplay(url: string): string {
|
||||
* @param endpoint 端点
|
||||
* @param params 参数
|
||||
* @param method HTTP 方法
|
||||
* @param data 请求体数据(用于POST/PATCH请求)
|
||||
*/
|
||||
function logPostgrestQuery(endpoint: string, params?: QueryParams, method: string = 'GET'): void {
|
||||
function logPostgrestQuery(endpoint: string, params?: QueryParams, method: string = 'GET', data?: Record<string, unknown>): void {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
// const baseUrl = 'http://172.16.0.119:9000/admin';
|
||||
// const baseUrl = 'http://172.18.0.100:3000';
|
||||
@@ -55,22 +64,37 @@ function logPostgrestQuery(endpoint: string, params?: QueryParams, method: strin
|
||||
// 确保 endpoint 格式正确
|
||||
const normalizedEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
|
||||
|
||||
// console.log('\n📦 PostgREST 查询日志 ======================start=============');
|
||||
// console.log(`📦 HTTP 方法: ${method}`);
|
||||
// console.log(`📦 API 端点: ${decodeUrlForDisplay(`${baseUrl}/${normalizedEndpoint}`)}`);
|
||||
let fullUrl = `${baseUrl}/${normalizedEndpoint}`;
|
||||
|
||||
// if (params && Object.keys(params).length > 0) {
|
||||
// console.log('📦 查询参数:');
|
||||
// 如果有查询参数,将其添加到URL中
|
||||
if (params && Object.keys(params).length > 0) {
|
||||
const queryString = Object.entries(params)
|
||||
.filter(([, value]) => value !== undefined)
|
||||
.map(([key, value]) => {
|
||||
// 处理特殊的PostgREST操作符(如eq, gt, lt等)
|
||||
if (typeof value === 'string' && value.includes('.')) {
|
||||
return `${key}=${encodeURIComponent(value)}`;
|
||||
}
|
||||
return `${key}=${encodeURIComponent(String(value))}`;
|
||||
})
|
||||
.join('&');
|
||||
|
||||
// // 以可读格式单独打印每个参数
|
||||
// Object.entries(params).forEach(([key, value]) => {
|
||||
// if (value !== undefined) {
|
||||
// console.log(` - ${key}: ${JSON.stringify(value)}`);
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
if (queryString) {
|
||||
fullUrl += `?${queryString}`;
|
||||
}
|
||||
}
|
||||
|
||||
// console.log('PostgREST 查询日志=============================end============\n');
|
||||
console.log('\n📦 PostgREST 查询日志 ========================');
|
||||
console.log(`📦 HTTP 方法: ${method}`);
|
||||
console.log(`📦 完整 URL: ${decodeUrlForDisplay(fullUrl)}`);
|
||||
|
||||
// 打印请求体数据(仅适用于POST/PATCH/PUT请求)
|
||||
if (['POST', 'PATCH', 'PUT'].includes(method) && data) {
|
||||
console.log('📦 请求体数据:');
|
||||
console.log(JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
console.log('📦 PostgREST 查询日志 ========================\n');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,12 +321,12 @@ export async function postgrestPost<T, D = Record<string, unknown>>(endpoint: st
|
||||
// 确保端点没有前导斜杠
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
|
||||
|
||||
// 打印查询信息(POST请求只打印端点)
|
||||
logPostgrestQuery(apiEndpoint, undefined, 'POST');
|
||||
|
||||
// 预处理数据,确保所有字段类型符合 PostgreSQL 要求
|
||||
const processedData = preprocessData(data as Record<string, unknown>);
|
||||
|
||||
// 打印查询信息
|
||||
logPostgrestQuery(apiEndpoint, undefined, 'POST', processedData);
|
||||
|
||||
// 确保数据是合法的JSON对象
|
||||
const requestBody = JSON.stringify(processedData);
|
||||
// console.log(`准备发送 PostgreSQL 插入请求到: ${apiEndpoint}`);
|
||||
@@ -422,16 +446,18 @@ export async function postgrestPut<T, D extends object>(
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
|
||||
|
||||
// 构建完整的URL,包含过滤条件
|
||||
let fullEndpoint = apiEndpoint;
|
||||
const fullEndpoint = apiEndpoint;
|
||||
const queryParams: QueryParams = {};
|
||||
|
||||
if (filters) {
|
||||
const filterString = Object.entries(filters)
|
||||
.map(([key, value]) => `${key}=eq.${value}`)
|
||||
.join('&');
|
||||
fullEndpoint = `${apiEndpoint}?${filterString}`;
|
||||
// 将过滤条件转换为PostgREST格式的查询参数
|
||||
Object.entries(filters).forEach(([key, value]) => {
|
||||
queryParams[key] = `eq.${value}`;
|
||||
});
|
||||
}
|
||||
|
||||
// 打印查询信息(PUT请求只打印端点)
|
||||
logPostgrestQuery(fullEndpoint, undefined, 'PATCH');
|
||||
// 打印查询信息
|
||||
logPostgrestQuery(fullEndpoint, queryParams, 'PATCH', data as unknown as Record<string, unknown>);
|
||||
|
||||
const response = await apiRequest<T>(
|
||||
fullEndpoint,
|
||||
|
||||
Reference in New Issue
Block a user