website cleanup
This commit is contained in:
@@ -9,7 +9,7 @@ import { Logger } from '../../interfaces/Logger';
|
||||
import { ErrorReporter } from '../../interfaces/ErrorReporter';
|
||||
|
||||
export class BaseApiClient {
|
||||
private baseUrl: string;
|
||||
protected baseUrl: string;
|
||||
private errorReporter: ErrorReporter;
|
||||
private logger: Logger;
|
||||
|
||||
@@ -19,12 +19,16 @@ export class BaseApiClient {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
protected async request<T>(method: string, path: string, data?: object): Promise<T> {
|
||||
protected async request<T>(method: string, path: string, data?: object | FormData): Promise<T> {
|
||||
this.logger.info(`${method} ${path}`);
|
||||
|
||||
const headers: HeadersInit = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
const isFormData = typeof FormData !== 'undefined' && data instanceof FormData;
|
||||
|
||||
const headers: HeadersInit = isFormData
|
||||
? {}
|
||||
: {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
const config: RequestInit = {
|
||||
method,
|
||||
@@ -33,7 +37,7 @@ export class BaseApiClient {
|
||||
};
|
||||
|
||||
if (data) {
|
||||
config.body = JSON.stringify(data);
|
||||
config.body = isFormData ? data : JSON.stringify(data);
|
||||
}
|
||||
|
||||
const response = await fetch(`${this.baseUrl}${path}`, config);
|
||||
@@ -45,7 +49,10 @@ export class BaseApiClient {
|
||||
} catch {
|
||||
// Keep default error message
|
||||
}
|
||||
const error = new Error(errorData.message || `API request failed with status ${response.status}`);
|
||||
const error = new Error(
|
||||
errorData.message || `API request failed with status ${response.status}`,
|
||||
) as Error & { status?: number };
|
||||
error.status = response.status;
|
||||
this.errorReporter.report(error);
|
||||
throw error;
|
||||
}
|
||||
@@ -76,4 +83,4 @@ export class BaseApiClient {
|
||||
protected patch<T>(path: string, data: object): Promise<T> {
|
||||
return this.request<T>('PATCH', path, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user