import apiClient from '@/lib/services/core/api-client'; import {ApiErrorBase} from '@/lib/services/core/errors'; import type {InternalAxiosRequestConfig} from 'axios'; export interface LegacyApiResponse { success: boolean; message: string; data: T; } /** * OpenFlare legacy 业务 API 基类 * 解析 { success, message, data } 响应格式 */ export class LegacyOpenFlareBaseService { protected static readonly basePath: string = ''; protected static getFullPath(path: string): string { const full = `${this.basePath}${path}`; // Avoid Next.js 308 (strip slash) <-> Gin 301 (add slash) redirect loops in dev proxy. if (full.length > 1 && full.endsWith('/')) { return full.slice(0, -1); } return full; } protected static parseLegacyResponse(body: LegacyApiResponse): T { if (!body.success) { throw new ApiErrorBase(body.message || '请求失败'); } return body.data; } protected static async legacyGet( path: string, params?: Record, config?: InternalAxiosRequestConfig, ): Promise { const response = await apiClient.get>( this.getFullPath(path), { ...config, params } as InternalAxiosRequestConfig, ); return this.parseLegacyResponse(response.data); } protected static async legacyPost( path: string, data?: unknown, config?: InternalAxiosRequestConfig, ): Promise { const response = await apiClient.post>( this.getFullPath(path), data, config, ); return this.parseLegacyResponse(response.data); } }