velxio/frontend/src/services/projectService.ts

60 lines
1.7 KiB
TypeScript

import axios from 'axios';
const API_BASE = import.meta.env.VITE_API_BASE || 'http://localhost:8001/api';
const api = axios.create({ baseURL: API_BASE, withCredentials: true });
export interface ProjectResponse {
id: string;
name: string;
slug: string;
description: string | null;
is_public: boolean;
board_type: string;
code: string;
components_json: string;
wires_json: string;
owner_username: string;
created_at: string;
updated_at: string;
}
export interface ProjectSaveData {
name: string;
description?: string;
is_public: boolean;
board_type: string;
code: string;
components_json: string;
wires_json: string;
}
export async function getMyProjects(): Promise<ProjectResponse[]> {
const { data } = await api.get<ProjectResponse[]>('/projects/me');
return data;
}
export async function getUserProjects(username: string): Promise<ProjectResponse[]> {
const { data } = await api.get<ProjectResponse[]>(`/user/${username}`);
return data;
}
export async function getProject(username: string, slug: string): Promise<ProjectResponse> {
const { data } = await api.get<ProjectResponse>(`/user/${username}/${slug}`);
return data;
}
export async function createProject(data: ProjectSaveData): Promise<ProjectResponse> {
const { data: result } = await api.post<ProjectResponse>('/projects/', data);
return result;
}
export async function updateProject(id: string, data: Partial<ProjectSaveData>): Promise<ProjectResponse> {
const { data: result } = await api.put<ProjectResponse>(`/projects/${id}`, data);
return result;
}
export async function deleteProject(id: string): Promise<void> {
await api.delete(`/projects/${id}`);
}