Body Transform
This transform function is called immediately after the REST call returns with a response, and is a hook for authors to transform the response body, if needed. This function is not guaranteed to be called in any specific order.
Example
A ServiceDataProvider variable that is configured with a custom body response transform is shown below. While it overrides the body
response transforms whereas, the paginate
response transforms function used to process the response returned by fetch call, is the default transforms associated to the service.
{
"variables": {
"incidentsList": {
"type": "vb/ServiceDataProvider",
"defaultValue": {
"endpoint": "ifixitfast-openapi3/getCustomers",
"keyAttributes": "id",
"itemsPath": "items",
"responseType": {
"items": "customerResponse[]",
"extraResult": "extraResponse"
},
"transforms": {
"response": {
"body": " {{ $page.functions.bodyResponse }}"
}
}
}
}
}
}
The default paginate
response transforms functions for a Business Object based service returns an object with the properties { totalSize, hasMore }
as shown below:
define([], function () {
class Response {
/**
* Called after response returns from a fetch call, this is a good place to process
* response, to provide pagination info such as totalSize and hasMore.
*
* @param configuration - a Map containing the following properties
* - headers: response header
* - body: body of the response
* - fetchConfiguration: the configuration that triggered this fetch call.
*
* @param transformsContext transforms context
*
* @returns {{}}
*/
static paginateResponse(configuration, transformsContext) {
const ps = {};
const tr = {};
if (configuration.body) {
const rb = configuration.body;
if (rb.totalCount) {
tr.totalSize = rb.totalCount;
}
if (rb.totalCount > 0) {
tr.hasMore = !!rb.hasMore;
} else {
tr.hasMore = false;
}
}
return tr;
};
}
var Response = function() {};
var Metadata = function() {};
// Note: as an example, the Request object is expanded to include just the sort property
return {
metadata: Metadata,
request: Request,
response: { paginate: Response.paginateResponse },
};
});
The custom body response transforms function configured in the SDP variable is defined in the PageModule JS. It appends extra results to the return value.
define([], function () {
class PageModule {
/**
* Fix up response data and extract other info and return a transformed result body.
* The object returned must have the body that the caller is configured for
*
* @param configuration - a Map containing the following properties
* - headers: response header
* - body: body of the response
* - fetchConfiguration: the configuration that triggered this fetch call.
*
* @param transformsContext transforms context
*
* @returns {*}
*/
static bodyResponse = (configuration, transformsContext) => {
const tr = {};
const c = configuation;
if (c.body) {
// fix up result.body from REST if needed and set the new body in tr
tr.items = c.body.items;
// you can also store additional data.
tr.extraResult = { foo: 'bar' };
}
return tr;
};
};
return PageModule;
});