Echoing the request
FaasPlus allows you to effortlessly manipulate incoming data. Whether you're adding additional fields, transforming data, or modifying structures, the flexibility of FaasPlus makes this process extremely straightforward. Let's dive into how simple it is to manipulate data using some practical examples.
Example 1: Adding a Timestamp
In this example, we'll add an execution timestamp to the data received in the request body.
const handleRequest = async (req) => {
let res={...req.body};
res['ExecutedAt']=new Date();
return res;
}
{
"FullName": "Alan Turing"
}
{
"ExecutedAt": "2024-10-03T06:15:45.991Z",
"FullName": "Alan Turing"
}
Example 2: Renaming Fields
In this example, we'll rename a field from "Name" to "FullName" in the incoming request.
const handleRequest = async (req) => {
let res = {};
res['FullName'] = req.body.Name;
return res;
};
{
"Name": "Alan Turing"
}
{
"FullName": "Alan Turing"
}
Here, we take the "Name" field, rename it to "FullName," and return the updated object. This type of manipulation can be useful when you need to adjust or standardize field names between systems.
Example 3: Combining Fields
In the following example, we'll combine two fields from the request body into one.
const handleRequest = async (req) => {
let res = {};
res['FullName'] = req.body.FirstName + ' ' + req.body.LastName;
return res;
};
{
"FirstName": "Alan",
"LastName": "Turing"
}
{
"FullName": "Alan Turing"
}