Add admin UI
Some checks failed
Some checks failed
This commit is contained in:
51
frontend/admin/src/api/client.ts
Normal file
51
frontend/admin/src/api/client.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import axios, { AxiosInstance } from 'axios'
|
||||
import type { APIResponse } from '../types/api'
|
||||
|
||||
class APIClient {
|
||||
private client: AxiosInstance
|
||||
|
||||
constructor() {
|
||||
this.client = axios.create({
|
||||
baseURL: '/admin/api/v1',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
// Request interceptor for auth
|
||||
this.client.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('auth_token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
// Response interceptor for error handling
|
||||
this.client.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
console.error('API Error:', error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async get<T>(url: string): Promise<T> {
|
||||
const response = await this.client.get<APIResponse<T>>(url)
|
||||
if (response.data.success && response.data.data) {
|
||||
return response.data.data
|
||||
}
|
||||
throw new Error(response.data.error?.message || 'Unknown error')
|
||||
}
|
||||
|
||||
async post<T>(url: string, data: any): Promise<T> {
|
||||
const response = await this.client.post<APIResponse<T>>(url, data)
|
||||
if (response.data.success && response.data.data) {
|
||||
return response.data.data
|
||||
}
|
||||
throw new Error(response.data.error?.message || 'Unknown error')
|
||||
}
|
||||
}
|
||||
|
||||
export const apiClient = new APIClient()
|
||||
Reference in New Issue
Block a user