Last active
July 21, 2021 10:57
-
-
Save Mulperi/0fa352af51ad6dc1cb4a7daeb8fc3a59 to your computer and use it in GitHub Desktop.
react service usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Todo } from '../models/todo.model'; | |
const TodoService = { | |
get: () => { | |
return new Promise((resolve) => { | |
return fetch('http://localhost:5000/todos') | |
.then((resp) => resp.json()) | |
.then((data) => { | |
resolve(data); | |
}); | |
}) as Promise<Todo[]>; | |
}, | |
post: (body: any) => { | |
return new Promise((resolve) => { | |
return fetch('http://localhost:5000/todos', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify(body), | |
}) | |
.then((resp) => resp.json()) | |
.then((data) => { | |
resolve(data); | |
}); | |
}) as Promise<Todo>; | |
}, | |
}; | |
export default TodoService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment