封装评查点的相关接口,完成评查点列表的简单搜索和查询
This commit is contained in:
+204
-13
@@ -12,7 +12,147 @@ export interface PostgrestParams {
|
||||
offset?: number;
|
||||
filter?: Record<string, unknown>;
|
||||
schema?: string; // 指定 PostgreSQL schema
|
||||
or?: Array<Record<string, unknown>> | string; // 支持OR条件查询
|
||||
[key: string]: unknown; // 允许添加其他参数
|
||||
headers?: Record<string, string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印 PostgREST 查询日志
|
||||
* @param endpoint 端点
|
||||
* @param params 参数
|
||||
*/
|
||||
function logPostgrestQuery(endpoint: string, params?: QueryParams): void {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
const baseUrl = 'http://172.16.0.119:9000/admin';
|
||||
|
||||
console.log('\n📦 PostgREST 查询日志 ========================');
|
||||
console.log(`📦 API 端点: ${baseUrl}/${endpoint}`);
|
||||
|
||||
if (params && Object.keys(params).length > 0) {
|
||||
console.log('📦 查询参数:');
|
||||
|
||||
// 以可读格式单独打印每个参数
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
if (key === 'select' && typeof value === 'string') {
|
||||
// 美化 select 参数,使其看起来像 SQL 查询
|
||||
console.log(` - ${key}:`);
|
||||
const fields = value.replace(/\s+/g, ' ').trim().split(',');
|
||||
fields.forEach(field => {
|
||||
console.log(` ${field.trim()}`);
|
||||
});
|
||||
} else if (key === 'order' && typeof value === 'string') {
|
||||
// 格式化排序参数
|
||||
console.log(` - ${key}: ${value.replace(/\./g, ' ')}`); // 例如:created_at desc
|
||||
} else if (key === 'or' && typeof value === 'string') {
|
||||
// 格式化OR条件
|
||||
console.log(` - ${key}: ${value.replace(/\./g, ' -> ').replace(/,/g, ' 或 ')}`);
|
||||
} else {
|
||||
console.log(` - ${key}: ${JSON.stringify(value)}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 构建人类可读的简化URL
|
||||
const readableQueryString = Object.entries(params)
|
||||
.filter(([, value]) => value !== undefined)
|
||||
.map(([key, value]) => {
|
||||
if (key === 'select' && typeof value === 'string') {
|
||||
// 简化select查询
|
||||
return `${key}=...`;
|
||||
}
|
||||
return `${key}=${value}`;
|
||||
})
|
||||
.join('&');
|
||||
|
||||
console.log(`\n📦 可读URL: ${baseUrl}/${endpoint}${readableQueryString ? '?' + readableQueryString : ''}`);
|
||||
|
||||
// 格式化查询为 PostgreSQL 风格的查询
|
||||
let postgrestQuery = `SELECT `;
|
||||
|
||||
if (params.select && typeof params.select === 'string') {
|
||||
postgrestQuery += params.select.replace(/\s+/g, ' ').trim();
|
||||
} else {
|
||||
postgrestQuery += '*';
|
||||
}
|
||||
|
||||
postgrestQuery += ` FROM ${endpoint}`;
|
||||
|
||||
const conditions: string[] = [];
|
||||
|
||||
// 添加过滤条件
|
||||
if (params.filter) {
|
||||
Object.entries(params.filter).forEach(([key, value]) => {
|
||||
if (value !== undefined && typeof value === 'string') {
|
||||
// 解析 eq.X, neq.X, gt.X 等
|
||||
const parts = value.toString().split('.');
|
||||
if (parts.length >= 2) {
|
||||
const operator = parts[0];
|
||||
const operatorMap: Record<string, string> = {
|
||||
'eq': '=',
|
||||
'neq': '!=',
|
||||
'gt': '>',
|
||||
'gte': '>=',
|
||||
'lt': '<',
|
||||
'lte': '<=',
|
||||
'like': 'LIKE',
|
||||
'ilike': 'ILIKE'
|
||||
};
|
||||
|
||||
const sqlOperator = operatorMap[operator] || operator;
|
||||
const value = parts.slice(1).join('.');
|
||||
conditions.push(`${key} ${sqlOperator} '${value}'`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 添加 OR 条件
|
||||
if (params.or) {
|
||||
if (typeof params.or === 'string') {
|
||||
if (params.or.startsWith('(') && params.or.endsWith(')')) {
|
||||
// 处理 or=(cond1,cond2) 格式
|
||||
const orConditions = params.or.slice(1, -1).split(',').map(cond => {
|
||||
const [field, operator] = cond.split('.');
|
||||
return `${field} ${operator.replace(/ilike/i, 'ILIKE')}`;
|
||||
});
|
||||
|
||||
if (orConditions.length > 0) {
|
||||
conditions.push(`(${orConditions.join(' OR ')})`);
|
||||
}
|
||||
} else {
|
||||
// 简单 or 条件
|
||||
conditions.push(params.or);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加WHERE子句
|
||||
if (conditions.length > 0) {
|
||||
postgrestQuery += ` WHERE ${conditions.join(' AND ')}`;
|
||||
}
|
||||
|
||||
// 添加ORDER BY
|
||||
if (params.order && typeof params.order === 'string') {
|
||||
const [field, direction] = params.order.split('.');
|
||||
postgrestQuery += ` ORDER BY ${field} ${direction.toUpperCase()}`;
|
||||
}
|
||||
|
||||
// 添加LIMIT和OFFSET
|
||||
if (params.limit !== undefined) {
|
||||
postgrestQuery += ` LIMIT ${params.limit}`;
|
||||
}
|
||||
|
||||
if (params.offset !== undefined) {
|
||||
postgrestQuery += ` OFFSET ${params.offset}`;
|
||||
}
|
||||
|
||||
console.log('\n📦 等效SQL查询:');
|
||||
console.log(postgrestQuery);
|
||||
console.log('=========================================\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,6 +187,30 @@ export function transformParams(params: PostgrestParams): QueryParams {
|
||||
result.schema = params.schema;
|
||||
}
|
||||
|
||||
// 处理或条件 (OR) - 两种格式支持
|
||||
if (params.or) {
|
||||
// 如果是字符串格式 (例如 "name.ilike.*keyword*,code.ilike.*keyword*")
|
||||
if (typeof params.or === 'string') {
|
||||
result.or = params.or;
|
||||
}
|
||||
// 如果是数组格式, 转换为 PostgREST 的 or=(condition1,condition2) 格式
|
||||
else if (Array.isArray(params.or)) {
|
||||
const orConditions: string[] = [];
|
||||
|
||||
params.or.forEach(condition => {
|
||||
const entries = Object.entries(condition);
|
||||
if (entries.length === 1) {
|
||||
const [field, operator] = entries[0];
|
||||
orConditions.push(`${field}.${operator}`);
|
||||
}
|
||||
});
|
||||
|
||||
if (orConditions.length > 0) {
|
||||
result.or = `(${orConditions.join(',')})`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理过滤条件 - PostgresREST 格式
|
||||
if (params.filter) {
|
||||
Object.entries(params.filter).forEach(([key, value]) => {
|
||||
@@ -61,7 +225,7 @@ export function transformParams(params: PostgrestParams): QueryParams {
|
||||
// 处理其他额外参数
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
// 跳过已处理的特殊参数
|
||||
if (!['select', 'order', 'limit', 'offset', 'filter', 'schema'].includes(key) && value !== undefined) {
|
||||
if (!['select', 'order', 'limit', 'offset', 'filter', 'schema', 'or'].includes(key) && value !== undefined) {
|
||||
result[key] = value as string | number | boolean;
|
||||
}
|
||||
});
|
||||
@@ -75,15 +239,29 @@ export function transformParams(params: PostgrestParams): QueryParams {
|
||||
* @param params 查询参数
|
||||
* @returns 响应数据
|
||||
*/
|
||||
export async function postgrestGet<T>(endpoint: string, params?: PostgrestParams): Promise<{data: T; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
export async function postgrestGet<T>(endpoint: string, params?: PostgrestParams): Promise<{data: T; headers?: Record<string, string>; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
const queryParams = params ? transformParams(params) : {};
|
||||
// 添加前缀表示使用 docauditai 数据库
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
|
||||
// 确保端点没有前导斜杠,因为API_BASE_URL已经包含了路径前缀
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
|
||||
|
||||
// 打印查询信息
|
||||
logPostgrestQuery(apiEndpoint, queryParams);
|
||||
|
||||
// 提取并移除自定义头部参数
|
||||
const headers: Record<string, string> = params?.headers || {};
|
||||
|
||||
// 清除查询参数中的headers属性,避免将其作为URL参数
|
||||
if (queryParams.headers) {
|
||||
delete queryParams.headers;
|
||||
}
|
||||
|
||||
const response = await apiRequest<T>(
|
||||
apiEndpoint,
|
||||
{ method: 'GET' },
|
||||
{
|
||||
method: 'GET',
|
||||
headers: headers
|
||||
},
|
||||
queryParams
|
||||
);
|
||||
|
||||
@@ -91,7 +269,11 @@ export async function postgrestGet<T>(endpoint: string, params?: PostgrestParams
|
||||
throw new Error(response.error);
|
||||
}
|
||||
|
||||
return { data: response.data as T };
|
||||
// 返回数据和响应头
|
||||
return {
|
||||
data: response.data as T,
|
||||
headers: response.headers // 假设apiRequest函数已返回响应头
|
||||
};
|
||||
} catch (error) {
|
||||
const apiError = handleApiError(error);
|
||||
return { error: apiError.message, status: apiError.status };
|
||||
@@ -106,8 +288,11 @@ export async function postgrestGet<T>(endpoint: string, params?: PostgrestParams
|
||||
*/
|
||||
export async function postgrestPost<T, D = Record<string, unknown>>(endpoint: string, data: D): Promise<{data: T; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
// 添加前缀表示使用 docauditai 数据库
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
|
||||
// 确保端点没有前导斜杠
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
|
||||
|
||||
// 打印查询信息(POST请求只打印端点)
|
||||
logPostgrestQuery(apiEndpoint);
|
||||
|
||||
const response = await apiRequest<T>(
|
||||
apiEndpoint,
|
||||
@@ -139,13 +324,16 @@ export async function postgrestPost<T, D = Record<string, unknown>>(endpoint: st
|
||||
*/
|
||||
export async function postgrestPut<T, D = Record<string, unknown>>(endpoint: string, data: D): Promise<{data: T; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
// 添加前缀表示使用 docauditai 数据库
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
|
||||
// 确保端点没有前导斜杠
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
|
||||
|
||||
// 打印查询信息(PUT请求只打印端点)
|
||||
logPostgrestQuery(apiEndpoint);
|
||||
|
||||
const response = await apiRequest<T>(
|
||||
apiEndpoint,
|
||||
{
|
||||
method: 'PUT',
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(data),
|
||||
headers: {
|
||||
'Prefer': 'return=representation'
|
||||
@@ -171,8 +359,11 @@ export async function postgrestPut<T, D = Record<string, unknown>>(endpoint: str
|
||||
*/
|
||||
export async function postgrestDelete<T>(endpoint: string): Promise<{data: T; error?: never} | {data?: never; error: string; status?: number}> {
|
||||
try {
|
||||
// 添加前缀表示使用 docauditai 数据库
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
|
||||
// 确保端点没有前导斜杠
|
||||
const apiEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
|
||||
|
||||
// 打印查询信息(DELETE请求只打印端点)
|
||||
logPostgrestQuery(apiEndpoint);
|
||||
|
||||
const response = await apiRequest<T>(
|
||||
apiEndpoint,
|
||||
|
||||
Reference in New Issue
Block a user