Refactor API response handling across multiple components to ensure consistency with the unified format. Update error handling and response validation in ValidationCard, usePersistentToggle, Status, Prompts, and api.ts to improve user feedback and maintain compatibility with the new API standards.
This commit is contained in:
@@ -47,14 +47,16 @@ export default function ValidationCard({
|
||||
|
||||
try {
|
||||
// Get saved settings to get API key and model
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So settingsData IS the data object (config object)
|
||||
const settingsData = await fetchAPI(`/v1/system/settings/integrations/${integrationId}/`);
|
||||
|
||||
let apiKey = '';
|
||||
let model = 'gpt-4.1';
|
||||
|
||||
if (settingsData.success && settingsData.data) {
|
||||
apiKey = settingsData.data.apiKey || '';
|
||||
model = settingsData.data.model || 'gpt-4.1';
|
||||
if (settingsData && typeof settingsData === 'object') {
|
||||
apiKey = settingsData.apiKey || '';
|
||||
model = settingsData.model || 'gpt-4.1';
|
||||
}
|
||||
|
||||
if (!apiKey) {
|
||||
@@ -79,30 +81,42 @@ export default function ValidationCard({
|
||||
};
|
||||
}
|
||||
|
||||
// Test endpoint returns Response({success: True, ...}) directly (not unified format)
|
||||
// So fetchAPI may or may not extract it - handle both cases
|
||||
const data = await fetchAPI(`/v1/system/settings/integrations/${integrationId}/test/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
if (data.success) {
|
||||
setTestResult({
|
||||
success: true,
|
||||
message: data.message || 'API connection successful!',
|
||||
model_used: data.model_used || data.model,
|
||||
response: data.response,
|
||||
tokens_used: data.tokens_used,
|
||||
total_tokens: data.total_tokens,
|
||||
cost: data.cost,
|
||||
full_response: data.full_response || {
|
||||
image_url: data.image_url,
|
||||
provider: data.provider,
|
||||
size: data.size,
|
||||
},
|
||||
});
|
||||
// Check if data has success field (direct Response format) or is extracted data
|
||||
if (data && typeof data === 'object' && ('success' in data ? data.success : true)) {
|
||||
// If data has success field, use it; otherwise assume success (extracted data)
|
||||
const isSuccess = data.success !== false;
|
||||
if (isSuccess) {
|
||||
setTestResult({
|
||||
success: true,
|
||||
message: data.message || 'API connection successful!',
|
||||
model_used: data.model_used || data.model,
|
||||
response: data.response,
|
||||
tokens_used: data.tokens_used,
|
||||
total_tokens: data.total_tokens,
|
||||
cost: data.cost,
|
||||
full_response: data.full_response || {
|
||||
image_url: data.image_url,
|
||||
provider: data.provider,
|
||||
size: data.size,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
setTestResult({
|
||||
success: false,
|
||||
message: data.error || data.message || 'API connection failed',
|
||||
});
|
||||
}
|
||||
} else {
|
||||
setTestResult({
|
||||
success: false,
|
||||
message: data.error || data.message || 'API connection failed',
|
||||
message: 'Invalid response format',
|
||||
});
|
||||
}
|
||||
} catch (error: any) {
|
||||
|
||||
Reference in New Issue
Block a user