Response Transformation Functions

Response transformation (transform) functions are called right after a request returns successfully. They provide a hook for page authors to transform the response further for the consumption of the ServiceDataProvider or user interface.

The ServiceDataProvider supports a predefined list of response transformation function types, described in this section. Note that there are no guarantees of the order in which transform functions are called.

A response transformation function has the following signature: function (result). It can be defined on the service endpoint, but can also be overridden on the variable. The parameters to this function are:

  • result: an object that has the following properties:

    • response: the response object, an implementation of the Response interface of the Fetch web API.

    • body: The (response) body that corresponds to the requested content type.

Types of Response Transform Functions

paginate

The paginate response transform function can be used to transform any paging state that is endpoint specific, to a form that the ServiceDataProvider understands. For example, for an endpoint that returns the 'count' in its response, the transform function can return an object like { 'totalSize': count }. The properties the SDP looks for on paginate return value are:

  • totalSize: the <canonical> total count of the records in the backend service/endpoint

  • hasMore: <generally required> A boolean that indicates whether there are more records to fetch. For example, in Business object REST API usecases this would map to the hasMore boolean property commonly returned in the response. See explanation below for behavior of SDP when hasMore is not set.

  • pagingState: can be used to store information needed between requests for the service endpoint's custom paging strategy, like a hateos link based paging.

// Variable Configuration
"transforms": {
  "response": {
    "paginate": " {{ $page.functions.paginateResponse }}"
  }
}
/** paginate() Response Transform Function
 * Called after response returns from a fetch call, this is a good place to post-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. If fetch was initiated by SDP this includes
 *   fetch capability, context, externalContext and fetchParameters property. Refer to the docs for the
 *   mergeTransformsOptions func callback for details.
 *
 * @param transformsContext transforms context
 *
 * @returns {{}}
 */
PageModule.prototype.paginateResponse = function (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;
};

body

This transform function can be used to modify the response body, or to build an entirely new one, that the ServiceDataProvider or component expects.

A ServiceDataProvider variable that is configured with a custom body response transform is shown below:

// Variable Configuration
"transforms": {
  "response": {
    "body": " {{ $page.functions.bodyResponse }}"
  }
}

An example of a body transform function might look like this:

/**
 * Called after response returns from a fetch call, this is a good place to post-process
 * response, fix up data and extract other info (like aggregations) and return a transformed result.
 * The object returned must have the body that SDP is configured for, in addition to any data
 * that might be needed.
 *
 * @param configuration - a Map containing the following properties
 * - headers: response header
 * - body: body of the response
 * - fetchConfiguration: the configuration that triggered this fetch call. If fetch was initiated by SDP this includes
 *   fetch capability, context, externalContext and fetchParameters property. Refer to the docs for the
 *   mergeTransformsOptions func callback for details.
 *
 * @param transformsContext transforms context
 *
 * @returns {{}}
 */
PageModule.prototype.bodyResponse(configuration, transformsContext) {
  var tr = {};
  if (configuration.body) {
    // fix up result.body from REST if needed and set the new body in tr
    tr = configuration.body;
  }
  // you can also store additional data - example aggregations for Elastic endpoint responses.
  tr.aggregations = { foo: 4 };
  return tr;
}