dev experience
This commit is contained in:
@@ -10,6 +10,7 @@ import { ErrorReporter } from '../../interfaces/ErrorReporter';
|
||||
import { ApiError, ApiErrorType } from './ApiError';
|
||||
import { RetryHandler, CircuitBreakerRegistry, DEFAULT_RETRY_CONFIG } from './RetryHandler';
|
||||
import { ApiConnectionMonitor } from './ApiConnectionMonitor';
|
||||
import { getGlobalApiLogger } from '@/lib/infrastructure/ApiRequestLogger';
|
||||
|
||||
export interface BaseApiClientOptions {
|
||||
timeout?: number;
|
||||
@@ -214,6 +215,28 @@ export class BaseApiClient {
|
||||
}
|
||||
|
||||
const startTime = Date.now();
|
||||
let requestId: string | undefined;
|
||||
|
||||
// Log request start (only in development for maximum transparency)
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
try {
|
||||
const apiLogger = getGlobalApiLogger();
|
||||
const headerObj: Record<string, string> = {};
|
||||
if (typeof headers === 'object') {
|
||||
Object.entries(headers).forEach(([key, value]) => {
|
||||
headerObj[key] = value;
|
||||
});
|
||||
}
|
||||
requestId = apiLogger.logRequest(
|
||||
endpoint,
|
||||
method,
|
||||
headerObj,
|
||||
data
|
||||
);
|
||||
} catch (e) {
|
||||
// Silent fail - logger might not be initialized
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(endpoint, config);
|
||||
@@ -235,6 +258,17 @@ export class BaseApiClient {
|
||||
circuitBreaker.recordFailure();
|
||||
this.connectionMonitor.recordFailure(error);
|
||||
this.handleError(error);
|
||||
|
||||
// Log error
|
||||
if (process.env.NODE_ENV === 'development' && requestId) {
|
||||
try {
|
||||
const apiLogger = getGlobalApiLogger();
|
||||
apiLogger.logError(requestId, error, responseTime);
|
||||
} catch (e) {
|
||||
// Silent fail
|
||||
}
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -243,9 +277,31 @@ export class BaseApiClient {
|
||||
|
||||
const text = await response.text();
|
||||
if (!text) {
|
||||
// Log empty response
|
||||
if (process.env.NODE_ENV === 'development' && requestId) {
|
||||
try {
|
||||
const apiLogger = getGlobalApiLogger();
|
||||
apiLogger.logResponse(requestId, response, null, responseTime);
|
||||
} catch (e) {
|
||||
// Silent fail
|
||||
}
|
||||
}
|
||||
return null as T;
|
||||
}
|
||||
return JSON.parse(text) as T;
|
||||
|
||||
const parsedData = JSON.parse(text) as T;
|
||||
|
||||
// Log successful response
|
||||
if (process.env.NODE_ENV === 'development' && requestId) {
|
||||
try {
|
||||
const apiLogger = getGlobalApiLogger();
|
||||
apiLogger.logResponse(requestId, response, parsedData, responseTime);
|
||||
} catch (e) {
|
||||
// Silent fail
|
||||
}
|
||||
}
|
||||
|
||||
return parsedData;
|
||||
|
||||
} catch (error) {
|
||||
const responseTime = Date.now() - startTime;
|
||||
@@ -260,6 +316,16 @@ export class BaseApiClient {
|
||||
circuitBreaker.recordFailure();
|
||||
this.connectionMonitor.recordFailure(apiError);
|
||||
this.handleError(apiError);
|
||||
|
||||
// Log network error
|
||||
if (process.env.NODE_ENV === 'development' && requestId) {
|
||||
try {
|
||||
const apiLogger = getGlobalApiLogger();
|
||||
apiLogger.logError(requestId, apiError, responseTime);
|
||||
} catch (e) {
|
||||
// Silent fail
|
||||
}
|
||||
}
|
||||
|
||||
throw apiError;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user