Refactor task list example application
This commit is contained in:
50
examples/task-list/src/api.ts
Normal file
50
examples/task-list/src/api.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
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');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user