The following example script provides comments on specific details that need to be added for an application programming interface (API) for an oData remote data source.
Note: In the section following this image, Example Code is provided that you can copy and paste as required.
You can click on the image to open in a separate tab to improve viewing where required.
Example Code SAP-data setup
You can cut and paste the following for customizing for working with remote data sources:
import camelCase from "lodash/camelCase";
const sapdata = async ({ name, params }) => {
const prefixlessName = name.replace("all", "");
const select = params["select"];
const info = params["data_source_info"];
const skip = params["skip"];
const take = params["take"];
const url =
info.source.host +
info.model.meta_info.odata_endpoint +
"?$skip=" +
skip +
"&$top=" +
take +
"&$inlinecount=allpages";
const apiKey = info.source.api_key;
const response = await fetch(url, {
method: "GET",
headers: {
APIKey: apiKey,
accept: "application/json",
},
})
.then((response) => response.json())
.catch((err) => {
console.error(err);
});
const results = response.d.results.map((object) => {
const record = Object.keys(object).reduce(
(o, k) => {
o[camelCase(k)] = object[k];
return o;
},
{
id: object[prefixlessName],
createdAt: "2020-08-25T15:26:40+02:00",
updatedAt: "2020-08-25T15:26:40+02:00",
}
);
return select.reduce((o, k) => {
o[k] = record.hasOwnProperty(k) ? record[k] : "";
return o;
}, {});
});
console.log(JSON.stringify(results));
return {
response: {
results: results,
totalCount: parseInt(response.d.__count),
},
};
};
export default sapdata;
Example Code Functions
If you would like the remote data source action events to support options, you will need to create a functions file as well.
The functions.json file will work together with your index file to support the creation of option you can use within your actions.
You can find example code for functions.json down below.
{
"description": "Description",
"label": "sapdata",
"category": "Misc",
"icon": "CreateIcon",
"options": [
{
"meta": {
"type": "Text"
},
"name": "name",
"label": "Name",
"info": ""
},
{
"meta": {
"type": "Object"
},
"name": "params",
"label": "Params",
"info": ""
},
{
"meta": {
"type": "Output",
"output": {
"type": "Text"
}
},
"name": "response",
"label": "Result as",
"info": ""
}
],
"yields": "NONE"
}