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:
@@ -1061,11 +1061,28 @@ export async function autoGenerateImages(taskIds: number[]): Promise<{ success:
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateImagePrompts(contentIds: number[]): Promise<any> {
|
||||
return fetchAPI('/v1/writer/content/generate_image_prompts/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ ids: contentIds }),
|
||||
});
|
||||
export async function generateImagePrompts(contentIds: number[]): Promise<{ success: boolean; task_id?: string; message?: string; error?: string }> {
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So response is already the data object: {task_id: "...", ...}
|
||||
const response = await fetchAPI('/v1/writer/content/generate_image_prompts/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ ids: contentIds }),
|
||||
});
|
||||
|
||||
// Wrap extracted data with success: true for frontend compatibility
|
||||
if (response && typeof response === 'object') {
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
|
||||
return { success: true, ...response } as any;
|
||||
} catch (error: any) {
|
||||
// Error responses are thrown by fetchAPI, but wrap them for consistency
|
||||
if (error.response && typeof error.response === 'object') {
|
||||
return { success: false, error: error.message, ...error.response } as any;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// TaskImages API functions
|
||||
@@ -1210,14 +1227,31 @@ export async function bulkUpdateImagesStatus(contentId: number, status: string):
|
||||
});
|
||||
}
|
||||
|
||||
export async function generateImages(imageIds: number[], contentId?: number): Promise<any> {
|
||||
return fetchAPI('/v1/writer/images/generate_images/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
ids: imageIds,
|
||||
content_id: contentId
|
||||
}),
|
||||
});
|
||||
export async function generateImages(imageIds: number[], contentId?: number): Promise<{ success: boolean; task_id?: string; message?: string; error?: string }> {
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So response is already the data object: {task_id: "...", ...}
|
||||
const response = await fetchAPI('/v1/writer/images/generate_images/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
ids: imageIds,
|
||||
content_id: contentId
|
||||
}),
|
||||
});
|
||||
|
||||
// Wrap extracted data with success: true for frontend compatibility
|
||||
if (response && typeof response === 'object') {
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
|
||||
return { success: true, ...response } as any;
|
||||
} catch (error: any) {
|
||||
// Error responses are thrown by fetchAPI, but wrap them for consistency
|
||||
if (error.response && typeof error.response === 'object') {
|
||||
return { success: false, error: error.message, ...error.response } as any;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchImages(filters: ImageFilters = {}): Promise<ImageListResponse> {
|
||||
@@ -1451,8 +1485,10 @@ export interface ModuleSetting {
|
||||
}
|
||||
|
||||
export async function fetchModuleSettings(moduleName: string): Promise<ModuleSetting[]> {
|
||||
// fetchAPI extracts data from unified format {success: true, data: [...]}
|
||||
// So response IS the array, not an object with results
|
||||
const response = await fetchAPI(`/v1/system/settings/modules/module/${moduleName}/`);
|
||||
return response.results || [];
|
||||
return Array.isArray(response) ? response : [];
|
||||
}
|
||||
|
||||
export async function createModuleSetting(data: { module_name: string; key: string; config: Record<string, any>; is_active?: boolean }): Promise<ModuleSetting> {
|
||||
@@ -1571,12 +1607,11 @@ export interface UsageLimitsResponse {
|
||||
export async function fetchUsageLimits(): Promise<UsageLimitsResponse> {
|
||||
console.log('Fetching usage limits from:', '/v1/billing/credits/usage/limits/');
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: { limits: [...] }}
|
||||
// So response IS the data object
|
||||
const response = await fetchAPI('/v1/billing/credits/usage/limits/');
|
||||
console.log('Usage limits API response:', response);
|
||||
// Extract data field from unified API response format
|
||||
// Response format: { success: true, data: { limits: [...] }, request_id: "..." }
|
||||
const limitsData = response?.data || response;
|
||||
return limitsData;
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Error fetching usage limits:', error);
|
||||
throw error;
|
||||
@@ -1670,14 +1705,31 @@ export async function fetchSeedKeywords(filters?: {
|
||||
* Add SeedKeywords to workflow (create Keywords records)
|
||||
*/
|
||||
export async function addSeedKeywordsToWorkflow(seedKeywordIds: number[], siteId: number, sectorId: number): Promise<{ success: boolean; created: number; errors?: string[] }> {
|
||||
return fetchAPI('/v1/planner/keywords/bulk_add_from_seed/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
seed_keyword_ids: seedKeywordIds,
|
||||
site_id: siteId,
|
||||
sector_id: sectorId,
|
||||
}),
|
||||
});
|
||||
try {
|
||||
// fetchAPI extracts data from unified format {success: true, data: {...}}
|
||||
// So response is already the data object: {created: X, ...}
|
||||
const response = await fetchAPI('/v1/planner/keywords/bulk_add_from_seed/', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
seed_keyword_ids: seedKeywordIds,
|
||||
site_id: siteId,
|
||||
sector_id: sectorId,
|
||||
}),
|
||||
});
|
||||
|
||||
// Wrap extracted data with success: true for frontend compatibility
|
||||
if (response && typeof response === 'object') {
|
||||
return { success: true, ...response } as any;
|
||||
}
|
||||
|
||||
return { success: true, ...response } as any;
|
||||
} catch (error: any) {
|
||||
// Error responses are thrown by fetchAPI, but wrap them for consistency
|
||||
if (error.response && typeof error.response === 'object') {
|
||||
return { success: false, error: error.message, ...error.response } as any;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Author Profiles API
|
||||
|
||||
Reference in New Issue
Block a user