环境搭建配置
This commit is contained in:
@@ -6,26 +6,36 @@ import axios, {
|
||||
type InternalAxiosRequestConfig,
|
||||
} from 'axios'
|
||||
|
||||
export interface ApiSuccess<T> {
|
||||
export interface LegacyApiSuccess<T> {
|
||||
success: true
|
||||
msg?: string
|
||||
data?: T
|
||||
}
|
||||
|
||||
export interface ApiFailure {
|
||||
export interface LegacyApiFailure {
|
||||
success: false
|
||||
error?: string
|
||||
}
|
||||
|
||||
export type ApiResponse<T> = ApiSuccess<T> | ApiFailure
|
||||
export type ApiResponse<T> = LegacyApiSuccess<T> | LegacyApiFailure
|
||||
export interface JavaApiResponse<T> {
|
||||
success: boolean
|
||||
message: string
|
||||
data: T | null
|
||||
}
|
||||
export type RequestOptions<D = unknown> = AxiosRequestConfig<D>
|
||||
|
||||
function extractErrorMessage(error: unknown) {
|
||||
if (error instanceof AxiosError) {
|
||||
const data = error.response?.data
|
||||
|
||||
if (data && typeof data === 'object' && 'error' in data) {
|
||||
return String(data.error)
|
||||
if (data && typeof data === 'object') {
|
||||
if ('error' in data && data.error) {
|
||||
return String(data.error)
|
||||
}
|
||||
if ('message' in data && data.message) {
|
||||
return String(data.message)
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof data === 'string' && data.trim()) {
|
||||
@@ -105,3 +115,11 @@ export const requestGetJson = get
|
||||
export const requestPostJson = post
|
||||
export const requestPutJson = put
|
||||
export const requestDeleteJson = del
|
||||
|
||||
export async function unwrapJavaResponse<T>(promise: Promise<JavaApiResponse<T>>) {
|
||||
const response = await promise
|
||||
if (!response.success) {
|
||||
throw new Error(response.message || '请求失败')
|
||||
}
|
||||
return response.data as T
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user