This commit is contained in:
@@ -6,6 +6,32 @@ import {
|
||||
SearchProjectDto
|
||||
} from "@/types/dto/project/project.dto";
|
||||
|
||||
type ApiEnvelope<T> = {
|
||||
data?: ApiEnvelope<T> | T;
|
||||
message?: string;
|
||||
statusCode?: number;
|
||||
};
|
||||
|
||||
function unwrapApiData<T>(payload: ApiEnvelope<T> | T): ApiEnvelope<T> | T | null {
|
||||
let current: ApiEnvelope<T> | T | null = payload;
|
||||
|
||||
while (
|
||||
current &&
|
||||
typeof current === "object" &&
|
||||
"data" in current &&
|
||||
current.data !== undefined
|
||||
) {
|
||||
current = current.data;
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
function unwrapArrayResponse<T>(payload: ApiEnvelope<T[]> | T[]): T[] {
|
||||
const unwrapped = unwrapApiData(payload);
|
||||
return Array.isArray(unwrapped) ? unwrapped : [];
|
||||
}
|
||||
|
||||
export const projectService = {
|
||||
// --- Basic CRUD ---
|
||||
|
||||
@@ -16,11 +42,7 @@ export const projectService = {
|
||||
getAll: async (params?: SearchProjectDto) => {
|
||||
// GET /projects
|
||||
const response = await apiClient.get("/projects", { params });
|
||||
// Handle paginated response
|
||||
if (response.data && Array.isArray(response.data.data)) {
|
||||
return response.data.data;
|
||||
}
|
||||
return response.data;
|
||||
return unwrapArrayResponse(response.data);
|
||||
},
|
||||
|
||||
/** ดึงรายละเอียดโครงการตาม UUID */
|
||||
|
||||
Reference in New Issue
Block a user