26 lines
1.0 KiB
TypeScript
26 lines
1.0 KiB
TypeScript
|
|
// services are kinda whatever, but in general its a good idea for all api calls to be within a service (at least thats how angular handles it)
|
|
// this user service will handle all to <-> from the server when handling user objects
|
|
// should be injected with the http client (I think its axios ?)
|
|
|
|
import axios from "axios";
|
|
import type {AxiosResponse } from "axios";
|
|
import type { User } from "../models/User.ts";
|
|
|
|
const API_URL: string = "/users";
|
|
|
|
const baseUrl: string = import.meta.env.DEV ? import.meta.env.VITE_DEV_API_URL : import.meta.env.VITE_PROD_API_URL; // TODO: overarching api service
|
|
const api = axios.create({
|
|
baseURL: baseUrl
|
|
});
|
|
|
|
export const getUsers = () => api.get<User[]>(`${API_URL}`);
|
|
|
|
export const getUser = (id: number) => api.get<User>(`${API_URL}/${id}`);
|
|
|
|
export const createUser = (data: User) => api.post<User>(`${API_URL}`, data);
|
|
|
|
export const updateUser = (id: number, data: User) => api.put<User>(`${API_URL}/${id}`, data);
|
|
|
|
export const deleteUser = (id: number) => api.delete<User>(`${API_URL}/${id}`);
|