mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-13 10:26:38 +08:00
migrate frontend
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import apiClient from '@/lib/services/core/api-client';
|
||||
import {ApiErrorBase} from '@/lib/services/core/errors';
|
||||
import type {InternalAxiosRequestConfig} from 'axios';
|
||||
|
||||
export interface LegacyApiResponse<T> {
|
||||
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 {
|
||||
return `${this.basePath}${path}`;
|
||||
}
|
||||
|
||||
protected static parseLegacyResponse<T>(body: LegacyApiResponse<T>): T {
|
||||
if (!body.success) {
|
||||
throw new ApiErrorBase(body.message || '请求失败');
|
||||
}
|
||||
return body.data;
|
||||
}
|
||||
|
||||
protected static async legacyGet<T>(
|
||||
path: string,
|
||||
params?: Record<string, unknown>,
|
||||
config?: InternalAxiosRequestConfig,
|
||||
): Promise<T> {
|
||||
const response = await apiClient.get<LegacyApiResponse<T>>(
|
||||
this.getFullPath(path),
|
||||
{ ...config, params } as InternalAxiosRequestConfig,
|
||||
);
|
||||
return this.parseLegacyResponse(response.data);
|
||||
}
|
||||
|
||||
protected static async legacyPost<T>(
|
||||
path: string,
|
||||
data?: unknown,
|
||||
config?: InternalAxiosRequestConfig,
|
||||
): Promise<T> {
|
||||
const response = await apiClient.post<LegacyApiResponse<T>>(
|
||||
this.getFullPath(path),
|
||||
data,
|
||||
config,
|
||||
);
|
||||
return this.parseLegacyResponse(response.data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user