62 lines
2.1 KiB
TypeScript
62 lines
2.1 KiB
TypeScript
import axios from 'axios'
|
|
|
|
const api = axios.create({
|
|
baseURL: '/api',
|
|
withCredentials: true,
|
|
})
|
|
|
|
api.interceptors.response.use(
|
|
(res) => res,
|
|
(err) => {
|
|
if (err.response?.status === 401 && window.location.pathname !== '/login') {
|
|
window.location.href = '/login'
|
|
}
|
|
return Promise.reject(err)
|
|
}
|
|
)
|
|
|
|
export default api
|
|
|
|
// --- Auth ---
|
|
export const authApi = {
|
|
login: (username: string, password: string) =>
|
|
api.post('/auth/login', { username, password }),
|
|
logout: () => api.post('/auth/logout'),
|
|
me: () => api.get('/auth/me'),
|
|
register: (username: string, email: string, password: string) =>
|
|
api.post('/auth/register', { username, email, password }),
|
|
}
|
|
|
|
// --- Configs ---
|
|
export const configsApi = {
|
|
list: () => api.get('/configs'),
|
|
create: (data: object) => api.post('/configs', data),
|
|
get: (id: number) => api.get(`/configs/${id}`),
|
|
update: (id: number, data: object) => api.put(`/configs/${id}`, data),
|
|
delete: (id: number) => api.delete(`/configs/${id}`),
|
|
generate: (id: number, format: 'json' | 'zip' = 'json') =>
|
|
api.post(`/configs/${id}/generate?format=${format}`, null, {
|
|
responseType: format === 'zip' ? 'blob' : 'json',
|
|
}),
|
|
regenerateToken: (id: number) =>
|
|
api.post<{ download_token: string }>(`/configs/${id}/regenerate-token`),
|
|
}
|
|
|
|
// --- Nested resources (zones, interfaces, policies, rules, snat) ---
|
|
const nestedApi = (resource: string) => ({
|
|
list: (configId: number) => api.get(`/configs/${configId}/${resource}`),
|
|
create: (configId: number, data: object) => api.post(`/configs/${configId}/${resource}`, data),
|
|
update: (configId: number, id: number, data: object) =>
|
|
api.put(`/configs/${configId}/${resource}/${id}`, data),
|
|
delete: (configId: number, id: number) =>
|
|
api.delete(`/configs/${configId}/${resource}/${id}`),
|
|
})
|
|
|
|
export const zonesApi = nestedApi('zones')
|
|
export const interfacesApi = nestedApi('interfaces')
|
|
export const policiesApi = nestedApi('policies')
|
|
export const rulesApi = nestedApi('rules')
|
|
export const snatApi = nestedApi('snat')
|
|
export const hostsApi = nestedApi('hosts')
|
|
export const paramsApi = nestedApi('params')
|