website refactor
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { DashboardApiClient } from '@/lib/api/dashboard/DashboardApiClient';
|
||||
import { DashboardOverviewDTO } from '@/lib/types/generated/DashboardOverviewDTO';
|
||||
import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError } from '@/lib/contracts/services/Service';
|
||||
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
import { ConsoleErrorReporter } from '@/lib/infrastructure/logging/ConsoleErrorReporter';
|
||||
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
import { ApiError } from '@/lib/api/base/ApiError';
|
||||
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
||||
|
||||
/**
|
||||
* DashboardService
|
||||
@@ -11,11 +13,11 @@ import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
||||
* Pure service that creates its own API client and returns Result types.
|
||||
* No business logic, only data fetching and error mapping.
|
||||
*/
|
||||
export class DashboardService {
|
||||
export class DashboardService implements Service {
|
||||
private apiClient: DashboardApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = process.env.NEXT_PUBLIC_API_URL || '';
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const errorReporter = new ConsoleErrorReporter();
|
||||
const logger = new ConsoleLogger();
|
||||
this.apiClient = new DashboardApiClient(baseUrl, errorReporter, logger);
|
||||
@@ -26,18 +28,26 @@ export class DashboardService {
|
||||
const dto = await this.apiClient.getDashboardOverview();
|
||||
return Result.ok(dto);
|
||||
} catch (error) {
|
||||
console.error('DashboardService.getDashboardOverview failed:', error);
|
||||
// Convert ApiError to DomainError
|
||||
if (error instanceof ApiError) {
|
||||
switch (error.type) {
|
||||
case 'NOT_FOUND':
|
||||
return Result.err({ type: 'notFound', message: error.message });
|
||||
case 'AUTH_ERROR':
|
||||
return Result.err({ type: 'unauthorized', message: error.message });
|
||||
case 'SERVER_ERROR':
|
||||
return Result.err({ type: 'serverError', message: error.message });
|
||||
case 'NETWORK_ERROR':
|
||||
case 'TIMEOUT_ERROR':
|
||||
return Result.err({ type: 'networkError', message: error.message });
|
||||
default:
|
||||
return Result.err({ type: 'unknown', message: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
// Handle non-ApiError cases
|
||||
if (error instanceof Error) {
|
||||
if (error.message.includes('403') || error.message.includes('401')) {
|
||||
return Result.err({ type: 'unauthorized', message: error.message });
|
||||
}
|
||||
if (error.message.includes('404')) {
|
||||
return Result.err({ type: 'notFound', message: error.message });
|
||||
}
|
||||
if (error.message.includes('5') || error.message.includes('server')) {
|
||||
return Result.err({ type: 'serverError', message: error.message });
|
||||
}
|
||||
return Result.err({ type: 'unknown', message: error.message });
|
||||
}
|
||||
|
||||
return Result.err({ type: 'unknown', message: 'Dashboard fetch failed' });
|
||||
|
||||
Reference in New Issue
Block a user