Byte-size Gatsby Functions examples.
Learn more about the FuncJam challenge on the Gatsby Blog
Example functions within this repo can be seen here: gatsby-funcjam-21-byte-size
There are a number of functions within this repo and all have a corresponding page.
Some functions require user input and will POST data captured in the UI via inputs and send them to the Function whilst others are GET request and can be run from either the page or by visiting the url in your browsers address bar.
A simple GET request that returns a 200 and message. This endpoint can visited from the browsers address bar or via the UI.
GET | /api/whats-the-time
function src: /src/api/whats-the-time.js
page src: /src/pages/whats-the-time.js
๐: whats-the-time
| Name | Type | Required | Summary |
|---|---|---|---|
| n/a | n/a | n/a | n/a |
A simple GET request that returns a 200 and message. This endpoint can visited from the browsers address bar or via the UI and has been written in TypesScript
GET | /api/typescript-function
function src: /src/api/typescript-function.ts
page src: /src/pages/typescript-function.js
๐: typescript-function
| Name | Type | Required | Summary |
|---|---|---|---|
| n/a | n/a | n/a | n/a |
A simple POST request that sends an emoji and returns a 200 and message.
function src: /src/api/post-body-params-with-fetch.js
page src: /src/pages/post-body-params-with-fetch.js
๐: post-body-params-with-fetch
| Name | Type | Required | Summary |
|---|---|---|---|
| emoji | string | false | The emoji to send |
A POST request that sends an email address to the server for validation using Yup
function src: /src/api/validate-email.js
page src: /src/pages/validate-email.js
๐: validate-email
| Name | Type | Required | Summary |
|---|---|---|---|
| string | true | email address |
A POST request with restricted access to a defined list of allowedOrigins using CORS middleware
function src: /src/api/custom-middleware.js
page src: /src/pages/custom-middleware.js
๐: custom-middleware
| Name | Type | Required | Summary |
|---|---|---|---|
| message | string | true | a simple message |
A POST request that sends data to a Google Spreadsheet and returns a status 200 and message
function src: /src/api/post-to-google-sheets.js
page src: /src/pages/post-to-google-sheets.js
๐: post-to-google-sheets
| Name | Type | Required | Summary |
|---|---|---|---|
| userAnswer | string | true | the value from an input |
A POST request that sends a message to a user defined email address using SendGrid. Returns a status 200 and message
function src: /src/api/post-to-send-grid.js
page src: /src/pages/post-to-send-grid.js
๐: post-to-send-grid
| Name | Type | Required | Summary |
|---|---|---|---|
| string | true | valid email address | |
| message | string | true | the message to include in the email |
A POST request that sends a reaction to a Fauna database. Returns a status 200 and message
function src: /src/api/post-to-fauna.js
page src: /src/pages/post-to-fauna.js
๐: post-to-fauna
| Name | Type | Required | Summary |
|---|---|---|---|
| userReaction | string | true | emoji associated with input value |
For brevity we've tried to leave all but essential code in the example functions. There are however one or two best practices we encourage, these are as follows.
To ensure your functions are used correctly it's sometimes helpful to catch incorrect req.methods and send a status code an advisory message back to the client.
E.g
export default function handler(req, res) {
if (req.method !== 'GET') {
res.status(405).json({ message: 'req.method should be GET' });
}
// rest of your function
}export default function handler(req, res) {
if (req.method !== 'POST') {
res.status(405).json({ message: 'req.method should be POST' });
}
// rest of your function
}It's sometimes helpful to catch absent parameters and send a status code an advisory message back to the client.
E.g
export default function handler(req, res) {
const { email } = req.body;
if (!email) {
res.status(400).json({ message: 'No email found on req.body' });
}
// rest of your function
}