Files
introduction-to-testing/examples/task-list/src/api.ts
2024-09-18 07:52:42 -06:00

51 lines
1.1 KiB
TypeScript

import { Task } from './types';
export const all = async (): Promise<Task[]> => {
const response = await fetch('/api/tasks');
if (!response.ok) {
throw new Error('Failed to fetch tasks');
}
return response.json();
};
export const add = async (title: string): Promise<Task> => {
const response = await fetch('/api/tasks', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title }),
});
if (!response.ok) {
throw new Error('Failed to add task');
}
return response.json();
};
export const update = async (
id: string,
updatedTask: Partial<Task>,
): Promise<void> => {
const response = await fetch(`/api/tasks/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updatedTask),
});
if (!response.ok) {
throw new Error('Failed to update task');
}
};
export const remove = async (id: string): Promise<void> => {
const response = await fetch(`/api/tasks/${id}`, {
method: 'DELETE',
});
if (!response.ok) {
throw new Error('Failed to delete task');
}
};