Files
ocean-cleanup/src/services/api.ts
Paul Bailey 4ac49d55e6 fix: Configure proper API URLs for production and development
- Add .env.production with production API URL (https://litter-api.psbailey.uk/api)
- Create getApiBaseUrl() helper function for static assets
- Update ReportDetailModal to use dynamic API URL
- Fix default API port to 3005 across all services
- Images will now load correctly in both dev and production
2025-11-18 18:05:25 +00:00

82 lines
2.1 KiB
TypeScript

// API service configuration and base client
import { useAuth } from '@clerk/clerk-react';
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3005/api';
// Export function to get the base URL without /api suffix for static assets
export const getApiBaseUrl = () => {
return (import.meta.env.VITE_API_BASE_URL || 'http://localhost:3005/api').replace('/api', '');
};
export interface ApiError {
code: string;
message: string;
details?: any;
timestamp: string;
}
export class ApiClient {
private baseUrl: string;
private getToken?: () => Promise<string | null>;
constructor(baseUrl: string = API_BASE_URL) {
this.baseUrl = baseUrl;
}
setTokenGetter(getToken: () => Promise<string | null>) {
this.getToken = getToken;
}
private async request<T>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
const url = `${this.baseUrl}${endpoint}`;
// Get auth token if available
const token = this.getToken ? await this.getToken() : null;
const response = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
...options.headers,
},
});
if (!response.ok) {
const error: ApiError = await response.json();
throw new Error(error.message || 'An error occurred');
}
return response.json();
}
async get<T>(endpoint: string, options?: RequestInit): Promise<T> {
return this.request<T>(endpoint, { ...options, method: 'GET' });
}
async post<T>(endpoint: string, data?: any, options?: RequestInit): Promise<T> {
return this.request<T>(endpoint, {
...options,
method: 'POST',
body: JSON.stringify(data),
});
}
async patch<T>(endpoint: string, data?: any, options?: RequestInit): Promise<T> {
return this.request<T>(endpoint, {
...options,
method: 'PATCH',
body: JSON.stringify(data),
});
}
async delete<T>(endpoint: string, options?: RequestInit): Promise<T> {
return this.request<T>(endpoint, { ...options, method: 'DELETE' });
}
}
export const apiClient = new ApiClient();