Skip to content

Instantly share code, notes, and snippets.

@Mulperi
Last active July 21, 2021 10:57
Show Gist options
  • Save Mulperi/0fa352af51ad6dc1cb4a7daeb8fc3a59 to your computer and use it in GitHub Desktop.
Save Mulperi/0fa352af51ad6dc1cb4a7daeb8fc3a59 to your computer and use it in GitHub Desktop.
react service usage
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