Functions

💡

Functions are the interface of your backend. It allows you to run backend code from your frontend.

Request & Response

All Functions need a Request and Response block. Learn why.

Any Function you make inside Busywork needs a Request-block and at least one Response-block.

Simple function that inserts a record into the database
Simple function that inserts a record into the database

Request Block

The Request-block is the Function entry point. This is what allows the Function to accept outside data when a request is being made. Remember that a Function is an API endpoint of your app. Your app will likely have many of these API endpoints. Usually, you create a Function for each interaction a user can have with your app.

Let's look at the simple example of a todo app, that allows a user to create a task and complete a task. Such an app would require a collection structure that contains a description of the task and a field that shows whether the task has been completed or not.

Example of how a todo app's tasks collection can be built
Example of how a todo app's tasks collection can be built

For a user to create a task and mark a task as being done, two Functions are required. The API of the app could look something like this:

API Endpoints

NameURLDescriptionRequestResponse
New Task
Creates a new task, with the users description of the task goal
Description
Success if inserted Failed if not inserted
Complete Task
Changes the done value of a task to true.
Task ID
Success if updated Failed if not updated

In this above case, a user interaction could be a form submission. In turn, the form submission would insert a record to the database and return a message to the user.

Calling a Function and displaying the returned message to the user, can be accomplished using Connect. Connect generates JavaScript for your static site, so you don't have to.

Response Block

The Response-block(s) of a Function is where you can pass data back to the app. It allows you to configure,

  • Status of the response
  • Data payload
  • A message
  • And a unique status code
How you could set up a Response-block
How you could set up a Response-block

Status: is a text-field. You should use this to indicate whether the Function was completed successfully or not. Inside Connect, where you connect your frontend and backend, this can be used to determine if you show an error message or success message based on a Function outcome.

Data: allows you to send data back to your app. This could be data returned from the Find-block (database) or a 3rd party API.

Message: is a text-field that allows you to return a unique message for every possible outcome of your Function. In the example of a todo app: if a user tries to submit a new task you could have the following outcomes,

  • Description too short
  • Description empty (ideally handled in the frontend)
  • All good

Each outcome can be handled while making the Function. This allows you to create a unique message, that you can show the user.

Status Code: is a number-field that allows you to distinguish between multiple successful and failed outcomes. In Connect you can chain conditions to change your frontend accordingly.

Adding Blocks

image

Connecting Blocks

image

Setting Up Blocks

image

Deploying Functions

Continue Reading

🏭
Ins and Outs