This commit is contained in:
@@ -5,6 +5,32 @@ import {
|
||||
SearchOrganizationDto,
|
||||
} from "@/types/dto/organization/organization.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 organizationService = {
|
||||
/**
|
||||
* Get all organizations (supports filtering by projectId)
|
||||
@@ -12,11 +38,7 @@ export const organizationService = {
|
||||
*/
|
||||
getAll: async (params?: SearchOrganizationDto) => {
|
||||
const response = await apiClient.get("/organizations", { params });
|
||||
// Normalize response if wrapped in data.data or direct data
|
||||
if (response.data && Array.isArray(response.data.data)) {
|
||||
return response.data.data;
|
||||
}
|
||||
return response.data.data || response.data;
|
||||
return unwrapArrayResponse(response.data);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user