standardize mobile api response parsing with shared helper
This commit is contained in:
1 parent
b1f84722af
commit
ff9f3d582a
5 files changed
+43
-90
No files matched your search
@@ -0,0 +1,14 @@
|
||||
import { ApiError, handleResponse } from "./responses";
|
||||
import { type ApiResponse } from "./types";
|
||||
|
||||
export function parseApiData<T>(payload: unknown): T {
|
||||
if (Array.isArray(payload)) {
|
||||
return payload as T;
|
||||
}
|
||||
|
||||
if (payload && typeof payload === "object" && "success" in payload) {
|
||||
return handleResponse(payload as ApiResponse<T>);
|
||||
}
|
||||
|
||||
throw new ApiError("Invalid response format");
|
||||
}
|
||||
Reference in new issue
Block a user