Skip to content
Clone
2f11d69
32 lines 881 B
/**
* Transport envelope shared by feature slices that call the generated Alova
* client. Mirrors the backend convention:
* `code == HTTP status`, payload in `data`, detail in `error`.
*/
export interface ApiEnvelope<T> {
code: number
message: string
data?: T | null
error?: string | null
error_code?: string | null
}
export class ApiFailure extends Error {
readonly code: number
readonly errorCode?: string | null
constructor(message: string, code: number, errorCode?: string | null) {
super(message)
this.name = 'ApiFailure'
this.code = code
this.errorCode = errorCode
}
}
export async function unwrapApi<T>(method: PromiseLike<ApiEnvelope<T>>): Promise<T> {
const res = await method
if (res.code !== 200) {
throw new ApiFailure(res.error || res.message || 'request failed', res.code, res.error_code)
}
return res.data as T
}