Add user management
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { makeApiUrl } from '.';
|
||||
import type { CreateUserRequest, UpdateUserRequest, User } from './types/user';
|
||||
|
||||
export const getUsers = async (): Promise<User[]> => {
|
||||
const response = await fetch(makeApiUrl('/users'), {
|
||||
method: 'GET',
|
||||
});
|
||||
if (response.status === 404) {
|
||||
return [];
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch sites');
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const getUserById = async (id: string): Promise<User> => {
|
||||
const response = await fetch(makeApiUrl(`/users/${id}`), {
|
||||
method: 'GET',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch user');
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const createUser = async (data: CreateUserRequest): Promise<User> => {
|
||||
const response = await fetch(makeApiUrl('/users'), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to create user');
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const updateUser = async (id: string, data: Partial<UpdateUserRequest>): Promise<User> => {
|
||||
const response = await fetch(makeApiUrl(`/users/${id}`), {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to update user');
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const deleteUser = async (id: string): Promise<void> => {
|
||||
const response = await fetch(makeApiUrl(`/users/${id}`), {
|
||||
method: 'DELETE',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete user');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user