From d390d662a9405369b4879e2738b7e9022aa1e597 Mon Sep 17 00:00:00 2001 From: DavidWells Date: Sun, 1 Jul 2018 21:44:49 -0700 Subject: [PATCH] add frontend api methods --- README.md | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/README.md b/README.md index 9dba709..335507e 100644 --- a/README.md +++ b/README.md @@ -599,6 +599,67 @@ So far we have created our `todo-create` function done and we've seen how we mak ``` +After we deploy all these functions, we will be able to call them from our frontend code with these fetch calls: + + + +```js +/* Frontend code from src/utils/api.js */ +/* Api methods to call /functions */ + +const create = (data) => { + return fetch('/.netlify/functions/todos-create', { + body: JSON.stringify(data), + method: 'POST' + }).then(response => { + return response.json() + }) +} + +const readAll = () => { + return fetch('/.netlify/functions/todos-read-all').then((response) => { + return response.json() + }) +} + +const update = (todoId, data) => { + return fetch(`/.netlify/functions/todos-update/${todoId}`, { + body: JSON.stringify(data), + method: 'POST' + }).then(response => { + return response.json() + }) +} + +const deleteTodo = (todoId) => { + return fetch(`/.netlify/functions/todos-delete/${todoId}`, { + method: 'POST', + }).then(response => { + return response.json() + }) +} + +const batchDeleteTodo = (todoIds) => { + return fetch(`/.netlify/functions/todos-delete-batch`, { + body: JSON.stringify({ + ids: todoIds + }), + method: 'POST' + }).then(response => { + return response.json() + }) +} + +export default { + create: create, + readAll: readAll, + update: update, + delete: deleteTodo, + batchDelete: batchDeleteTodo +} +``` + + ### Wrapping Up I hope you have enjoyed this tutorial on building your own CRUD API using Netlify serverless functions and FaunaDB.