Implement unified API standard across backend viewsets and serializers, enhancing error handling and response formatting. Update AccountModelViewSet to standardize CRUD operations with success and error responses. Refactor various viewsets to inherit from AccountModelViewSet, ensuring compliance with the new standard. Improve frontend components to handle API responses consistently and update configuration for better user experience.
This commit is contained in:
@@ -640,18 +640,33 @@ export async function autoClusterKeywords(keywordIds: number[], sectorId?: numbe
|
||||
const requestBody = { ids: keywordIds, sector_id: sectorId };
|
||||
|
||||
try {
|
||||
// fetchAPI will automatically extract data from unified format
|
||||
// For action endpoints, response is {success: true, data: {...}}
|
||||
// fetchAPI extracts and returns the data field, so response should already be the data object
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
// Check if response indicates an error (success: false)
|
||||
if (response && response.success === false) {
|
||||
// Return error response as-is so caller can check result.success
|
||||
return response;
|
||||
// After fetchAPI processing, response should be the data object (not wrapped in success/data)
|
||||
// But check if it's still wrapped (shouldn't happen, but for safety)
|
||||
if (response && typeof response === 'object') {
|
||||
if ('success' in response && response.success === false) {
|
||||
// Error response - return as-is
|
||||
return response as any;
|
||||
}
|
||||
// If response has data field, extract it
|
||||
if ('data' in response && response.data) {
|
||||
return { success: true, ...response.data } as any;
|
||||
}
|
||||
// Response is already the data object (after fetchAPI extraction)
|
||||
// Ensure it has success: true
|
||||
if (!('success' in response)) {
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
}
|
||||
|
||||
return response;
|
||||
return response as any;
|
||||
} catch (error: any) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user